-
Notifications
You must be signed in to change notification settings - Fork 1
/
byd3.js
1047 lines (885 loc) · 35.3 KB
/
byd3.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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* byd3.js
* Version 0.0.1
*
* D3 Graphics Library
* Brian Yu
*/
/****************************
* SVG Canvas Setup
***************************/
// creates an SVG element to hold graphics
function createSVG(container_name, svg_name, width, height, padding) {
var container = d3.select(container_name);
var svg = container.append('svg')
.attr('id', svg_name)
.style('width', px(width))
.style('height', px(height))
.style('padding', px(padding));
return svg;
}
/****************************
* Custom Graphs
***************************/
// draws a line graph with discrete x categories at (x, y) with given size
// minY and maxY should be the range of the y values, ticksY is the number of ticks
// data should be a list of [x, y] arrays
function drawDiscreteLineGraph(svg, coords, data, yAttrs, attributes) {
// compute the axes
var xData = data.map(function(elt) { return elt[0]; });
var xAxis = createDiscreteAxisX(
xData, // domain, possible x values
[coords['x'], coords['x'] + coords['width']], // range, coordinate locations
coords['axisPadding']); // padding
var yAxis = createLinearAxisY(
[yAttrs['min'], yAttrs['max']], // domain, possible y values
[coords['y'] + coords['height'], coords['y']], // range, coordinate locations
yAttrs['ticks'], coords['axisPadding']); // ticks and padding
// draw the gridlines
if (style('lineGraphGridlinesX'))
drawXGridlines(svg, xAxis, coords['height'] - 2 * coords['axisPadding'],
data.length, coords['y'] + coords['height'] - coords['axisPadding'], {});
if (style('lineGraphGridlinesY'))
drawYGridlines(svg, yAxis, coords['width'] - 2 * coords['axisPadding'],
yAttrs['ticks'], coords['x'] + coords['axisPadding'], {});
drawAxisX(svg, xAxis, coords['y'] + coords['height'], coords['axisPadding']);
drawAxisY(svg, yAxis, coords['x'], coords['axisPadding']);
labelAxes(svg, coords, attributes);
// draw the actual line path
if (data.length !== 0) {
var dataLocations = computeDataLocations(data, xAxis, yAxis);
var points = drawLinePath(svg, dataLocations, attributes);
addTooltipsToPoints(svg, points, data, attributes);
}
return [xAxis, yAxis];
}
function drawLineGraph(svg, coords, data, xAttrs, yAttrs, attributes) {
// compute the axes
var xAxis = createLinearAxisX(
[xAttrs['min'], xAttrs['max']], // domain, possible x values
[coords['x'], coords['x'] + coords['width']], // range, coordinate locations
xAttrs['ticks'], coords['axisPadding']); // ticks and padding
var yAxis = createLinearAxisY(
[yAttrs['min'], yAttrs['max']], // domain, possible y values
[coords['y'] + coords['height'], coords['y']], // range, coordinate locations
yAttrs['ticks'], coords['axisPadding']); // ticks and padding
// draw the gridlines
if (style('lineGraphGridlinesX'))
drawXGridlines(svg, xAxis, coords['height'] - 2 * coords['axisPadding'],
xAttrs['ticks'], coords['y'] + coords['height'] - coords['axisPadding'], {});
if (style('lineGraphGridlinesY'))
drawYGridlines(svg, yAxis, coords['width'] - 2 * coords['axisPadding'],
yAttrs['ticks'], coords['x'] + coords['axisPadding'], {});
drawAxisX(svg, xAxis, coords['y'] + coords['height'], coords['axisPadding']);
drawAxisY(svg, yAxis, coords['x'], coords['axisPadding']);
labelAxes(svg, coords, attributes);
// draw the actual line path
if (data.length !== 0) {
var dataLocations = computeDataLocations(data, xAxis, yAxis);
var points = drawLinePath(svg, dataLocations, attributes);
addTooltipsToPoints(svg, points, data, attributes);
}
return [xAxis, yAxis];
}
// draws a bar graph
// bar graph size should be in range (0, 1)
function drawBarGraph(svg, coords, data, yAttrs, attributes) {
var barSize = get(attributes, 'barSize', style('barSize'));
// compute the axes
var xData = data.map(function(elt) { return elt[0]; });
var xAxis = createBarAxisX(
xData, // domain, possible x values
[coords['x'], coords['x'] + coords['width']], // range, coordinate locations
coords['axisPadding']); // padding
xAxis.scale().padding(1 - barSize);
var yAxis = createLinearAxisY(
[yAttrs['min'], yAttrs['max']], // domain, possible y values
[coords['y'] + coords['height'], coords['y']], // range, coordinate locations
yAttrs['ticks'], coords['axisPadding']); // ticks and padding
// draw gridlines
if (style('barGraphGridlines'))
drawYGridlines(svg, yAxis, coords['width'] - 2 * coords['axisPadding'],
yAttrs['ticks'], coords['x'] + coords['axisPadding'], {});
// draw axes
drawAxisX(svg, xAxis, coords['y'] + coords['height'], coords['axisPadding']);
drawAxisY(svg, yAxis, coords['x'], coords['axisPadding']);
labelAxes(svg, coords, attributes);
// draw each bar
barColor = get(attributes, 'color', style('barColor'));
for (var i = 0; i < data.length; i++) {
var bar = svg.append('rect')
.attr('x', xAxis.scale()(data[i][0]))
.attr('y', yAxis.scale()(data[i][1]))
.attr('width', xAxis.scale().bandwidth())
.attr('height', coords['y'] + coords['height']
- coords['axisPadding'] - yAxis.scale()(data[i][1]))
.style('fill', barColor);
if (style('showTooltips')) {
var contents = data[i][1];
if ('tooltipFunction' in attributes)
contents = attributes['tooltipFunction'](data[i]);
addTooltipToPoint(svg, bar, contents, attributes);
}
}
return [xAxis, yAxis];
}
// multiple bar graph
// data is an array of dictionaries
// each dictionary contains a list of [x,y] pairs as 'data'
// each dictionary also contains a 'color'
function drawMultiBarGraph(svg, coords, data, yAttrs, attributes) {
// compute the axes
var xData = data[0]['data'].map(function(elt) { return elt[0] });
var xAxis = createBarAxisX(
xData,
[coords['x'], coords['x'] + coords['width']],
coords['axisPadding']);
xAxis.scale().padding(style('multiBarPadding'));
var yAxis = createLinearAxisY(
[yAttrs['min'], yAttrs['max']],
[coords['y'] + coords['height'], coords['y']],
yAttrs['ticks'], coords['axisPadding']);
// draw gridlines
if (style('barGraphGridlines'))
drawYGridlines(svg, yAxis, coords['width'] - 2 * coords['axisPadding'],
yAttrs['ticks'], coords['x'] + coords['axisPadding'], {});
// draw axes
drawAxisX(svg, xAxis, coords['y'] + coords['height'], coords['axisPadding']);
drawAxisY(svg, yAxis, coords['x'], coords['axisPadding']);
labelAxes(svg, coords, attributes);
// go through each set of bars
var numBars = data.length;
var totalBarWidth = xAxis.scale().bandwidth();
var eachBarWidth = totalBarWidth / numBars;
for (var i = 0; i < data.length; i++) {
var barColor = data[i]['color'];
var barData = data[i]['data'];
// go through each bar in the set
for (var j = 0; j < barData.length; j++) {
var bar = svg.append('rect')
.attr('x', xAxis.scale()(barData[j][0]) + i * eachBarWidth)
.attr('y', yAxis.scale()(barData[j][1]))
.attr('width', eachBarWidth)
.attr('height', coords['y'] + coords['height']
- coords['axisPadding'] - yAxis.scale()(barData[j][1]))
.style('fill', barColor);
if (style('showTooltips'))
addTooltipToPoint(svg, bar, barData[j][1], attributes);
}
}
return [xAxis, yAxis];
}
function addLineToGraph(svg, data, axes, attributes) {
var dataLocations = computeDataLocations(data, axes[0], axes[1]);
var points = drawLinePath(svg, dataLocations, attributes);
return points;
}
// labels the axes, if they are specified in the attributes of the graph
function labelAxes(svg, coords, attributes) {
if ('x-label' in attributes) {
svg.append('text')
.attr('x', coords['x'] + coords['width'] / 2)
.attr('y', coords['y'] + coords['height'])
.attr('text-anchor', 'middle')
.style('font-family', style('axisFont'))
.style('font-size', style('axisFontSize'))
.text(attributes['x-label']);
}
if ('y-label' in attributes) {
svg.append('text')
.attr('x', coords['x'] - coords['height'] / 2 - coords['axisPadding'])
.attr('y', coords['y'])
.attr('transform', 'rotate(-90)')
.attr('text-anchor', 'middle')
.style('font-family', style('axisFont'))
.style('font-size', style('axisFontSize'))
.text(attributes['y-label']);
}
}
/****************************
* Graph Helper Functions
***************************/
// creates a scale mapping data in domain to pixels in range, with padding
function scale(type, domain, range, padding) {
var scale = null;
// modify range to allow for padding
if (range[0] > range[1]) {
range[0] -= padding;
range[1] += padding;
} else {
range[0] += padding;
range[1] -= padding;
}
if (type === 'linear') {
scale = d3.scaleLinear().domain(domain).range(range);
} else if (type === 'point') {
scale = d3.scalePoint().domain(domain).range(range);
} else if (type === 'band') {
scale = d3.scaleBand().domain(domain).range(range);
}
return scale;
}
// returns an array of two arrays: the first is the x domain, the second is y domain
function domains(data) {
var xMin = data[0][0];
var xMax = data[0][0];
var yMin = data[0][1];
var yMax = data[0][1];
for (var i = 1; i < data.length; i++) {
if (data[i][0] < xMin)
xMin = data[i][0];
else if (data[i][0] > xMax)
xMax = data[i][0];
if (data[i][1] < yMin)
yMin = data[i][1];
else if (data[i][1] > yMax)
yMax = data[i][1];
}
}
// creates, but does not draw, an axis for continuous x data
function createLinearAxisX(domain, range, tickCount, axisPadding) {
var axisScale = scale('linear', domain, range, axisPadding);
return d3.axisBottom().scale(axisScale).ticks(tickCount);
}
// creates, but does not draw, an x axis for discrete x data
function createDiscreteAxisX(domain, range, axisPadding) {
var axisScale = scale('point', domain, range, axisPadding);
return d3.axisBottom().scale(axisScale);
}
// creates, but does not draw, an x axis for a bar graph
function createBarAxisX(domain, range, axisPadding) {
var axisScale = scale('band', domain, range, axisPadding);
return d3.axisBottom().scale(axisScale);
}
// creates, but does not draw, an axis for y data
function createLinearAxisY(domain, range, tickCount, axisPadding) {
var axisScale = scale('linear', domain, range, axisPadding);
return d3.axisLeft().scale(axisScale).ticks(tickCount);
}
function computeDataLocations(data, xAxis, yAxis) {
var dataLocations = [];
for (var i = 0; i < data.length; i++) {
dataLocations.push([xAxis.scale()(data[i][0]), yAxis.scale()(data[i][1])]);
}
return dataLocations;
}
// draws the x axis on svg canvas. Requires an axis, and details about how tall graph is
function drawAxisX(svg, xAxis, graphHeight, axisPadding) {
drawAxis(svg, xAxis, 'translate(0, ' + (graphHeight - axisPadding) + ')', {});
}
// draws the y axis on svg canvas.
function drawAxisY(svg, yAxis, graphXStart, axisPadding) {
drawAxis(svg, yAxis, 'translate(' + (graphXStart + axisPadding) + ', 0)', {});
}
function drawAxis(svg, axis, transformation, attributes) {
var axis = svg.append('g')
.attr('transform', transformation)
.call(axis);
// modify CSS for the axes themselves
axis.select('path')
.style('stroke', style('axisColor'))
.style('stroke-width', style('lineGraphAxisWidth'));
// modify CSS for ticks
axis.selectAll('.tick').selectAll('text')
.style('font-family', style('axisFont'))
.style('font-size', style('axisFontSize'));
}
// draws gridlines for y intervals
function drawYGridlines(svg, axis, width, ticks, xStart, attributes) {
var gridlines = d3.axisRight()
.tickFormat('')
.tickSize(width)
.ticks(ticks)
.scale(axis.scale());
drawGridlines(svg, gridlines, 'translate(' + xStart + ', 0)', {});
}
function drawXGridlines(svg, axis, height, ticks, yStart, attributes) {
var gridlines = d3.axisTop()
.tickFormat('')
.tickSize(height)
.ticks(ticks)
.scale(axis.scale());
drawGridlines(svg, gridlines, 'translate(0, ' + yStart + ')', {});
}
function drawGridlines(svg, gridlines, transformation, attributes) {
var g = svg.append('g')
.attr('transform', transformation)
.call(gridlines);
g.select('.tick').remove();
g.select('.domain').remove();
// style the line
g.selectAll('.tick').selectAll('line')
.style('stroke', style('gridlineColor'));
}
// draws a line connecting [x, y] points in data, useful for line graphs
function drawLinePath(svg, data, attributes) {
var color = get(attributes, 'color', style('lineColor'));
var strokeWidth = get(attributes, 'stroke-width', style('lineGraphLineWidth'));
var drawPoints = get(attributes, 'drawPoints', style('lineGraphDrawPoints'));
svg.append('path')
.attr('d', line(data))
.style('stroke', color)
.style('fill', 'none')
.style('stroke-width', strokeWidth);
var points = [];
if (drawPoints) {
for (var i = 0; i < data.length; i++) {
var point = drawPoint(svg, data[i][0], data[i][1], attributes);
points.push(point);
}
}
return points;
}
// draws a point at (x, y)
function drawPoint(svg, x, y, attributes) {
var radius = get(attributes, 'pointRadius', style('pointRadius'));
var innerRadius = get(attributes, 'innerPointRadius', style('innerPointRadius'));
var color = get(attributes, 'pointColor', style('pointColor'));
var tooltipContents = get(attributes, 'tooltipContents', null);
// draw the main point
var point = svg.append('circle')
.attr('cx', x)
.attr('cy', y)
.attr('r', radius)
.attr('fill', color);
// if we need a hollow center, draw that
if (style('pointsHollowCenter')) {
var innerPoint = svg.append('circle')
.attr('cx', x)
.attr('cy', y)
.attr('r', innerRadius)
.attr('fill', style('white'))
.style('pointer-events', 'none');
}
// take care of tooltip
if (tooltipContents != null)
addTooltipToPoint(svg, point, tooltipContents, attributes);
return point;
}
function createTooltip(svg, x, y, width, html, attributes) {
var color = get(attributes, 'tooltipColor', style('tooltipColor'));
var talign = get(attributes, 'tooltipAlign', style('tooltipAlign'));
// determines if should erase all previous tooltips
var shouldEraseTooltip = get(attributes, 'eraseTooltip', true);
// should adjust for edge of screen
var realign = get(attributes, 'realignTooltip', true);
// compute if should go right or left
var toRight = x < (pxtonum(svg.style('width')) / 2);
// make sure that tooltip faces the inside of svg
if (!toRight && realign)
x -= width;
if (shouldEraseTooltip)
eraseTooltip(svg);
var fo = svg.append('foreignObject')
.attr('x', x)
.attr('y', y)
.attr('class', 'byd3-tooltip')
.attr('width', width);
fo.append('xhtml:div')
.style('border-radius', px(style('tooltipBorderRadius')))
.style('padding', px(style('tooltipPadding')))
.style('text-align', talign)
.style('width', width)
.style('font-family', style('tooltipFont'))
.style('background-color', color)
.html(html);
}
function eraseTooltip(svg) {
svg.selectAll('.byd3-tooltip').remove();
}
// adds listener on point to create tooltip when hovered
function addTooltipToPoint(svg, point, contents, attributes) {
function createTooltipAtCursor() {
var x = d3.mouse(svg.node())[0];
var y = d3.mouse(svg.node())[1];
var tt = createTooltip(svg, x, y, style('tooltipWidth'), contents, attributes);
}
point.on('mouseover', createTooltipAtCursor)
.on('mousemove', createTooltipAtCursor)
.on('click', createTooltipAtCursor)
.on('mouseout', function() {
eraseTooltip(svg);
});
}
function addTooltipsToPoints(svg, points, data, attributes) {
var tooltipFunction = get(attributes, 'tooltipFunction', null);
var xLabel = get(attributes, 'x-label', null);
var yLabel = get(attributes, 'y-label', null);
for (var i = 0; i < points.length; i++) {
var content; // contents of the tooltip
if (tooltipFunction !== null) // custom function for computing inside
content = tooltipFunction(data[i]);
else { // compute tooltip contents just by default
var content = '';
if (!xLabel && !yLabel)
content += '(' + data[i][0] + ', ' + data[i][1] + ')';
if (xLabel)
content += xLabel + ': ' + data[i][0];
if (xLabel && yLabel)
content += '<br/>';
if (yLabel)
content += yLabel + ': ' + data[i][1];
}
addTooltipToPoint(svg, points[i], content, attributes);
}
}
/****************************
* Infographic Helper Functions
***************************/
function addHeadline(identifier, width, text) {
var headline = d3.select(identifier).append('div')
.attr('class', 'infographic-headline')
.style('width', px(width))
.style('padding', px(style('headlinePadding')))
.style('text-align', 'center')
.style('font-size', px(style('headlineFontSize')))
.style('font-family', style('headlineFont'))
.text(text);
return headline;
}
/****************************
* Other Graphics Features
***************************/
// draws person with head beginning at (x, y)
function drawPerson(svg, x, y, height, attributes) {
var color = get(attributes, 'personColor', style('personColor'));
var padding = 0.05 * height;
var headSize = (2/7) * (height - padding);
var bodySize = height - padding - headSize;
var distToBody = (headSize / 2) + padding + (bodySize / 2);
x += headSize / 2;
y += headSize / 2;
var head = svg.append('circle')
.attr('cx', x)
.attr('cy', y)
.attr('r', headSize / 2)
.attr('distToBody', distToBody)
.attr('headSize', headSize)
.style('fill', color);
var body = svg.append('ellipse')
.attr('cx', x)
.attr('cy', y + distToBody)
.attr('rx', 1.1 * (headSize / 2))
.attr('ry', bodySize / 2)
.style('fill', color);
return [head, body];
}
function movePerson(svg, person, x, y, duration, attributes) {
var color = get(attributes, 'personColor', person[0].attr('fill'));
var delay = get(attributes, 'delay', 0);
var headSize = parseFloat(person[0].attr('headSize'));
// move the head
person[0].transition()
.duration(duration)
.delay(delay)
.ease(d3.easeLinear)
.attr('cx', x + headSize / 2)
.attr('cy', y + headSize / 2)
.style('fill', color);
// move the body
person[1].transition()
.duration(duration)
.delay(delay)
.ease(d3.easeLinear)
.attr('cx', x + headSize / 2)
.attr('cy', y + headSize / 2 + parseFloat(person[0].attr('distToBody')))
.style('fill', color);
}
// creates a person cluster graph of moving people
// takes in the selector for a <select> dropdown whose values are the names of categories
// also takes in data, which should be in the format:
// data is an array of dictionaries, each dictionary representing a category (page)
// each category dictionary as a 'name', which defines its category
// each category dictionary also has 'data', which is a list of dictionaries,
// each defining a group (grouping on same page).
// each group has a label 'name', 'location' [x,y] of the label
// 'pctLocation' [x,y] of the percent, 'color' for the grouping,
// 'value' for the number of people, 'gridLoc' [x, y] for the origin of the grid of people,
// 'gridRows' and 'gridCols' to define the dimensions of the grid
// numPeople defines the number of people to draw
// width and height define the canvas size
function createPersonClusterGraph(selector, svg, data, numPeople, width, height, attributes) {
var randomizePersonShuffle = get(attributes, 'randomizePersonShuffle', true);
var personHeight = width / 15;
var personWidth = Math.ceil(personHeight / 3);
var personPadding = height / 120;
var people = [];
var labels = []; // visible, but disappear on transition
// draw all the data of screen
for (var i = 0; i < numPeople; i++) {
var person = drawPerson(svg, - (2 * personWidth), - (2 * personHeight), personHeight, {});
people.push(person);
}
d3.select(selector).on('change', function() {
var selection = d3.select(selector).node().value;
transitionTo(selection);
});
function transitionTo(selection) {
// remove labels if there are any
for (var i = 0; i < labels.length; i++) {
labels[i].remove();
}
labels = [];
if (randomizePersonShuffle)
people = shuffle(people);
var personIndex = 0; // number of people already sorted
// find index of selection
var index = null;
for (var i = 0; i < data.length; i++) {
if (data[i]["name"] == selection) {
index = i;
break;
}
}
// go through that selection's data
var selectionData = data[index]['data']
for (var i = 0; i < selectionData.length; i++) {
var x = selectionData[i]['gridLoc'][0] * width;
var y = selectionData[i]['gridLoc'][1] * height;
// move all of the people into place
for (var j = 0; j < selectionData[i]['gridRows']; j++) {
for (var k = 0; k < selectionData[i]['gridCols']; k++) {
if (j * selectionData[i]['gridCols'] + k >= selectionData[i]['value']) {
// exit the loop if we've moved too many people
break;
}
movePerson(svg, people[personIndex], x, y, 1000,
{"personColor": selectionData[i]['color']});
personIndex++;
x += personWidth;
}
x = selectionData[i]['gridLoc'][0] * width;
y += personHeight + personPadding;
}
// print the text
var labelScale = d3.scaleLinear().domain([200, 700]).range([12, 24]);
if (selectionData[i]['location'].length !== 0) {
var name = svg.append('text')
.attr('x', selectionData[i]['location'][0] * width)
.attr('y', selectionData[i]['location'][1] * height)
.style('font-family', style('textFontSans'))
.style('font-size', labelScale(width))
.style('fill', selectionData[i]['color'])
.style('opacity', 0)
.text(selectionData[i]['name']);
labels.push(name);
name.transition()
.duration(1000)
.style('opacity', 1);
}
// show the percentage
var pctScale = d3.scaleLinear().domain([200, 700]).range([24, 60])
if (selectionData[i]['pctLoc'].length !== 0) {
var percent = svg.append('text')
.attr('x', selectionData[i]['pctLoc'][0] * width)
.attr('y', selectionData[i]['pctLoc'][1] * height)
.style('font-family', style('textFontSans'))
.style('font-size', pctScale(width))
.style('fill', selectionData[i]['color'])
.style('opacity', 0)
.text(selectionData[i]['value'] + '%');
labels.push(percent);
percent.transition()
.duration(1000)
.style('opacity', 1);
}
}
while (personIndex < numPeople) {
movePerson(svg, people[personIndex],
-(2 * personWidth), -(2 * personHeight), 1000,
{'personColor': '#ffffff'});
personIndex++;
}
}
transitionTo(data[0]["name"]);
};
/****************************
* Other Graphics
***************************/
// creates a rating chart
// selector is the name of the select dropdown
// data is an array of dictionaries, each being a selected category
// each category has a 'name' which is the value of the selector
// each category also has data, an array of dictionaries
// each dictionary has a 'name', 'color', and 'value' (0-100)
function createRatingChart(selector, svg, data, width, height) {
var padding = style('padding');
var x = padding;
var y = padding;
var barWidth = width - 2 * padding;
var barHeight = height - 2 * padding;
var initialData = data[0]['data'];
var bars = [];
for (var i = 0; i < initialData.length; i++) {
var bar = svg.append('rect')
.attr('x', 0)
.attr('y', 0)
.attr('width', 0)
.attr('height', 0);
bars.push(bar);
}
var transitionTime = 0;
function transitionTo(selection) {
// get the selected choice
var index = null;
for (var i = 0; i < data.length; i++) {
if (data[i]['name'] == selection) {
index = i;
break;
}
}
var dist = 0;
var selectedData = data[i]['data'];
// compute the total value
var total = 0;
for (var i = 0; i < selectedData.length; i++)
total += selectedData[i]['value'];
// draw the bars
for (var i = 0; i < selectedData.length; i++) {
var thisWidth = (selectedData[i]['value'] / total) * barWidth;
bars[i].transition()
.duration(transitionTime)
.attr('x', x + dist)
.attr('y', y)
.attr('data-name', selectedData[i]['name'])
.attr('data-value', selectedData[i]['value'])
.attr('width', thisWidth)
.attr('height', barHeight)
.attr('fill', selectedData[i]['color'])
.on('end', function() {
var tooltipContents = d3.select(this).attr('data-name')
+ '<br/>' + d3.select(this).attr('data-value') + '%';
if (d3.select(this).attr('data-name').length > 0)
createTooltip(svg,
parseFloat(d3.select(this).attr('x')) +
d3.select(this).attr('width') / 4,
parseFloat(d3.select(this).attr('y')) + barHeight / 4,
parseFloat(d3.select(this).attr('width')) / 2,
tooltipContents,
{"eraseTooltip": false, "realignTooltip": false,
"tooltipDisplayDelay": transitionTime});
});
dist += thisWidth;
}
}
transitionTo(data[0]['name']);
// update when something is selected
d3.select(selector).on('change', function() {
eraseTooltip(svg);
var selection = d3.select(selector).node().value;
transitionTo(selection);
});
transitionTime = 1000;
}
function createDonut(svg, x, y, r, pct, attributes) {
var color = get(attributes, 'donutColor', style('donutColor'));
var size = get(attributes, 'donutSize', 0.4); // percent of radius to show
// define the arcs for the circle and the arc
var innerRadius = (1 - size) * r;
var circle = d3.arc()
.innerRadius(innerRadius)
.outerRadius(r)
.startAngle(0)
.endAngle(2 * Math.PI);
var arc = d3.arc()
.innerRadius(innerRadius)
.outerRadius(r)
.startAngle(0);
// let text size be half of the inner radius
var textSize = (2/3) * innerRadius;
// draw the paths
var circlePath = svg.append('path')
.attr('d', circle)
.attr('transform', translateBy(x, y))
.style('fill', style('donutBgColor'));
var arcPath = svg.append('path')
.datum({'endAngle': (pct / 100) * (2 * Math.PI)})
.attr('d', arc)
.attr('data-r', r)
.attr('data-size', size)
.attr('transform', translateBy(x, y))
.style('fill', color);
var headline = svg.append('text')
.attr('x', x)
.attr('y', y + textSize / 2 - 5)
.style('font-family', style('donutFont'))
.style('font-size', textSize)
.style('text-anchor', 'middle')
.style('fill', '#000000')
.text(pct + '%');
return [circlePath, arcPath, headline];
}
function donutChangeData(svg, donut, pct, attributes) {
var duration = get(attributes, 'donutChangeDuration', style('transitionTime'));
var arcPath = donut[1];
var headline = donut[2];
var r = parseFloat(arcPath.attr('data-r'));
var size = parseFloat(arcPath.attr('data-size'));
var arc = d3.arc()
.innerRadius((1 - size) * r)
.outerRadius(r)
.startAngle(0);
// computes transition angles during the movement
// adapted from http://bl.ocks.org/mbostock/5100636
function arcTween(newAngle) {
return function(d) {
var interpolate = d3.interpolate(d.endAngle, newAngle);
return function(t) {
d.endAngle = interpolate(t);
return arc(d);
}
}
}
arcPath.transition()
.duration(duration)
.attrTween('d', arcTween((pct / 100) * (2 * Math.PI)),
(1 - size) * r, r);
headline.transition()
.duration(duration)
.text(pct + '%');
}
function createDynamicDonut(selector, svg, data, width, attributes) {
var padding = style('padding');
var radius = (width / 2) - padding;
var cx = padding + radius;
var cy = padding + radius;
// draw the donut graph
var donut = createDonut(svg, cx, cy, radius, 0, attributes);
function transitionTo(selection) {
index = null;
for (var i = 0; i < data.length; i++) {
if (data[i]['name'] == selection) {
index = i;
break;
}
}
donutChangeData(svg, donut, data[index]['value'], attributes);
}
// allow multiple donuts to bind to the same selector
var eventName = 'eventName' in attributes ? 'change.' + attributes['eventName'] : 'change';
d3.select(selector).on(eventName, function() {
var selection = d3.select(selector).node().value;
transitionTo(selection);
});
transitionTo(data[0]["name"]);
return donut;
}
/****************************
* Stylesheet
***************************/
// default stylesheet for byd3
var stylesheet = {}
// spacing
stylesheet['padding'] = 10;
stylesheet['headlinePadding'] = 10;
stylesheet['tooltipPadding'] = 5;
stylesheet['multiBarPadding'] = 0.2;
// text sizes
stylesheet['textFontSize'] = 12;
stylesheet['axisFontSize'] = 12;
stylesheet['headlineFontSize'] = 24;
// other sizes
stylesheet['lineGraphLineWidth'] = 2;
stylesheet['lineGraphAxisWidth'] = 2;
stylesheet['barSize'] = 0.5;
stylesheet['pointRadius'] = 4;
stylesheet['innerPointRadius'] = 2; // size of inner hollow point for line graphs
stylesheet['tooltipBorderRadius'] = 4;
stylesheet['tooltipWidth'] = 100;
// base fonts
stylesheet['slab'] = '"Roboto Slab", serif';
stylesheet['merriweather'] = '"Merriweather", serif';
stylesheet['lato'] = '"Lato", sans-serif';
stylesheet['noto'] = '"Noto Sans", sans-serif';
// font choices
stylesheet['mainFont'] = stylesheet['merriweather'];
stylesheet['textFontSerif'] = stylesheet['merriweather'];
stylesheet['textFontSans'] = stylesheet['lato'];
stylesheet['axisFont'] = stylesheet['merriweather'];
stylesheet['headlineFont'] = stylesheet['merriweather'];
stylesheet['tooltipFont'] = stylesheet['lato'];
stylesheet['donutFont'] = stylesheet['lato'];
// base colors
stylesheet['black'] = '#000000';
stylesheet['white'] = '#ffffff';
stylesheet['red'] = '#a82931';
stylesheet['blue1'] = '#004e6a';
stylesheet['blue2'] = '#7799b7';
stylesheet['blue3'] = '#b0cfe7';
stylesheet['green'] = '#298848';
stylesheet['yellow'] = '#dbd300';
stylesheet['grey1'] = '#9b9b9b';
stylesheet['grey2'] = '#c0c0c0';
// color choices
stylesheet['lineColor'] = stylesheet['black'];
stylesheet['axisColor'] = stylesheet['black'];
stylesheet['barColor'] = stylesheet['red'];
stylesheet['pointColor'] = stylesheet['black'];
stylesheet['gridlineColor'] = stylesheet['grey1'];
stylesheet['tooltipColor'] = stylesheet['grey2'];
stylesheet['personColor'] = stylesheet['red'];
stylesheet['donutColor'] = stylesheet['red'];
stylesheet['donutBgColor'] = stylesheet['grey1'];
// other configuration settings
stylesheet['lineGraphDrawPoints'] = true;
stylesheet['pointsHollowCenter'] = false; // give line graph points hollow middle
stylesheet['lineGraphGridlinesY'] = true;
stylesheet['lineGraphGridlinesX'] = false;
stylesheet['barGraphGridlines'] = true;
stylesheet['tooltipAlign'] = 'center';
stylesheet['showTooltips'] = true;
stylesheet['transitionTime'] = 1000;
// gets the styling for a particular attribute, using defaults if nothing overriden
function style(attribute) {
if (window.stylesheetOverride && attribute in stylesheetOverride) {
return stylesheetOverride[attribute];
} else {
return stylesheet[attribute];
}
}
/****************************
* Helper Functions
***************************/
// shuffles an array
// source: http://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array
function shuffle(array) {
var currentIndex = array.length, temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {