forked from wa0x6e/cal-heatmap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cal-heatmap.js
executable file
·2888 lines (2493 loc) · 83.9 KB
/
cal-heatmap.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
/*! cal-heatmap v3.3.1 (Mon Oct 07 2013 21:53:51)
* ---------------------------------------------
* Cal-Heatmap is a javascript module to create calendar heatmap to visualize time series data, a la github contribution graph
* https://github.com/kamisama/cal-heatmap
* Licensed under the MIT license
* Copyright 2013 Wan Qi Chen
*/
var CalHeatMap = function() {
"use strict";
var self = this;
var allowedDataType = ["json", "csv", "tsv", "txt"];
// Default settings
this.options = {
// selector string of the container to append the graph to
// Accept any string value accepted by document.querySelector or CSS3
// or an Element object
itemSelector: "#cal-heatmap",
// Whether to paint the calendar on init()
// Used by testsuite to reduce testing time
paintOnLoad: true,
// ================================================
// DOMAIN
// ================================================
// Number of domain to display on the graph
range: 12,
// Size of each cell, in pixel
cellSize: 10,
// Padding between each cell, in pixel
cellPadding: 2,
// For rounded subdomain rectangles, in pixels
cellRadius: 0,
domainGutter: 2,
domainMargin: [0,0,0,0],
domain: "hour",
subDomain: "min",
// Number of columns to split the subDomains to
// If not null, will takes precedence over rowLimit
colLimit: null,
// Number of rows to split the subDomains to
// Will be ignored if colLimit is not null
rowLimit: null,
// First day of the week is Monday
// 0 to start the week on Sunday
weekStartOnMonday: true,
// Start date of the graph
// @default now
start: new Date(),
minDate: null,
maxDate: null,
// URL, where to fetch the original datas
data: "",
dataType: allowedDataType[0],
// Whether to consider missing date:value from the datasource
// as equal to 0, or just leave them as missing
considerMissingDataAsZero: false,
// Load remote data on calendar creation
// When false, the calendar will be left empty
loadOnInit: true,
// Calendar orientation
// false: display domains side by side
// true : display domains one under the other
verticalOrientation: false,
// Domain dynamic width/height
// The width on a domain depends on the number of
domainDynamicDimension: true,
// Domain Label properties
label: {
// valid: top, right, bottom, left
position: "bottom",
// Valid: left, center, right
// Also valid are the direct svg values: start, middle, end
align: "center",
// By default, there is no margin/padding around the label
offset: {
x: 0,
y: 0
},
rotate: null,
// Used only on vertical orientation
width: 100,
// Used only on horizontal orientation
height: null
},
// ================================================
// LEGEND
// ================================================
// Threshold for the legend
legend: [10,20,30,40],
// Whether to display the legend
displayLegend: true,
legendCellSize: 10,
legendCellPadding: 2,
legendMargin: [0, 0, 0, 0],
// Legend vertical position
// top: place legend above calendar
// bottom: place legend below the calendar
legendVerticalPosition: "bottom",
// Legend horizontal position
// accepted values: left, center, right
legendHorizontalPosition: "left",
// Legend rotation
// accepted values: horizontal, vertical
legendOrientation: "horizontal",
// Objects holding all the heatmap different colors
// null to disable, and use the default css styles
//
// Examples:
// legendColors: {
// min: "green",
// max: "red",
// empty: "#ffffff",
// base: "grey",
// overflow: "red"
// }
legendColors: null,
// ================================================
// HIGHLIGHT
// ================================================
// List of dates to highlight
// Valid values:
// - []: don't highlight anything
// - "now": highlight the current date
// - an array of Date objects: highlight the specified dates
highlight: [],
// ================================================
// TEXT FORMATTING / i18n
// ================================================
// Name of the items to represent in the calendar
itemName: ["item", "items"],
// Formatting of the domain label
// @default: null, will use the formatting according to domain type
// Accept a string used as specifier by d3.time.format()
// or a function
//
// Refer to https://github.com/mbostock/d3/wiki/Time-Formatting
// for accepted date formatting used by d3.time.format()
domainLabelFormat: null,
// Formatting of the title displayed when hovering a subDomain cell
subDomainTitleFormat: {
empty: "{date}",
filled: "{count} {name} {connector} {date}"
},
// Formatting of the {date} used in subDomainTitleFormat
// @default: null, will use the formatting according to subDomain type
// Accept a string used as specifier by d3.time.format()
// or a function
//
// Refer to https://github.com/mbostock/d3/wiki/Time-Formatting
// for accepted date formatting used by d3.time.format()
subDomainDateFormat: null,
// Formatting of the text inside each subDomain cell
// @default: null, no text
// Accept a string used as specifier by d3.time.format()
// or a function
//
// Refer to https://github.com/mbostock/d3/wiki/Time-Formatting
// for accepted date formatting used by d3.time.format()
subDomainTextFormat: null,
// Formatting of the title displayed when hovering a legend cell
legendTitleFormat: {
lower: "less than {min} {name}",
inner: "between {down} and {up} {name}",
upper: "more than {max} {name}"
},
// Animation duration, in ms
animationDuration: 500,
nextSelector: false,
previousSelector: false,
itemNamespace: "cal-heatmap",
// ================================================
// EVENTS CALLBACK
// ================================================
// Callback when clicking on a time block
onClick: null,
// Callback after painting the empty calendar
afterLoad: null,
// Callback after loading the next domain in the calendar
afterLoadNextDomain: null,
// Callback after loading the previous domain in the calendar
afterLoadPreviousDomain: null,
// Callback after finishing all actions on the calendar
onComplete: null,
// Callback after fetching the datas, but before applying them to the calendar
// Used mainly to convert the datas if they're not formatted like expected
// Takes the fetched "data" object as argument, must return a json object
// formatted like {timestamp:count, timestamp2:count2},
afterLoadData: function(data) { return data; },
// Callback triggered after calling next().
// The `status` argument is equal to true if there is no
// more next domain to load
//
// This callback is also executed once, after calling previous(),
// only when the max domain is reached
onMaxDomainReached: null,
// Callback triggered after calling previous().
// The `status` argument is equal to true if there is no
// more previous domain to load
//
// This callback is also executed once, after calling next(),
// only when the min domain is reached
onMinDomainReached: null
};
this._domainType = {
"min": {
name: "minute",
level: 10,
row: function() {
if (self.options.colLimit > 0) {
return Math.ceil(60 / self.options.colLimit);
}
return self.options.rowLimit || 10;
},
column: function() {
if (self.options.rowLimit > 0) {
return Math.ceil(60 / self.options.rowLimit);
}
return self.options.colLimit || 6;
},
position: {
x: function(d) { return Math.floor(d.getMinutes() / self._domainType.min.row(d)); },
y: function(d) { return d.getMinutes() % self._domainType.min.row(d); }
},
format: {
date: "%H:%M, %A %B %-e, %Y",
legend: "",
connector: "at"
},
extractUnit: function(d) {
var dt = new Date(d.getFullYear(), d.getMonth(), d.getDate(), d.getHours(), d.getMinutes());
return dt.getTime();
}
},
"hour": {
name: "hour",
level: 20,
row: function(d) {
if (self.options.colLimit > 0) {
var total = 24;
if (self.options.domain === "week") {
total = 24 * 7;
} else if (self.options.domain === "month") {
total = 24 * self.getDayCountInMonth(d);
}
return Math.ceil(total / self.options.colLimit);
}
return self.options.rowLimit || 6; },
column: function(d) {
if (self.options.colLimit > 0) {
return self.options.colLimit;
}
switch(self.options.domain) {
case "day": return self.options.rowLimit > 0 ? Math.ceil(24 / self.options.rowLimit) : 4;
case "week": return self.options.rowLimit > 0 ? Math.ceil(24 * 7 / self.options.rowLimit) : 28;
case "month": return self.options.rowLimit > 0 ?
Math.ceil(24 * 31 / self.options.rowLimit) :
((self.options.domainDynamicDimension ? self.getEndOfMonth(d).getDate(): 31) * 4);
}
},
position: {
x: function(d) {
if (self.options.domain === "month") {
if (self.options.colLimit > 0 || self.options.rowLimit > 0) {
return Math.floor((d.getHours() + (d.getDate()-1)*24) / self._domainType.hour.row(d));
}
return Math.floor(d.getHours() / self._domainType.hour.row(d)) + (d.getDate()-1)*4;
} else if (self.options.domain === "week") {
if (self.options.colLimit > 0 || self.options.rowLimit > 0) {
return Math.floor((d.getHours() + self.getWeekDay(d)*24) / self._domainType.hour.row(d));
}
return Math.floor(d.getHours() / self._domainType.hour.row(d)) + self.getWeekDay(d)*4;
}
return Math.floor(d.getHours() / self._domainType.hour.row(d));
},
y: function(d) {
if (self.options.colLimit > 0 || self.options.rowLimit > 0) {
switch(self.options.domain) {
case "month": return Math.floor((d.getHours() + (d.getDate()-1)*24) % self._domainType.hour.row(d));
case "week": return Math.floor((d.getHours() + self.getWeekDay(d)*24) % self._domainType.hour.row(d));
}
}
return d.getHours() % self._domainType.hour.row(d);
}
},
format: {
date: "%Hh, %A %B %-e, %Y",
legend: "%H:00",
connector: "at"
},
extractUnit: function(d) {
var dt = new Date(d.getFullYear(), d.getMonth(), d.getDate(), d.getHours());
return dt.getTime();
}
},
"day": {
name: "day",
level: 30,
row: function(d) {
if (self.options.colLimit > 0) {
var total = 0;
switch(self.options.domain) {
case "year": total = self.getDayCountInYear(d); break;
case "month": total = self.getDayCountInMonth(d); break;
case "week": total = 7;
}
return Math.ceil(total / self.options.colLimit);
}
return self.options.rowLimit || 7;
},
column: function(d) {
if (self.options.colLimit > 0) {
return self.options.colLimit;
}
d = new Date(d);
switch(self.options.domain) {
case "year":
if (self.options.rowLimit > 0) {
var dayCountInMonth = self.options.domainDynamicDimension ? self.getDayCountInYear(d) : 366;
return Math.ceil(dayCountInMonth / self.options.rowLimit);
}
return (self.options.domainDynamicDimension ? (self.getWeekNumber(new Date(d.getFullYear(), 11, 31)) - self.getWeekNumber(new Date(d.getFullYear(), 0)) + 1): 54);
case "month":
if (self.options.rowLimit > 0) {
var dayNumber = self.options.domainDynamicDimension ? new Date(d.getFullYear(), d.getMonth()+1, 0).getDate() : 31;
return Math.ceil(dayNumber / self.options.rowLimit);
}
if (self.options.verticalOrientation) {
return 6;
}
return self.options.domainDynamicDimension ? (self.getWeekNumber(new Date(d.getFullYear(), d.getMonth()+1, 0)) - self.getWeekNumber(d) + 1): 6;
case "week": return Math.ceil(7 / self._domainType.day.row(d));
}
},
position: {
x: function(d) {
switch(self.options.domain) {
case "week": return Math.floor(self.getWeekDay(d) / self._domainType.day.row(d));
case "month":
if (self.options.colLimit > 0 || self.options.rowLimit > 0) {
return Math.floor((d.getDate() - 1)/ self._domainType.day.row(d));
}
return self.getWeekNumber(d) - self.getWeekNumber(new Date(d.getFullYear(), d.getMonth()));
case "year":
if (self.options.colLimit > 0 || self.options.rowLimit > 0) {
return Math.floor((self.getDayOfYear(d) - 1) / self._domainType.day.row(d));
}
return self.getWeekNumber(d);
}
},
y: function(d) {
if (self.options.colLimit > 0 || self.options.rowLimit > 0) {
switch(self.options.domain) {
case "year": return Math.floor((self.getDayOfYear(d) - 1) % self._domainType.day.row(d));
case "week": return Math.floor(self.getWeekDay(d) % self._domainType.day.row(d));
case "month": return Math.floor((d.getDate() - 1) % self._domainType.day.row(d));
}
}
return self.getWeekDay(d) % self._domainType.day.row(d);
}
},
format: {
date: "%A %B %-e, %Y",
legend: "%e %b",
connector: "on"
},
extractUnit: function(d) {
var dt = new Date(d.getFullYear(), d.getMonth(), d.getDate());
return dt.getTime();
}
},
"week": {
name: "week",
level: 40,
row: function() {
if (self.options.colLimit > 0) {
return Math.ceil(54 / self.options.colLimit);
}
return self.options.rowLimit || 1;
},
column: function(d) {
if (self.options.colLimit > 0) {
return self.options.colLimit;
}
d = new Date(d);
switch(self.options.domain) {
case "year": return 54;
case "month": return self.getWeekNumber(new Date(d.getFullYear(), d.getMonth()+1, 0)) - self.getWeekNumber(d);
default: return 1;
}
},
position: {
x: function(d) {
switch(self.options.domain) {
case "year":
if (self.options.colLimit > 0 || self.options.rowLimit > 0) {
return Math.floor(self.getWeekNumber(d) / self._domainType.week.row(d));
}
return self.getWeekNumber(d);
case "month":
var weekNumberInMonth = self.getMonthWeekNumber(d);
if (self.options.colLimit > 0 || self.options.rowLimit > 0) {
return Math.floor((weekNumberInMonth - 1) / self._domainType.week.row(d));
}
return weekNumberInMonth;
}
},
y: function(d) {
return self.getWeekNumber(d) % self._domainType.week.row(d);
}
},
format: {
date: "%B Week #%W",
legend: "%B Week #%W",
connector: "on"
},
extractUnit: function(d) {
var dt = new Date(d.getFullYear(), d.getMonth(), d.getDate());
// According to ISO-8601, week number computation are based on week starting on Monday
var weekDay = dt.getDay()-1;
if (weekDay < 0) {
weekDay = 6;
}
dt.setDate(dt.getDate() - weekDay);
return dt.getTime();
}
},
"month": {
name: "month",
level: 50,
row: function() {
if (self.options.colLimit > 0) {
return Math.ceil(12 / self.options.colLimit);
}
return self.options.rowLimit || 1;
},
column: function() {
if (self.options.rowLimit > 0) {
return Math.ceil(12 / self.options.rowLimit);
}
return self.options.colLimit || 12;
},
position: {
x: function(d) { return Math.floor(d.getMonth() / self._domainType.month.row(d)); },
y: function(d) { return d.getMonth() % self._domainType.month.row(d); }
},
format: {
date: "%B %Y",
legend: "%B",
connector: "on"
},
extractUnit: function(d) {
var dt = new Date(d.getFullYear(), d.getMonth());
return dt.getTime();
}
},
"year": {
name: "year",
level: 60,
row: function() { return self.options.rowLimit || 1; },
column: function() { return self.options.colLimit || 1; },
position: {
x: function() { return 1; },
y: function() { return 1; }
},
format: {
date: "%Y",
legend: "%Y",
connector: "on"
},
extractUnit: function(d) {
var dt = new Date(d.getFullYear());
return dt.getTime();
}
}
};
for (var type in this._domainType) {
this._domainType["x_" + type] = {};
this._domainType["x_" + type].name = "x_" + type;
this._domainType["x_" + type].level = this._domainType[type].level;
this._domainType["x_" + type].row = this._domainType[type].column;
this._domainType["x_" + type].column = this._domainType[type].row;
this._domainType["x_" + type].position = {};
this._domainType["x_" + type].position.x = this._domainType[type].position.y;
this._domainType["x_" + type].position.y = this._domainType[type].position.x;
this._domainType["x_" + type].format = this._domainType[type].format;
this._domainType["x_" + type].extractUnit = this._domainType[type].extractUnit;
}
// Exception: always return the maximum number of weeks
// to align the label vertically
this._domainType.x_day.row = function(d) {
d = new Date(d);
switch(self.options.domain) {
case "year": return (self.options.domainDynamicDimension ? (self.getWeekNumber(new Date(d.getFullYear(), 11, 31)) - self.getWeekNumber(new Date(d.getFullYear(), 0)) + 1): 54);
case "month":
if (!self.options.verticalOrientation) {
return 6;
}
return self.options.domainDynamicDimension ? (self.getWeekNumber(new Date(d.getFullYear(), d.getMonth()+1, 0)) - self.getWeekNumber(d) + 1): 6;
case "week": return 1;
}
};
// Record the address of the last inserted domain when browsing
this.lastInsertedSvg = null;
this._completed = false;
// Record all the valid domains
// Each domain value is a timestamp in milliseconds
this._domains = d3.map();
this.graphDim = {
width: 0,
height: 0
};
this.legendDim = {
width: 0,
height: 0
};
this.NAVIGATE_LEFT = 1;
this.NAVIGATE_RIGHT = 2;
// Various update mode when using the update() API
this.RESET_ALL_ON_UPDATE = 0;
this.RESET_SINGLE_ON_UPDATE = 1;
this.APPEND_ON_UPDATE = 2;
this.DEFAULT_LEGEND_MARGIN = 10;
this.root = null;
this._maxDomainReached = false;
this._minDomainReached = false;
this.domainPosition = new DomainPosition();
this.Legend = null;
this.legendScale = null;
/**
* Display the graph for the first time
* @return bool True if the calendar is created
*/
function _init() {
self.getDomain(self.options.start).map(function(d) { return d.getTime(); }).map(function(d) {
self._domains.set(d, self.getSubDomain(d).map(function(d) { return {t: self._domainType[self.options.subDomain].extractUnit(d), v: null}; }));
});
self.root = d3.select(self.options.itemSelector).append("svg").attr("class", "cal-heatmap-container");
self.root.attr("x", 0).attr("y", 0).append("svg").attr("class", "graph");
self.Legend = new Legend(self);
if (self.options.paintOnLoad) {
self.verticalDomainLabel = (self.options.label.position === "top" || self.options.label.position === "bottom");
self.domainVerticalLabelHeight = self.options.label.height === null ? Math.max(25, self.options.cellSize*2): self.options.label.height;
self.domainHorizontalLabelWidth = 0;
if (self.options.domainLabelFormat === "" && self.options.label.height === null) {
self.domainVerticalLabelHeight = 0;
}
if (!self.verticalDomainLabel) {
self.domainVerticalLabelHeight = 0;
self.domainHorizontalLabelWidth = self.options.label.width;
}
// @todo: check validity
if (typeof self.options.domainMargin === "number") {
self.options.domainMargin = [self.options.domainMargin, self.options.domainMargin, self.options.domainMargin, self.options.domainMargin];
}
self.paint();
// =========================================================================//
// ATTACHING DOMAIN NAVIGATION EVENT //
// =========================================================================//
if (self.options.nextSelector !== false) {
d3.select(self.options.nextSelector).on("click." + self.options.itemNamespace, function() {
d3.event.preventDefault();
return self.loadNextDomain();
});
}
if (self.options.previousSelector !== false) {
d3.select(self.options.previousSelector).on("click." + self.options.itemNamespace, function() {
d3.event.preventDefault();
return self.loadPreviousDomain();
});
}
// Display legend if needed
if (self.options.displayLegend) {
self.Legend.redraw(self.graphDim.width - self.options.domainGutter - self.options.cellPadding);
}
if (self.options.afterLoad !== null) {
self.afterLoad();
}
// Fill the graph with some datas
if (self.options.loadOnInit) {
var domains = self.getDomainKeys();
self.getDatas(
self.options.data,
new Date(domains[0]),
self.getSubDomain(domains[domains.length-1]).pop(),
function() {
self.fill();
self.onComplete();
}
);
} else {
self.onComplete();
}
}
return true;
}
// Return the width of the domain block, without the domain gutter
// @param int d Domain start timestamp
function w(d, outer) {
var width = self.options.cellSize*self._domainType[self.options.subDomain].column(d) + self.options.cellPadding*self._domainType[self.options.subDomain].column(d);
if (typeof outer !== "undefined" && outer === true) {
return width += self.domainHorizontalLabelWidth + self.options.domainGutter + self.options.domainMargin[1] + self.options.domainMargin[3];
}
return width;
}
// Return the height of the domain block, without the domain gutter
function h(d, outer) {
var height = self.options.cellSize*self._domainType[self.options.subDomain].row(d) + self.options.cellPadding*self._domainType[self.options.subDomain].row(d);
if (typeof outer !== "undefined" && outer === true) {
height += self.options.domainGutter + self.domainVerticalLabelHeight + self.options.domainMargin[0] + self.options.domainMargin[2];
}
return height;
}
/**
*
*
* @param int navigationDir
*/
this.paint = function(navigationDir) {
if (typeof navigationDir === "undefined") {
navigationDir = false;
}
// Painting all the domains
var domainSvg = self.root.select(".graph")
.selectAll(".graph-domain")
.data(
function() {
var data = self.getDomainKeys();
return navigationDir === self.NAVIGATE_LEFT ? data.reverse(): data;
},
function(d) { return d; }
)
;
var enteringDomainDim = 0;
var exitingDomainDim = 0;
// =========================================================================//
// PAINTING DOMAIN //
// =========================================================================//
var svg = domainSvg
.enter()
.append("svg")
.attr("width", function(d) {
return w(d, true);
})
.attr("height", function(d) {
return h(d, true);
})
.attr("x", function(d) {
if (self.options.verticalOrientation) {
self.graphDim.width = w(d, true);
return 0;
} else {
return getDomainPosition(d, self.graphDim, "width", w(d, true));
}
})
.attr("y", function(d) {
if (self.options.verticalOrientation) {
return getDomainPosition(d, self.graphDim, "height", h(d, true));
} else {
self.graphDim.height = h(d, true);
return 0;
}
})
.attr("class", function(d) {
var classname = "graph-domain";
var date = new Date(d);
switch(self.options.domain) {
case "hour": classname += " h_" + date.getHours();
/* falls through */
case "day": classname += " d_" + date.getDate() + " dy_" + date.getDay();
/* falls through */
case "week": classname += " w_" + self.getWeekNumber(date);
/* falls through */
case "month": classname += " m_" + (date.getMonth() + 1);
/* falls through */
case "year": classname += " y_" + date.getFullYear();
}
return classname;
})
;
self.lastInsertedSvg = svg;
function getDomainPosition(domainIndex, graphDim, axis, domainDim) {
var tmp = 0;
switch(navigationDir) {
case false:
tmp = graphDim[axis];
graphDim[axis] += domainDim;
self.domainPosition.setPosition(domainIndex, tmp);
return tmp;
case self.NAVIGATE_RIGHT:
self.domainPosition.setPosition(domainIndex, graphDim[axis]);
enteringDomainDim = domainDim;
exitingDomainDim = self.domainPosition.getPositionFromIndex(1);
self.domainPosition.shiftRightBy(exitingDomainDim);
return graphDim[axis];
case self.NAVIGATE_LEFT:
tmp = -domainDim;
enteringDomainDim = -tmp;
exitingDomainDim = graphDim[axis] - self.domainPosition.getLast();
self.domainPosition.setPosition(domainIndex, tmp);
self.domainPosition.shiftLeftBy(enteringDomainDim);
return tmp;
}
}
svg.append("rect")
.attr("width", function(d) { return w(d, true) - self.options.domainGutter - self.options.cellPadding; })
.attr("height", function(d) { return h(d, true) - self.options.domainGutter - self.options.cellPadding; })
.attr("class", "domain-background")
;
// =========================================================================//
// PAINTING SUBDOMAINS //
// =========================================================================//
var subDomainSvgGroup = svg.append("svg")
.attr("x", function() {
if (self.options.label.position === "left") {
return self.domainHorizontalLabelWidth + self.options.domainMargin[3];
} else {
return self.options.domainMargin[3];
}
})
.attr("y", function() {
if (self.options.label.position === "top") {
return self.domainVerticalLabelHeight + self.options.domainMargin[0];
} else {
return self.options.domainMargin[0];
}
})
.attr("class", "graph-subdomain-group")
;
var rect = subDomainSvgGroup
.selectAll("g")
.data(function(d) { return self._domains.get(d); })
.enter()
.append("g")
;
rect
.append("rect")
.attr("class", function(d) {
return "graph-rect" + self.getHighlightClassName(d.t) + (self.options.onClick !== null ? " hover_cursor": "");
})
.attr("width", self.options.cellSize)
.attr("height", self.options.cellSize)
.attr("x", function(d) { return self.positionSubDomainX(d.t); })
.attr("y", function(d) { return self.positionSubDomainY(d.t); })
.on("click", function(d) {
if (self.options.onClick !== null) {
return self.onClick(new Date(d.t), d.v);
}
})
.call(function(selection) {
if (self.options.cellRadius > 0) {
selection
.attr("rx", self.options.cellRadius)
.attr("ry", self.options.cellRadius)
;
}
if (self.legendScale !== null && self.options.legendColors !== null && self.options.legendColors.hasOwnProperty("base")) {
selection.attr("fill", self.options.legendColors.base);
}
})
;
// Appending a title to each subdomain
rect.append("title").text(function(d){ return self.formatDate(new Date(d.t), self.options.subDomainDateFormat); });
// =========================================================================//
// PAINTING LABEL //
// =========================================================================//
if (self.options.domainLabelFormat !== "") {
svg.append("text")
.attr("class", "graph-label")
.attr("y", function(d) {
var y = self.options.domainMargin[0];
switch(self.options.label.position) {
case "top": y += self.domainVerticalLabelHeight/2; break;
case "bottom": y += h(d) + self.domainVerticalLabelHeight/2;
}
return y + self.options.label.offset.y *
(
((self.options.label.rotate === "right" && self.options.label.position === "right") ||
(self.options.label.rotate === "left" && self.options.label.position === "left")) ?
-1: 1
);
})
.attr("x", function(d){
var x = self.options.domainMargin[3];
switch(self.options.label.position) {
case "right": x += w(d); break;
case "bottom":
case "top": x += w(d)/2;
}
if (self.options.label.align === "right") {
return x + self.domainHorizontalLabelWidth - self.options.label.offset.x *
(self.options.label.rotate === "right" ? -1: 1);
}
return x + self.options.label.offset.x;
})
.attr("text-anchor", function() {
switch(self.options.label.align) {
case "start":
case "left": return "start";
case "end":
case "right": return "end";
default: return "middle";
}
})
.attr("dominant-baseline", function() { return self.verticalDomainLabel ? "middle": "top"; })
.text(function(d) { return self.formatDate(new Date(d), self.options.domainLabelFormat); })
.call(domainRotate)
;
}
function domainRotate(selection) {
switch (self.options.label.rotate) {
case "right":
selection
.attr("transform", function(d) {
var s = "rotate(90), ";
switch(self.options.label.position) {
case "right": s += "translate(-" + w(d) + " , -" + w(d) + ")"; break;
case "left": s += "translate(0, -" + self.domainHorizontalLabelWidth + ")"; break;
}
return s;
});
break;
case "left":
selection
.attr("transform", function(d) {
var s = "rotate(270), ";
switch(self.options.label.position) {
case "right": s += "translate(-" + (w(d) + self.domainHorizontalLabelWidth) + " , " + w(d) + ")"; break;
case "left": s += "translate(-" + (self.domainHorizontalLabelWidth) + " , " + self.domainHorizontalLabelWidth + ")"; break;
}
return s;
});
break;
}
}
// =========================================================================//
// PAINTING DOMAIN SUBDOMAIN CONTENT //
// =========================================================================//
if (self.options.subDomainTextFormat !== null) {
rect
.append("text")
.attr("class", function(d) { return "subdomain-text" + self.getHighlightClassName(d.t); })
.attr("x", function(d) { return self.positionSubDomainX(d.t) + self.options.cellSize/2; })
.attr("y", function(d) { return self.positionSubDomainY(d.t) + self.options.cellSize/2; })
.attr("text-anchor", "middle")
.attr("dominant-baseline", "central")
.text(function(d){ return self.formatDate(new Date(d.t), self.options.subDomainTextFormat); })
;
}
// =========================================================================//
// ANIMATION //
// =========================================================================//
if (navigationDir !== false) {
domainSvg.transition().duration(self.options.animationDuration)
.attr("x", function(d){
return self.options.verticalOrientation ? 0: self.domainPosition.getPosition(d);
})
.attr("y", function(d){
return self.options.verticalOrientation? self.domainPosition.getPosition(d): 0;
})
;
}
var tempWidth = self.graphDim.width;
var tempHeight = self.graphDim.height;