D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
lcastrogarcia
Full window
Github gist
TP3-small-multiples
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: 12px sans-serif; } .line { fill: none; stroke: black; stroke-width: 2px; } </style> </head> <body> <script> //x ne change pas, y change : translation, supperposition, overlapp var m = [40, 60, 20, 20], width = 960 - m[1] - m[3], height = 120 - m[0] - m[2]; var color = d3.scaleOrdinal(d3.schemeCategory10); 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 area = d3.area() .curve(d3.curveBasis) .x(function(d) { return x(d.date); }) .y1(function(d) { return y(d.price); }); var line = d3.line() .curve(d3.curveBasis) .x(function(d) { return x(d.date); }) .y(function(d) { return y(d.price); }); var axis = d3.line() .curve(d3.curveBasis) .x(function(d) { return x(d.date); }) .y(height); d3.text('data.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); // Calculer valeurs pour chaque symbole symbols.forEach(function(s) { s.values.forEach(function(d) { d.date = parseDate(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; }); }); x.domain(d3.extent(data, function(d) { return d.date; })); y.domain([0, d3.max(symbols.map(function(d) { return d.maxPrice; }))]) area.y0(y(0)); var svg = d3.select("body").selectAll("svg") .data(symbols) .enter().append("svg") .attr("width", width + m[3] + m[1]) .attr("height", height + m[0] + m[2]) .append("g") .attr("transform", "translate(" + m[3] + "," + m[0] + ")"); //ajouter chemin svg.append("path") .attr("class", "area") .attr("d", function(d) { y.domain([0, d.maxPrice]); return area(d.values); }) .style("fill", function(d) { return color(d.key)}) axis.y(height / 4 + 46); svg.append("path") .attr("class", "line") .attr("d", function(d) { return axis(d.values); }); svg.append("text") .attr("x", width+10) .attr("y", 40) .text(function(d) { return d.key; }); }); </script> </body>
https://d3js.org/d3.v4.min.js