Skip to content

Instantly share code, notes, and snippets.

@wykhuh
Last active April 20, 2018 18:24
Show Gist options
  • Save wykhuh/a01e7ca284bdd6d2df7e5cbcae194175 to your computer and use it in GitHub Desktop.
Save wykhuh/a01e7ca284bdd6d2df7e5cbcae194175 to your computer and use it in GitHub Desktop.
Stacked Bar Chart
license: mit
.d3-tip {
line-height: 1;
font-weight: bold;
padding: 12px;
background: rgba(0, 0, 0, 0.8);
color: #fff;
border-radius: 2px;
}
/* Creates a small triangle extender for the tooltip */
.d3-tip:after {
box-sizing: border-box;
display: inline;
font-size: 10px;
width: 100%;
line-height: 1;
color: rgba(0, 0, 0, 0.8);
content: "\25BC";
position: absolute;
text-align: center;
}
/* Style northward tooltips differently */
.d3-tip.n:after {
margin: -1px 0 0 0;
top: 100%;
left: 0;
}
<!DOCTYPE html>
<meta charset="utf-8">
<svg width="960" height="500"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
var data = [
{month: "Q1-2016", apples: 2000, bananas: 500},
{month: "Q2-2016", apples: 1500, bananas: -500},
{month: "Q3-2016", apples: 1000, bananas: 0 },
];
var series = d3.stack()
.keys(["apples", "bananas", "cherries", "dates"])
.offset(d3.stackOffsetDiverging)
(data);
var svg = d3.select("svg"),
margin = {top: 20, right: 30, bottom: 30, left: 60},
width = +svg.attr("width"),
height = +svg.attr("height");
var x = d3.scaleBand()
.domain(data.map(function(d) { return d.month; }))
.rangeRound([margin.left, width - margin.right])
.padding(0.1);
var y = d3.scaleLinear()
.domain([d3.min(series, stackMin), d3.max(series, stackMax)])
.rangeRound([height - margin.bottom, margin.top]);
var z = d3.scaleOrdinal(d3.schemeCategory10);
svg.append("g")
.selectAll("g")
.data(series)
.enter().append("g")
.attr("fill", function(d) { return z(d.key); })
.selectAll("rect")
.data(function(d) { return d; })
.enter().append("rect")
.attr("width", x.bandwidth)
.attr("x", function(d) { return x(d.data.month); })
.attr("y", function(d) { return y(d[1]); })
.attr("height", function(d) { return y(d[0]) - y(d[1]); })
svg.append("g")
.attr("transform", "translate(0," + y(0) + ")")
.call(d3.axisBottom(x));
svg.append("g")
.attr("transform", "translate(" + margin.left + ",0)")
.call(d3.axisLeft(y));
function stackMin(serie) {
return d3.min(serie, function(d) { return d[0]; });
}
function stackMax(serie) {
return d3.max(serie, function(d) { return d[1]; });
}
</script>
function renderStackedBarChart(inputData, dom_element_to_append_to, colorScheme) {
data = inputData
var margin = {top: 20, right: 20, bottom: 30, left: 40},
width = 900 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom,
height2 = height*0.75;
var x = d3.scale.ordinal()
.rangeRoundBands([0, width], .1);
var y = d3.scale.linear()
.rangeRound([height2, 0]);
var color = d3.scale.ordinal()
.range(colorScheme);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom")
.tickFormat(function(d) {
return (new Date(+d) + "").substring(3,16);
});
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.tickFormat(d3.format(".2s"));
var tip = d3.tip()
.attr('class', 'd3-tip')
.offset([-10, 0])
.html(function(d) {
return "<div><strong> " +d.name + ":</strong></div><div> <span style='color:white'>" + (d.y1 - d.y0).toFixed(0) + "</span></div>";
})
var svg = d3.select(dom_element_to_append_to).append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
var main = svg.append("g")
.attr("height", height2)
.attr("transform", "translate(" + margin.left + "," + (height*0.20) + ")");
var active_link = "0";
var legendClicked;
var legendClassArray = [];
var y_orig;
svg.call(tip);
color.domain(d3.keys(data[0]).filter(function(key) { return key !== "label"; }));
data.forEach(function(d) {
var mylabel = d.label;
var y0 = 0;
d.params = color.domain().map(function(name) { return {mylabel:mylabel, name: name, y0: y0, y1: y0 += +d[name]}; });
d.total = d.params[d.params.length - 1].y1;
});
data.sort(function(a, b) { return b.total - a.total; });
x.domain(data.map(function(d) { return (d.label); }));
y.domain([0, d3.max(data, function(d) { return d.total; })]);
main.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height2 + ")")
.call(xAxis);
main.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end");
var state = main.selectAll(".state")
.data(data)
.enter().append("g")
.attr("class", "g")
.attr("transform", function(d) { return "translate(" + "0" + ",0)"; });
state.selectAll("rect")
.data(function(d) {
return d.params;
})
.enter().append("rect")
.attr("width", x.rangeBand())
.attr("y", function(d) { return y(d.y1); })
.attr("x",function(d) {
return x(d.mylabel)
})
.attr("height", function(d) { return y(d.y0) - y(d.y1); })
.attr("class", function(d) {
var classLabel = d.name.replace(/\s/g, '');
return "class" + classLabel;
})
.style("fill", function(d) { return color(d.name); });
state.selectAll("rect")
.on("mouseover", function(d){
var delta = d.y1 - d.y0;
var xPos = parseFloat(d3.select(this).attr("x"));
var yPos = parseFloat(d3.select(this).attr("y"));
var height = parseFloat(d3.select(this).attr("height"))
d3.select(this).attr("stroke","blue").attr("stroke-width",0.8);
tip.show(d);
/*main.append("text")
.attr("x",xPos)
.attr("y",yPos +height/2)
.attr("class","tooltip")
.text(d.name +": "+ delta.toFixed(0)); */
})
.on("mouseout",function(){
tip.hide();
/*main.select(".tooltip").remove();*/
d3.select(this).attr("stroke","pink").attr("stroke-width",0.2);
})
var legend = svg.selectAll(".legend")
.data(color.domain().slice().reverse())
.enter().append("g")
.attr("class", function (d) {
legendClassArray.push(d.replace(/\s/g, ''));
return "legend";
})
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });
legendClassArray = legendClassArray.reverse();
legend.append("rect")
.attr("x", width - 18)
.attr("width", 18)
.attr("height", 18)
.style("fill", color)
.attr("id", function (d, i) {
return "id" + d.replace(/\s/g, '');
})
.on("mouseover",function(){
if (active_link === "0") d3.select(this).style("cursor", "pointer");
else {
if (active_link.split("class").pop() === this.id.split("id").pop()) {
d3.select(this).style("cursor", "pointer");
} else d3.select(this).style("cursor", "auto");
}
})
.on("click",function(d){
if (active_link === "0") {
d3.select(this)
.style("stroke", "black")
.style("stroke-width", 2);
active_link = this.id.split("id").pop();
plotSingle(this);
for (i = 0; i < legendClassArray.length; i++) {
if (legendClassArray[i] != active_link) {
d3.select("#id" + legendClassArray[i])
.style("opacity", 0.5);
}
}
}
else {
if (active_link === this.id.split("id").pop()) {
d3.select(this)
.style("stroke", "none");
active_link = "0";
for (i = 0; i < legendClassArray.length; i++) {
d3.select("#id" + legendClassArray[i])
.style("opacity", 1);
}
restorePlot(d);
}
}
});
legend.append("text")
.attr("x", width - 24)
.attr("y", 9)
.attr("dy", ".35em")
.style("text-anchor", "end")
.text(function(d) { return d; });
function restorePlot(d) {
state.selectAll("rect").forEach(function (d, i) {
d3.select(d[idx])
.transition()
.duration(1000)
.attr("y", y_orig[i]);
})
for (i = 0; i < legendClassArray.length; i++) {
if (legendClassArray[i] != class_keep) {
d3.selectAll(".class" + legendClassArray[i])
.transition()
.duration(1000)
.delay(750)
.style("opacity", 1);
}
}
}
function plotSingle(d) {
class_keep = d.id.split("id").pop();
idx = legendClassArray.indexOf(class_keep);
for (i = 0; i < legendClassArray.length; i++) {
if (legendClassArray[i] != class_keep) {
d3.selectAll(".class" + legendClassArray[i])
.transition()
.duration(1000)
.style("opacity", 0);
}
}
y_orig = [];
state.selectAll("rect").forEach(function (d, i) {
h_keep = d3.select(d[idx]).attr("height");
y_keep = d3.select(d[idx]).attr("y");
y_orig.push(y_keep);
h_base = d3.select(d[0]).attr("height");
y_base = d3.select(d[0]).attr("y");
h_shift = h_keep - h_base;
y_new = y_base - h_shift;
d3.select(d[idx])
.transition()
.ease("bounce")
.duration(1000)
.delay(750)
.attr("y", y_new);
})
}
};
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.x.axis path {
display: none;
}
.tooltip{
text-anchor: middle;
font-family: sans-serif;
font-size: 20px;
font-weight: bold;
fill:black;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment