D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
lucaguery
Full window
Github gist
barchart
Built with
blockbuilder.org
<!DOCTYPE html> <style> </style> <body> <script src="https://d3js.org/d3.v4.min.js"></script> <script> d3.csv('stocks.csv', function(error, data){ var n = 4, // number of samples m = 11; // number of series data.forEach(function(d) { d.price = +d.price d.date = d.date.slice(-4) ;}) var data_reshaped = d3.nest() .key(function(d) {return d.date;}) .sortKeys(d3.ascending) .key(function(d) {return d.symbol;}) .rollup(function(d) { return { avg_stock_value:d3.mean(d,function(g) { return +g.price}) }}) .entries(data); dataFormat = d3.range(m).map(function(index) { return d3.range(n).map(function(i) { return Math.round(data_reshaped[index].values[i].value.avg_stock_value*10)/10 }); }) data_stacked = d3.stack().keys(d3.range(m))(dataFormat) var max = d3.max(data,function(d,index){ return +d.price}) var margin = {top: 10, right: 10, bottom: 10, left: 10}, width = 960 - margin.left - margin.right, height = 500 - margin.top - margin.bottom; var y = d3.scaleLinear() .domain([0, max]) .range([0,height]); var x0 = d3.scaleBand() .range([0, width]) .domain(d3.range(m)); var x1 = d3.scaleBand() .domain(d3.range(n)) .range([0, x0.bandwidth()]); var z = d3.scaleOrdinal() .range(["#fe9c23","#d0743c","#a05d56","#6b486b"]); var svg = d3.select("body").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 + ")"); var bar = svg.append("g").selectAll("g") .data(dataFormat) .enter().append("g") .attr("transform", function(d, i) { return "translate(" + x0(i) + ",0)"; }) var bar_group = bar.selectAll("rect") .data(function(d) { return d; }) .enter() .append("rect") .style("fill",function(d,i){ return z(i)}) .attr("x", function(d, i) { return x1(i); }) .attr("height", y) .attr("y", function(d) { return height - y(d); }) .attr("width", x1.bandwidth()*0.8) }); </script>
https://d3js.org/d3.v4.min.js