9951 explained code solutions for 126 technologies


javascript-d3How do I create a graph using JavaScript and D3?


Creating a graph using JavaScript and D3 is straightforward. First, you need to include the D3 library in your HTML file. This can be done by adding the following line of code:

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

You can then create an SVG element to hold the graph. This can be done by adding the following code to your JavaScript file:

const svg = d3.select("body").append("svg")
    .attr("width", width)
    .attr("height", height);

Next, you can define the data you want to display in the graph. This can be done using a JavaScript array, like so:

const data = [1, 2, 3, 4, 5];

You can then define the scales for your graph. This will determine how the data is mapped to the SVG element. For example, if you want to map the data to the width of the SVG element, you can use the following code:

const xScale = d3.scaleLinear()
    .domain([0, d3.max(data)])
    .range([0, width]);

Finally, you can draw the graph by using the data and scales. This can be done by using D3's built-in shape generators. For example, if you want to draw a line graph, you can use the following code:

const line = d3.line()
    .x(d => xScale(d))
    .y(d => height - yScale(d));

svg.append("path")
    .datum(data)
    .attr("fill", "none")
    .attr("stroke", "#00bfa5")
    .attr("stroke-width", 2)
    .attr("d", line);

This code will create a line graph with the data provided.

List of Code Parts with Explanation
  • Including the D3 library in the HTML file - This is done by adding the following line of code: <script src="https://d3js.org/d3.v4.min.js"></script>
  • Creating an SVG element - This is done by adding the following code to the JavaScript file: const svg = d3.select("body").append("svg") .attr("width", width) .attr("height", height);
  • Defining the data - This is done by using a JavaScript array, like so: const data = [1, 2, 3, 4, 5];
  • Defining the scales - This is done by using D3's built-in scale functions. For example, if you want to map the data to the width of the SVG element, you can use the following code: const xScale = d3.scaleLinear() .domain([0, d3.max(data)]) .range([0, width]);
  • Drawing the graph - This is done by using D3's built-in shape generators. For example, if you want to draw a line graph, you can use the following code: const line = d3.line() .x(d => xScale(d)) .y(d => height - yScale(d)); svg.append("path") .datum(data) .attr("fill", "none") .attr("stroke", "#00bfa5") .attr("stroke-width", 2) .attr("d", line);
Relevant Links

Edit this code on GitHub