javascript-d3How do I create a multiple line chart using d3.js?
Creating a multiple line chart using d3.js is relatively simple. The following example code will generate a multiple line chart from a dataset of values:
// 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 parseTime = d3.timeParse("%d-%b-%y");
// Set the ranges
var x = d3.scaleTime().range([0, width]);
var y = d3.scaleLinear().range([height, 0]);
// Define the line
var valueline = d3.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.close); });
// Define the div for the tooltip
var div = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 0);
// Get the data
d3.csv("data.csv", function(error, data) {
if (error) throw error;
// Format the data
data.forEach(function(d) {
d.date = parseTime(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")
.data([data])
.attr("class", "line")
.attr("d", valueline);
// Add the X Axis
svg.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
// Add the Y Axis
svg.append("g")
.call(d3.axisLeft(y));
});
This code will create a multiple line chart with a tooltip. It will use the data from a csv file called data.csv
. The code does the following:
- Sets the dimensions of the canvas/graph
- Parses the date/time from the csv file
- Sets the ranges of the x and y axes
- Defines the line
- Defines the tooltip
- Formats the data
- Scales the range of the data
- Adds the valueline path
- Adds the x and y axes
Helpful links
More of Javascript D3
- How can I display Unix time using d3.js?
- How do I create a zoomable chart using d3.js?
- How can I use d3.js to create a zoom scale?
- How do I use the z-index property with d3.js?
- How do I set up the x axis in d3.js?
- How do I create a timeline using d3.js?
- How do I create a dashboard using d3.js?
- How do I create a zoomable line chart using d3.js?
- How do I install and use D3.js with Yarn?
- How do I implement zooming in a d3.js visualization?
See more codes...