D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
matthieuMoisson
Full window
Github gist
TP3_dataviz_stream
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 c = d3.scaleOrdinal(d3.schemeCategory10); 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"); 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 numberOfSymbols = symbols.length, // number of layers m = symbols[0].values.length; // number of samples per layer var stack = d3.stack().keys(d3.range(numberOfSymbols)).offset(d3.stackOffsetWiggle), layers = stack(d3.transpose(d3.range(numberOfSymbols).map(function(indice) { return bumps(symbols,indice); }))); var x = d3.scaleLinear() .domain([0, m - 1]) .range([0, width]); var y = d3.scaleLinear() .domain([d3.min(layers, stackMin), d3.max(layers, stackMax)]) .range([height, 0]); var z = d3.interpolateCool; var area = d3.area() .x(function(d, i) { return x(i); }) .y0(function(d) { return y(d[0]); }) .y1(function(d) { return y(d[1]); }); svg.selectAll("path") .data(layers) .enter().append("path") .attr("d", area) .attr("fill", function(d) { return c(d.key) }); }); function stackMax(layer) { return d3.max(layer, function(d) { return d[1]; }); } function stackMin(layer) { return d3.min(layer, function(d) { return d[0]; }); } function bumps(symbols, indice) { var map = []; var nbValues = symbols[0].values.length; for(var i = 0; i < nbValues; ++i){ map[i] = parseInt(symbols[indice].values[i].price); } return map; } </script> </body>
https://d3js.org/d3.v4.min.js