D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
VirginiaHNg
Full window
Github gist
Class: 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; } path{ fill: none; stroke: grey; stroke-width: 5; } </style> </head> <body> <script> var svg = d3.select("body").append("svg") .attr("width", 960) .attr("height", 500) var data = [[10, 10], [20, 70], [50, 30], [55, 85], [90, 90]] var xScale = d3.scaleLinear() .domain([0, 100]) .range([0,960]); //chart becomes flipped if enter [960, 0] into range instead var yScale = d3.scaleLinear() .domain([0, 100]) .range([500,0]); var line = d3.line() .x(function(d){return xScale(d[0]) }) .y(function(d){return yScale(d[1]) }) .curve(d3.curveBasis); console.log(line(data)); svg.append("path") .datum(data) //.datum tells the computer to create ONE thing, vs plotting multiple circles .attr("d", line); //The shape of an SVG Path element is defined by one attribute: d </script> </body>
https://d3js.org/d3.v4.min.js