-
Notifications
You must be signed in to change notification settings - Fork 554
/
Copy pathdimple.v2.2.0.js
5456 lines (5076 loc) · 264 KB
/
dimple.v2.2.0.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
// Copyright: 2015 AlignAlytics
// License: "https://github.com/PMSI-AlignAlytics/dimple/blob/master/MIT-LICENSE.txt"
// Source: /src/objects/begin.js
// Wrap all application code in a self-executing function which handles optional AMD/CommonJS publishing
(function (context, dimple) {
"use strict";
if (typeof exports === "object") {
// CommonJS
module.exports = dimple(require('d3'));
} else {
if (typeof define === "function" && define.amd) {
// RequireJS | AMD
define(["d3"], function (d3) {
// publish dimple to the global namespace for backwards compatibility
// and define it as an AMD module
context.dimple = dimple(d3);
return context.dimple;
});
} else {
// No AMD, expect d3 to exist in the current context and publish
// dimple to the global namespace
if (!context.d3) {
if (console && console.warn) {
console.warn("dimple requires d3 to run. Are you missing a reference to the d3 library?");
} else {
throw "dimple requires d3 to run. Are you missing a reference to the d3 library?";
}
} else {
context.dimple = dimple(context.d3);
}
}
}
}(this, function (d3) {
"use strict";
// Create the stub object
var dimple = {
version: "2.2.0",
plot: {},
aggregateMethod: {}
};
// Copyright: 2015 AlignAlytics
// License: "https://github.com/PMSI-AlignAlytics/dimple/blob/master/MIT-LICENSE.txt"
// Source: /src/objects/axis/begin.js
// Help: http://github.com/PMSI-AlignAlytics/dimple/wiki/dimple.axis
dimple.axis = function (chart, position, categoryFields, measure, timeField, autoRotateLabel) {
// Help: http://github.com/PMSI-AlignAlytics/dimple/wiki/dimple.axis#wiki-chart
this.chart = chart;
// Help: http://github.com/PMSI-AlignAlytics/dimple/wiki/dimple.axis#wiki-position
this.position = position;
// Help: http://github.com/PMSI-AlignAlytics/dimple/wiki/dimple.axis#wiki-categoryFields
this.categoryFields = (timeField === null || timeField === undefined ? categoryFields : [].concat(timeField));
// Help: http://github.com/PMSI-AlignAlytics/dimple/wiki/dimple.axis#wiki-measure
this.measure = measure;
// Help: http://github.com/PMSI-AlignAlytics/dimple/wiki/dimple.axis#wiki-timeField
this.timeField = timeField;
// Help: http://github.com/PMSI-AlignAlytics/dimple/wiki/dimple.axis#wiki-floatingBarWidth
this.floatingBarWidth = 5;
// Help: http://github.com/PMSI-AlignAlytics/dimple/wiki/dimple.axis#wiki-hidden
this.hidden = false;
// Help: http://github.com/PMSI-AlignAlytics/dimple/wiki/dimple.axis#wiki-showPercent
this.showPercent = false;
// Help: http://github.com/PMSI-AlignAlytics/dimple/wiki/dimple.axis#wiki-colors
this.colors = null;
// Help: http://github.com/PMSI-AlignAlytics/dimple/wiki/dimple.axis#wiki-overrideMin
this.overrideMin = null;
// Help: http://github.com/PMSI-AlignAlytics/dimple/wiki/dimple.axis#wiki-overrideMax
this.overrideMax = null;
// Help: http://github.com/PMSI-AlignAlytics/dimple/wiki/dimple.axis#wiki-shapes
this.shapes = null;
// Help: http://github.com/PMSI-AlignAlytics/dimple/wiki/dimple.axis#wiki-showGridlines
this.showGridlines = null;
// Help: http://github.com/PMSI-AlignAlytics/dimple/wiki/dimple.axis#wiki-gridlineShapes
this.gridlineShapes = null;
// Help: http://github.com/PMSI-AlignAlytics/dimple/wiki/dimple.axis#wiki-titleShape
this.titleShape = null;
// Help: http://github.com/PMSI-AlignAlytics/dimple/wiki/dimple.axis#wiki-dateParseFormat
this.dateParseFormat = null;
// Help: http://github.com/PMSI-AlignAlytics/dimple/wiki/dimple.axis#wiki-tickFormat
this.tickFormat = null;
// Help: http://github.com/PMSI-AlignAlytics/dimple/wiki/dimple.axis#wiki-timePeriod
this.timePeriod = null;
// Help: http://github.com/PMSI-AlignAlytics/dimple/wiki/dimple.axis#wiki-timeInterval
this.timeInterval = 1;
// Help: http://github.com/PMSI-AlignAlytics/dimple/wiki/dimple.axis#wiki-useLog
this.useLog = false;
// Help: http://github.com/PMSI-AlignAlytics/dimple/wiki/dimple.axis#wiki-logBase
this.logBase = 10;
// Help: http://github.com/PMSI-AlignAlytics/dimple/wiki/dimple.axis#wiki-title
this.title = undefined;
// Help: http://github.com/PMSI-AlignAlytics/dimple/wiki/dimple.axis#wiki-clamp
this.clamp = true;
// Help: http://github.com/PMSI-AlignAlytics/dimple/wiki/dimple.axis#wiki-ticks
this.ticks = null;
// Help: http://github.com/PMSI-AlignAlytics/dimple/wiki/dimple.axis#wiki-fontSize
this.fontSize = "10px";
// Help: http://github.com/PMSI-AlignAlytics/dimple/wiki/dimple.axis#wiki-fontFamily
this.fontFamily = "sans-serif";
// Help: http://github.com/PMSI-AlignAlytics/dimple/wiki/dimple.axis#wiki-autoRotateLabel
this.autoRotateLabel = (autoRotateLabel === null || autoRotateLabel === undefined ? true : autoRotateLabel);
// If this is a composite axis, store links to all slaves
this._slaves = [];
// The scale determined by the update method
this._scale = null;
// The minimum and maximum axis values
this._min = 0;
this._max = 0;
// Chart origin before and after an update. This helps
// with transitions
this._previousOrigin = null;
this._origin = null;
// The order definition array
this._orderRules = [];
// The group order definition array
this._groupOrderRules = [];
// Copyright: 2015 AlignAlytics
// License: "https://github.com/PMSI-AlignAlytics/dimple/blob/master/MIT-LICENSE.txt"
// Source: /src/objects/axis/methods/_draw.js
this._draw = null;
// Copyright: 2015 AlignAlytics
// License: "https://github.com/PMSI-AlignAlytics/dimple/blob/master/MIT-LICENSE.txt"
// Source: /src/objects/axis/methods/_getAxisData.js
// Get all the datasets which may affect this axis
this._getAxisData = function () {
var i,
series,
returnData = [],
addChartData = false;
if (this.chart && this.chart.series) {
for (i = 0; i < this.chart.series.length; i += 1) {
series = this.chart.series[i];
// If the series is related to this axis
if (series[this.position] === this) {
// If the series has its own data set add it to the return array
if (series.data && series.data.length > 0) {
returnData = returnData.concat(series.data);
} else {
addChartData = true;
}
}
}
if (addChartData && this.chart.data) {
returnData = returnData.concat(this.chart.data);
}
}
return returnData;
};
// Copyright: 2015 AlignAlytics
// License: "https://github.com/PMSI-AlignAlytics/dimple/blob/master/MIT-LICENSE.txt"
// Source: /src/objects/axis/methods/_getFontSize.js
this._getFontSize = function () {
var fontSize;
if (!this.fontSize || this.fontSize.toString().toLowerCase() === "auto") {
fontSize = (this.chart._heightPixels() / 35 > 10 ? this.chart._heightPixels() / 35 : 10) + "px";
} else if (!isNaN(this.fontSize)) {
fontSize = this.fontSize + "px";
} else {
fontSize = this.fontSize;
}
return fontSize;
};
// Copyright: 2015 AlignAlytics
// License: "https://github.com/PMSI-AlignAlytics/dimple/blob/master/MIT-LICENSE.txt"
// Source: /src/objects/axis/methods/_getFormat.js
this._getFormat = function () {
var returnFormat,
max,
min,
len,
chunks,
suffix,
dp;
if (this.tickFormat !== null && this.tickFormat !== undefined) {
if (this._hasTimeField()) {
returnFormat = d3.time.format(this.tickFormat);
} else {
returnFormat = d3.format(this.tickFormat);
}
} else if (this.showPercent) {
returnFormat = d3.format("%");
} else if (this.useLog && this.measure !== null) {
// With linear axes the range is used to apply uniform
// formatting but with a log axis it is based on each number
// independently
returnFormat = function (n) {
var l = Math.floor(Math.abs(n), 0).toString().length,
c = Math.min(Math.floor((l - 1) / 3), 4),
s = "kmBT".substring(c - 1, c),
d = (Math.round((n / Math.pow(1000, c)) * 10).toString().slice(-1) === "0" ? 0 : 1);
return (n === 0 ? 0 : d3.format(",." + d + "f")(n / Math.pow(1000, c)) + s);
};
} else if (this.measure !== null) {
max = Math.floor(Math.abs(this._max), 0).toString();
min = Math.floor(Math.abs(this._min), 0).toString();
len = Math.max(min.length, max.length);
if (len > 3) {
chunks = Math.min(Math.floor((len - 1) / 3), 4);
suffix = "kmBT".substring(chunks - 1, chunks);
dp = (len - chunks * 3 <= 1 ? 1 : 0);
returnFormat = function (n) {
return (n === 0 ? 0 : d3.format(",." + dp + "f")(n / Math.pow(1000, chunks)) + suffix);
};
} else {
dp = -Math.floor(Math.log(this._tick_step) / Math.LN10);
returnFormat = d3.format(",." + dp + "f");
}
} else {
returnFormat = function (n) { return n; };
}
return returnFormat;
};
// Copyright: 2015 AlignAlytics
// License: "https://github.com/PMSI-AlignAlytics/dimple/blob/master/MIT-LICENSE.txt"
// Source: /src/objects/axis/methods/_getTimePeriod.js
this._getTimePeriod = function () {
// A javascript date object
var outPeriod = this.timePeriod,
maxPeriods = 30,
diff = this._max - this._min;
if (this._hasTimeField() && !this.timePeriod) {
// Calculate using millisecond values for speed. Using the date range requires creating an array
// which in the case of seconds kills the browser. All constants are straight sums of milliseconds
// except months taken as (86400000 * 365.25) / 12 = 2629800000
if (diff / 1000 <= maxPeriods) {
outPeriod = d3.time.seconds;
} else if (diff / 60000 <= maxPeriods) {
outPeriod = d3.time.minutes;
} else if (diff / 3600000 <= maxPeriods) {
outPeriod = d3.time.hours;
} else if (diff / 86400000 <= maxPeriods) {
outPeriod = d3.time.days;
} else if (diff / 604800000 <= maxPeriods) {
outPeriod = d3.time.weeks;
} else if (diff / 2629800000 <= maxPeriods) {
outPeriod = d3.time.months;
} else {
outPeriod = d3.time.years;
}
}
// Return the date
return outPeriod;
};
// Copyright: 2015 AlignAlytics
// License: "https://github.com/PMSI-AlignAlytics/dimple/blob/master/MIT-LICENSE.txt"
// Source: /src/objects/axis/methods/getTooltipText.js
this._getTooltipText = function (rows, d) {
if (this._hasTimeField()) {
if (d[this.position + "Field"][0]) {
rows.push(this.timeField + ": " + this._getFormat()(d[this.position + "Field"][0]));
}
} else if (this._hasCategories()) {
// Add the categories
this.categoryFields.forEach(function (c, i) {
if (c !== null && c !== undefined && d[this.position + "Field"][i]) {
// If the category name and value match don't display the category name
rows.push(c + (d[this.position + "Field"][i] !== c ? ": " + d[this.position + "Field"][i] : ""));
}
}, this);
} else if (this._hasMeasure()) {
switch (this.position) {
case "x":
rows.push(this.measure + ": " + this._getFormat()(d.width));
break;
case "y":
rows.push(this.measure + ": " + this._getFormat()(d.height));
break;
case "p":
rows.push(this.measure + ": " + this._getFormat()(d.angle) + " (" + (d3.format("%")(d.piePct)) + ")");
break;
default:
rows.push(this.measure + ": " + this._getFormat()(d[this.position + "Value"]));
break;
}
}
};
// Copyright: 2015 AlignAlytics
// License: "https://github.com/PMSI-AlignAlytics/dimple/blob/master/MIT-LICENSE.txt"
// Source: /src/objects/axis/methods/_getTopMaster.js
this._getTopMaster = function () {
// The highest level master
var topMaster = this;
if (this.master !== null && this.master !== undefined) {
topMaster = this.master._getTopMaster();
}
return topMaster;
};
// Copyright: 2015 AlignAlytics
// License: "https://github.com/PMSI-AlignAlytics/dimple/blob/master/MIT-LICENSE.txt"
// Source: /src/objects/axis/methods/_hasCategories.js
this._hasCategories = function () {
return (this.categoryFields !== null && this.categoryFields !== undefined && this.categoryFields.length > 0);
};
// Copyright: 2015 AlignAlytics
// License: "https://github.com/PMSI-AlignAlytics/dimple/blob/master/MIT-LICENSE.txt"
// Source: /src/objects/axis/methods/_hasMeasure.js
this._hasMeasure = function () {
return (this.measure !== null && this.measure !== undefined);
};
// Copyright: 2015 AlignAlytics
// License: "https://github.com/PMSI-AlignAlytics/dimple/blob/master/MIT-LICENSE.txt"
// Source: /src/objects/axis/methods/_hasTimeField.js
this._hasTimeField = function () {
return (this.timeField !== null && this.timeField !== undefined);
};
// Copyright: 2015 AlignAlytics
// License: "https://github.com/PMSI-AlignAlytics/dimple/blob/master/MIT-LICENSE.txt"
// Source: /src/objects/axis/methods/_parseDate.js
this._parseDate = function (inDate) {
// A javascript date object
var outDate;
if (this.dateParseFormat === null || this.dateParseFormat === undefined) {
// Moved this into the condition so that using epoch time requires no data format to be set.
// For example 20131122 might be parsed as %Y%m%d not treated as epoch time.
if (!isNaN(inDate)) {
// If inDate is a number, assume it's epoch time
outDate = new Date(inDate);
} else {
// If nothing has been explicity defined you are in the hands of the browser gods
// may they smile upon you...
outDate = Date.parse(inDate);
}
} else {
outDate = d3.time.format(this.dateParseFormat).parse(inDate);
}
// Return the date
return outDate;
};
// Copyright: 2015 AlignAlytics
// License: "https://github.com/PMSI-AlignAlytics/dimple/blob/master/MIT-LICENSE.txt"
// Source: /src/objects/axis/methods/_update.js
this._update = function (refactor) {
var distinctCats = [],
ticks,
step,
remainder,
origin,
tickCount = this.ticks || 10,
getOrderedCategories = function (self, axPos, oppPos) {
var category = self.categoryFields[0],
axisData = self._getAxisData(),
sortBy = category,
desc = false,
isDate = true,
currentValue = null,
i,
definitions;
// Check whether this field is a date
for (i = 0; i < axisData.length; i += 1) {
currentValue = self._parseDate(axisData[i][category]);
if (currentValue !== null && currentValue !== undefined && isNaN(currentValue)) {
isDate = false;
break;
}
}
if (!isDate) {
// Find the first series which connects this axis to another
self.chart.series.forEach(function (s) {
if (s[axPos] === self && s[oppPos]._hasMeasure()) {
sortBy = s[oppPos].measure;
desc = true;
}
}, this);
}
definitions = self._orderRules.concat({ ordering : sortBy, desc : desc });
return dimple._getOrderedList(axisData, category, definitions);
};
// If the axis is a percentage type axis the bounds must be between -1 and 1. Sometimes
// binary rounding means it can fall outside that bound so tidy up here
this._min = (this.showPercent && this._min < -1 ? -1 : this._min);
this._max = (this.showPercent && this._max > 1 ? 1 : this._max);
// Override or round the min or max
this._min = (this.overrideMin !== null ? this.overrideMin : this._min);
this._max = (this.overrideMax !== null ? this.overrideMax : this._max);
// If this is an x axis
if (this.position === "x" && (this._scale === null || refactor)) {
if (this._hasTimeField()) {
this._scale = d3.time.scale()
// Previously used rangeRound which causes problems with the area chart (Issue #79)
.range([this.chart._xPixels(), this.chart._xPixels() + this.chart._widthPixels()])
.domain([this._min, this._max])
.clamp(this.clamp);
} else if (this.useLog) {
this._scale = d3.scale.log()
.range([this.chart._xPixels(), this.chart._xPixels() + this.chart._widthPixels()])
.domain([
(this._min === 0 ? Math.pow(this.logBase, -1) : this._min),
(this._max === 0 ? -1 * Math.pow(this.logBase, -1) : this._max)
])
.clamp(this.clamp)
.base(this.logBase)
.nice();
} else if (this.measure === null || this.measure === undefined) {
distinctCats = getOrderedCategories(this, "x", "y");
// If there are any slaves process accordingly
if (this._slaves !== null && this._slaves !== undefined) {
this._slaves.forEach(function (slave) {
distinctCats = distinctCats.concat(getOrderedCategories(slave, "x", "y"));
}, this);
}
this._scale = d3.scale.ordinal()
.rangePoints([this.chart._xPixels(), this.chart._xPixels() + this.chart._widthPixels()])
.domain(distinctCats.concat([""]));
} else {
this._scale = d3.scale.linear()
.range([this.chart._xPixels(), this.chart._xPixels() + this.chart._widthPixels()])
.domain([this._min, this._max])
.clamp(this.clamp)
.nice();
}
// If it's visible, orient it at the top or bottom if it's first or second respectively
if (!this.hidden) {
switch (this.chart._axisIndex(this, "x")) {
case 0:
this._draw = d3.svg.axis()
.orient("bottom")
.scale(this._scale);
if (this.ticks) {
this._draw.ticks(tickCount);
}
break;
case 1:
this._draw = d3.svg.axis()
.orient("top")
.scale(this._scale);
if (this.ticks) {
this._draw.ticks(tickCount);
}
break;
default:
break;
}
}
} else if (this.position === "y" && (this._scale === null || refactor)) {
if (this._hasTimeField()) {
this._scale = d3.time.scale()
// Previously used rangeRound which causes problems with the area chart (Issue #79)
.range([this.chart._yPixels() + this.chart._heightPixels(), this.chart._yPixels()])
.domain([this._min, this._max])
.clamp(this.clamp);
} else if (this.useLog) {
this._scale = d3.scale.log()
.range([this.chart._yPixels() + this.chart._heightPixels(), this.chart._yPixels()])
.domain([
(this._min === 0 ? Math.pow(this.logBase, -1) : this._min),
(this._max === 0 ? -1 * Math.pow(this.logBase, -1) : this._max)
])
.clamp(this.clamp)
.base(this.logBase)
.nice();
} else if (this.measure === null || this.measure === undefined) {
distinctCats = getOrderedCategories(this, "y", "x");
// If there are any slaves process accordingly
if (this._slaves !== null && this._slaves !== undefined) {
this._slaves.forEach(function (slave) {
distinctCats = distinctCats.concat(getOrderedCategories(slave, "y", "x"));
}, this);
}
this._scale = d3.scale.ordinal()
.rangePoints([this.chart._yPixels() + this.chart._heightPixels(), this.chart._yPixels()])
.domain(distinctCats.concat([""]));
} else {
this._scale = d3.scale.linear()
.range([this.chart._yPixels() + this.chart._heightPixels(), this.chart._yPixels()])
.domain([this._min, this._max])
.clamp(this.clamp)
.nice();
}
// if it's visible, orient it at the left or right if it's first or second respectively
if (!this.hidden) {
switch (this.chart._axisIndex(this, "y")) {
case 0:
this._draw = d3.svg.axis()
.orient("left")
.scale(this._scale);
if (this.ticks) {
this._draw.ticks(tickCount);
}
break;
case 1:
this._draw = d3.svg.axis()
.orient("right")
.scale(this._scale);
if (this.ticks) {
this._draw.ticks(tickCount);
}
break;
default:
break;
}
}
} else if (this.position.length > 0 && this.position[0] === "z" && this._scale === null) {
if (this.useLog) {
this._scale = d3.scale.log()
.range([this.chart._heightPixels() / 300, this.chart._heightPixels() / 10])
.domain([
(this._min === 0 ? Math.pow(this.logBase, -1) : this._min),
(this._max === 0 ? -1 * Math.pow(this.logBase, -1) : this._max)
])
.clamp(this.clamp)
.base(this.logBase);
} else {
this._scale = d3.scale.linear()
.range([1, this.chart._heightPixels() / 10])
.domain([this._min, this._max])
.clamp(this.clamp);
}
} else if (this.position.length > 0 && this.position[0] === "p" && this._scale === null) {
if (this.useLog) {
this._scale = d3.scale.log()
.range([0, 360])
.domain([
(this._min === 0 ? Math.pow(this.logBase, -1) : this._min),
(this._max === 0 ? -1 * Math.pow(this.logBase, -1) : this._max)
])
.clamp(this.clamp)
.base(this.logBase);
} else {
this._scale = d3.scale.linear()
.range([0, 360])
.domain([this._min, this._max])
.clamp(this.clamp);
}
} else if (this.position.length > 0 && this.position[0] === "c" && this._scale === null) {
this._scale = d3.scale.linear()
.range([0, (this.colors === null || this.colors.length === 1 ? 1 : this.colors.length - 1)])
.domain([this._min, this._max])
.clamp(this.clamp);
}
// Apply this scale to all slaves as well
if (this._slaves !== null && this._slaves !== undefined && this._slaves.length > 0) {
this._slaves.forEach(function (slave) {
slave._scale = this._scale;
}, this);
}
// Check that the axis ends on a labelled tick
if ((refactor === null || refactor === undefined || refactor === false) && !this._hasTimeField() && this._scale !== null && this._scale.ticks !== null && this._scale.ticks !== undefined && this._scale.ticks(tickCount).length > 0 && (this.position === "x" || this.position === "y")) {
// Get the ticks determined based on the specified split
ticks = this._scale.ticks(tickCount);
// Get the step between ticks
step = ticks[1] - ticks[0];
// Get the remainder
remainder = ((this._max - this._min) % step).toFixed(0);
// Store the tick step if needed to calculate _getFormat.
this._tick_step = step;
// If the remainder is not zero
if (remainder !== 0) {
// Set the bounds
this._max = Math.ceil(this._max / step) * step;
this._min = Math.floor(this._min / step) * step;
// Recursively call the method to recalculate the scale. This shouldn't enter this block again.
this._update(true);
}
}
// Populate the origin. Previously this incorrectly looked up 0 on the axis which only works
// for measure axes leading to Issue #19. This fix uses the first category value in cases where
// one is required.
if (distinctCats !== null && distinctCats !== undefined && distinctCats.length > 0) {
origin = this._scale.copy()(distinctCats[0]);
} else if (this._min > 0) {
origin = this._scale.copy()(this._min);
} else if (this._max < 0) {
origin = this._scale.copy()(this._max);
} else {
origin = this._scale.copy()(0);
}
if (this._origin !== origin) {
this._previousOrigin = (this._origin === null ? origin : this._origin);
this._origin = origin;
}
// Return axis for chaining
return this;
};
// Copyright: 2015 AlignAlytics
// License: "https://github.com/PMSI-AlignAlytics/dimple/blob/master/MIT-LICENSE.txt"
// Source: /src/objects/axis/methods/addGroupOrderRule.js
// Help: http://github.com/PMSI-AlignAlytics/dimple/wiki/dimple.axis#wiki-addGroupOrderRule
this.addGroupOrderRule = function (ordering, desc) {
this._groupOrderRules.push({ ordering : ordering, desc : desc });
};
// Copyright: 2015 AlignAlytics
// License: "https://github.com/PMSI-AlignAlytics/dimple/blob/master/MIT-LICENSE.txt"
// Source: /src/objects/axis/methods/addOrderRule.js
// Help: http://github.com/PMSI-AlignAlytics/dimple/wiki/dimple.axis#wiki-addOrderRule
this.addOrderRule = function (ordering, desc) {
this._orderRules.push({ ordering : ordering, desc : desc });
};
};
// End dimple.axis
// Copyright: 2015 AlignAlytics
// License: "https://github.com/PMSI-AlignAlytics/dimple/blob/master/MIT-LICENSE.txt"
// Source: /src/objects/chart/begin.js
// Help: http://github.com/PMSI-AlignAlytics/dimple/wiki/dimple.chart
dimple.chart = function (svg, data) {
// Help: http://github.com/PMSI-AlignAlytics/dimple/wiki/dimple.chart#wiki-svg
this.svg = svg;
// Help: http://github.com/PMSI-AlignAlytics/dimple/wiki/dimple.chart#wiki-x
this.x = "10%";
// Help: http://github.com/PMSI-AlignAlytics/dimple/wiki/dimple.chart#wiki-y
this.y = "10%";
// Help: http://github.com/PMSI-AlignAlytics/dimple/wiki/dimple.chart#wiki-width
this.width = "80%";
// Help: http://github.com/PMSI-AlignAlytics/dimple/wiki/dimple.chart#wiki-height
this.height = "80%";
// Help: http://github.com/PMSI-AlignAlytics/dimple/wiki/dimple.chart#wiki-data
this.data = data;
// Help: http://github.com/PMSI-AlignAlytics/dimple/wiki/dimple.chart#wiki-noFormats
this.noFormats = false;
// Help: http://github.com/PMSI-AlignAlytics/dimple/wiki/dimple.chart#wiki-axes
this.axes = [];
// Help: http://github.com/PMSI-AlignAlytics/dimple/wiki/dimple.chart#wiki-series
this.series = [];
// Help: http://github.com/PMSI-AlignAlytics/dimple/wiki/dimple.chart#wiki-legends
this.legends = [];
// Help: http://github.com/PMSI-AlignAlytics/dimple/wiki/dimple.chart#wiki-storyboard
this.storyboard = null;
// Help: http://github.com/PMSI-AlignAlytics/dimple/wiki/dimple.chart#wiki-titleShape
this.titleShape = null;
// Help: http://github.com/PMSI-AlignAlytics/dimple/wiki/dimple.chart#wiki-shapes
this.shapes = null;
// Help: http://github.com/PMSI-AlignAlytics/dimple/wiki/dimple.chart#wiki-ease
this.ease = "cubic-in-out";
// Help: http://github.com/PMSI-AlignAlytics/dimple/wiki/dimple.chart#wiki-staggerDraw
this.staggerDraw = false;
// The group within which to put all of this chart's objects
this._group = svg.append("g");
this._group.attr('class', 'dimple-chart');
this._gridlines_group = this._group.insert('g');
this._gridlines_group.attr('class', 'dimple-gridlines-group');
this._axis_group = this._group.insert('g');
this._axis_group.attr('class', 'dimple-axis-group');
// The group within which to put tooltips. This is not initialised here because
// the group would end up behind other chart contents in a multi chart output. It will
// therefore be added and removed by the mouse enter/leave events
this._tooltipGroup = null;
// Colors assigned to chart contents. E.g. a series value.
this._assignedColors = {};
// Classes assigned to series values
this._assignedClasses = {};
// The next colour index to use, this value is cycled around for all default colours
this._nextColor = 0;
// The next series class index to use, this value is cycled around for all default classes
this._nextClass = 0;
// Copyright: 2015 AlignAlytics
// License: "https://github.com/PMSI-AlignAlytics/dimple/blob/master/MIT-LICENSE.txt"
// Source: /src/objects/chart/methods/_axisIndex.js
// Return the ordinal value of the passed axis. If an orientation is passed, return the order for the
// specific orientation, otherwise return the order from all axes. Returns -1 if the passed axis isn't part of the collection
this._axisIndex = function (axis, orientation) {
var i = 0,
axisCount = 0,
index = -1;
for (i = 0; i < this.axes.length; i += 1) {
if (this.axes[i] === axis) {
index = axisCount;
break;
}
if (orientation === null || orientation === undefined || orientation[0] === this.axes[i].position[0]) {
axisCount += 1;
}
}
return index;
};
// Copyright: 2015 AlignAlytics
// License: "https://github.com/PMSI-AlignAlytics/dimple/blob/master/MIT-LICENSE.txt"
// Source: /src/objects/chart/methods/_getAllData.js
// Mash together all of the datasets
this._getAllData = function () {
// The return array will include all data for chart as well as any series
var returnData = [];
// If there is data at the chart level
if (this.data !== null && this.data !== undefined && this.data.length > 0) {
returnData = returnData.concat(this.data);
}
// If there are series defined
if (this.series !== null && this.series !== undefined && this.series.length > 0) {
this.series.forEach(function (s) {
if (s.data !== null && s.data !== undefined && s.data.length > 0) {
returnData = returnData.concat(s.data);
}
});
}
// Return the final dataset
return returnData;
};
// Copyright: 2015 AlignAlytics
// License: "https://github.com/PMSI-AlignAlytics/dimple/blob/master/MIT-LICENSE.txt"
// Source: /src/objects/chart/methods/_getData.js
// Create a dataset containing positioning information for every series
this._getData = function (data, cats, agg, order, stacked, x, y, z, p, c) {
// The data for this series
var returnData = [],
// Handle multiple category values by iterating the fields in the row and concatenate the values
// This is repeated for each axis using a small anon function
getField = function (axis, row) {
var returnField = [];
if (axis !== null) {
if (axis._hasTimeField()) {
returnField.push(axis._parseDate(row[axis.timeField]));
} else if (axis._hasCategories()) {
axis.categoryFields.forEach(function (cat) {
returnField.push(row[cat]);
}, this);
}
}
return returnField;
},
// Catch a non-numeric value and re-calc as count
useCount = { x: false, y: false, z: false, p: false, c: false },
// If the elements are grouped a unique list of secondary category values will be required
secondaryElements = { x: [], y: [] },
// Get the x and y totals for percentages. This cannot be done in the loop above as we need the data aggregated before we get an abs total.
// otherwise it will wrongly account for negatives and positives rolled together.
totals = { x: [], y: [], z: [], p: [] },
colorBounds = { min: null, max: null },
tot,
running = { x: [], y: [], z: [], p: [] },
addedCats = [],
catTotals = {},
grandTotals = { x: 0, y: 0, z: 0, p: 0 },
key,
storyCat = "",
orderedStoryboardArray = [],
seriesCat = [],
orderedSeriesArray = [],
xCat = "",
xSortArray = [],
yCat = "",
ySortArray = [],
pCat = "",
pSortArray = [],
rules = [],
sortedData = data,
groupRules = [];
if (this.storyboard && this.storyboard.categoryFields.length > 0) {
storyCat = this.storyboard.categoryFields[0];
orderedStoryboardArray = dimple._getOrderedList(sortedData, storyCat, this.storyboard._orderRules);
}
// Deal with mekkos
if (x && x._hasCategories() && x._hasMeasure()) {
xCat = x.categoryFields[0];
xSortArray = dimple._getOrderedList(sortedData, xCat, x._orderRules.concat([{ ordering : x.measure, desc : true }]));
}
if (y && y._hasCategories() && y._hasMeasure()) {
yCat = y.categoryFields[0];
ySortArray = dimple._getOrderedList(sortedData, yCat, y._orderRules.concat([{ ordering : y.measure, desc : true }]));
}
if (p && p._hasCategories() && p._hasMeasure()) {
pCat = p.categoryFields[0];
pSortArray = dimple._getOrderedList(sortedData, pCat, p._orderRules.concat([{ ordering : p.measure, desc : true }]));
}
if (sortedData.length > 0 && cats && cats.length > 0) {
// Concat is used here to break the reference to the parent array, if we don't do this, in a storyboarded chart,
// the series rules to grow and grow until the system grinds to a halt trying to deal with them all.
rules = [].concat(order);
seriesCat = [];
cats.forEach(function (cat) {
if (sortedData[0][cat] !== undefined) {
seriesCat.push(cat);
}
}, this);
if (p && p._hasMeasure()) {
rules.push({ ordering : p.measure, desc : true });
} else if (c && c._hasMeasure()) {
rules.push({ ordering : c.measure, desc : true });
} else if (z && z._hasMeasure()) {
rules.push({ ordering : z.measure, desc : true });
} else if (x && x._hasMeasure()) {
rules.push({ ordering : x.measure, desc : true });
} else if (y && y._hasMeasure()) {
rules.push({ ordering : y.measure, desc : true });
}
orderedSeriesArray = dimple._getOrderedList(sortedData, seriesCat, rules);
}
sortedData.sort(function (a, b) {
var returnValue = 0,
categories,
comp,
p,
q,
aMatch,
bMatch;
if (storyCat !== "") {
returnValue = orderedStoryboardArray.indexOf(a[storyCat]) - orderedStoryboardArray.indexOf(b[storyCat]);
}
if (xCat !== "" && returnValue === 0) {
returnValue = xSortArray.indexOf(a[xCat]) - xSortArray.indexOf(b[xCat]);
}
if (yCat !== "" && returnValue === 0) {
returnValue = ySortArray.indexOf(a[yCat]) - ySortArray.indexOf(b[yCat]);
}
if (pCat !== "" && returnValue === 0) {
returnValue = pSortArray.indexOf(a[pCat]) - ySortArray.indexOf(b[pCat]);
}
if (seriesCat && seriesCat.length > 0 && returnValue === 0) {
categories = [].concat(seriesCat);
returnValue = 0;
for (p = 0; p < orderedSeriesArray.length; p += 1) {
comp = [].concat(orderedSeriesArray[p]);
aMatch = true;
bMatch = true;
for (q = 0; q < categories.length; q += 1) {
aMatch = aMatch && (a[categories[q]] === comp[q]);
bMatch = bMatch && (b[categories[q]] === comp[q]);
}
if (aMatch && bMatch) {
returnValue = 0;
break;
} else if (aMatch) {
returnValue = -1;
break;
} else if (bMatch) {
returnValue = 1;
break;
}
}
}
return returnValue;
});
// Iterate every row in the data to calculate the return values
sortedData.forEach(function (d) {
// Reset the index
var foundIndex = -1,
xField = getField(x, d),
yField = getField(y, d),
zField = getField(z, d),
pField = getField(p, d),
// Get the aggregate field using the other fields if necessary
aggField = [],
key,
k,
i,
newRow,
updateData;
if (!cats || cats.length === 0) {
aggField = ["All"];
} else {
// Iterate the category fields
for (i = 0; i < cats.length; i += 1) {
// Either add the value of the field or the name itself. This allows users to add custom values, for example
// Setting a particular color for a set of values can be done by using a non-existent final value and then coloring
// by it
if (d[cats[i]] === undefined) {
aggField.push(cats[i]);
} else {
aggField.push(d[cats[i]]);
}
}
}
// Add a key
key = aggField.join("/") + "_" + xField.join("/") + "_" + yField.join("/") + "_" + pField.join("/") + "_" + zField.join("/");
// See if this field has already been added.
for (k = 0; k < returnData.length; k += 1) {
if (returnData[k].key === key) {
foundIndex = k;
break;
}
}
// If the field was not added, do so here
if (foundIndex === -1) {
newRow = {
key: key,
aggField: aggField,
xField: xField,
xValue: null,
xCount: 0,
yField: yField,
yValue: null,
yCount: 0,
pField: pField,
pValue: null,
pCount: 0,
zField: zField,
zValue: null,
zCount: 0,
cValue: 0,
cCount: 0,
x: 0,
y: 0,
xOffset: 0,
yOffset: 0,
width: 0,
height: 0,
cx: 0,
cy: 0,
xBound: 0,
yBound: 0,
xValueList: [],
yValueList: [],
zValueList: [],
pValueList: [],
cValueList: [],
fill: {},
stroke: {}
};
returnData.push(newRow);
foundIndex = returnData.length - 1;
}
// Update the return data for the passed axis
updateData = function (axis, storyboard) {
var passStoryCheck = true,
lhs = { value: 0, count: 1 },
rhs = { value: 0, count: 1 },
selectStoryValue,
compare = "",
retRow;
if (storyboard !== null) {
selectStoryValue = storyboard.getFrameValue();
storyboard.categoryFields.forEach(function (cat, m) {
if (m > 0) {
compare += "/";
}
compare += d[cat];
passStoryCheck = (compare === selectStoryValue);
}, this);
}
if (axis !== null && axis !== undefined) {
if (passStoryCheck) {
retRow = returnData[foundIndex];
if (axis._hasMeasure() && d[axis.measure] !== null && d[axis.measure] !== undefined) {
// Keep a distinct list of values to calculate a distinct count in the case of a non-numeric value being found
if (retRow[axis.position + "ValueList"].indexOf(d[axis.measure]) === -1) {
retRow[axis.position + "ValueList"].push(d[axis.measure]);
}
// The code above is outside this check for non-numeric values because we might encounter one far down the list, and
// want to have a record of all values so far.
if (isNaN(parseFloat(d[axis.measure]))) {
useCount[axis.position] = true;
}
// Set the value using the aggregate function method
lhs.value = retRow[axis.position + "Value"];
lhs.count = retRow[axis.position + "Count"];
rhs.value = d[axis.measure];
retRow[axis.position + "Value"] = agg(lhs, rhs);
retRow[axis.position + "Count"] += 1;
}
}
}
};
// Update all the axes
updateData(x, this.storyboard);
updateData(y, this.storyboard);
updateData(z, this.storyboard);
updateData(p, this.storyboard);
updateData(c, this.storyboard);
}, this);