D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
jjcruzhernandez
Full window
Github gist
lineChart_D3
<!-- Used example https://bl.ocks.org/d3noob/38744a17f9c0141bcd04 --> <!DOCTYPE html> <meta charset="utf-8"> <style> .line {fill: none; stroke: steelblue; stroke-width: 3px;} </style> <body> <!-- load the d3.js library --> <script src="https://d3js.org/d3.v4.min.js"></script> <script> // set the dimensions and margins of the graph var margin = {top: 50, right: 50, bottom: 100, left: 100}, width = 960 - margin.left - margin.right, height = 500 - margin.top - margin.bottom; // set X and Y axis var x = d3.scaleBand() .range([0, width]) .padding(0.1); var y = d3.scaleLinear() .range([height, 0]); var valueline = d3.line() .x(function(d) {return x(d.Year);}) .y(function(d) {return y(d.Population);}); //create svg 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 + ")"); // getting data d3.csv("alabasterPD.csv", function(error, data) { if (error) throw error; // formating data data.forEach(function(d) { d.Year = d.Year; d.Population = +d.Population; }); // Scale the range of the data for X and Y axis x.domain(data.map(function(d) { return d.Year; })); y.domain([0, d3.max(data, function(d) { return d.Population; })]); // append line svg.append("path") .data([data]) .attr("class", "line") .attr("d", valueline); // add the x-axis svg.append("g") .attr("transform", "translate(0," + height + ")") .call(d3.axisBottom(x)) .selectAll("text") .style("text-anchor", "end") .attr("dx", ".8em") .style("font-size", "15px") .attr("dy", ".75em") .attr("transform", "rotate(-35)"); // add x-axis label svg.append("text") .attr("text-anchor", "end") .attr("x", width/2) .attr("y", height + 50) .text("Year"); // add the y-axis svg.append("g") .call(d3.axisLeft(y)); //add y-axis label svg.append("text") .attr("transform", "rotate(-90)") .attr("x",0 - (height / 2)) .attr("y", 0 - margin.left + 20) .attr("dy", "1em") .style("text-anchor", "middle") .text("Population"); //adding title svg.append("text") .attr("text-anchor", "end") .attr("x", width/2 + 50) .attr("y", 0) .text("Alabaster Population per Year"); }); </script> </body>
https://d3js.org/d3.v4.min.js