D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
ohdebby
Full window
Github gist
Small multiple line chart
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; } svg { border:1px solid #d0d0d0; } </style> </head> <body> <script> var numCountries = 10 var margin = {top: 20, right: 10, bottom: 20, left: 10}; var width = 80 - margin.left - margin.right; var height = 300 - margin.top - margin.bottom; var compareCountries = ["United Kingdom", "Norway", "Germany", "Canada", "Netherlands", "France", "Sweden", "Ireland", "Spain",] d3.csv("incomes.csv", function(data){ console.log(data); var svg = d3.select("body").append("svg") .attr("width", (width + margin.right+ margin.left)*compareCountries.length) .attr("height", height + margin.top + margin.bottom) .append("g") .attr('transform', 'translate('+[margin.left, margin.top]+')') var filteredData = data.filter(function(d){ return compareCountries.indexOf(d.country)> -1 && d.cutoff ==='cop50'; }); filteredData.forEach(function(d) { d.year = +d.year; d.val = +d.val; }); var americaData = data.filter(function(d){ return d.country === 'United States' && d.cutoff === 'cop50'; }) var incomeByCountry = d3.nest() .key(function(d) {return d.country}) .entries(filteredData); var yScale = d3.scaleLinear() .range([height,0]) .domain([0, d3.max(filteredData, function(d){ return d.val})]); console.log(yScale.domain()) var xScale = d3.scaleLinear() .range ([0,width]) .domain (d3.extent(filteredData, function(d){return d.year; })); var yAxis = d3.axisLeft() .scale(yScale) .tickFormat(d3.format('$,')); var xAxis = d3.axisBottom() .scale(xScale) .tickFormat(d3.format('')) var line = d3.line() .x(function(d) {return xScale(d.year)}) .y(function(d) {return yScale(d.val)}) console.log(incomeByCountry) var countries = svg.selectAll('.country') .data(incomeByCountry) .enter() .append('g') .attr('transform', function(d,i){ return 'translate('+(i * width)+',0)'; }); // countries.filter(function(d,i) {return i === 0}) // .append('g') // .call(yAxis); // countries.append('g') // .attr('transform', 'translate('+[0,height]+')') // .call(xAxis); countries.selectAll('path') .data(function(d){return [americaData, d.values]}) .enter() .append('path') .attr('d', line) .attr('fill','none') .attr('stroke', function (d,i){ return i === 0 ? 'steelblue':'gray'}) ; // svg.selectAll('path') // .data(incomeByCountry) // .enter() // .append('path') // .attr('d', function(d) {return line(d.values)}) // .attr('fill', 'none') // .attr('stroke', function(d) {return d.key ==='United States' ? 'steelblue' : 'gray'}) // .attr('stroke-width', function(d){return d.key === 'United States'? 3: 1}); // }) // line(filteredData) // svg.append('path') // .attr('d',line(filteredData)) // .attr('fill', 'none') // .attr('stroke', 'steelblue') // svg.append("text") // .text("Edit the code below to change me!") // .attr("y", 200) // .attr("x", 120) // .style("font-size", 36) // .style("font-family", "monospace") }); </script> </body>
https://d3js.org/d3.v4.min.js