D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
SebDelSab
Full window
Github gist
rendu_TP3
Built with
blockbuilder.org
<!DOCTYPE html> <meta charset="utf-8"> <style type="text/css"> svg { font-family: "Helvetica Neue", Helvetica; } .line { fill: none; stroke: #000; stroke-width: 2px; } </style> <body> <script src="//d3js.org/d3.v3.min.js"></script> <script> //not the good version of d3 //this code was inspired by https://bl.ocks.org/mbostock/1256572 var m = [20, 20, 30, 20], w = 960 - m[1] - m[3], h = 500 - m[0] - m[2]; var x, y, duration = 1000, delay = 500; 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]) .append("g") .attr("transform", "translate(" + m[3] + "," + m[0] + ")"); var stocks, symbols; // An 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("https://gist.githubusercontent.com/mbostock/1256572/raw/44c086a6019de56dce35a47197a19468b3e4ad57/stocks.csv", function(data) { var parse = d3.time.format("%b %Y").parse; // Nest stock values by symbol. symbols = d3.nest() .key(function(d) { return d.symbol; }) .entries(stocks = data); // Parse dates and numbers. We assume values are sorted by date. // Also compute the maximum price per symbol, needed for the y-domain. 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; }); }); // Sort by maximum price, descending. symbols.sort(function(a, b) { return b.maxPrice - a.maxPrice; }); var g = svg.selectAll("g") .data(symbols) .enter().append("g") .attr("class", "symbol"); setTimeout(prepare_areas); }); function prepare_areas() { 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 k = 1, n = symbols[0].values.length; d3.timer(function() { if ((k += 2) >= n - 1) { setTimeout(areas, 0); return true; } }); svg.insert("defs", ".symbol") .append("clipPath") .attr("id", "clip") function areas() { var g = svg.selectAll(".symbol") .attr("clip-path", "url(#clip)"); g.each(function(d) { var e = d3.select(this); e.append("text") .attr("x", 12) .attr("dy", ".31em") .text(d.key); }); area .y0(h / 4 - 20); g.select("circle").transition() .duration(duration) .attr("transform", function(d) { return "translate(" + (w - 60) + "," + (-h / 4) + ")"; }) .remove(); var g = svg.selectAll(".symbol") .attr("transform", function(d, i) { return "translate(0," + (i * h / 4 + 10) + ")"; }); g.select("text").transition() .duration(duration) .attr("transform", function(d) { return "translate(" + (w - 60) + "," + (h / 4 - 20) + ")"; }) .attr("dy", "0em"); 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") .attr("transform", function(d) { return "translate(0," + (d * (h / 4 - 20)) + ")"; }) d3.select(this).selectAll(".area").transition() .duration(duration) .style("fill-opacity", 1) .attr("d", area(d.values)) .each("end", function() { d3.select(this).style("fill-opacity", null); }); }); var g = svg.selectAll(".symbol"); g.select(".line") .attr("d", function(d) { return axis(d.values); }); g.each(function(d) { y.domain([0, d.maxPrice]); d3.select(this).select(".line").transition() .duration(duration) .style("stroke-opacity", 1) .each("end", function() { d3.select(this).style("stroke-opacity", null); }); d3.select(this).selectAll(".area") .filter(function(d, i) { return i; }) .transition() .duration(duration) .style("fill-opacity", 1e-6) .attr("d", area(d.values)) .remove(); d3.select(this).selectAll(".area") .filter(function(d, i) { return !i; }) .transition() .duration(duration) .style("fill", color(d.key)) .attr("d", area(d.values)); }); svg.select("defs").transition() .duration(duration) .remove(); g.transition() .duration(duration) .each("end", function() { d3.select(this).attr("clip-path", null); }); setTimeout(stackedArea, duration + delay); } } function stackedArea() { var stack = d3.layout.stack() .values(function(d) { return d.values; }) .x(function(d) { return d.date; }) .y(function(d) { return d.price; }) .out(function(d, y0, y) { d.price0 = y0; }) .order("reverse"); stack(symbols); y .domain([0, d3.max(symbols[0].values.map(function(d) { return d.price + d.price0; }))]) .range([h, 0]); area .y0(function(d) { return y(d.price0); }) .y1(function(d) { return y(d.price0 + d.price); }); var t = svg.selectAll(".symbol").transition() .duration(duration) .attr("transform", "translate(0,0)") .each("end", function() { d3.select(this).attr("transform", null); }); t.select("path.area") .attr("d", function(d) { return area(d.values); }); t.select("path.line") .style("stroke-opacity", function(d, i) { return i < 3 ? 1e-6 : 1; }) .attr("d", function(d) { return line(d.values); }); t.select("text") .attr("transform", function(d) { d = d.values[d.values.length - 1]; return "translate(" + (w - 60) + "," + y(d.price / 2 + d.price0) + ")"; }); setTimeout(streamgraph, duration + delay); } function streamgraph() { var stack = d3.layout.stack() .values(function(d) { return d.values; }) .x(function(d) { return d.date; }) .y(function(d) { return d.price; }) .out(function(d, y0, y) { d.price0 = y0; }) .order("reverse") .offset("wiggle"); stack(symbols); var t = svg.selectAll(".symbol").transition() .duration(duration); t.select("path.area") .attr("d", function(d) { return area(d.values); }); t.select("text") .attr("transform", function(d) { d = d.values[d.values.length - 1]; return "translate(" + (w - 60) + "," + y(d.price / 2 + d.price0) + ")"; }); setTimeout(overlappingArea, duration + delay); } function overlappingArea() { var g = svg.selectAll(".symbol"); y .domain([0, d3.max(symbols.map(function(d) { return d.maxPrice; }))]) .range([h, 0]); area .y0(h) .y1(function(d) { return y(d.price); }); var t = g.transition() .duration(duration); t.select(".area") .style("fill-opacity", .5) .attr("d", function(d) { return area(d.values); }); t.select("text") .attr("dy", ".31em") .attr("transform", function(d) { d = d.values[d.values.length - 1]; return "translate(" + (w - 60) + "," + y(d.price) + ")"; }); setTimeout(function() { svg.selectAll("*").remove(); svg.selectAll("g").data(symbols).enter().append("g").attr("class", "symbol"); prepare_areas(); }, duration+delay); } </script>
https://d3js.org/d3.v3.min.js