-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd3.chart.js
477 lines (398 loc) · 12.6 KB
/
d3.chart.js
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
(function(){d3.chart = {};
// Inspired by http://informationandvisualization.de/blog/box-plot
d3.chart.boxplot = function() {
var width = 1,
height = 1,
duration = 0,
domain = null,
value = Number,
whiskers = d3_chart_boxplotWhiskers,
quartiles = d3_chart_boxplotQuartiles,
tickFormat = null;
// For each small multiple…
function boxplot(g) {
g.each(function(d, i) {
d = d.map(value).sort(d3.ascending);
var len = d.length,
min = d[0],
max = d[len-1],
whiskerIndices = whiskers.call(this, d, i),
whiskerData = whiskerIndices.map(function(i) { return d[i]; }),
firstWhisker = whiskerIndices[0],
lastWhisker = whiskerIndices[whiskerIndices.length - 1],
whiskerRange = lastWhisker - firstWhisker,
quartileData = quartiles(d.slice(firstWhisker, lastWhisker)),
outliers = d.slice(0, firstWhisker).concat(d.slice(lastWhisker + 1, len)),
g = d3.select(this);
// Compute the new x-scale.
var x1 = d3.scale.linear()
.domain(domain ? domain.call(this, d, i) : [min, max])
.range([height, 0]);
// Retrieve the old x-scale, if this is an update.
var x0 = this.__chart__ || d3.scale.linear()
.domain([0, Infinity])
.range(x1.range());
// Stash the new scale.
this.__chart__ = x1;
// Update center line.
var center = g.selectAll("line.center")
.data([[min, max]]);
center.enter().append("svg:line")
.attr("class", "center")
.attr("x1", width / 2)
.attr("y1", function(d) { return x0(d[0]); })
.attr("x2", width / 2)
.attr("y2", function(d) { return x0(d[1]); })
.transition()
.duration(duration)
.attr("y1", function(d) { return x1(d[0]); })
.attr("y2", function(d) { return x1(d[1]); });
center.transition()
.duration(duration)
.attr("y1", function(d) { return x1(d[0]); })
.attr("y2", function(d) { return x1(d[1]); });
center.exit().remove();
// Update boxes.
var box = g.selectAll("rect.box")
.data([[quartileData[0], quartileData[2]]]);
box.enter().append("svg:rect")
.attr("class", "box")
.attr("x", 0)
.attr("y", function(d) { return x0(d[1]); })
.attr("width", width)
.attr("height", function(d) { return x0(d[0]) - x0(d[1]); })
.transition()
.duration(duration)
.attr("y", function(d) { return x1(d[1]); })
.attr("height", function(d) { return x1(d[0]) - x1(d[1]); });
box.transition()
.duration(duration)
.attr("y", function(d) { return x1(d[1]); })
.attr("height", function(d) { return x1(d[0]) - x1(d[1]); });
box.exit().remove();
// Update median line
var medianLine = g.selectAll("line.median")
.data([quartileData[1]]);
medianLine.enter().append("svg:line")
.attr("class", "median")
.attr("x1", 0)
.attr("y1", x0)
.attr("x2", width)
.attr("y2", x0)
.transition()
.duration(duration)
.attr("y1", x1)
.attr("y2", x1);
medianLine.transition()
.duration(duration)
.attr("y1", x1)
.attr("y2", x1);
medianLine.exit().remove();
// Update whiskers.
var whisker = g.selectAll("line.whisker")
.data(whiskerData);
whisker.enter().append("svg:line")
.attr("class", "whisker")
.attr("x1", 0)
.attr("y1", x0)
.attr("x2", width)
.attr("y2", x0)
.transition()
.duration(duration)
.attr("y1", x1)
.attr("y2", x1);
whisker.transition()
.duration(duration)
.attr("y1", x1)
.attr("y2", x1);
whisker.exit().remove();
// Update outliers.
var outlier = g.selectAll("circle.outlier")
.data(outliers);
outlier.enter().append("svg:circle")
.attr("class", "outlier")
.attr("r", 5)
.attr("cx", width / 2)
.attr("cy", x0)
.transition()
.duration(duration)
.attr("cy", x1);
outlier.transition()
.duration(duration)
.attr("cy", x1);
outlier.exit().remove();
// Compute the tick format.
var format = tickFormat || x1.tickFormat(8);
// Update ticks.
var tick = g.selectAll("text")
.data(quartileData.concat(whiskerData));
tick.enter().append("svg:text")
.attr("dy", ".3em")
.attr("dx", function(d, i) { return i&1 ? 8 : -8 })
.attr("x", function(d, i) { return i&1 ? width : 0 })
.attr("y", x0)
.attr("text-anchor", function(d, i) { return i&1 ? "start" : "end"; })
.text(format)
.transition()
.duration(duration)
.attr("y", x1);
tick.text(format)
.transition()
.duration(duration)
.attr("y", x1);
});
}
boxplot.width = function(x) {
if (!arguments.length) return width;
width = x;
return boxplot;
};
boxplot.height = function(x) {
if (!arguments.length) return height;
height = x;
return boxplot;
};
boxplot.tickFormat = function(x) {
if (!arguments.length) return tickFormat;
tickFormat = x;
return boxplot;
};
boxplot.duration = function(x) {
if (!arguments.length) return duration;
duration = x;
return boxplot;
};
boxplot.domain = function(x) {
if (!arguments.length) return domain;
domain = d3.functor(x);
return boxplot;
};
boxplot.value = function(x) {
if (!arguments.length) return value;
value = x;
return boxplot;
};
boxplot.whiskers = function(x) {
if (!arguments.length) return whiskers;
whiskers = x;
return boxplot;
};
boxplot.quartiles = function(x) {
if (!arguments.length) return quartiles;
quartiles = x;
return boxplot;
};
return boxplot;
};
function d3_chart_boxplotWhiskers(d) {
return [0, d.length-1];
}
function d3_chart_boxplotQuartiles(d) {
var len = d.length;
return [.25, .5, .75].map(function(q) {
q *= len;
if (parseInt(q) === q) return (d[q] + q[q + 1]) / 2;
return d[Math.round(q)];
});
}
// ranges (bad, satisfactory, good)
// measures (actual, forecast)
// markers (previous, goal)
/*
* Chart design based on the recommendations of Stephen Few. Implementation
* based on the work of Clint Ivy, Jamie Love, and Jason Davies.
* http://projects.instantcognition.com/protovis/bulletchart/
*/
d3.chart.bullet = function() {
var orient = "left", // TODO top & bottom
reverse = false,
duration = 0,
ranges = d3_chart_bulletRanges,
markers = d3_chart_bulletMarkers,
measures = d3_chart_bulletMeasures,
width = 380,
height = 30,
tickFormat = null;
// For each small multiple…
function bullet(g) {
g.each(function(d, i) {
var rangez = ranges.call(this, d, i).slice().sort(d3.descending),
markerz = markers.call(this, d, i).slice().sort(d3.descending),
measurez = measures.call(this, d, i).slice().sort(d3.descending),
g = d3.select(this);
// Compute the new x-scale.
var x1 = d3.scale.linear()
.domain([0, Math.max(rangez[0], markerz[0], measurez[0])])
.range(reverse ? [width, 0] : [0, width]);
// Retrieve the old x-scale, if this is an update.
var x0 = this.__chart__ || d3.scale.linear()
.domain([0, Infinity])
.range(x1.range());
// Stash the new scale.
this.__chart__ = x1;
// Derive width-scales from the x-scales.
var w0 = d3_chart_bulletWidth(x0),
w1 = d3_chart_bulletWidth(x1);
// Update the range rects.
var range = g.selectAll("rect.range")
.data(rangez);
range.enter().append("svg:rect")
.attr("class", function(d, i) { return "range s" + i; })
.attr("width", w0)
.attr("height", height)
.attr("x", reverse ? x0 : 0)
.transition()
.duration(duration)
.attr("width", w1)
.attr("x", reverse ? x1 : 0);
range.transition()
.duration(duration)
.attr("x", reverse ? x1 : 0)
.attr("width", w1)
.attr("height", height);
// Update the measure rects.
var measure = g.selectAll("rect.measure")
.data(measurez);
measure.enter().append("svg:rect")
.attr("class", function(d, i) { return "measure s" + i; })
.attr("width", w0)
.attr("height", height / 3)
.attr("x", reverse ? x0 : 0)
.attr("y", height / 3)
.transition()
.duration(duration)
.attr("width", w1)
.attr("x", reverse ? x1 : 0);
measure.transition()
.duration(duration)
.attr("width", w1)
.attr("height", height / 3)
.attr("x", reverse ? x1 : 0)
.attr("y", height / 3);
// Update the marker lines.
var marker = g.selectAll("line.marker")
.data(markerz);
marker.enter().append("svg:line")
.attr("class", "marker")
.attr("x1", x0)
.attr("x2", x0)
.attr("y1", height / 6)
.attr("y2", height * 5 / 6)
.transition()
.duration(duration)
.attr("x1", x1)
.attr("x2", x1);
marker.transition()
.duration(duration)
.attr("x1", x1)
.attr("x2", x1)
.attr("y1", height / 6)
.attr("y2", height * 5 / 6);
// Compute the tick format.
var format = tickFormat || x1.tickFormat(8);
// Update the tick groups.
var tick = g.selectAll("g.tick")
.data(x1.ticks(8), function(d) {
return this.textContent || format(d);
});
// Initialize the ticks with the old scale, x0.
var tickEnter = tick.enter().append("svg:g")
.attr("class", "tick")
.attr("transform", d3_chart_bulletTranslate(x0))
.attr("opacity", 1e-6);
tickEnter.append("svg:line")
.attr("y1", height)
.attr("y2", height * 7 / 6);
tickEnter.append("svg:text")
.attr("text-anchor", "middle")
.attr("dy", "1em")
.attr("y", height * 7 / 6)
.text(format);
// Transition the entering ticks to the new scale, x1.
tickEnter.transition()
.duration(duration)
.attr("transform", d3_chart_bulletTranslate(x1))
.attr("opacity", 1);
// Transition the updating ticks to the new scale, x1.
var tickUpdate = tick.transition()
.duration(duration)
.attr("transform", d3_chart_bulletTranslate(x1))
.attr("opacity", 1);
tickUpdate.select("line")
.attr("y1", height)
.attr("y2", height * 7 / 6);
tickUpdate.select("text")
.attr("y", height * 7 / 6);
// Transition the exiting ticks to the new scale, x1.
tick.exit().transition()
.duration(duration)
.attr("transform", d3_chart_bulletTranslate(x1))
.attr("opacity", 1e-6)
.remove();
});
}
// left, right, top, bottom
bullet.orient = function(x) {
if (!arguments.length) return orient;
orient = x;
reverse = orient == "right" || orient == "bottom";
return bullet;
};
bullet.ranges = function(x) {
if (!arguments.length) return ranges;
ranges = x;
return bullet;
};
bullet.markers = function(x) {
if (!arguments.length) return markers;
markers = x;
return bullet;
};
bullet.measures = function(x) {
if (!arguments.length) return measures;
measures = x;
return bullet;
};
bullet.width = function(x) {
if (!arguments.length) return width;
width = x;
return bullet;
};
bullet.height = function(x) {
if (!arguments.length) return height;
height = x;
return bullet;
};
bullet.tickFormat = function(x) {
if (!arguments.length) return tickFormat;
tickFormat = x;
return bullet;
};
bullet.duration = function(x) {
if (!arguments.length) return duration;
duration = x;
return bullet;
};
return bullet;
};
function d3_chart_bulletRanges(d) {
return d.ranges;
}
function d3_chart_bulletMarkers(d) {
return d.markers;
}
function d3_chart_bulletMeasures(d) {
return d.measures;
}
function d3_chart_bulletTranslate(x) {
return function(d) {
return "translate(" + x(d) + ",0)";
};
}
function d3_chart_bulletWidth(x) {
var x0 = x(0);
return function(d) {
return Math.abs(x(d) - x0);
};
}
})()