Skip to content

Instantly share code, notes, and snippets.

@dmignon1907
Created February 2, 2018 09:03
Show Gist options
  • Save dmignon1907/b00abe41e860f70b71510663b3aabea8 to your computer and use it in GitHub Desktop.
Save dmignon1907/b00abe41e860f70b71510663b3aabea8 to your computer and use it in GitHub Desktop.
Stack_transition_mignon
license: mit
<!DOCTYPE html>
<style>
.axis .domain {
display: none;
}
.button1{
display:inline-block;
}
</style>
<body>
<div class="button1">
<button class="button" id="transition1">Transition 1</button>
</div>
<div class="button1">
<button class="button" id="transition2">Transition 2</button>
</div>
<div class="button1">
<button class="button" id="transition3">Transition 3</button>
</div>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
// Source: https://bl.ocks.org/mbostock/3886208
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 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, n])
.rangeRound([height, 0])
.nice();
var x = d3.scaleBand()
.rangeRound([0, width])
.paddingInner(0.1)
.align(0.1)
.domain(d3.range(m));
var x0 = d3.scaleBand()
.domain(d3.range(m))
.range([0, width], 0);
var x1 = d3.scaleBand()
.domain(d3.range(n))
.range([0, x0.bandwidth()]);
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(d3.stack().keys(d3.range(n))(data))
.enter().append("g")
.attr("class","transition")
.style("fill", function(d) { return z(d.key); })
.selectAll("rect")
.data(function(d) { return d; })
.enter().append("rect")
.attr("class","transitionr")
.attr("x", function(d, i) { return x(i); })
.attr("y", function(d) { return y(d[1]); })
.attr("height", function(d) { return y(d[0]) - y(d[1]); })
.attr("width", x.bandwidth());
var transition1=d3.select("#transition1")
transition1.on("click", function() {
console.log("test1");
console.log(data);
d3.selectAll(".transitionr")
.transition().duration(1000).style("opacity",0.8)
.attr("width", x.bandwidth()/10);
});
var transition2=d3.select("#transition2")
transition2.on("click", function() {
console.log("test2");
console.log(data);
d3.selectAll(".transition")
.transition().duration(1000).style("opacity",1)
.attr("transform", function(d, i) { return "translate(" + x1(i) + ",0)"; });
});
var transition3=d3.select("#transition3")
transition3.on("click", function() {
console.log("test3");
console.log(data);
d3.selectAll(".transitionr")
.transition().duration(1000).style("opacity",1)
.attr("height", function(d) { return height - y(d[1]); });
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment