D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
Hugues-Moreau
Full window
Github gist
grouped bar chart
Built with
blockbuilder.org
<!DOCTYPE html> <meta charset="utf-8"> <style> .axis text { font: 10px sans-serif; } .axis path, .axis line { fill: none; stroke: #000; shape-rendering: crispEdges; } .x.axis path { display: none; } </style> <body> <script src="//d3js.org/d3.v4.min.js"></script> <script> /* var n = 10, // number of samples m = 5; // number of series var data = d3.range(m).map(function() { return d3.range(n).map(Math.random); }); */ var data_AMZN = []; var data_MSFT = []; var data_IBM = []; var data_AAPL = []; d3.csv('stocks.csv', function(error, data){ data.forEach(function(d){ if (d.symbol == "AMZN"){ data_AMZN.push(+d.price) } else if (d.symbol == "IBM"){ data_IBM.push(+d.price) } else if (d.symbol == "AAPL"){ data_AAPL.push(+d.price) } else if (d.symbol == "MSFT"){ data_MSFT.push(+d.price) }; }); }); var data = [data_AMZN , data_MSFT , data_IBM , data_AAPL]; var n = data.length; var m = data_AMZN.length; console.log(data) var margin = {top: 20, right: 30, bottom: 30, left: 40}, width = 960 - margin.left - margin.right, height = 500 - margin.top - margin.bottom; var y = d3.scaleLinear() .domain([0, 1]) .range([height, 0]); var x0 = d3.scaleBand() .domain(d3.range(n)) .range([0, width], .2); var x1 = d3.scaleBand() .domain(d3.range(m)) .range([0, x0.bandwidth() - 10]); var z = d3.scaleOrdinal() .range(["#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56", "#d0743c", "#ff8c00"]); 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 + ")"); svg.append("g").selectAll("g") .data(data) .enter().append("g") .style("fill", function(d, i) { return z(i); }) .attr("transform", function(d, i) { return "translate(" + x1(i) + ",0)"; }) .selectAll("rect") .data(function(d) { return d; }) .enter().append("rect") .attr("width", x1.bandwidth()) .attr("height", y) .attr("x", function(d, i) { return x0(i); }) .attr("y", function(d) { return height - y(d); }); </script>
https://d3js.org/d3.v4.min.js