9951 explained code solutions for 126 technologies


javascript-d3What is the purpose of using d3.js?


The purpose of using D3.js is to create interactive data visualizations in the browser. It allows developers to bind data to the Document Object Model (DOM) and then apply data-driven transformations to the document. This makes it possible to create dynamic, animated visualizations that respond to user input and data changes.

For example, the following code block uses D3.js to create a bar chart showing the number of people in each age group:

<div id="chart"></div>

<script>
  const data = [
    {age: '18-24', people: 20},
    {age: '25-34', people: 30},
    {age: '35-44', people: 40},
    {age: '45-54', people: 25},
    {age: '55-64', people: 15},
    {age: '65+', people: 5}
  ];

  const svg = d3.select('#chart')
    .append('svg')
    .attr('width', 400)
    .attr('height', 300);

  const xScale = d3.scaleBand()
    .domain(data.map(d => d.age))
    .range([0, 400])
    .padding(0.2);

  const yScale = d3.scaleLinear()
    .domain([0, d3.max(data, d => d.people)])
    .range([300, 0]);

  const bars = svg.selectAll('rect')
    .data(data)
    .enter()
    .append('rect')
    .attr('x', d => xScale(d.age))
    .attr('y', d => yScale(d.people))
    .attr('width', xScale.bandwidth())
    .attr('height', d => 300 - yScale(d.people));
</script>

This code will produce a bar chart like this:

Bar Chart

The code can be broken down into the following parts:

  1. Selecting the element: The first two lines select the element with the id chart and append an SVG element to it.

  2. Creating the scales: The next two lines create the x-scale and y-scale, which are used to map the data to the SVG element.

  3. Creating the bars: The last four lines create the bars in the chart and bind the data to them.

D3.js is a powerful tool for creating data visualizations and can be used to create a variety of different charts, maps, and diagrams.

Helpful links

Edit this code on GitHub