javascript-d3How do I create a zoomable line chart using d3.js?
Creating a zoomable line chart using d3.js is relatively straightforward. The following example code block will create a zoomable line chart:
// set the dimensions and margins of the graph
var margin = {top: 10, right: 30, bottom: 30, left: 60},
width = 460 - margin.left - margin.right,
height = 400 - margin.top - margin.bottom;
// append the svg object to the body of the page
var svg = d3.select("#my_dataviz")
.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 + ")");
//Read the data
d3.csv("https://raw.githubusercontent.com/holtzy/data_to_viz/master/Example_dataset/3_TwoNumOrdered_comma.csv", function(data) {
// Add X axis --> it is a date format
var x = d3.scaleTime()
.domain(d3.extent(data, function(d) { return d.date; }))
.range([ 0, width ]);
svg.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
// Add Y axis
var y = d3.scaleLinear()
.domain([0, d3.max(data, function(d) { return +d.value; })])
.range([ height, 0 ]);
svg.append("g")
.call(d3.axisLeft(y));
// Add the line
svg.append("path")
.datum(data)
.attr("fill", "none")
.attr("stroke", "steelblue")
.attr("stroke-width", 1.5)
.attr("d", d3.line()
.x(function(d) { return x(d.date) })
.y(function(d) { return y(d.value) })
)
// Add the zoom functionality
svg.call(d3.zoom()
.scaleExtent([1, 8]) // This control how much you can zoom out
.on("zoom", zoomed));
function zoomed() {
// create new scale ojects based on event
var new_xScale = d3.event.transform.rescaleX(x);
var new_yScale = d3.event.transform.rescaleY(y);
// update axes
svg.select(".x-axis").call(d3.axisBottom(new_xScale));
svg.select(".y-axis").call(d3.axisLeft(new_yScale));
// update line
svg.selectAll(".line")
.attr("d", d3.line()
.x(function(d) { return new_xScale(d.date) })
.y(function(d) { return new_yScale(d.value) })
)
}
This code will create a line chart with axes, that can be zoomed in and out. It is composed of the following parts:
- Setting the dimensions and margins of the graph:
var margin = {top: 10, right: 30, bottom: 30, left: 60}, width = 460 - margin.left - margin.right, height = 400 - margin.top - margin.bottom;
- Appending the svg object to the body of the page:
var svg = d3.select("#my_dataviz").append("svg")
- Reading the data:
d3.csv("https://raw.githubusercontent.com/holtzy/data_to_viz/master/Example_dataset/3_TwoNumOrdered_comma.csv", function(data) {
- Adding the X axis:
var x = d3.scaleTime().domain(d3.extent(data, function(d) { return d.date; })).range([ 0, width ]); svg.append("g").attr("transform", "translate(0," + height + ")").call(d3.axisBottom(x));
- Adding the Y axis:
var y = d3.scaleLinear().domain([0, d3.max(data, function(d) { return +d.value; })]).range([ height, 0 ]); svg.append("g").call(d3.axisLeft(y));
- Adding the line:
svg.append("path").datum(data).attr("fill", "none").attr("stroke", "steelblue").attr("stroke-width", 1.5).attr("d", d3.line().x(function(d) { return x(d.date) }).y(function(d) { return y(d.value) }))
- Adding the zoom functionality:
svg.call(d3.zoom().scaleExtent([1, 8]).on("zoom", zoomed));
This code will generate a zoomable line chart like this:
Helpful links
More of Javascript D3
- How do I create a zoomable chart using d3.js?
- How do I use D3.js to zoom on the x-axis?
- How do I use the viewbox feature in d3.js?
- How do I create a graph using D3.js?
- How do I create a bar chart using d3.js?
- How can I use D3.js to read a CSV file?
- How do I use the z-index property with d3.js?
- How do I implement zooming in a d3.js visualization?
- How to create a Gantt chart using D3.js?
See more codes...