9951 explained code solutions for 126 technologies


javascript-d3How do I create a timeline using d3.js?


Creating a timeline using d3.js is a relatively straightforward process. First, you'll need to include the d3.js library in your HTML. This can be done using the <script> tag:

<script src="https://d3js.org/d3.v5.min.js"></script>

Next, you'll need to create a <div> element in your HTML to house the timeline. This can be done with the following code:

<div id="timeline"></div>

Once the <div> element is in place, you can use d3.js to create the timeline. This can be done with the following code:

// Create the SVG element
var svg = d3.select("#timeline")
    .append("svg")
    .attr("width", width)
    .attr("height", height);

// Create the timeline
svg.selectAll("rect")
    .data(data)
    .enter()
    .append("rect")
    .attr("x", function(d, i) {
        return i * (width / data.length);
    })
    .attr("y", 0)
    .attr("width", width / data.length - barPadding)
    .attr("height", function(d) {
        return d * 4;
    });

The code above creates an SVG element and appends it to the <div> element. It then creates the timeline by looping through the data and creating a <rect> element for each data point. The x and y attributes are used to position the rectangles in the timeline, and the width and height attributes are used to set the size of the rectangles.

The output of the code above will be a timeline with rectangles representing the data points.

Code explanation

  • <script> tag: This is used to include the d3.js library in the HTML.
  • <div> element: This is used to create an element in the HTML to house the timeline.
  • var svg = d3.select("#timeline"): This creates an SVG element and appends it to the <div> element.
  • .append("svg"): This creates the timeline by appending an SVG element.
  • .attr("x", function(d, i) {...}): This sets the x attribute of the <rect> element to position the rectangles in the timeline.
  • .attr("y", 0): This sets the y attribute of the <rect> element to position the rectangles in the timeline.
  • .attr("width", width / data.length - barPadding): This sets the width attribute of the <rect> element to set the size of the rectangles.
  • .attr("height", function(d) {...}): This sets the height attribute of the <rect> element to set the size of the rectangles.

Helpful links

Edit this code on GitHub