D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
romsson
Full window
Github gist
line chart (temporal data)
Built with
blockbuilder.org
<!DOCTYPE html> <head> <meta charset="utf-8"> <script src="https://d3js.org/d3.v4.min.js"></script> <style> body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; } </style> </head> <body> <script> // Feel free to change or delete any of the code you see in this editor! var margin = {top: 20, right: 10, bottom: 20, left: 10}; var width = 480 - margin.left - margin.right, height = 250 - margin.top - margin.bottom; var svg = d3.select("body").append("svg") .attr("width", width) .attr("height", height) // var data = d3.range(10).map(Math.random) d3.json("dataset.json", function(data) { var parseDate = d3.timeParse("%Y-%m") var displayDate = d3.timeFormat("%b %y") data.forEach(function(d) { d.value = +d.value; d.date = parseDate(d.date); }); var x = d3.scaleTime() .domain(d3.extent(data, function(d) { return d.date; })) .range([margin.left, width + margin.left]) var y = d3.scaleLinear() .domain([d3.min(data, function(d) { return d.value; }), d3.max(data, function(d) { return d.value; })]) .range([margin.top, height + margin.top]) var line = d3.line() .curve(d3.curveMonotoneX) .x(function(d, i) { return x(d.date); }) .y(function(d, i) { return height-y(d.value); }) svg.selectAll("path").data([data]).enter() .append("path") .style("fill", "none") .style("stroke", "black") .attr("d", line); svg.selectAll("text").data(data).enter() .append('text') .attr("x", function(d, i) { return x(d.date); }) .attr("y", function(d, i) { return height; }) .text(function(d) { return displayDate(d.date)}) }) </script> </body>
https://d3js.org/d3.v4.min.js