D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
AminaSGitHub
Full window
Github gist
step1
Built with
blockbuilder.org
<!DOCTYPE html> <meta charset="utf-8"> <body> <script src="//d3js.org/d3.v3.min.js"></script> <script> var m = [20, 20, 30, 20], w = 960 - m[1] - m[3], h = 500 - m[0] - m[2]; var x,y; var color = d3.scale.category10(); var svg = d3.select("body").append("svg") .attr("width", w + m[1] + m[3]) .attr("height", h + m[0] + m[2]); // Generateur de lignes var line = d3.svg.line(); // A line generator, for the dark stroke. var axis = d3.svg.line() // A area generator, for the dark stroke. var area = d3.svg.area() .interpolate("basis") .x(function(d) { return x(d.date); }) .y1(function(d) { return y(d.price); }); d3.csv("dataset.csv", function(data) { var parse = d3.time.format("%b %Y").parse; // regrouper par symbol symbols = d3.nest() .key(function(d) { return d.symbol; }) .entries(stocks = data); symbols.forEach(function(s) { s.values.forEach(function(d) { d.date = parse(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; }); }); var g = svg.selectAll("g") .data(symbols) .enter().append("g") .attr("class", "symbol"); x = d3.time.scale().range([0, w - 60]); y = d3.scale.linear().range([h / 4 - 20, 0]); // Compute the minimum and maximum date across symbols. x.domain([ d3.min(symbols, function(d) { return d.values[0].date; }), d3.max(symbols, function(d) { return d.values[d.values.length - 1].date; }) ]); var g = svg.selectAll(".symbol") .attr("transform", function(d, i) { return "translate(0," + (i * h / 4 + 10) + ")"; }); g.each(function(d) { var e = d3.select(this); e.append("path") .attr("class", "line"); }); var g = svg.selectAll(".symbol"); area .y0(h / 4 - 20); g.each(function(d) { y.domain([0, d.maxPrice]); d3.select(this).selectAll(".area") .data(d3.range(3)) .enter().insert("path", ".line") .attr("class", "area"); }); var g = svg.selectAll(".symbol"); axis .y(h / 4 - 21); g.each(function(d) { y.domain([0, d.maxPrice]); d3.select(this).selectAll(".area") .filter(function(d, i) { return !i; }) .attr("d", area(d.values)); }); }); </script>
https://d3js.org/d3.v3.min.js