D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
hassam7
Full window
Github gist
d3 scaleBandExample
Built with
blockbuilder.org
<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; } </style> </head> <body> <script> var width = 960; var height = 500; var svg = d3.select("body").append("svg") .attr("width", width) .attr("height", height) var data = [10, 20, 30, 40, 50] var yScale = d3.scaleLinear() .domain([0, 50]) .rangeRound([ 10, 500 ]) var xScale = d3.scaleBand() .domain(d3.range(data.length)) .rangeRound([0, 300]) .paddingInner(0.05); svg.selectAll('rect') .data(data) .enter() .append('rect') .attr('x', (d, i) => xScale(i)) .attr('y', (d, i) => height - yScale(d)) .attr('width', xScale.bandwidth()) .attr('height', (d, i) => yScale(d)) function update() { data = [] for(var i = 0;i < 5; i++) data[i] = Math.round(Math.random() * 50); console.log(data) svg.selectAll('rect') .data(data) .transition() .attr('y', d => height - yScale(d)) .attr('height', d => yScale(d)) } // setInterval(() => update(), 200) </script> </body>
https://d3js.org/d3.v4.min.js