-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathdot_plot.html
More file actions
281 lines (226 loc) · 6.27 KB
/
dot_plot.html
File metadata and controls
281 lines (226 loc) · 6.27 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Adding Labels</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 {
background-color: #ddddff;
font-family: sans-serif;
}
h3 {
margin-left: 200px;
}
p {
margin-left: 200px;
font-size: 11pt;
color: gray;
}
div.y2015 {
color: #6699FF;
display: inline;
}
div.y1990 {
color: orange;
display: inline;
}
svg {
background-color: white;
}
circle {
stroke-width: 1;
}
circle.y2015 {
fill: #6699FF;
}
circle.y1990 {
fill: orange;
}
circle:hover {
stroke-width: 3;
stroke: white;
}
line.grid {
stroke: #eee;
}
line.between {
stroke: black;
}
.axis path,
.axis line {
fill: none;
stroke: black;
shape-rendering: crispEdges;
}
.axis text {
font-family: sans-serif;
font-size: 11px;
}
.xlabel {
font-famile: sans-serif;
font-size: 11px;
color: gray;
}
</style>
</head>
<body>
<h3>Access to Good Water Sources by World Region, <div class="y1990">1990</div> versus <div class="y2015">2015</div></h3>
<p>Source: WHO/UNICEF data, 2015.
<script type="text/javascript">
// this is the size of the svg container -- the white part
var fullwidth = 1000,
fullheight = 500;
// these are the margins around the graph. Axes labels go in margins.
var margin = {top: 20, right: 25, bottom: 20, left: 200};
var width = 1000 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var widthScale = d3.scale.linear()
.range([ 0, width]);
var heightScale = d3.scale.ordinal()
.rangeRoundBands([ margin.top, height], 0.2);
var xAxis = d3.svg.axis()
.scale(widthScale)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(heightScale)
.orient("left")
.innerTickSize([0]);
var svg = d3.select("body")
.append("svg")
.attr("width", fullwidth)
.attr("height", fullheight);
d3.csv("data/water_improvement_data.csv", function(error, data) {
if (error) {
console.log("error reading file");
}
data.sort(function(a, b) {
return d3.descending(+a.year2015, +b.year2015);
});
// in this case, i know it's out of 100 because it's percents.
widthScale.domain([0, 100]);
// js map: will make a new array out of all the d.name fields
heightScale.domain(data.map(function(d) { return d.name; } ));
// Make the faint lines from y labels to highest dot
var linesGrid = svg.selectAll("lines.grid")
.data(data)
.enter()
.append("line");
linesGrid.attr("class", "grid")
.attr("x1", margin.left)
.attr("y1", function(d) {
return heightScale(d.name) + heightScale.rangeBand()/2;
})
.attr("x2", function(d) {
return margin.left + widthScale(+d.year2015);
})
.attr("y2", function(d) {
return heightScale(d.name) + heightScale.rangeBand()/2;
});
// Make the dotted lines between the dots
var linesBetween = svg.selectAll("lines.between")
.data(data)
.enter()
.append("line");
linesBetween.attr("class", "between")
.attr("x1", function(d) {
return margin.left + widthScale(+d.year1990);
})
.attr("y1", function(d) {
return heightScale(d.name) + heightScale.rangeBand()/2;
})
.attr("x2", function(d) {
return margin.left + widthScale(d.year2015);
})
.attr("y2", function(d) {
return heightScale(d.name) + heightScale.rangeBand()/2;
})
.attr("stroke-dasharray", "5,5")
.attr("stroke-width", function(d, i) {
if (i == 7) {
return "1";
} else {
return "0.5";
}
});
// Make the dots for 1990
var dots1990 = svg.selectAll("circle.y1990")
.data(data)
.enter()
.append("circle");
dots1990
.attr("class", "y1990")
.attr("cx", function(d) {
return margin.left + widthScale(+d.year1990);
})
.attr("r", heightScale.rangeBand()/2)
.attr("cy", function(d) {
return heightScale(d.name) + heightScale.rangeBand()/2;
})
.style("stroke", function(d){
if (d.name === "The World") {
return "black";
}
})
.style("fill", function(d){
if (d.name === "The World") {
return "darkorange";
}
})
.append("title")
.text(function(d) {
return d.name + " in 1990: " + d.year1990 + "%";
});
// Make the dots for 2015
var dots2015 = svg.selectAll("circle.y2015")
.data(data)
.enter()
.append("circle");
dots2015
.attr("class", "y2015")
.attr("cx", function(d) {
return margin.left + widthScale(+d.year2015);
})
.attr("r", heightScale.rangeBand()/2)
.attr("cy", function(d) {
return heightScale(d.name) + heightScale.rangeBand()/2;
})
.style("stroke", function(d){
if (d.name === "The World") {
return "black";
}
})
.style("fill", function(d){
if (d.name === "The World") {
return "#476BB2";
}
})
.append("title")
.text(function(d) {
return d.name + " in 2015: " + d.year2015 + "%";
});
// add the axes
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(" + margin.left + "," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.attr("transform", "translate(" + margin.left + ",0)")
.call(yAxis);
svg.append("text")
.attr("class", "xlabel")
.attr("transform", "translate(" + (margin.left + width / 2) + " ," +
(height + margin.bottom) + ")")
.style("text-anchor", "middle")
.attr("dy", "12")
.text("Percent");
// Style one of the Y labels bold:
// a hack that works if you can unravel the selections - to style "The World" bold in the axis label, which is the 8th element:
var allYAxisLabels = d3.selectAll("g.y.axis g.tick text")[0]; // un-nest array
d3.select(allYAxisLabels[7]).style("font-weight", "bold");
// You could also use tick formatting to get a % sign on each axis tick
});
</script>
</body>
</html>