9951 explained code solutions for 126 technologies


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


Creating a line chart using d3.js is a simple process. First, you need to include the d3 library in your HTML file:

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

Next, you can set up the SVG container for your chart. This will define the size of the chart:

<svg width="400" height="400"></svg>

You can then set up the scales for the x and y axes of the chart. These will define the range of data points that will be plotted:


    let xScale = d3.scaleLinear()
    .domain([0, 10])
    .range([0, 400]);

    let yScale = d3.scaleLinear()
    .domain([0, 10])
    .range([400, 0]);
</script>```

Next, you can define the line generator. This will define the shape of the line on the chart:

```<script>
    let line = d3.line()
    .x(function(d) { return xScale(d.x); })
    .y(function(d) { return yScale(d.y); });
</script>```

You can then define the data points that will be plotted on the chart. These should be in the form of an array of objects with x and y properties:

```<script>
    let data = [
        {x: 0, y: 5},
        {x: 1, y: 9},
        {x: 2, y: 7},
        {x: 3, y: 5},
        {x: 4, y: 3},
        {x: 6, y: 4},
        {x: 7, y: 2},
        {x: 8, y: 3},
        {x: 9, y: 2}
    ];
</script>```

Finally, you can add the line to the SVG container:

```<script>
    svg.append("path")
    .datum(data)
    .attr("fill", "none")
    .attr("stroke", "steelblue")
    .attr("stroke-width", 1.5)
    .attr("d", line);
</script>```

This should create a line chart with the data points plotted.

#### List of Code Parts With Detailed Explanation

1. `<script src="https://d3js.org/d3.v5.min.js"></script>`: This includes the d3 library in the HTML file.
2. `<svg width="400" height="400"></svg>`: This sets up the SVG container for the chart, defining its size.
3. `let xScale = d3.scaleLinear()`: This sets up the x-axis scale for the chart, defining the range of data points that will be plotted.
4. `let yScale = d3.scaleLinear()`: This sets up the y-axis scale for the chart, defining the range of data points that will be plotted.
5. `let line = d3.line()`: This defines the line generator, which will define the shape of the line on the chart.
6. `let data = []`: This defines the data points that will be plotted on the chart.
7. `svg.append("path")`: This adds the line to the SVG container.

#### Relevant Links

- [D3.js Documentation](https://github.com/d3/d3/wiki)
- [D3 Tutorials](https://www.d3indepth.com/)
- [D3 Gallery](https://github.com/d3/d3/wiki/Gallery)

Edit this code on GitHub