D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
ssongalice
Full window
Github gist
Module 2 : Stacked area chart
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Stacked area chart</title> <script src="//d3js.org/d3.v3.min.js" charset="utf-8"></script> <style type="text/css"> body { background-color: #222222; font-family: Helvetica, sans-serif; } .content { width: 800px; margin:0 auto; padding:50px; text-align: center; } h1 { font-size: 24px; margin: 0; color:#ffffff; } p { font-size: 14px; color:#cccccc; } path:hover { opacity:1; } .axis path, .axis line { fill: none; stroke: #888888; shape-rendering: crispEdges; } .axis text { font-family: sans-serif; font-size: 11px; fill:#888888; } </style> </head> <body> <div class="content"> <h1>Population at regional level in South Korea</h1> <p>South Korea's total population is 20,724,094 in 2014. <br> And the area in South Korea with the largest population is Seoul and Gyeonggi.</p> <p>1992-2014, Source : Korea Statistical information Service</p> <p>Songyi Lim, www.attrs.co.kr</p> <div class="chart"></div> </div> <script type="text/javascript"> var stack = d3.layout.stack() .values(function(d) { return d.population; }) .order("reverse"); var w = 800; var h = 500; var padding = [ 20, 10, 50, 100 ]; var dateFormat = d3.time.format("%Y"); var xScale = d3.time.scale() .range([ padding[3], w - padding[1] - padding[3] ]); var yScale = d3.scale.linear() .range([ padding[0], h - padding[2] ]); var xAxis = d3.svg.axis() .scale(xScale) .orient("bottom") .ticks(15) .tickFormat(function(d) { return dateFormat(d); }); var yAxis = d3.svg.axis() .scale(yScale) .orient("left") .ticks(5); var area = d3.svg.area() .x(function(d) { return xScale(dateFormat.parse(d.x)); }) .y0(function(d) { return yScale(d.y0); }) .y1(function(d) { return yScale(d.y0 + d.y); }); var color = d3.scale.category10(); var svg = d3.select(".chart") .append("svg") .attr("width", w) .attr("height", h); var color = d3.scale.ordinal() .range(["#FADBAE", "#FFC794 ", "#FFA16D ", "#FC7F58 ", "#FF7865 "]); d3.csv("data.csv", function(data) { var years = ["1992", "1993", "1994", "1995", "1996", "1997", "1998", "1999", "2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009", "2010", "2011","2012","2013","2014"]; var dataset = []; for (var i = 0; i < data.length; i++) { dataset[i] = { regionName: data[i].regionName, population: [] }; for (var j = 0; j < years.length; j++) { var population = null; if (data[i][years[j]]) { population = +data[i][years[j]]; } dataset[i].population.push({ x: years[j], y: population }); } } stack(dataset); xScale.domain([ d3.min(years, function(d) { return dateFormat.parse(d); }), d3.max(years, function(d) { return dateFormat.parse(d); }) ]); var totals = []; for (i = 0; i < years.length; i++) { totals[i] = 0; for (j = 0; j < dataset.length; j++) { totals[i] += dataset[j].population[i].y; } } yScale.domain([ d3.max(totals), 0 ]); var paths = svg.selectAll("path") .data(dataset) .enter() .append("path") .attr("class", "area") .attr("d", function(d) { return area(d.population); }) .attr("stroke", "none") .attr("fill", function(d, i) { return color(i); }) .attr("opacity",0.8); paths.append("title") .text(function(d) { return d.regionName; }); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + (h - padding[2]) + ")") .call(xAxis); svg.append("g") .attr("class", "y axis") .attr("transform", "translate(" + padding[3] + ",0)") .call(yAxis); }); </script> </body> </html>
https://d3js.org/d3.v3.min.js