-
Notifications
You must be signed in to change notification settings - Fork 35
/
21_bar_linked.html
355 lines (293 loc) · 6.98 KB
/
21_bar_linked.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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>D3: Linked charts</title>
<script type="text/javascript" src="d3.js"></script>
<style type="text/css">
body {
background-color: gray;
}
#buttonContainer {
margin-bottom: 10px;
}
button {
padding: 15px;
}
svg {
display: block;
margin-bottom: 10px;
background-color: white;
}
g.bar text {
font-family: sans-serif;
font-size: 11px;
fill: white;
font-style: bold;
text-anchor: middle;
opacity: 0;
}
g.bar.highlight text {
opacity: 1;
}
g.bar.highlight rect {
fill: Maroon;
}
.axis path,
.axis line {
fill: none;
stroke: black;
shape-rendering: crispEdges;
}
.axis text {
font-family: sans-serif;
font-size: 11px;
}
</style>
</head>
<body>
<div id="buttonContainer">
<button id="sort">Sort</button>
</div>
<script type="text/javascript">
//Width, height, padding
var w = 600;
var h = 250;
var padding = 25;
//Sample data
var dataset = [
{
name: "Alice",
sales: 50,
bonus: 5
},
{
name: "Bob",
sales: 40,
bonus: 10
},
{
name: "Carmen",
sales: 65,
bonus: 15
},
{
name: "Dan",
sales: 55,
bonus: 30
},
{
name: "Esmerelda",
sales: 45,
bonus: 20
},
{
name: "Frank",
sales: 30,
bonus: 5
}
];
//Configure x and y scale functions
var xScale = d3.scale.ordinal()
.domain(d3.range(dataset.length))
.rangeRoundBands([ padding, w - padding ], 0.05);
//Now using two different y scales for two different charts
var salesScale = d3.scale.linear()
.domain([ 0, d3.max(dataset, function(d) {
return d.sales;
}) ])
.rangeRound([ h - padding, padding ]);
var bonusScale = d3.scale.linear()
.domain([ 0, d3.max(dataset, function(d) {
return d.bonus;
}) ])
.rangeRound([ h - padding, padding ]);
//Now using two different y axes
var salesAxis = d3.svg.axis()
.scale(salesScale)
.orient("left")
.ticks(5);
var bonusAxis = d3.svg.axis()
.scale(bonusScale)
.orient("left")
.ticks(5);
//
// Make the first chart (sales data)
//
//Create SVG element
var svg = d3.select("body")
.append("svg")
.attr("id", "salesChart")
.attr("width", w)
.attr("height", h);
//Create groups
var groups = svg.selectAll("g")
.data(dataset)
.enter()
.append("g")
.attr("class", "bar")
.attr("transform", function(d, i) {
return "translate(" + xScale(i) + ",0)";
});
//Add bar to each group
var rects = groups.append("rect")
.attr("x", 0)
.attr("y", function(d) {
return h - padding;
})
.attr("width", xScale.rangeBand())
.attr("height", 0)
.attr("fill", "SteelBlue");
//Add label to each group
groups.append("text")
.attr("x", xScale.rangeBand() / 2)
.attr("y", function(d) {
return salesScale(d.sales) + 14;
})
.text(function(d) {
return d.name + ": " + d.sales;
})
//Transition rects into place
rects.transition()
.delay(function(d, i) {
return i * 100;
})
.duration(1500)
.attr("y", function(d) {
return salesScale(d.sales);
})
.attr("height", function(d) {
return h - padding - salesScale(d.sales);
});
//Create y axis
svg.append("g")
.attr("class", "axis")
.attr("transform", "translate(" + padding + ",0)")
.attr("opacity", 0)
.call(salesAxis)
.transition()
.delay(2000)
.duration(1500)
.attr("opacity", 1.0);
//
// Make the second chart (bonus data)
//
//Create SVG element
svg = d3.select("body")
.append("svg")
.attr("id", "bonusChart")
.attr("width", w)
.attr("height", h);
//Create groups
groups = svg.selectAll("g")
.data(dataset)
.enter()
.append("g")
.attr("class", "bar")
.attr("transform", function(d, i) {
return "translate(" + xScale(i) + ",0)";
});
//Add bar to each group
rects = groups.append("rect")
.attr("x", 0)
.attr("y", function(d) {
return h - padding;
})
.attr("width", xScale.rangeBand())
.attr("height", 0)
.attr("fill", "SteelBlue");
//Add label to each group
groups.append("text")
.attr("x", xScale.rangeBand() / 2)
.attr("y", function(d) {
return bonusScale(d.bonus) + 14;
})
.text(function(d) {
return d.name + ": " + d.bonus;
})
//Transition rects into place
rects.transition()
.delay(function(d, i) {
return i * 100;
})
.duration(1500)
.attr("y", function(d) {
return bonusScale(d.bonus);
})
.attr("height", function(d) {
return h - padding - bonusScale(d.bonus);
});
//Create y axis
svg.append("g")
.attr("class", "axis")
.attr("transform", "translate(" + padding + ",0)")
.attr("opacity", 0)
.call(bonusAxis)
.transition()
.delay(2000)
.duration(1500)
.attr("opacity", 1.0);
//New functionality for interaction for ALL groups
//in BOTH charts
d3.selectAll("g.bar")
.on("mouseover", function(d) {
//Instead of d3.select(this), now we want to
//select ALL groups that match the same
//criteria as this one, i.e. this group
//and the corresponding one in the other chart.
//
//We begin by selecting all groups, then
//filtering to exclude those that don't match.
//
//We'll use each person's "name" as a unique
//identifier. (With a real-world data set, you'd
//probably use an ID number here.)
var thisName = d.name;
d3.selectAll("g.bar")
.filter(function(d) {
//If the name from the original group on
//which mouseover was triggered matches the
//name on the group we're evaluating right now…
if (thisName == d.name) {
return true; //…then it's a match
}
})
.classed("highlight", true);
})
.on("mouseout", function() {
//We could be selective, using the same filtering
//criteria above, or, for simplicity, just
//de-highlight all the groups.
d3.selectAll("g.bar")
.classed("highlight", false);
})
//Sorting logic
d3.select("#sort")
.on("click", function() {
//Need to reselect all groups in each chart
d3.selectAll("#salesChart g.bar").sort(function(a, b) {
return d3.ascending(a.sales, b.sales);
})
.transition()
.delay(function(d, i) {
return i * 50;
})
.duration(1000)
.attr("transform", function(d, i) {
return "translate(" + xScale(i) + ",0)";
});
d3.selectAll("#bonusChart g.bar").sort(function(a, b) {
return d3.ascending(a.bonus, b.bonus);
})
.transition()
.delay(function(d, i) {
return i * 50;
})
.duration(1000)
.attr("transform", function(d, i) {
return "translate(" + xScale(i) + ",0)";
});
});
</script>
</body>
</html>