D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
JuanSMartinez
Full window
Github gist
Tutorial D3 2
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; } rect {fill: steelblue;} </style> </head> <body> <div id="chart"></div> <script> var myData=[12, 15, 45, 50, 45,56]; var margin = {top: 20, right: 20, bottom: 30, left: 40}, width = 100 - margin.left - margin.right, height = 500 - margin.top - margin.bottom; var wScale = d3.scaleLinear() .range([0, width]); var colScale = d3.scaleOrdinal(d3.schemeCategory20); var BAR_HEIGHT = 20; var svg = d3.select("#chart").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 + ")"); function update(myData) { wScale.domain([0, d3.max(myData)]); var bars = svg.selectAll(".bars") .data(myData); //Enter var barsEnter = bars.enter() .append("rect") .attr("class", "bars") .attr("width", 0); //Exit bars.exit() .transition() .duration(1000) .attr("width", 0) .remove(); //Update bars.merge(barsEnter) .attr("x", 0) .attr("y", function(d, i ) { return i * (BAR_HEIGHT+1);}) .attr("height", BAR_HEIGHT) .style("fill", function(d,i) { return colScale(i); }) .transition().duration(1000) .attr("width", function(d) { return wScale(d); }); } update(myData); </script> </body>
https://d3js.org/d3.v4.min.js