9951 explained code solutions for 126 technologies


javascript-d3How can I use d3.js with W3Schools?


D3.js is a powerful JavaScript library for manipulating documents based on data. It is used to create interactive data visualizations in the web browser. W3Schools provides a great resource for learning how to use D3.js.

Here is an example of how to use D3.js with W3Schools:

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

// 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;

// Parse the date / time
var parseDate = d3.time.format("%d-%b-%y").parse;

// Set the ranges
var x = d3.time.scale().range([0, width]);
var y = d3.scale.linear().range([height, 0]);

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

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

// Define the line
var valueline = d3.svg.line()
    .x(function(d) { return x(d.date); })
    .y(function(d) { return y(d.close); });

// Adds the svg canvas
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 + ")");

// Get the data
d3.csv("data.csv", function(error, data) {
    data.forEach(function(d) {
        d.date = parseDate(d.date);
        d.close = +d.close;
    });

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

    // Add the valueline path.
    svg.append("path")
        .attr("class", "line")
        .attr("d", valueline(data));

    // 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);

});

</script>

This example code will create an SVG element in the DOM and draw a line chart to represent the data from the csv file. It uses the d3.js library to set the dimensions of the canvas, parse the date/time, set the ranges, define the axes, and define the line. It then adds the SVG canvas, gets the data, scales the range of the data, and adds the valueline and axes.

Code explanation

  • <script src="https://d3js.org/d3.v4.js"></script> - This loads the d3.js library.
  • var margin = {top: 30, right: 20, bottom: 30, left: 50}, - This sets the margins for the canvas.
  • var parseDate = d3.time.format("%d-%b-%y").parse; - This parses the date/time.
  • var x = d3.time.scale().range([0, width]); - This sets the range for the x axis.
  • var y = d3.scale.linear().range([height, 0]); - This sets the range for the y axis.
  • var xAxis = d3.svg.axis().scale(x) - This defines the x axis.
  • var yAxis = d3.svg.axis().scale(y) - This defines the y axis.
  • var valueline = d3.svg.line() - This defines the line.
  • var svg = d3.select("body") - This adds the SVG canvas.
  • d3.csv("data.csv", function(error, data) { - This gets the data from the csv file.
  • x.domain(d3.extent(data, function(d) { return d.date; })); - This scales the range of the data for the x axis.
  • y.domain([0, d3.max(data, function(d) { return d.close; })]); - This scales the range of the data for the y axis.
  • svg.append("path") - This adds the valueline path.
  • svg.append("g") - This adds the x axis.
  • svg.append("g") - This adds the y axis.

For more information on using D3.js with W3Schools, please visit the following links:

Edit this code on GitHub