forked from speced/respec
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
838 lines (754 loc) · 26.6 KB
/
main.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
'use strict';
$(document).ready(function() {
//json data that we intend to update later on via on-screen controls
var split_by_data;
var torso = {};
torso.width = 375;
torso.height = 200;
torso.right = 20;
var trunk = {};
trunk.width = 320;
trunk.height = 150;
trunk.left = 35;
trunk.right = 10;
trunk.xax_count = 5;
var small = {};
small.width = 240;
small.height = 140;
small.left = 20;
small.right = 20;
small.top = 20;
small.xax_count = 5;
assignEventListeners();
//few observations
d3.json('data/missing-y.json', function(data) {
var fff = d3.time.format('%Y-%m-%d');
for(var i=0;i<data.length;i++) {
var d = data[i];
d['date'] = fff.parse(d['date']);
}
//add a line chart that has a few observations
moz_chart({
title: "Few Observations",
description: "We sometimes have only a few observations. By setting <i>missing_is_zero: true</i>, missing values for a time-series will be interpreted as zeros. In this example, we've override the rollover callback to show 'no date' for missing observations and have set <i>min_x</i> and <i>max_x</i> to dates beyond our date range.",
data: data,
interpolate: 'basic',
missing_is_zero: true,
width: torso.width,
height: torso.height,
right: torso.right,
min_x: new Date('2014-01-01'),
max_x: new Date('2014-06-01'),
target: '#missing-y',
x_accessor: 'date',
y_accessor: 'value',
rollover_callback: function(d, i) {
var df = d3.time.format('%b %d, %Y');
var date = df(d['date']);
var y_val = (d.value == 0) ? 'no data' : d.value;
$('#missing-y svg .active_datapoint')
.html(date + ' ' + y_val);
}
})
});
d3.json('data/small-range.json', function(data) {
var fff = d3.time.format('%Y-%m-%d');
for(var i=0;i<data.length;i++) {
var d = data[i];
d['date'] = fff.parse(d['date']);
}
//small range
moz_chart({
title: "Small Range of Integers",
description: "When we have a data object of integers and a small range of values, the auto-generated set of y-axis ticks are filtered so that we don't include fractional values.",
data: data,
interpolate: 'basic',
width: torso.width,
height: torso.height,
right: torso.right,
target: '#small-range',
x_accessor: 'date',
y_accessor: 'value'
})
});
d3.json('data/fake_users1.json', function(data) {
var fff = d3.time.format('%Y-%m-%d');
for(var i=0;i<data.length;i++) {
var d = data[i];
d['date'] = fff.parse(d['date']);
}
var fake_baselines = [{value:160000000, label:'a baseline'}]
//add a line chart
moz_chart({
title: "Line Chart",
description: "This is a simple line chart. You can remove the area portion by adding <i>area: false</i> to the arguments list.",
data: data,
width: torso.width,
height: torso.height,
right: torso.right,
baselines: fake_baselines,
target: '#fake_users1',
x_accessor: 'date',
y_accessor: 'value'
})
moz_chart({
title: "No X Axis",
description: "Here is an example hiding the x axis.",
data: data,
decimals: 0,
width: trunk.width,
height: trunk.height,
right: trunk.right,
xax_count: 4,
target: '#hidden1',
x_accessor: 'date',
y_accessor: 'value',
area: false,
x_axis: false,
small_text: true
})
var markers = [{
'date': new Date('2014-03-17T00:00:00.000Z'),
'label': 'Look, a spike!'
}];
//add a chart with annotations
moz_chart({
title: "Annotations",
description: "By setting the chart's target a class name of main-area-solid, markers don't extend down to the bottom of the chart, which better draws attention to, say, spikes.",
data: data,
width: torso.width,
height: torso.height,
right: torso.right,
markers: markers,
target: '#spike',
x_accessor: 'date',
y_accessor: 'value'
})
})
d3.json('data/fake_users2.json', function(data) {
for(var i=0;i<data.length;i++) {
data[i] = convert_dates(data[i], 'date');
}
//add a multi-line chart
moz_chart({
title:"Multi-Line Chart",
description: "This line chart contains multiple lines.",
data: data,
width: torso.width,
height: torso.height,
right: torso.right,
target: '#fake_users2',
x_accessor: 'date',
y_accessor: 'value'
})
//add a wide multi-line chart
moz_chart({
title:"Multi-Line Chart Wide",
description: "This line chart contains multiple lines and has extended ticks enabled.",
area: false,
legend: ['Line 3','Line 2','Line 1'],
legend_target: '.legend',
data: data,
width: torso.width*2,
height: torso.height,
right: trunk.right,
show_years: false,
xax_tick: 0,
y_extended_ticks: true,
target: '#fake_users3',
x_accessor: 'date',
y_accessor: 'value'
})
//linked multi-line charts
moz_chart({
title:"Multi-Line Linked",
description: "Demoing linked multi-line charts.",
data: data,
width: torso.width,
height: torso.height,
right: torso.right,
target: '#linked_multi1',
linked: true,
x_accessor: 'date',
y_accessor: 'value'
})
})
d3.json('data/fake_users3.json', function(data) {
for(var i=0;i<data.length;i++) {
data[i] = convert_dates(data[i], 'date');
}
//linked multi-line charts
moz_chart({
title:"Multi-Line Linked 2",
description: "Demoing linked multi-line charts.",
data: data,
width: torso.width,
height: torso.height,
right: torso.right,
target: '#linked_multi2',
linked: true,
x_accessor: 'date',
y_accessor: 'value'
})
})
d3.json('data/confidence_band.json', function(data) {
data = convert_dates(data, 'date');
moz_chart({
title: "Confidence Band",
description: "This is an example of a chart with a confidence band and extended x-axis ticks enabled.",
data: data,
format: 'percentage',
width: torso.width*2,
height: torso.height,
right: trunk.right,
target: '#confidence_band',
show_years: false,
show_confidence_band: ['l', 'u'],
x_extended_ticks: true,
min_y: 0,
max_y: 1,
x_accessor: 'date',
y_accessor: 'value'
})
})
d3.json('data/log.json', function(data){
data = [data];
for(var i=0;i<data.length;i++) {
data[i] = convert_dates(data[i], 'date');
};
//add a chart that has a log scale
moz_chart({
title: "Log Scale",
description: "This is a simple line chart. You can remove the area portion by adding <i>area: false</i> to the arguments list.",
data: data,
y_scale_type:'log',
width: torso.width*2,
height: torso.height,
right: torso.right,
target: '#log1',
x_accessor: 'date',
y_accessor: 'value'
})
})
d3.json('data/some_percentage.json', function(data) {
for(var i=0;i<data.length;i++) {
data[i] = convert_dates(data[i], 'date');
}
var markers = [{
'date': new Date('2014-02-01T00:00:00.000Z'),
'label': '1st Milestone'
}, {
'date': new Date('2014-03-15T00:00:00.000Z'),
'label': '2nd Milestone'
}]
moz_chart({
title: "Some Percentages",
description: "Here is an example that shows percentages.",
data: data,
width: torso.width,
height: torso.height,
right: torso.right,
markers: markers,
format: 'percentage',
target: '#percentage',
x_accessor: 'date',
y_accessor: 'value'
})
moz_chart({
title: "Changing Precision 2",
description: "Here we set <i>decimals: 0</i> for percentages.",
data: data,
decimals: 0,
format: 'Percentage',
width: trunk.width,
height: trunk.height,
right: trunk.right,
xax_count: 4,
target: '#precision2',
x_accessor: 'date',
y_accessor: 'value'
})
moz_chart({
title: "... Or No Rollover Text",
description: "By setting <i>show_rollover_text: false</i>, you can hide the default rollover text from even appearing. This, coupled with the custom callback, gives a lot of interesting options for controlling rollovers.",
data: data,
decimals: 0,
show_rollover_text: false,
format: 'Percentage',
width: trunk.width,
height: trunk.height,
right: trunk.right,
xax_count: 4,
target: '#no-rollover-text',
x_accessor: 'date',
y_accessor: 'value'
})
})
d3.json('data/some_currency.json', function(data) {
data = convert_dates(data, 'date');
moz_chart({
title: "Some Currency",
description: "Here is an example that uses custom units for currency.",
data: data,
width: torso.width,
height: torso.height,
right: torso.right,
target: '#currency',
x_accessor: 'date',
yax_units: '$',
y_accessor: 'value'
})
})
d3.json('data/xnotdate.json', function(data) {
moz_chart({
left: 80,
bottom: 50,
title: "X-Axis Not Time, Animated",
description: "A chart where we're not plotting dates on the x-axis and where the axes include labels and the line animates on load.",
data: data,
animate_on_load: true,
area: false,
width: torso.width,
height: torso.height,
right: trunk.right,
target: '#xnotdate',
x_accessor: 'males',
y_accessor: 'females',
x_label: 'males',
y_label: 'females',
})
})
moz_chart({
title: "Glorious Chart",
error: 'This data is blocked by Lorem Ipsum. Get your stuff together, Ipsum.',
chart_type: 'missing-data',
description: "This is an example of a chart whose data is currently missing. We've also set the <i>error</i> option, which appends an error icon to the title and logs an error to the browser's console.",
target: '#glorious_chart',
width: torso.width,
height: torso.height
})
// lower section
d3.json('data/brief-1.json', function(data) {
data = convert_dates(data, 'date');
moz_chart({
title: "Linked Charts",
description: "The two charts in this section are linked together. A rollover in one causes a rollover in the other.",
data: data,
width: trunk.width,
linked: true,
height: trunk.height,
right: trunk.right,
xax_count: 4,
target: '#briefing-1',
x_accessor: 'date',
y_accessor: 'value'
})
moz_chart({
title: "Small Text Inferred By Size",
description: "If <i>args.width - args.left - args.right</i> is smaller than <i>args.small_width_threshold</i> (and the flip for the height) then the text size automatically scales to be slightly smaller.",
data: data,
width: small.width,
height: small.height,
right: small.right,
top: small.top,
xax_count: 4,
target: '#small1',
x_accessor: 'date',
y_accessor: 'value'
});
moz_chart({
title: "No Y Axis",
description: "Here is an example hiding the y axis.",
data: data,
decimals: 0,
width: trunk.width,
height: trunk.height,
right: trunk.right,
xax_count: 4,
target: '#hidden2',
x_accessor: 'date',
area: false,
y_accessor: 'value',
small_text: true,
y_axis: false
})
})
d3.json('data/split_by.json', function(data) {
data = convert_dates(data, 'date');
split_by_data = moz_chart({
title: "Downloads by Channel",
description: "The chart is gracefully updated depending on the selected channel.",
data: data,
width: torso.width*2,
height: trunk.height,
right: trunk.right,
xax_count: 4,
target: '#split_by',
x_accessor: 'date',
y_accessor: 'release'
})
moz_chart({
title: "Beta Downloads",
description: "The chart is gracefully updated depending on the chosen time period.",
data: data,
width: torso.width*2,
height: trunk.height,
right: trunk.right,
show_years: false,
xax_count: 4,
target: '#modify_time_period',
x_accessor: 'date',
y_accessor: 'beta'
})
})
d3.json('data/brief-2.json', function(data) {
data = convert_dates(data, 'date');
moz_chart({
title: "Other Linked Chart",
description: "Roll over and watch as the chart to the left triggers.",
data: data,
area: false,
linked: true,
width: trunk.width,
height: trunk.height,
right: trunk.right,
xax_count: 4,
target: '#briefing-2',
x_accessor: 'date',
y_accessor: 'value'
})
moz_chart({
title: "Small Text",
description: "By adding small_text:true to the args list, we can force the use of smaller axis text regardless of the width or height",
data: data,
width: trunk.width,
height: trunk.height,
right: trunk.right,
small_text: true,
xax_count: 4,
target: '#small2',
x_accessor: 'date',
y_accessor: 'value'
})
})
d3.json('data/float.json', function(data) {
data = convert_dates(data, 'date');
moz_chart({
title: "Changing Precision 1",
description: "Here we set <i>decimals: 3</i> to get three decimals in the rollover for percentages.",
data: data,
decimals: 3,
width: trunk.width,
height: trunk.height,
right: trunk.right,
xax_count: 4,
target: '#precision1',
x_accessor: 'date',
y_accessor: 'value'
})
moz_chart({
title: "Custom Rollover Text",
description: "Here is an example of changing the rollover text. You could in theory actually update any DOM element with the data from that rollover - a title, for instance.",
data: data,
width: trunk.width,
height: trunk.height,
right: trunk.right,
xax_count: 4,
rollover_callback: function(d, i) {
//custom format the rollover text, show days
var prefix = d3.formatPrefix(d.value);
$('#custom-rollover svg .active_datapoint')
.html('Day ' + (i+1) + ' '
+ prefix.scale(d.value).toFixed(2) + prefix.symbol);
},
target: '#custom-rollover',
x_accessor: 'date',
y_accessor: 'value'
})
})
d3.json('data/neg1.json', function(data) {
data = convert_dates(data, 'date');
moz_chart({
title: "Negative Values 1",
description: "Currently defaults to having no area by default.",
data: data,
width: trunk.width,
height: trunk.height,
right: trunk.right,
target: '#neg1',
x_accessor: 'date',
y_accessor: 'value'
})
})
d3.json('data/neg2.json', function(data) {
moz_chart({
title: "Negative Values 2",
description: "Check for same with two numbers instead of date.",
data: data,
width: trunk.width,
height: trunk.height,
right: trunk.right,
xax_format: function(f) {
var pf = d3.formatPrefix(f);
return pf.scale(f) + pf.symbol;
},
target: '#neg2',
x_accessor: 'subject',
y_accessor: 'measure'
})
})
//add histograms
//generate a Bates distribution of 10 random variables
var values = d3.range(10000).map(d3.random.bates(10));
moz_chart({
title: "Histogram 1",
description: "Raw data values being fed in. Here, we specify the number of bins to be 50 and have bar margins set to 0.",
data: values,
chart_type: 'histogram',
width: trunk.width,
height: trunk.height,
right: trunk.right,
bins: 50,
bar_margin: 0,
target: '#histogram1',
y_extended_ticks: true,
rollover_callback: function(d, i) {
$('#histogram1 svg .active_datapoint')
.html('Value: ' + d3.round(d.x,2) + ' Count: ' + d.y);
}
})
var second = d3.range(10000).map(function(d){return Math.random()*10});
second = d3.layout.histogram()(second)
.map(function(d){
return {'count': d.y, 'value':d.x};
});
moz_chart({
title: "Histogram 2",
description: "Already binned data being fed in.",
data: second,
binned: true,
chart_type: 'histogram',
width: trunk.width,
height: trunk.height,
right: trunk.right,
target: '#histogram2',
y_extended_ticks: true,
x_accessor:'value',
y_accessor:'count',
rollover_callback: function(d, i) {
$('#histogram2 svg .active_datapoint')
.html('Value: ' + d3.round(d.x,2) + ' Count: ' + d.y);
}
})
var third = d3.range(1000).map(d3.random.bates(10));
third = third.map(function(d,i){ return {'val1': d, 'val2': i} });
moz_chart({
title: "Histogram 3",
description: "Unbinned, but in same format as other line chart data.",
data: third,
chart_type: 'histogram',
width: trunk.width,
height: trunk.height,
right: trunk.right,
target: '#histogram3',
y_extended_ticks: true,
x_accessor:'val1',
rollover_callback: function(d, i) {
$('#histogram3 svg .active_datapoint')
.html('Value: ' + d3.round(d.x,2) + ' Count: ' + d.y);
}
})
// check for negative values, for sanity.
var fourth = d3.range(1000).map(d3.random.bates(10));
fourth = fourth.map(function(d,i){return d-.5});
moz_chart({
title: "Histogram 4",
description: "Sanity-checking negative data.",
data: fourth,
chart_type: 'histogram',
width: trunk.width,
height: trunk.height,
right: trunk.right,
target: '#histogram4',
y_extended_ticks: true,
x_accessor:'val1',
rollover_callback: function(d, i) {
$('#histogram4 svg .active_datapoint')
.html('Value: ' + d3.round(d.x,2) + ' Count: ' + d.y);
}
})
//add bar charts
var names = ['first', 'second', 'third', 'fourth', 'fifth']
var bar_data = [];
d3.range(5).map(function(d){
return Math.floor(d3.random.logNormal()()*100)
}).forEach(function(d,i){
d3.range(d).forEach(function(di,ii){
bar_data.push(names[i])
})
});
moz_chart({
title:'Bar Prototype',
description:'work in progress',
data: bar_data,
chart_type: 'bar',
width:trunk.width,
height:trunk.height,
right:trunk.right,
target: '#bar1'
})
moz_chart({
title:'No Axis',
description:'work in progress',
data: bar_data,
chart_type: 'bar',
width:trunk.width,
height:trunk.height,
right:trunk.right,
target: '#bar2',
x_axis: false
})
//add scatterplots
d3.json('data/points1.json', function(data) {
moz_chart({
title: "Scatterplot",
description: "A first example of a scatterplot.",
data: data,
chart_type: 'point',
width: trunk.width,
height: trunk.height*1.5,
right: trunk.right,
target: '#scatter1',
xax_format: function(f) {
var pf = d3.formatPrefix(f);
return pf.scale(f) + pf.symbol;
},
x_accessor: 'x',
y_accessor: 'y'
})
moz_chart({
title: "Simple Line of Best Fit",
description: "For any scatterplot, set <i>least_squares</i> to true to add",
data: data,
least_squares: true,
chart_type: 'point',
width: trunk.width,
height: trunk.height*1.5,
right: trunk.right,
target: '#scatter2',
xax_format: function(f) {
var pf = d3.formatPrefix(f);
return pf.scale(f) + pf.symbol;
},
x_accessor: 'x',
y_accessor: 'y'
})
// moz_chart({
// title: "Lowess",
// description: "use lowess: true",
// data: data,
// lowess: true,
// chart_type: 'point',
// width: trunk.width,
// height: trunk.height,
// right: trunk.right,
// target: '#scatter3',
// xax_format: function(f) {
// var pf = d3.formatPrefix(f);
// return pf.scale(f) + pf.symbol;
// },
// x_accessor: 'x',
// y_accessor: 'y'
// })
})
function assignEventListeners() {
$('#dark-css').click(function () {
$('.missing')
.css('background-image', 'url(images/missing-data-dark.png)');
$('.transparent-rollover-rect')
.attr('fill', 'white');
$('.wip')
.css('background-color', '#3b3b3b');
$('.pill').removeClass('active');
$(this).toggleClass('active');
$('#dark').attr({href : 'css/metrics-graphics-darkness.css'});
return false;
})
$('#light-css').click(function () {
$('.missing')
.css('background-image', 'url(images/missing-data.png)');
$('.transparent-rollover-rect')
.attr('fill', 'black');
$('.wip')
.css('background-color', '#f1f1f1');
$('.pill').removeClass('active');
$(this).toggleClass('active');
$('#dark').attr({href : ''});
return false;
})
$('.split-by-controls button').click(function() {
var new_y_accessor = $(this).data('y_accessor');
//change button state
$(this).addClass('active')
.siblings()
.removeClass('active');
//update data
moz_chart({
data: split_by_data,
width: torso.width*2,
height: trunk.height,
right: trunk.right,
xax_count: 4,
target: '#split_by',
x_accessor: 'date',
y_accessor: new_y_accessor
})
})
$('.modify-time-period-controls button').click(function() {
var past_n_days = $(this).data('time_period');
var data = modify_time_period(split_by_data, past_n_days);
//change button state
$(this).addClass('active')
.siblings()
.removeClass('active');
//update data
moz_chart({
data: data,
width: torso.width*2,
height: trunk.height,
right: trunk.right,
show_years: false,
transition_on_update: false,
xax_count: 4,
target: '#modify_time_period',
x_accessor: 'date',
y_accessor: 'beta'
})
})
}
//replace all SVG images with inline SVG
//http://stackoverflow.com/questions/11978995/how-to-change-color-of-svg
//-image-using-css-jquery-svg-image-replacement
$('img.svg').each(function() {
var $img = jQuery(this);
var imgID = $img.attr('id');
var imgClass = $img.attr('class');
var imgURL = $img.attr('src');
$.get(imgURL, function(data) {
// Get the SVG tag, ignore the rest
var $svg = jQuery(data).find('svg');
// Add replaced image's ID to the new SVG
if(typeof imgID !== 'undefined') {
$svg = $svg.attr('id', imgID);
}
// Add replaced image's classes to the new SVG
if(typeof imgClass !== 'undefined') {
$svg = $svg.attr('class', imgClass+' replaced-svg');
}
// Remove any invalid XML tags as per http://validator.w3.org
$svg = $svg.removeAttr('xmlns:a');
// Replace image with new SVG
$img.replaceWith($svg);
}, 'xml');
})
})