Last active
February 23, 2016 20:20
-
-
Save cflavs/c072a0d89095dc1093d9 to your computer and use it in GitHub Desktop.
Tooltip Group Bar Chart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Uf | 2010 | 2011 | 2012 | 2013 | |
---|---|---|---|---|---|
AC | 0 | 20.23 | 22.8 | 22.5 | |
AL | 61.98 | 74.51 | 63.91 | 58.86 | |
AP | 3.88 | 17.1 | 29.2 | 23.4 | |
AM | 26.29 | 29.19 | 28.13 | 23.4 | |
BA | 33.06 | 33.37 | 2.53 | 34.45 | |
CE | 31.32 | 30.69 | 40.58 | 47.16 | |
DF | 25.34 | 28.29 | 31.37 | 27.28 | |
ES | 48.76 | 47.79 | 46.39 | 40.74 | |
GO | 16.29 | 16.41 | 13.61 | 29.06 | |
MA | 15.3 | 18.49 | 21.71 | 23.21 | |
MT | 28.7 | 30.69 | 29.95 | 31.46 | |
MS | 18.42 | 17.48 | 14.65 | 17.39 | |
MG | 14.69 | 18.4 | 19.76 | 19.89 | |
PA | 22.12 | 14.71 | 38.89 | 39.61 | |
PB | 38.18 | 43.07 | 38.69 | 34.33 | |
PR | 26.66 | 18.6 | 13.76 | 11.18 | |
PE | 36.87 | 36.67 | 34.3 | 30.94 | |
PI | 7.7 | 10.86 | 15.15 | 15.30 | |
RJ | 26.17 | 23.55 | 22.52 | 25.56 | |
RN | 25.5 | 0 | 11.43 | 25.02 | |
RS | 15.48 | 16 | 18.45 | 16.51 | |
RO | 35.07 | 25.31 | 28.05 | 26.91 | |
RR | 19.76 | 11.73 | 13.02 | 20.28 | |
SC | 11.51 | 5.62 | 9.32 | 10.06 | |
SP | 3.69 | 10.08 | 11.54 | 10.18 | |
SE | 30.42 | 32.11 | 38.04 | 40.08 | |
TO | 18.43 | 18.27 | 20.88 | 19.42 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<style> | |
body { | |
font: 10px sans-serif; | |
} | |
.axis path, | |
.axis line { | |
fill: none; | |
stroke: #000; | |
shape-rendering: crispEdges; | |
} | |
.bar { | |
fill: steelblue; | |
} | |
.x.axis path { | |
display: none; | |
} | |
.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; | |
</style> | |
<body> | |
<script src="//d3js.org/d3.v3.min.js"></script> | |
<script src="http://labratrevenge.com/d3-tip/javascripts/d3.tip.v0.6.3.js"></script> | |
<script> | |
var margin = {top: 20, right: 20, bottom: 30, left: 40}, | |
width = 960 - margin.left - margin.right, | |
height = 750 - margin.top - margin.bottom; | |
var x0 = d3.scale.ordinal() | |
.rangeRoundBands([0, width], .1); | |
var x1 = d3.scale.ordinal(); | |
var y = d3.scale.linear() | |
.range([height, 0]); | |
var color = d3.scale.ordinal() | |
.range(["#98abc5", "#a05d56", "#d0743c", "#ff8c00"]); | |
var xAxis = d3.svg.axis() | |
.scale(x0) | |
.orient("bottom"); | |
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 "<strong> Percentual:</strong> <span style='color:red'>" + d.value + "</span>"; | |
}) | |
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.call(tip); | |
d3.csv("data.csv", function(error, data) { | |
if (error) throw error; | |
var ageNames = d3.keys(data[0]).filter(function(key) { return key !== "Uf"; }); | |
data.forEach(function(d) { | |
d.ages = ageNames.map(function(name) { return {name: name, value: +d[name]}; }); | |
}); | |
x0.domain(data.map(function(d) { return d.Uf; })); | |
x1.domain(ageNames).rangeRoundBands([0, x0.rangeBand()]); | |
y.domain([0, d3.max(data, function(d) { return d3.max(d.ages, function(d) { return d.value; }); })]); | |
svg.append("g") | |
.attr("class", "x axis") | |
.attr("transform", "translate(0," + height + ")") | |
.call(xAxis); | |
svg.append("g") | |
.attr("class", "y axis") | |
.call(yAxis) | |
.append("text") | |
.attr("transform", "rotate(-90)") | |
.attr("y", 6) | |
.attr("dy", ".71em") | |
.style("text-anchor", "end") | |
.text("Homicidios Dolosos"); | |
var state = svg.selectAll(".uf") | |
.data(data) | |
.enter().append("g") | |
.attr("class", "uf") | |
.attr("transform", function(d) { return "translate(" + x0(d.Uf) + ",0)"; }); | |
state.selectAll("rect") | |
.data(function(d) { return d.ages; }) | |
.enter().append("rect") | |
.attr("width", x1.rangeBand()) | |
.attr("x", function(d) { return x1(d.name); }) | |
.attr("y", function(d) { return y(d.value); }) | |
.attr("height", function(d) { return height - y(d.value); }) | |
.on('mouseover', tip.show) | |
.on('mouseout', tip.hide) | |
.style("fill", function(d) { return color(d.name); }); | |
var legend = svg.selectAll(".legend") | |
.data(ageNames.slice().reverse()) | |
.enter().append("g") | |
.attr("class", "legend") | |
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; }); | |
legend.append("rect") | |
.attr("x", width - 18) | |
.attr("width", 18) | |
.attr("height", 18) | |
.style("fill", color); | |
legend.append("text") | |
.attr("x", width - 24) | |
.attr("y", 9) | |
.attr("dy", ".35em") | |
.style("text-anchor", "end") | |
.text(function(d) { return d; }); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment