9951 explained code solutions for 126 technologies


javascript-d3How can I use JavaScript and D3 to create interactive data visualizations?


Using JavaScript and D3, you can create interactive data visualizations. D3 is a JavaScript library that allows you to bind data to HTML documents and manipulate the document based on the data.

For example, you can use the following code to create a simple bar chart:

// Set the dimensions of the canvas / graph
var margin = {top: 30, right: 20, bottom: 30, left: 50},
    width = 600 - margin.left - margin.right,
    height = 270 - margin.top - margin.bottom;

// Set the ranges
var x = d3.scale.ordinal().rangeRoundBands([0, width], .05);

var y = d3.scale.linear().range([height, 0]);

// Define the axes
var xAxis = d3.svg.axis()
    .scale(x)
    .orient("bottom")

var yAxis = d3.svg.axis()
    .scale(y)
    .orient("left")
    .ticks(10);

// Add the SVG element
var svg = d3.select("body").append("svg")
    .attr("width", width + margin.left + margin.right)
    .attr("height", height + margin.top + margin.bottom)
  .append("g")
    .attr("transform",
          "translate(" + margin.left + "," + margin.top + ")");

// Load the data
d3.csv("data.csv", function(error, data) {

    data.forEach(function(d) {
        d.value = +d.value;
    });

  // Scale the range of the data
  x.domain(data.map(function(d) { return d.name; }));
  y.domain([0, d3.max(data, function(d) { return d.value; })]);

  // Add the bars
  svg.selectAll("bar")
      .data(data)
    .enter().append("rect")
      .attr("class", "bar")
      .attr("x", function(d) { return x(d.name); })
      .attr("width", x.rangeBand())
      .attr("y", function(d) { return y(d.value); })
      .attr("height", function(d) { return height - y(d.value); });

  // Add the X Axis
  svg.append("g")
      .attr("class", "x axis")
      .attr("transform", "translate(0," + height + ")")
      .call(xAxis);

  // Add the Y Axis
  svg.append("g")
      .attr("class", "y axis")
      .call(yAxis);

});

This code will generate a bar chart with the data from the data.csv file. The x and y variables are used to define the X and Y axes of the chart, and the svg variable is used to create the SVG element that will contain the chart. The data.forEach loop is used to loop through the data in the CSV file and convert it into a usable format. Finally, the svg.selectAll and svg.append functions are used to create the bars and axes of the chart.

You can find more information about using JavaScript and D3 to create interactive data visualizations in the D3 documentation.

Parts of code:

  • var margin: defines the dimensions of the canvas/graph
  • var x: defines the range of the X axis
  • var y: defines the range of the Y axis
  • var xAxis: defines the X axis
  • var yAxis: defines the Y axis
  • var svg: creates the SVG element that will contain the chart
  • d3.csv: loads the data from the CSV file
  • data.forEach: loops through the data in the CSV file and converts it into a usable format
  • svg.selectAll: creates the bars of the chart
  • svg.append: creates the axes of the chart

Edit this code on GitHub