D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
SpaceActuary
Full window
Github gist
Loss Triangles
Built with
blockbuilder.org
<!DOCTYPE html> <head> <meta charset="utf-8"> <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script> <script src="colorbrewer.js"></script> <style> body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; font-family: sans-serif; } svg { width:100%; height: 100% } text { text-anchor: end; } form { position: absolute; right: 10px; top: 50px; font-family: sans-serif; } </style> </head> <body> <form> <label><input type="checkbox" name="tritype" value="Calendar"> Calendar Year</label> </form> <script> var m = {t:50, r: 20, b: 20, l:120}, h = 400 - m.t - m.b, w = 700 - m.l - m.r, numFmt = d3.format(",.0f"); var svg = d3.select("body").append("svg") .attr("height", h + m.t + m.b) .attr("width", w + m.l + m.r) .append("g") .attr("transform", "translate(" + m.l + "," + m.t + ")"); var y = d3.scale.linear().range([0, h]), x = d3.scale.linear().range([0, w]), color = d3.scale.ordinal().range(colorbrewer.Spectral[9]), t = 200; d3.csv("data.csv", function(data){ data = data.filter(function(d){ return d.d <= 9; }) y.domain(d3.extent(data, function(d){ return d.k; })); x.domain(d3.extent(data, function(d){ return d.j; })); color.domain(data.map(function(d){ return d.d; })); console.log(color.range()) var text = svg.selectAll("text.amount").data(data) text.enter().append("text").attr("class", "amount") .text(function(d){ return numFmt(d.paid); }) .attr("x", function(d){ return x(d.j); }) .attr("y", function(d){ return y(d.k); }) .attr("fill", function(d){ return d3.hsl(color(d.d)).darker(2.5); }) .attr("stroke", "none"); d3.selectAll("input[type='checkbox']") .on("change", function change() { if(d3.select(this).property("checked") === true){ console.log("calendar year"); x.domain(d3.extent(data, function(d){ return d.d; })); svg.selectAll("text.amount") .transition() .ease("linear") //.delay(function(d){ return d.k * 500; }) .duration(function(d){ return (d.k - 1) * t ; }) .attr("x", function(d){ return x(d.d); }) } else { console.log("development age"); x.domain(d3.extent(data, function(d){ return d.j; })); svg.selectAll("text.amount") .transition() .ease("linear") //.delay(function(d){ return d.k * 500; }) .duration(function(d){ return (d.k - 1) * t; }) .attr("x", function(d){ return x(d.j); }) }; } ); }) </script> </body>
https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js