D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
BarbierJeremy
Full window
Github gist
Stream_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; } svg { font: 10px sans-serif; } .line { fill: none; stroke: black; stroke-width: 2px; } </style> </head> <body> <script> var margin = {top: 20, right: 30, bottom: 20, left: 100}, width = 760 - margin.left - margin.right, height = 300 - margin.top - margin.bottom; 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 + ")"); var parseDate = d3.timeParse("%b %Y"); var x = d3.scaleTime().range([0, width]), y = d3.scaleLinear().range([height, 0]), c = d3.scaleOrdinal(d3.schemeCategory10); var line = d3.line() .curve(d3.curveBasis) .x(function(d) { return x(d.date); }) .y(function(d) { return y(d.price); }); var area = d3.area() .curve(d3.curveBasis) .x(function(d) { return x(d.data.date); }) .y0(function(d) { return y(d[0]); }) .y1(function(d) { return y(d[1]); }); d3.text('dataset.csv', function(error, raw) { var dsv = d3.dsvFormat(',') var data = dsv.parse(raw); // Nest stock values by symbol. var symbols = d3.nest() .key(function(d) { return d.symbol; }) .entries(data); var dates = d3.nest() .key(function(d) { return d.date; }) .entries(data); var keys = []; //console.log(dates); // Parse and caculate some values for each symbols symbols.forEach(function(s) { s.values.forEach(function(d) { d.date = parseDate(d.date); d.price = +d.price; }); keys.push(s.key) s.maxPrice = d3.max(s.values, function(d) { return d.price; }); s.sumPrice = d3.sum(s.values, function(d) { return d.price; }); //console.log(symbols); }); var table = [] dates.forEach(function(d){ var valeur = {}; var c = 0; valeur["date"] = parseDate(d.key); d.values.forEach(function(v){ valeur[keys[c]] = v.price; c++; }); //console.log(valeur); table.push(valeur); }); //console.log(keys); //console.log(table); var stack = d3.stack().keys(keys).offset(d3.stackOffsetWiggle); var series = stack(table); x.domain(d3.extent(data, function(d) { return d.date; })); y.domain([d3.min(series, stackMin), d3.max(series, stackMax)]) svg.selectAll(".layer") .data(series) .enter().append("g") .attr("class", "layer") .append("path") .attr("class", "area") .style("fill", function(d) { return c(d.key); }) .attr("d", area); svg.append("g") .attr("class", "axis axis--x") .attr("transform", "translate(0," + height + ")") .call(d3.axisBottom(x)); }); function stackMax(layer) { return d3.max(layer, function(d) { return d[1]; }); } function stackMin(layer) { return d3.min(layer, function(d) { return d[0]; }); } </script> </body>
https://d3js.org/d3.v4.min.js