-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
1011 lines (837 loc) · 29.5 KB
/
index.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
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
<!DOCTYPE html>
<!--[if IE 9]><html class="lt-ie10" lang="en" > <![endif]-->
<html class="no-js" lang="en" >
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Comédie Française Registers Project</title>
<link rel="stylesheet" href="css/fonts.css">
<!-- If you are using CSS version, only link these 2 files, you may add app.css to use for your overrides if you like. -->
<link rel="stylesheet" href="css/normalize.css">
<link rel="stylesheet" href="css/foundation.css">
<link rel="stylesheet" href="css/style.css">
<link rel="stylesheet" href="app.css">
<script src="js/vendor/modernizr.js"></script>
</head>
<body>
<!-- body content here -->
<div class="fixed">
<nav class="top-bar" data-topbar>
<section class="top-bar-section">
<!-- Right Nav Section -->
<ul class="right">
<li id="top-menu-link" class="top-nav-link"><a href="#">Menu</a></li>
<li class="top-nav-link"><a href="#">The data</a></li>
<li class="top-nav-link"><a href="#">Project Background</a></li>
<li class="top-nav-link"><a href="#">Case Studies</a></li>
<li class="top-nav-link"><a href="#">Scholarship</a></li>
<li class="top-nav-link"><a href="#">News and Updates</a></li>
<li class="top-nav-link" id="french-language"><a href="#">French</a></li>
<li class="top-nav-link" id="english-language"><a href="#">English</a></li>
<li id="go-to-app"><a href="#">Go to the app</a></li>
</ul>
<!-- Left Nav Section -->
<ul class="left">
<li class="flag" id="flag-left">
<a href="#"><img src="img/french-flag.jpg"></a>
</li>
<li class="flag" id="flag-right">
<a href="#"><img src="img/UK-US-flag.png"></a>
</li>
</ul>
<a href="#" id="cfrp-logo-wide"><img src="img/cfrp-logo-top-nav.png"></a>
</section>
</nav>
<div id="cfrp-logo-container">
<a href="#" id="cfrp-logo-narrow"><img src="img/cfrp-logo-top-nav.png"></a>
</div>
</div>
<div class="titlebar">
<div class="row hero-captions">
<div class="medium-8 columns"></div>
<div class="medium-4 columns">
<h1>Demo</h1>
<h6>Multi-dimensional analysis</h6>
<div id="progressbar"></div>
</div>
</div>
</div>
<!--Begin main content area-->
<div id="charts">
<div class="row content_row">
<div class="small-2 columns">
<form id="measure_form">
<div class="title">Mesure</div>
</form>
<form id="granularity_form">
<div class="title">Timeline</div>
</form>
</div>
<div id="author" class="chart sparse small-3 columns">
<div class="title">Auteur 1</div>
</div>
<div id="genre" class="chart sparse small-3 columns">
<div class="title">Genre 1</div>
</div>
<div id="section" class="chart sparse small-2 columns">
<div class="title">Place</div>
</div>
<div id="weekday" class="chart sparse small-2 columns">
<div class="title">Jour de la semaine</div>
</div>
</div>
<div class="row content_row">
<div class="small-12 columns">
<div id="timeline" class="chart">
<div class="title">Date</div>
</div>
</div>
</div>
</div>
<!--End main content area-->
<!--BEGIN FOOTER-->
<div id="footer-wrapper">
<!-- usage information -->
<div class="row">
<!--contact area-->
<div class="small-12 medium-3 columns">
<p>DATA<br>
Current as of 10-Oct-2014
</p>
<p>CONTACT<br>
<a href="#">christopher.w.york (at) gmail.com</a>
</p>
<!-- email signup
<form>
<div class="row collapse">
<div class="small-10 columns">
<input type="text" placeholder="Join The Email List">
</div>
<div class="small-2 columns">
<a href="#" class="button postfix">→</a>
</div>
</div>
</form>
-->
</div>
<div class="small-12 medium-3 columns">
<p>This demo is intended for use on Chrome (version 38.0+), on OS X 10.9+ or Windows 7+ with at least 2GB of memory.</p>
<p>If the page does not load, check your browser and OS details.</p>
</div>
</div>
<div class="row" id="logos">
<div class="small-12 columns show-for-large">
<img src="img/footer_logos_8up.png" alt="footer_logos_8up" width="1043" height="86">
</div>
<div class="large-6 columns hide-for-large">
<img src="img/footer_logos_4up_1.png">
</div>
<div class="large-6 columns hide-for-large">
<img src="img/footer_logos_4up_2.png">
</div>
</div>
</div>
<!--END FOOTER-->
<!--end body content-->
<script src="js/vendor/jquery.js"></script>
<script src="js/foundation.min.js"></script>
<script src="crossfilter.min.js"></script>
<script src="d3.min.js"></script>
<script>
/* jshint globalstrict: true */
"use strict";
/* global d3, crossfilter, $, window, document, setTimeout */
$(document).foundation();
var dateFormat = d3.time.format('%Y-%m-%d');
var sales,
playbill,
playbillIdx;
function reduceDistinct(fn) {
return {
add: function (p, d) {
var val = fn(d);
if (val in p.counts)
p.counts[val]++;
else
p.counts[val] = 1;
return p;
},
remove: function (p, d) {
var val = fn(d);
p.counts[val]--;
if (p.counts[val] === 0)
delete p.counts[val];
return p;
},
init: function () {
var p = {};
p.counts = {};
p.final = function() { return Object.keys(p.counts).length; };
return p;
}
};
}
function reduceSum(fn) {
return {
add: function(p, v) {
p.sum += fn(v);
return p;
},
remove: function(p, v) {
p.sum -= fn(v);
return p;
},
init: function() {
var p = {};
p.sum = 0;
p.final = function () { return Math.max(0, d3.round(p.sum, 0)); };
return p;
}
};
}
function lookupAuthor(date, n, dflt) {
var pb = playbillIdx.get(date);
if (pb) { return pb[0].author; }
else { return dflt; }
}
function lookupGenre(date, n, dflt) {
var pb = playbillIdx.get(date);
if (pb) { return pb[0].genre; }
else { return dflt; }
}
function decade(date) {
var rounded = d3.time.year(date),
by10 = d3.round(date.getFullYear(), -1);
rounded.setFullYear(by10);
return rounded;
}
var aggregates = [ { name: "Représentation", reducer: reduceDistinct(function(d) { return d.register_id; }), checked: true },
{ name: "Recettes", reducer: reduceSum(function(d) { return d.sold * d.price; }), unit: "l." },
{ name: "Billets vendus", reducer: reduceSum(function(d) { return d.sold; }), disabled: true } ];
var granularities = [ { name: "Décennie", group: decade, ticks: [d3.time.year, 10] },
{ name: "Année", group: d3.time.year, ticks: [d3.time.year, 5], checked: true },
{ name: "Mois", group: d3.time.month, ticks: [d3.time.month, 4] },
{ name: "Semaine", group: d3.time.week, ticks: [d3.time.month, 1] } ];
// set up crossfilter visualization
var plabel = "",
pcount = 0;
function progress(l, n, d) {
// Sadly, different browsers and servers send inconsistent values for xhr progress
// For now, ignore progress measures and just increment a counter...
if (l === plabel) {
pcount += 1;
} else {
plabel = l; pcount = 0;
}
d3.select("#progressbar").text(plabel + " " + (pcount > 0 ? pcount : ""));
}
var tasks = function() { return; };
function task(msg, fn) {
var prev = tasks;
tasks = function() {
prev();
setTimeout(function() {
progress(msg);
fn();
}, 0);
};
}
function visualize() {
if (!sales || !playbill) { return; }
// state for this visualization
var sale;
task("indexing", function () {
sale = crossfilter(sales);
});
var date, timelineAggs;
task("indexing date", function() {
date = sale.dimension(function(d) { return d.date; });
timelineAggs = granularities.map(function (agg) { return date.group(agg.group); });
});
var author, authorAgg;
task("indexing author", function() {
author = sale.dimension(function(d) { return lookupAuthor(d.date, 0, "Inconnu"); });
authorAgg = author.group();
});
var genre, genreAgg;
task("indexing genre", function() {
genre = sale.dimension(function(d) { return lookupGenre(d.date, 0, "Inconnu"); });
genreAgg = genre.group();
});
var section, sectionAgg;
task("indexing section", function() {
section = sale.dimension(function(d) { return d.section; });
sectionAgg = section.group();
});
var weekday, weekdayAgg;
task("indexing weekday", function() {
weekday = sale.dimension(function(d) { return d.weekday; });
weekdayAgg = weekday.group();
});
task("", function() {
// the measure selection form & listener
var agg_labels = d3.select("#measure_form").selectAll("label")
.data(aggregates)
.enter().append("label");
agg_labels.append("input")
.attr("type", "radio")
.attr("name", "measure")
.attr("value", function(d) { return d.name; })
.property("checked", function(d) { return d.checked; })
.property("disabled", function(d) { return d.disabled; });
agg_labels.append("span").text(function(d) { return d.name + (d.unit ? " (" + d.unit + ")" : ""); });
var checkedMeasure = function() {
var checked_name = d3.select("#measure_form input:checked").attr("value");
return aggregates.filter(function (d) { return d.name == checked_name; })[0];
};
// the granularity form & listener
var gran_labels = d3.select("#granularity_form").selectAll("label")
.data(granularities)
.enter().append("label");
gran_labels.append("input")
.attr("type", "radio")
.attr("name", "granularity")
.attr("value", function(d, i) { return i; })
.property("checked", function(d) { return d.checked; });
gran_labels.append("span").text(function(d) { return d.name; });
var updateMeasure = function(agg) {
[authorAgg, genreAgg, sectionAgg, weekdayAgg].concat(timelineAggs).forEach(function (grp) {
grp.reduce(agg.reducer.add, agg.reducer.remove, agg.reducer.init).order(function (d) { return d.final(); });
});
};
// prime reducers with the default checked measure
updateMeasure(checkedMeasure());
d3.select("#measure_form").on("change", function () {
var agg = checkedMeasure();
updateMeasure(agg);
charts.forEach(function (c) { c.immediate(true); });
renderAll();
});
var charts = [
sparseChart()
.dimension(author)
.group(authorAgg),
sparseChart()
.dimension(genre)
.group(genreAgg),
sparseChart()
.dimension(section)
.group(sectionAgg)
.width(130),
sparseChart()
.dimension(weekday)
.group(weekdayAgg)
.order(function (a, b) {
// not clear why weekdayFormat.parse always returns mondays, and mangles other aspects...
var keyWD = { Lundi: 1, Mardi: 2, Mercredi: 3, Jeudi: 4, Vendredi: 5, Samedi: 6, Dimanche: 7 };
return keyWD[a] - keyWD[b];
})
.width(110),
timelineChart()
.dimension(date)
];
var checkedGranularity = function() {
return d3.select("#granularity_form input:checked").attr("value");
};
var updateGranularity = function(agg_index) {
// TODO. better way to reference timeline
// TODO.... now we're getting REALLY messy...
var agg = timelineAggs[agg_index];
var round = granularities[agg_index].group;
var tickInfo = granularities[agg_index].ticks;
var timeline = charts[4];
timeline.group(agg).x(d3.time.scale()
.domain(d3.extent(sales, function(d) { return d.date; }))
.rangeRound([0, d3.max([14 * agg.size(), 900])]))
.ticks(tickInfo[0], tickInfo[1])
.round(round);
};
// prime granularity
updateGranularity(checkedGranularity());
d3.select("#granularity_form").on("change", function () {
var i = checkedGranularity();
updateGranularity(i);
d3.select("#timeline").html("");
renderAll();
});
// set up rest of chart
var chart = d3.selectAll(".chart")
.data(charts)
.each(function(chart) { chart.on("brush", renderAll).on("brushend", renderAll); });
renderAll();
function render(method) {
/* jshint validthis:true */
d3.select(this).call(method);
}
function renderAll() {
chart.each(render);
}
window.filter = function(filters) {
filters.forEach(function(d, i) { charts[i].filter(d); });
renderAll();
};
window.reset = function(i) {
charts[i].filter(null);
renderAll();
};
});
// start off the load
tasks();
}
// load data from server
d3.json("sales.json")
.on("error", function(error) { throw error; })
.on("progress", function() { progress("loading registers", d3.event.loaded, d3.event.total); })
.on("load", function(data) {
data.forEach(function (d, i) {
d.date = dateFormat.parse(d.date);
});
sales = data;
visualize();
}).get();
d3.json("playbill.json")
.on("error", function(error) { throw error; })
.on("progress", function() { progress("loading plays", d3.event.loaded, d3.event.total); })
.on("load", function(data) {
playbillIdx = d3.map();
data.forEach(function (d, i) {
d.date = dateFormat.parse(d.date);
if (!playbillIdx.has(d.date)) { playbillIdx.set(d.date, []); }
playbillIdx.get(d.date).push(d);
});
playbill = data;
visualize();
}).get();
function sparseChart() {
if (!sparseChart.id) sparseChart.id = 0;
var margin = {top: 10, right: 10, bottom: 10, left: 0},
barHeight = 12,
textHeight = 12,
height = 200,
width = 190,
x = d3.scale.linear().range([0, width]),
id = sparseChart.id++,
dimension,
group,
preview = false,
pinned = d3.set(),
order,
alphaNum = false,
event_ = d3.dispatch("brush", "brushend");
function filterPins(d) {
// return pinned.has(d) || (preview ? d === preview : pinned.empty());
return pinned.has(d) || pinned.empty();
}
function chart(div) {
var component = div,
commasFormatter = d3.format(",.0f");
x.domain([0, group.top(1)[0].value.final()]);
var y = function(d) { return d * (barHeight + textHeight); };
div.each(function() {
var div = d3.select(this),
scroller,
g = div.select("g.results"),
// g_pinned = div.select("g.pinned"),
bar,
preview_circle,
data = [];
// a hairy way to resort the results so the pinned items rise to the top
var pins_seen = 0,
raw_data;
if (order) {
raw_data = group.all().sort(function(a, b) { return order(a.key, b.key); });
} else if (alphaNum) {
raw_data = group.all();
} else {
raw_data = group.top(group.size());
}
raw_data.forEach(function (d, i) {
if (pinned.has(d.key)) {
data[pins_seen] = d;
pins_seen += 1;
} else {
data[pinned.size() + i - pins_seen] = d;
}
});
// Create initial chart
if (g.empty()) {
if (!order) {
div.select(".title").append("span")
.attr("class", "mode")
.on("click", function () { alphaNum = !alphaNum; chart(component); });
}
/*
// set up the pinned / selected list
g_pinned = div.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", y(pinned.size()) + margin.top)
.append("g")
.attr("class", "pinned")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
*/
// set up the normal results list
scroller = div.append("div")
.attr("class", "scroller");
g = scroller.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", y(group.size()) + margin.bottom)
.append("g")
.attr("class", "results")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
bar = g.selectAll(".bar")
.data(data)
.enter().append("g")
.attr("class", "background bar")
.attr("transform", function (d, i) { return "translate(0," + y(i) + ")"; });
preview_circle = bar.append("circle")
.attr("class", "preview")
.attr("cx", x.range()[1])
.attr("cy", -4)
.attr("stroke-width", 4)
.attr("r", 5); // effective size is 3 pixels (r - 1/2 of stroke width)
preview_circle.on("mouseover", function (d) {
div.classed("previewing", true);
preview_circle.classed("active", false);
d3.select(this).classed("active", true);
preview = d.key;
dimension.filterExact(d.key);
event_.brush();
});
preview_circle.on("mouseout", function (d) {
// wait 1 second, then if no new preview set, return to default filters
var old_preview = preview;
window.setTimeout(function () {
if (preview === old_preview) {
div.classed("previewing", false);
preview_circle.classed("active", false);
preview = false;
dimension.filterFunction(filterPins);
event_.brushend();
}}, 1000);
});
bar.on("click", function (d) {
if (pinned.has(d.key)) {
pinned.remove(d.key);
} else {
pinned.add(d.key);
}
// dimension.filterAll();
dimension.filterFunction(filterPins);
scroller[0][0].scrollTop = 0;
event_.brushend();
});
bar.append("path");
bar.append("g")
.attr("class", "author_name")
.attr("transform", "translate(0,0)")
.append("text");
bar.append("g")
.attr("class", "measure")
.append("text");
}
function measure_info(d) {
if (x(d.value.final()) - 4 > 35) {
return { offset: x(d.value.final()) - 4, anchor: "end" };
} else {
return { offset: x(d.value.final()) + 4, anchor: "start" };
}
}
// adjust for live values
if (!order) {
div.select(".mode").text(alphaNum ? "a-z" : "1-9");
}
/*
// pinned area
g_pinned.selectAll(".bar")
.data(pinned)
.append("g")
.attr("class", "foreground bar")
.attr("transform", function (d, i) { return "translate(0," + y(i) + ")"; });
*/
// results area
bar = g.selectAll(".bar")
.data(data)
.attr("class", function(d) { return "bar " + (pinned.has(d.key) ? "foreground" : "background"); });
bar.select(".author_name text")
.text(function (d) { return d.key; });
bar.select(".measure")
.attr("transform", function(d) { return "translate(" + measure_info(d).offset + "," + (textHeight - 1) + ")"; })
.select("text")
.attr("class", function(d) { return measure_info(d).anchor; })
.attr("text-anchor", function(d) { return measure_info(d).anchor; })
.text(function (d) { return commasFormatter(d.value.final()); });
bar.select("path")
.attr("d", function(d) { return "M0,2H" + x(d.value.final()) + "v" + barHeight + "H0";});
// if rescaling in the future, schedule the event
// ... code omitted for later
});
}
chart.dimension = function(_) {
if (!arguments.length) return dimension;
dimension = _;
dimension.filterAll();
dimension.filterFunction(filterPins);
return chart;
};
chart.group = function(_) {
if (!arguments.length) return group;
group = _;
return chart;
};
chart.width = function(_) {
if (!arguments.length) return width;
width = _;
x = d3.scale.linear().range([0, width]);
return chart;
};
chart.order = function(_) {
if (!arguments.length) return order;
order = _;
return chart;
};
chart.immediate = function(_) {
// code omitted for later; no-op
return true;
};
return d3.rebind(chart, event_, "on");
}
function timelineChart() {
if (!timelineChart.id) timelineChart.id = 0;
var margin = {top: 10, right: 80, bottom: 20, left: 10},
x,
y = d3.scale.linear().range([100, 0]),
id = timelineChart.id++,
axis_x = d3.svg.axis().orient("bottom"),
axis_y = d3.svg.axis().orient("right"),
brush = d3.svg.brush(),
brushDirty,
dimension,
group,
round,
animate = false,
delay_rescale = false,
timestamp;
var customTimeFormat = d3.time.format.multi([
["%a %d", function(d) { return d.getDay() && d.getDate() != 1; }],
["%b %d", function(d) { return d.getDate() != 1; }],
["%B", function(d) { return d.getMonth(); }],
["%Y", function() { return true; }]
]);
function chart(div) {
var width = x.range()[1],
height = y.range()[0];
var commasFormatter = d3.format(",.0f");
var max = group.top(1)[0].value.final();
if (!delay_rescale || !timestamp) {
y.domain([0, max]);
}
timestamp = Date.now();
axis_y.scale(y)
.tickFormat(function(d) { return commasFormatter(d); })
.ticks(4);
axis_x.tickFormat(customTimeFormat);
div.each(function() {
var div = d3.select(this),
scroller,
g = div.select("g"),
gBrush;
// Create the skeletal chart.
if (g.empty()) {
scroller = div.append("div")
.attr("class", "scroller");
g = scroller.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
g.append("g")
.attr("class", "axis y")
.attr("transform", "translate(" + (width + 10) + ",0)");
g.append("clipPath")
.attr("id", "clip-" + id)
.append("rect")
.attr("width", width)
.attr("height", height);
g.selectAll(".bar")
.data(["background", "foreground"])
.enter().append("path")
.attr("class", function(d) { return d + " bar"; })
.datum(group.all());
/* bar for previewing flip books
var crosshairs = g.selectAll(".crosshairs")
.data([new Date(1770, 7, 14)])
.enter().append("g")
.attr("class", "crosshairs");
crosshairs.append("line")
.attr("x1", function (d) { return x(d); })
.attr("y1", -margin.top)
.attr("x2", function (d) { return x(d); })
.attr("y2", height)
.attr("stroke-dasharray", "4 4");
crosshairs.append("text")
.attr("x", function (d) { return x(d); })
.attr("dx", ".4em")
.attr("dy", "-.4em")
.style("text-anchor", "start")
.text(dateFormat);
*/
g.selectAll(".foreground.bar")
.attr("clip-path", "url(#clip-" + id + ")");
g.append("g")
.attr("class", "axis")
.attr("transform", "translate(0," + (height + 2) + ")")
.call(axis_x);
// Initialize the brush component with pretty resize handles.
gBrush = g.append("g").attr("class", "brush").call(brush);
gBrush.selectAll("rect").attr("height", height);
gBrush.selectAll(".resize").append("path").attr("d", resizePath);
}
// Only redraw the brush if set externally.
if (brushDirty) {
brushDirty = false;
g.selectAll(".brush").call(brush);
div.select(".title a").style("display", brush.empty() ? "none" : null);
if (brush.empty()) {
g.selectAll("#clip-" + id + " rect")
.attr("x", 0)
.attr("width", width);
} else {
var extent = brush.extent();
g.selectAll("#clip-" + id + " rect")
.attr("x", x(extent[0]))
.attr("width", x(extent[1]) - x(extent[0]));
}
}
// this actually draws the new state
if (animate) {
g.selectAll(".bar").transition().duration(1500).attr("d", barPath);
g.select(".axis.y").transition().duration(1500).call(axis_y);
} else {
g.selectAll(".bar").attr("d", barPath);
g.select(".axis.y").call(axis_y);
}
// if rescaling in the future, schedule the event
if (delay_rescale) {
var old_timestamp = timestamp,
delay_ms = max > y.domain()[1] ? 500 : 2000;
setTimeout(function() {
// if component hasn't been updated again, animate to the new scale
if (timestamp === old_timestamp) {
animate = true;
delay_rescale = false;
chart(div);
}
}, delay_ms);
}
animate = false;
delay_rescale = true;
});
function barPath(groups) {
var path = [],
i = -1,
n = groups.length,
d;
while (++i < n) {
d = groups[i];
path.push("M", x(d.key), ",", height, "V", y(d.value.final()), "h9V", height);
}
return path.join("");
}
function resizePath(d) {
var e = +(d == "e"),
x = e ? 1 : -1,
y = height / 3;
return "M" + (0.5 * x) + "," + y +
"A6,6 0 0 " + e + " " + (6.5 * x) + "," + (y + 6) +
"V" + (2 * y - 6) +
"A6,6 0 0 " + e + " " + (0.5 * x) + "," + (2 * y) +
"Z" +
"M" + (2.5 * x) + "," + (y + 8) +
"V" + (2 * y - 8) +
"M" + (4.5 * x) + "," + (y + 8) +
"V" + (2 * y - 8);
}
}
brush.on("brushstart.chart", function() {
var div = d3.select(this.parentNode.parentNode.parentNode);
div.select(".title a").style("display", null);
});
brush.on("brush.chart", function() {
var g = d3.select(this.parentNode),
extent = brush.extent();
if (round) g.select(".brush")
.call(brush.extent(extent = extent.map(round)))
.selectAll(".resize")
.style("display", null);
g.select("#clip-" + id + " rect")
.attr("x", x(extent[0]))
.attr("width", x(extent[1]) - x(extent[0]));
dimension.filterRange(extent);
});
brush.on("brushend.chart", function() {
if (brush.empty()) {
var div = d3.select(this.parentNode.parentNode.parentNode);
div.select(".title a").style("display", "none");
div.select("#clip-" + id + " rect").attr("x", null).attr("width", "100%");
dimension.filterAll();
}
});
chart.margin = function(_) {
if (!arguments.length) return margin;
margin = _;
return chart;
};
chart.x = function(_) {
if (!arguments.length) return x;
x = _;
axis_x.scale(x);
brush.x(x);
return chart;
};
chart.ticks = function(a, b) {
axis_x.ticks(a, b);
return chart;
};
chart.y = function(_) {
if (!arguments.length) return y;
y = _;
return chart;
};
chart.dimension = function(_) {
if (!arguments.length) return dimension;
dimension = _;
return chart;
};
chart.filter = function(_) {
if (_) {
brush.extent(_);
dimension.filterRange(_);
} else {
brush.clear();
dimension.filterAll();
}
brushDirty = true;
return chart;
};
chart.group = function(_) {
if (!arguments.length) return group;
group = _;
return chart;
};
chart.round = function(_) {
if (!arguments.length) return round;
round = _;
return chart;
};
chart.immediate = function(_) {
if (!arguments.length) return !(animate || delay_rescale);
animate = ! _;