D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
ruridge
Full window
Github gist
Paths (Line Graph)
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> <svg></svg> <script> var width = 900 var height = 400 var padding = {top: 20, left: 40, right: 20, bottom: 40} var city = 'New York'; d3.tsv('data.tsv', (err, data) => { // clean data data.forEach(d => { d.date = d3.timeParse('%Y%m%d')(d.date) d[city] = +d[city] }) // setup extents console.log(data) var xExtent = d3.extent(data.map(d => d.date)) var yExtent = d3.extent(data, d => d[city]) console.log(xExtent) console.log(yExtent) // setup scales var xScale = d3.scaleTime() .domain(xExtent) .range([padding.left, width - padding.right]) var yScale = d3.scaleLinear() .domain(yExtent) .range([height - padding.bottom, padding.top]) var xColorScale = d3.scaleTime() .domain(xExtent) .range[0, 1] // draw svg var svg = d3.select('svg') .attr('width', width) .attr('height', height) var xAxis = d3.axisBottom(xScale) .tickFormat(d3.timeFormat('%b %Y')) var yAxis = d3.axisLeft(yScale) var line = d3.line() .x(d => xScale(d.date)) .y(d => yScale(d[city])) .curve(d3.curveStepAfter) // add a background svg.append('rect') .attr('x', padding.left) .attr('y', padding.top) .attr('width', width - padding.left - padding.right) .attr('height', height - padding.top - padding.bottom) .attr('fill', '#efefef') // draw a single path for the line svg.append("path") .attr('d', line(data)) .attr('fill', 'none') .attr('stroke', 'fuchsia') // draw x-axis svg.append("g") .attr('transform', 'translate(0,'+(height-padding.bottom)+')') .call(xAxis) // draw y-axis svg.append("g") .attr('transform', 'translate('+padding.left+',0)') .call(yAxis) }) </script> </body>
https://d3js.org/d3.v4.min.js