-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathbar_homework.html
More file actions
214 lines (162 loc) · 6.18 KB
/
bar_homework.html
File metadata and controls
214 lines (162 loc) · 6.18 KB
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>
//TODO: fill in the others here. What do you use for the values?
</select>
</div>
<div id="chart">
</div>
<script type="text/javascript">
var fullwidth = 400;
var fullheight = 500;
var margin = {top: 10, right: 20, bottom: 20, left: 10};
var height = fullheight - margin.top - margin.bottom;
var width = fullwidth - margin.left - margin.right;
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 + ")");
// load the data and do stuff
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); // you need to finish this function below.
console.log(column, dataset);
//setup our ui -- requires access to data variable, so inside csv
d3.select("#menu select")
.on("change", function() {
column = d3.select("#menu select").property("value"); //TODO: How do you get the current value of the select menu?
dataset = top20_by_column(data, column);//TODO: How do you get the current filter/storted data?
//console.log(column, dataset);
redraw(dataset, column);
});
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) {
// hint: +data[column]
// TODO: fill in this function. The answer direction is in the wiki page for week8.
// You want to sort the data by the column, descending order, and then slice.
}
// This function is used to draw and update the data. It takes different data each time.
function redraw(data, column) {
var max = d3.max(data, function(d) {return +d[column];});
xScale = d3.scale.linear()
.domain([0, max]) //TODO: what goes here?])
.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; //TODO: what is your key value?}); // 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 darkorange 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();
//TODO: what goes here at the end of exit?
// transition -- move the bars to proper widths and location
bars
.transition()
.duration(300)
.ease("quad")
.attr("width", function(d) {
return xScale(+d[column]); //TODO: what goes here?);
})
.attr("height", yScale.rangeBand())//TODO: In an ordinal scale bar chart, what goes here?)
.attr("fill", "darkorange") // do what you like with the colors
.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) { //TODO: what is your key here? same as above.}); // 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(//TODO: How long do you want this to last?)
.text(function(d) {
return d.Country + " " + format(//TODO: what goes here?);
})
.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>