javascript-d3How do I create a multiple line chart using D3.js?
Creating a multiple line chart using D3.js is a relatively simple process. The following example code will create a multi-line chart, using a dataset of three lines:
// set the dimensions and margins of the graph
var margin = {top: 20, right: 80, bottom: 30, left: 50},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
// parse the date / time
var parseTime = d3.timeParse("%Y-%m-%d");
// set the ranges
var x = d3.scaleTime().range([0, width]);
var y = d3.scaleLinear().range([height, 0]);
// define the line
var line1 = d3.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.line1); });
var line2 = d3.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.line2); });
var line3 = d3.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.line3); });
// append the svg obgect to the body of the page
// appends a 'group' element to 'svg'
// moves the 'group' element to the top left margin
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) {
if (error) throw error;
// format the data
data.forEach(function(d) {
d.date = parseTime(d.date);
d.line1 = +d.line1;
d.line2 = +d.line2;
d.line3 = +d.line3;
});
// Scale the range of the data
x.domain(d3.extent(data, function(d) { return d.date; }));
y.domain([
d3.min(data, function(d) { return Math.min(d.line1, d.line2, d.line3); }),
d3.max(data, function(d) { return Math.max(d.line1, d.line2, d.line3); })
]);
// Add the line1 path.
svg.append("path")
.data([data])
.attr("class", "line1")
.attr("d", line1);
// Add the line2 path.
svg.append("path")
.data([data])
.attr("class", "line2")
.attr("d", line2);
// Add the line3 path.
svg.append("path")
.data([data])
.attr("class", "line3")
.attr("d", line3);
});
This code will create three SVG paths, each representing a line in the chart. The code consists of the following parts:
- Setting the dimensions and margins of the graph.
- Parsing the date/time data.
- Setting the ranges for the x and y axes.
- Defining the lines for each of the three datasets.
- Appending the SVG object to the body of the page.
- Reading in the data from a CSV file.
- Formatting the data.
- Scaling the range of the data.
- Appending each of the three lines to the SVG object.
For more information on creating a multi-line chart with D3.js, see the following resources:
More of Javascript D3
- How can I use d3.js to create a zoom scale?
- How do I create a zoomable chart using d3.js?
- How do I create a zoomable line chart using d3.js?
- How can I display Unix time using d3.js?
- How do I implement zooming in a d3.js visualization?
- How do I use the z-index property with d3.js?
- How can I use different types of D3.js in my software development project?
- How can I create a mindmap using d3.js?
- How can I use D3.js to create interactive visualizations on Udemy?
- How do I check the license for d3.js?
See more codes...