D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
hamzaECL
Full window
Github gist
Stacked to grouped bar animation
Built with
blockbuilder.org
<!DOCTYPE html encoding="utf-8"> <html> <head> <script src="https://d3js.org/d3.v4.min.js"></script> </head> <style> form { font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; position: absolute; left: 10px; top: 10px; } label { display: block; } </style> <form> <label><input type="radio" name="mode" value="grouped"> Grouped</label> <label><input type="radio" name="mode" value="stacked" checked> Stacked</label> </form> <body> <script> var margin = { top: 20, right: 20, bottom: 25, left: 40 }, width = 960 - margin.left - margin.right, height = 500 - margin.top - margin.bottom; var svg = d3.select("body") .append("svg") .attr("width", width + margin.left + margin.right) .attr("height", height + margin.top + margin.bottom) var g = svg.append("g") .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); d3.csv("stocks.csv", function(data){ var companies = [], dates = []; data.forEach(function(d) { var i = 0; for( i = 0; i < companies.length; i++){ if(d.symbol === companies[i]){ break; } } if (i=== companies.length){ companies.push(d.symbol); } for( i = 0; i < dates.length; i++){ if(d.date === dates[i]){ break; } } if (i=== dates.length){ dates.push(d.date); } }); var measures_by_date = new Array(dates.length); for( var i =0; i < measures_by_date.length; i++){ measures_by_date[i] = new Array(companies.length); } data.forEach(function(d){ var company_index = companies.indexOf(d.symbol); var date_index = dates.indexOf(d.date); measures_by_date[date_index][company_index] = +d.price; }) var xz = companies, yz = measures_by_date, y01z = d3.stack().keys(d3.range(measures_by_date.length))(d3.transpose(yz)), yMax = d3.max(yz, function(y) { return d3.max(y); }), y1Max = d3.max(y01z, function(y) { return d3.max(y, function(d) { return d[1]; }); }); console.log(yMax) var x = d3.scaleBand() .domain(xz) .rangeRound([0, width]) .padding(0.08); var y = d3.scaleLinear() .domain([0, y1Max]) .range([height, height/5]); var color = d3.scaleOrdinal() .domain(d3.range(companies.length)) .range(d3.schemeCategory10); var series = g.selectAll(".series") .data(y01z) .enter().append("g") .attr("fill", function(d, i) { return color(i); }); var rect = series.selectAll("rect") .data(function(d) { return d; }) .enter().append("rect") .attr("x", function(d, i) { return x(companies[i%companies.length]); }) .attr("y", height) .attr("width", x.bandwidth()) .attr("height", 0); rect.transition() .delay(function(d, i) { return i * 10; }) .attr("y", function(d) { return y(d[1]); }) .attr("height", function(d) { return y(d[0]) - y(d[1]); }); g.append("g") .attr("transform", "translate(0," + height + ")") .call(d3.axisBottom(x) .tickSize(0) .tickPadding(6)); d3.selectAll("input") .on("change", changed); var timeout = d3.timeout(function() { d3.select("input[value=\"grouped\"]") .property("checked", true) .dispatch("change"); }, 2000); function changed() { timeout.stop(); if (this.value === "grouped") transitionGrouped(); else transitionStacked(); } function transitionGrouped() { y.domain([0, yMax]); g.select(".stacked_axis").remove(); g.append("g") .attr("class", "grouped_axis") .call(d3.axisLeft(y) .tickSize(0) .tickPadding(6)); rect.transition() .duration(800) .delay(function(d, i) { return i * 10; }) .attr("x", function(d, i) {return x(companies[i%companies.length]) + x.bandwidth() / measures_by_date.length * this.parentNode.__data__.key; }) .attr("width", x.bandwidth() / measures_by_date.length) .transition() .attr("y", function(d) { return y(d[1] - d[0]); }) .attr("height", function(d) { return y(0) - y(d[1] - d[0]); }); } function transitionStacked() { y.domain([0, y1Max]); g.select(".grouped_axis").remove() g.append("g") .attr("class", "stacked_axis") .call(d3.axisLeft(y) .tickSize(0) .tickPadding(6)); rect.transition() .duration(800) .delay(function(d, i) { return i * 10; }) .attr("y", function(d) { return y(d[1]); }) .attr("height", function(d) { return y(d[0]) - y(d[1]); }) .transition() .attr("x", function(d, i) { return x(companies[i%companies.length]); }) .attr("width", x.bandwidth()); } }); </script> </body>
https://d3js.org/d3.v4.min.js