-
Notifications
You must be signed in to change notification settings - Fork 39
/
grouped_bars.html
147 lines (116 loc) · 4.39 KB
/
grouped_bars.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<!DOCTYPE html>
<!-- Minor change to Mike Bostock's example http://bl.ocks.org/mbostock/3887051 -->
<meta charset="utf-8">
<style>
body {
font: 12px sans-serif;
padding: 50px;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.bar {
fill: steelblue;
}
.x.axis path {
display: none;
}
</style>
<body>
<h2>Deaths from Illnesses in 2013 in Selected South African Countries</h2>
<p><a href="http://apps.who.int/gho/data/node.main.ghe100-by-cause?lang=en">WHO data</a>, deaths of children 0-4 years old in selected South African countries.</p>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js"></script>
<script>
var fullheight = 500,
fullwidth = 1000;
var margin = {top: 20, right: 20, bottom: 60, left: 40},
width = fullwidth - margin.left - margin.right,
height = fullheight - margin.top - margin.bottom;
var groupScale = d3.scale.ordinal()
.rangeRoundBands([0, width], .3);
var barScale = d3.scale.ordinal(); // the actual bars inside each group
var yScale = d3.scale.linear()
.range([height, 0]);
var colorScale = d3.scale.category10();
var xAxis = d3.svg.axis()
.scale(groupScale)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(yScale)
.orient("left")
.tickFormat(d3.format(".2s"));
var svg = d3.select("body").append("svg")
.attr("width", fullwidth)
.attr("height", fullheight)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
d3.csv("data/deaths_04yearsold_excerpt_2013.csv", function(error, data) {
if (error) {console.log(error);}
// get them from the columns instead this time -- all columns except the Country one
var illnessNames = d3.keys(data[0]).filter(function(key) { return key !== "Country"; });
colorScale.domain(illnessNames);
data.sort(function(a, b) {return d3.ascending(a.Country,b.Country);}); // alphabetize
data.forEach(function(d) {
d.illnesses = illnessNames.map(function(name) { return {name: name, value: +d[name]}; });
});
// country labels, spaced out - these are your groups of bars.
groupScale.domain(data.map(function(d) { return d.Country; })); // just all the country names
// spacing for each bar, re-used across country groups
barScale.domain(illnessNames).rangeRoundBands([0, groupScale.rangeBand()]);
yScale.domain([0, d3.max(data, function(d) { return d3.max(d.illnesses, function(d) {
return d.value;
});
})
]);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.selectAll("text")
.attr("dy", ".5em")
.attr("transform", "rotate(-30)")
.style("text-anchor", "end"); // rotating the labels on the countries a bit
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("Deaths");
// do the country groups first
var country = svg.selectAll("g.country")
.data(data)
.enter().append("g")
.attr("class", "country")
.attr("transform", function(d) { return "translate(" + groupScale(d.Country) + ",0)"; });
country.selectAll("rect")
.data(function(d) { return d.illnesses; }) // get the data array for each country
.enter().append("rect")
.attr("width", barScale.rangeBand())
.attr("x", function(d) { return barScale(d.name); }) // illness bars scale
.attr("y", function(d) { return yScale(d.value); }) // drawing from top down
.attr("height", function(d) { return height - yScale(d.value); }) // bottom to top
.style("fill", function(d,i) { return colorScale(d.name); }); // the illness is the name
var legend = svg.selectAll(".legend")
.data(illnessNames)
.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", function(d, i) { return colorScale(d);}); // d is the illness name
legend.append("text")
.attr("x", width - 24)
.attr("y", 9)
.attr("dy", ".35em")
.style("text-anchor", "end") // align with rects
.text(function(d) { return d.replace(/_/g, " "); });
});
</script>