forked from arnicas/interactive-vis-course
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bar_homework_done_safe.html
214 lines (165 loc) · 6.05 KB
/
bar_homework_done_safe.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<!DOCTYPE html>
<!-- Modified example from enjalot: http://bl.ocks.org/enjalot/1429426 -->
<html>
<head>
<title>Top Causes of Deaths Bar Chart</title>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js"></script>
<style type="text/css">
body {
padding: 50px;
font-family: sans-serif;
font-size: 12pt;
}
button {
margin: 5px;
font-size: 15pt;
padding: 3px;
cursor: pointer;
}
input {
margin: 5px;
font-size: 15pt;
padding: 3px;
}
p {
width: 500px;
padding-left: 50px;
}
#chart {
padding-left: 75px;
}
#menu {
padding: 20px 75px;
}
#menu select {
background: #ccc;
width: 220px;
padding: 5px;
font-size: 14px;
line-height: 1;
border: 0;
border-radius: 0;
height: 34px;
}
.labels {
fill: white;
font-size: 10px;
}
</style>
</head>
<body>
<h2>Top 20 Contributors to Death of Under 4 Year Olds By Country</h2>
<p>For each country, these causes contributed what percent of the total deaths of under four year olds? Sorted to show the top 20 countries per disease cost. (Need real source here.)
<p>Blue bars were present in your previous selection.
<p>(Code Based on <a href="http://bl.ocks.org/enjalot/1429426">an example from enjalot</a>.)
<div id="menu">
<select>
<option value="Measles">Measles</option>
<option value="Sepsis">Sepsis</option>
<option value="Malaria">Malaria</option>
<option value="Meningitis_encephalitis">Meningitis and Encephalitis</option>
<option value="Respiratory_infections">Respiratory Infections</option>
<option value="Pertussis">Pertussis</option>
<option value="Congenital_anomalies">Congenital Defects</option>
<option value="HIV_AIDS">HIV and AIDS</option>
<option value="Diarrhoeal_diseases">Diarrheoa and Related</option>
<option value="Asphyxia_trauma">Asphyxia Trauma</option>
</select>
</div>
<div id="chart">
</div>
<script type="text/javascript">
var margin = {top: 5, right: 5, bottom: 5, left: 10};
var fullwidth = 400;
var fullheight = 500;
var height = fullheight - margin.top - margin.bottom;
var width = fullwidth - margin.right - margin.left;
var format = d3.format(".1%");
//setup the svg
var vis = d3.select("#chart").append("svg");
var svg = vis
.attr("width", fullwidth)
.attr("height", fullheight)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
d3.csv("data/deaths_04yearsold_2013percent.csv", function(error, data) {
var column = d3.select("#menu select").property("value");
var dataset = top20_by_column(data, column);
console.log(column, dataset);
redraw(dataset, column);
//setup our ui -- requires access to data variable, so inside csv
d3.select("#menu select")
.on("change", function() {
column = d3.select("select").property("value");
dataset = top20_by_column(data, column);
//console.log(column, dataset);
redraw(dataset, column);
});
}) // end csv
//make the bars for the first data set. They will be red at first.
function top20_by_column(data, column) {
return data.sort(function (a, b) {
return b[column] - a[column];
}).slice(0,20);
}
// This function is used to draw and update the data. It takes different data each time.
function redraw(data, column) {
console.log(column);
var max = d3.max(data, function(d) {return +d[column];});
xScale = d3.scale.linear()
.domain([0, max])
.range([0, width]);
yScale = d3.scale.ordinal()
.domain(d3.range(data.length))
.rangeBands([0, height], .2);
var bars = svg.selectAll("rect.bar")
.data(data, function (d) { return d.Country;}); // key function!
//update -- existing bars get blue when we "redraw". We don't change labels.
bars
.attr("fill", "steelblue");
//enter - new bars get set to orange when we "redraw."
bars.enter()
.append("rect")
.attr("class", "bar")
.attr("fill", "darkorange");
//exit -- remove ones that aren't in the index set
bars.exit()
.transition()
.duration(300)
.attr("width", 0)
.remove();
// transition all previous and new-- move the bars to proper widths and location
bars
.transition()
.duration(300)
.attr("width", function(d) {
return xScale(+d[column]);
})
.attr("height", yScale.rangeBand())
.attr("transform", function(d,i) {
return "translate(" + [0, yScale(i)] + ")"
});
// We are attaching the labels separately, not in a group with the bars...
var labels = svg.selectAll("text.labels")
.data(data, function (d) { return d.Country;}); // key function!
// everything gets a class and a text field. But we assign attributes in the transition.
labels.enter()
.append("text")
.attr("class", "labels");
labels.exit()
.remove();
labels.transition()
.duration(300)
.text(function(d) {
return d.Country + " " + format(d[column]);
})
.attr("transform", function(d,i) {
return "translate(" + xScale(+d[column]) + "," + yScale(i) + ")"
})
.attr("dy", "1.2em")
.attr("dx", "-3px")
.attr("text-anchor", "end");
} // end of draw function
</script>
</body>
</html>