This grouped bar chart is constructed from a CSV file storing the populations of different states by age group. The chart employs conventional margins and a number of D3 features:
forked from mbostock's block: Grouped Bar Chart
forked from emilioalvarado's block: Doing grouped bars...
xxxxxxxxxx
<meta charset="utf-8">
<style>
.rectangle:hover {
fill: orange;
}
.axis {
font: 12px sans-serif;
}
body {
font: 11px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
/*
body {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.bar {
fill: steelblue;
}
.x.axis path {
display: none;
}
*/
</style>
<body>
<div id="drop" align=center></div>
<label><input type="checkbox"> Sort values</label>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
var margin = {top: 20, right: 20, bottom: 30, left: 60},
width = 400 - margin.left - margin.right,
height = 400 - margin.top - margin.bottom;
var x0 = d3.scale.ordinal()
.rangeRoundBands([0, width], .2);
var x1 = d3.scale.ordinal();
var y = d3.scale.linear()
.range([height, 0]);
var color = d3.scale.ordinal()
.range(["#db0700", "#ff8d6b", "#32fe23"]);
var xAxis = d3.svg.axis()
.scale(x0)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.tickFormat(d3.format(".2s"));
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 + ")");
d3.csv("data.csv", function(error, data) {
if (error) throw error;
var voteType = d3.keys(data[0]).filter(function(key) { return (key !== "Municipio" & key !=="Ganador"); });
data.forEach(function(d) {
d.votes = voteType.map(function(name) { return {name: name, value: +d[name]}; });
});
x0.domain(data.map(function(d) { return d.Municipio; }));
x1.domain(voteType).rangeRoundBands([0, x0.rangeBand()]);
y.domain([10, d3.max(data, function(d) { return d3.max(d.votes, function(d) { return d.value; }); })]);
var legend = svg.selectAll(".legend")
.data(voteType.slice().reverse())
.enter().append("g")
.attr("class", "legend")
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });
legend.append("rect")
.attr("x", - 180)
.attr("width", 18)
.attr("height", 18)
.style("fill", color);
legend.append("text")
.attr("x", -160)
.attr("y", 9)
.attr("dy", ".35em")
//.style("text-anchor", "end")
.text(function(d) { return d; });
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.selectAll("text")
.style("font-size", "12px")
.style("text-anchor", "end")
.attr("dx", "-.8em")
.attr("dy", "-.55em")
.attr("transform", "rotate(-45)" );
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
svg.selectAll("rectangle")
.data(data)
.enter()
.append("rect")
.attr("class","rectangle").attr("fill","steelblue")
.attr("width", width/data.length-1)
.attr("height", function(d){
return height;
})
.append("title")
.text(function(d){
return d.Municipio;
});
//Ordenar los valores
d3.select("input").on("change", change);
var sortTimeout = setTimeout(function() {
d3.select("input").property("checked", true).each(change);
}, 2000);
function change() {
clearTimeout(sortTimeout);
// Copy-on-write since tweens are evaluated after a delay.
var xi = x0.domain(data.sort(this.checked
? function(a, b) { return b.VotoNo - a.VotoNo; }
: function(a, b) { return d3.ascending(a.Municipio, b.Municipio); })
.map(function(d) { return d.Municipio; }))
.copy();
svg.selectAll(".rectangle")
.sort(function(a, b) { return xi(a.Municipio) - xi(b.Municipio); });
var transition = svg.transition().duration(750),
delay = function(d, i) { return i * 50; };
transition.selectAll(".rectangle")
.delay(delay)
.attr("x", function(d) { return xi(d.Municipio); });
transition.select(".x.axis")
.call(xAxis)
.selectAll("g")
.delay(delay);
}
/*
var selector = d3.select("#drop")
.append("select")
.attr("id","dropdown")
.on("change", function(d){
selection = document.getElementById("dropdown");
y.domain([0, d3.max(data, function(d){
return +d[selection.value];})]);
yAxis.scale(y);
d3.selectAll(".rectangle")
.transition()
.attr("height", function(d){
return height - y(+d[selection.value])
console.log(+d[selection.value]);
}).attr("fill", getColor(selection.value))
.attr("x", function(d, i){
return (width / data.length) * i ;
})
.attr("y", function(d){
return y(+d[selection.value]);
})
.ease("linear")
.select("title")
.text(function(d){
return d.Country + " : " + d[selection.value];
});
d3.selectAll("g.y.axis")
.transition()
.call(yAxis);
});
selector.selectAll("option")
.data(elements)
.enter().append("option")
.attr("value", function(d){
return d;
})
.text(function(d){
return d;
})
*/
});
</script>
</body>
https://d3js.org/d3.v3.min.js