D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
tblondelle
Full window
Github gist
3. Stacked 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 { font: 10px sans-serif; } .line { fill: none; stroke: black; stroke-width: 2px; .area { fill: lightsteelblue; } } </style> </head> <body> <script> var margin = { top: 40, right: 40, bottom: 40, left: 40 }, width = 500, //interieur height = 300; //interieur var x = d3.scaleTime().range([0, width]), y = d3.scaleLinear().range([height, 0]), // MODIF c = d3.scaleOrdinal(d3.schemeCategory10); //https://www.d3noob.org/2013/01/filling-area-under-graph.html var area = d3.area() //MODIF .x(function(d) { return x(d.data.date) }) .y0(function(d) { return y(d[0]) }) .y1(function(d) { return y(d[1]) }); d3.text('stocks.csv', function(error, raw) { var dsv = d3.dsvFormat(',') var data = dsv.parse(raw); // Nest stock values by dates. var nested_data = d3.nest() .key(function(d) { return d.date; // MODIF }) .entries(data); // Parse and caculate some values for each nested_data nested_data.forEach(function(s) { s.values.forEach(function(d) { s.date = d3.timeParse("%b %Y")(d.date); s[d.symbol] = +d.price }); s.maxPrice = d3.max(s.values, function(d) { return d.price; }); s.sumPrice = d3.sum(s.values, function(d) { return d.price; }); }); // MODIF var stack = d3.stack() .keys(["MSFT", "AMZN", "IBM", "AAPL"]) .order(d3.stackOrderNone) .offset(d3.stackOffsetNone); // Create the default x-domain. x.domain(d3.extent(nested_data, function(d) { return d.date; })); //MODIF Create the default y-domain. var nested_data_by_company = d3.nest() .key(function(d) { return d.symbol; // MODIF }) .entries(data); nested_data_by_company.forEach(function(s) { s.values.forEach(function(d) { d.date = d3.timeParse("%b %Y")(d.date); d.price = +d.price; }); s.maxPrice = d3.max(s.values, function(d) { return d.price; }); s.sumPrice = d3.sum(s.values, function(d) { return d.price; }); }); y.domain([0, d3.sum(nested_data_by_company.map(function(d) { return d.maxPrice; }))]) // Create the container "svg" and 1 subcontainer "g" for margins. 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 + ")"); //MODIF From the subcontainer "g", create 4 subsubcontainer "g" for each graph. var g = svg.selectAll("g").data(stack(nested_data)).enter() //MODIF .append("g") //MODIF Create a path for the area. g.append("path") .attr("class", "symbols") .attr("d", function(d) { console.log(d) return area(d); }) .style("fill", function(d) { return c(d.index) //MODIF }) // Display last value. g.append("text") .text(function(d, i) { return d.key; }) .attr("y", function(d) { return height - (d[d.length - 1][0] + d[d.length - 1][1]) / 2 }) .attr("x", width + 2) .style("font-family", "monospace"); }); </script> </body>
https://d3js.org/d3.v4.min.js