Last active
February 26, 2016 23:15
-
-
Save cflavs/f322fa0af50e23ea0031 to your computer and use it in GitHub Desktop.
Horizontal 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
name | value | |
---|---|---|
AC | 0 | |
AL | 61.98 | |
AP | 3.88 | |
AM | 26.29 | |
BA | 33.06 | |
CE | 31.32 | |
DF | 25.34 | |
ES | 48.76 | |
GO | 16.29 | |
MA | 15.3 | |
MT | 28.7 | |
MS | 18.42 | |
MG | 14.69 | |
PA | 22.12 | |
PB | 33.18 | |
PR | 26.66 | |
PE | 36.87 | |
PI | 7.7 | |
RJ | 26.17 | |
RN | 26.5 | |
RS | 15.28 | |
RO | 35.07 | |
RR | 19.76 | |
SC | 11.51 | |
SP | 3.69 | |
SE | 30.42 | |
TO | 18.43 |
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; | |
} | |
.bar rect { | |
fill: steelblue; | |
} | |
.bar text.value { | |
fill: white; | |
} | |
.axis { | |
shape-rendering: crispEdges; | |
} | |
.axis path { | |
fill: none; | |
} | |
.x.axis line { | |
stroke: #fff; | |
stroke-opacity: .8; | |
} | |
.y.axis path { | |
stroke: black; | |
} | |
</style> | |
<body> | |
<script src="//d3js.org/d3.v3.min.js"></script> | |
<script> | |
var m = [30, 10, 10, 30], | |
w = 560 - m[1] - m[3], | |
h = 530 - m[0] - m[2]; | |
var format = d3.format("%"); | |
var x = d3.scale.linear().range([0, w]), | |
y = d3.scale.ordinal().rangeRoundBands([0, h], .1); | |
var xAxis = d3.svg.axis().scale(x).orient("top").tickSize(-h), | |
yAxis = d3.svg.axis().scale(y).orient("left").tickSize(0); | |
var svg = d3.select("body").append("svg") | |
.attr("width", w + m[1] + m[3]) | |
.attr("height", h + m[0] + m[2]) | |
.append("g") | |
.attr("transform", "translate(" + m[3] + "," + m[0] + ")"); | |
d3.tsv("data.tsv", function(error, data) { | |
if (error) throw error; | |
// Parse numbers, and sort by value. | |
data.forEach(function(d) { d.value = +d.value; }); | |
data.sort(function(a, b) { return b.value - a.value; }); | |
// Set the scale domain. | |
x.domain([0, d3.max(data, function(d) { return d.value; })]); | |
y.domain(data.map(function(d) { return d.name; })); | |
var bar = svg.selectAll("g.bar") | |
.data(data) | |
.enter().append("g") | |
.attr("class", "bar") | |
.attr("transform", function(d) { return "translate(0," + y(d.name) + ")"; }); | |
bar.append("rect") | |
.attr("width", function(d) { return x(d.value); }) | |
.attr("height", y.rangeBand()); | |
bar.append("text") | |
.attr("class", "value") | |
.attr("x", function(d) { return x(d.value); }) | |
.attr("y", y.rangeBand() / 2) | |
.attr("dx", -3) | |
.attr("dy", ".35em") | |
.attr("text-anchor", "end") | |
.text(function(d) { return format(d.value); }); | |
svg.append("g") | |
.attr("class", "x axis") | |
.call(xAxis); | |
svg.append("g") | |
.attr("class", "y axis") | |
.call(yAxis); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment