-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtchart.js
More file actions
6731 lines (5501 loc) · 252 KB
/
Copy pathtchart.js
File metadata and controls
6731 lines (5501 loc) · 252 KB
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
window.Graph = {
render: function (container, chart) {
var wrapper = document.createElement('div');
wrapper.className = 'tchart--wrapper';
container.appendChild(wrapper);
return new this.units.TChart({
container: wrapper,
data: chart
});
},
units: {}
};
(function () {
var easing = function (st, ed, per, tween) {
var functions = {
linear: function (t, b, c, d) {
return c * t / d + b;
},
easeInOutQuad: function (t, b, c, d) {
t /= d * 0.5;
if (t < 1) return c * 0.5 * t * t + b;
t--;
return -c / 2 * (t * (t - 2) - 1) + b;
}
};
return functions[tween](per, st, ed - st, 1);
};
var TAnimator = function (opts) {
this.composer = opts.composer;
this.state = opts.state;
this.queue = {};
this.queueSize = 0;
this.step = this.step.bind(this);
}
TAnimator.prototype = {
add: function (params) {
var i = 0;
var j = 0;
var cur = +new Date();
var item, param, delta;
var queue = this.queue;
while (i < params.length) {
param = params[i];
item = queue[param.prop];
if (!item) {
if (param.end == param.state[param.prop]) {
param.cbEnd && param.cbEnd(param.state);
i++;
continue;
}
item = {
lastStart: 1
}
queue[param.prop] = item;
this.queueSize++;
}
delta = cur - item.lastStart;
param.duration *= this.state.speed;
param.delay *= this.state.speed;
item.cbEnd = param.cbEnd;
item.state = param.state;
item.lastStart = cur;
item.start = param.state[param.prop];
item.end = param.end;
item.startDt = cur + (param.delay || 0);
item.endDt = item.startDt + (param.duration || 0) - (param.fixed ? 0 : Math.max(param.duration - delta, 0));
item.tween = param.tween || 'easeInOutQuad';
item.speed = param.speed;
item.group = param.group;
i++;
}
if (!this.animFrame) {
this.animFrame = requestAnimationFrame(this.step);
}
},
get: function (prop) {
return this.queue[prop];
},
step: function () {
var done = [];
var cur = +new Date();
var itemKey, item, time, duration, per, curVal, newVal, j, delayed;
var group = {top: false, bottom: false};
for (itemKey in this.queue) {
item = this.queue[itemKey];
time = cur;
duration = item.endDt - item.startDt;
curVal = item.state[itemKey];
delayed = time < item.startDt;
if (time < item.startDt) {
time = item.startDt;
} else if (time > item.endDt) {
time = item.endDt;
}
per = duration ? (time - item.startDt) / duration : (delayed ? 0 : 1);
if (per < 1) {
if (item.tween == 'exp') {
newVal = curVal + (item.end - curVal) * item.speed;
} else {
newVal = easing(item.start, item.end, per, item.tween);
}
} else {
newVal = item.end;
}
if (newVal != curVal) {
item.state[itemKey] = newVal;
group.top = group.top || item.group.top;
group.bottom = group.bottom || item.group.bottom;
} else if (newVal == item.end) {
done.push(itemKey);
}
}
//remove animations that is done
j = 0;
while (j < done.length) {
this.queue[done[j]].cbEnd && this.queue[done[j]].cbEnd(this.queue[done[j]].state);
delete this.queue[done[j]];
j++;
}
this.queueSize -= done.length;
this.composer.render(group);
if (!this.queueSize) {
delete this.animFrame;
} else {
this.animFrame = requestAnimationFrame(this.step);
}
}
}
window.Graph.units.TAnimator = TAnimator;
})();
(function () {
var units = window.Graph.units;
var TAreas = function (opts) {
this.opts = opts;
if (opts.additional.mini) {
this.$canvas = document.createElement('canvas');
this.ctx = this.$canvas.getContext('2d', {alpha: true});
}
}
TAreas.prototype = {
onResize: function () {
if (this.opts.additional.mini) {
var dpi = this.opts.settings.dpi;
var dims = this.opts.additional.mini ? this.opts.state.dims.mini : this.opts.state.dims.graph;
this.$canvas.width = dims.w * dpi;
this.$canvas.height = dims.h * dpi;
this.cached = '';
}
},
setDarkMode: function (enabled) {
this.isDarkMode = enabled;
},
render: function () {
var i, j, y, o, y1, y2, xScale, yScale, xShift, yShift;
var opts = this.opts;
var ys = opts.data.ys;
var state = opts.state;
var mini = opts.additional.mini;
var x1 = mini ? state.xg1 : state.x1;
var x2 = mini ? state.xg2 : state.x2;
var settings = opts.settings;
var pTop = settings['PADD' + (mini ? '_MINI_AREA' : '')][0];
var pRight = settings['PADD' + (mini ? '_MINI_AREA' : '')][1];
var pBottom = settings['PADD' + (mini ? '_MINI_AREA' : '')][2];
var pLeft = settings['PADD' + (mini ? '_MINI_AREA' : '')][3];
var x = opts.data.x;
var dpi = opts.settings.dpi;
var xInd1, xInd2;
var ctx = mini ? this.ctx : this.opts.ctx;
var prevY = [];
var totalPerX = [];
var totalPerY = [];
var overlap = mini ? 0 : 0;
var dims = mini ? state.dims.mini : state.dims.graph;
var zoomMorph = state.zoomMorph == undefined ? 0 : state.zoomMorph;
var morph = zoomMorph;
var zoom = state.zoomMode;
var ysLen = ys.length;
//cache rendered version
if (mini) {
var hash = [dims.w, dims.h, state.xg1, state.xg2, this.isDarkMode, state.zoomMode, zoomMorph];
for (i = 0; i < ysLen; i++) {
hash.push(state['om_' + i]);
hash.push(state['f_' + i]);
}
hash = hash.join(',');
if (hash == this.cached) {
this.opts.ctx.drawImage(this.$canvas, dims.l * dpi, dims.t * dpi);
return;
}
this.cached = hash;
}
xScale = (dims.w - pRight - pLeft) / (x2 - x1);
xInd1 = Math.floor(units.TUtils.getXIndex(x, x1 - pLeft / xScale));
xInd2 = Math.ceil(units.TUtils.getXIndex(x, x2 + pRight / xScale));
xScale *= dpi;
xShift = (pLeft + (mini ? 0 : dims.l)) * dpi - x1 * xScale;
hBottom = (dims.h - pBottom + (mini ? 0 : dims.t)) * dpi;
var visibleCols = [];
var opacityCols = [];
var textToCenter = 0; //animation for text moving to center for only 1 selected column
var fullyVisibleCount = 0;
var fullyVisibleInd = 0;
var hasUnfocusedColumns = false;
for (i = 0; i < ysLen; i++) {
o = mini ? state['om_' + i] : state['o_' + i];
hasUnfocusedColumns = hasUnfocusedColumns || state['f_' + i] < 1;
if (o < 1 && o > 0) {
textToCenter = o;
}
if (o > 0) {
visibleCols.push(i);
opacityCols.push(o);
if (o == 1 && state['e_' + i]) {
fullyVisibleCount++;
fullyVisibleInd = visibleCols.length - 1;
}
}
}
var colsLen = visibleCols.length;
textToCenter = fullyVisibleCount == 1 ? textToCenter : 1;
y1 = mini ? state['y1m'] : state['y1'];
y2 = mini ? state['y2m'] : state['y2'];
var optData = units.TUtils.simplifyData('line', x, ys, xScale, xShift, visibleCols, xInd1, xInd2, dims.w - pRight - pLeft);
xInd1 = optData.xInd1;
xInd2 = optData.xInd2;
var optX = optData.x;
var optYs = optData.ys;
var hasGapsInData = false;
for (j = xInd1; j <= xInd2; j++) {
prevY[j] = 0;
totalPerX[j] = 0;
for (i = 0; i < colsLen; i++) {
totalPerX[j] += (optYs[visibleCols[i]].y[j] || 0) * opacityCols[i];
}
if (totalPerX[j] == 0) {
hasGapsInData = true;
}
}
if (hasGapsInData || hasUnfocusedColumns) {
ctx.fillStyle = this.isDarkMode ? '#242f3e' : '#fff';
ctx.fillRect(0, 0, dims.w * dpi, dims.h * dpi);
}
//calc totals for fractional period so all animations will be transformed for pie representation
if (zoomMorph > 0 && !mini) {
if (morph == 1) {
this.savedX1 = x1;
this.savedX2 = x2;
}
if (morph < 1) {
var x1AnimItem = this.opts.animator.get('x1');
var x2AnimItem = this.opts.animator.get('x2');
x1End = x1AnimItem ? x1AnimItem.end : this.opts.state['x1'];
x2End = x2AnimItem ? x2AnimItem.end : this.opts.state['x2'];
var xInd1Real = units.TUtils.getXIndex(x, this.opts.state.zoomDir == -1 ? this.savedX1 : x1End, true);
var xInd2Real = units.TUtils.getXIndex(x, this.opts.state.zoomDir == -1 ? this.savedX2 : x2End, true);
} else {
var xInd1Real = units.TUtils.getXIndex(x, x1, true);
var xInd2Real = units.TUtils.getXIndex(x, x2, true);
}
xInd2Real--;
var xInd1RealFloor = Math.floor(xInd1Real);
var xInd1RealCeil = Math.ceil(xInd1Real);
var xInd2RealFloor = Math.floor(xInd2Real);
var xInd2RealCeil = Math.ceil(xInd2Real);
var totalForAll = 0;
var totalPerItem = [];
for (i = 0; i < colsLen; i++) {
totalPerItem[i] = 0;
var tmpY = ys[visibleCols[i]].y;
for (j = xInd1RealCeil; j <= xInd2RealFloor; j++) {
var tmp = (tmpY[j] || 0) * opacityCols[i];
totalPerItem[i] += tmp;
totalForAll += tmp;
}
//partly visible data from left side
var tmp = ((xInd1RealCeil - xInd1Real) * (tmpY[xInd1RealFloor] || 0)) * opacityCols[i];
totalPerItem[i] += tmp;
totalForAll += tmp;
//partly visible data from right side
var tmp = ((xInd2Real - xInd2RealFloor) * (tmpY[xInd2RealCeil] || 0)) * opacityCols[i];
totalPerItem[i] += tmp;
totalForAll += tmp;
}
//calc angles for pie representation
var elastic = this.opts.state.zoomDir == 1 ? Math.pow(Math.min(Math.max(morph < 0.85 ? (morph - 0.65) / 0.2 : 1 - (morph - 0.9) / 0.15, 0), 1), 0.8) : this.prevElastic;
var prevAngle = 2 * Math.PI - Math.PI / (7 - morph) - Math.PI / 8 * elastic;
var initAngle = prevAngle;
if (this.opts.state.zoomDir == 1) {
morph = Math.min(Math.max((morph - 0.25) / 0.4, 0), 1);
this.prevElastic = elastic;
} else {
morph = Math.min(Math.max((morph * 2.4) - 1.4, 0), 1);
}
var angles = [];
var radius = settings.PIE_RADIUS * (zoomMorph < 1 ? 2.31 : 1) * dpi; //during zoom animation use clipping, then use plain geometry to create sectors
var cx = dpi * (dims.w / 2 + dims.l);
var cy = dpi * (dims.h / 2 + dims.t + 2);
var rLen = 2 * Math.PI * radius / dpi;
var pointsPerArcLen = 1 / 13; //1 point per each 10 pixels of arc
for (i = 0; i < colsLen; i++) {
var percentage = totalPerItem[i] / totalForAll;
percentage = percentage || 0; //absent data
var len = 2 * Math.PI * percentage;
var newAngle = prevAngle - len;
var additionalPoints = Math.round(percentage * rLen * pointsPerArcLen);
if (i == colsLen - 1) newAngle = initAngle - 2 * Math.PI;
var overlapAng = Math.PI * 2 * 0.1 / (rLen);
var yItem = ys[visibleCols[i]];
angles.push({
st: prevAngle + overlapAng,
ed: newAngle - overlapAng,
mid: prevAngle - len / 2 - overlapAng / 2,
additionalPoints: Math.max(additionalPoints, 4),
percentage: percentage == 0 ? 0 : Math.max(Math.round(percentage * 100), 1),
percentageText: percentage == 0 ? '' : (percentage < 0.01 ? '<1%' : Math.round(percentage * 100) + '%'),
ind: visibleCols[i],
value: totalPerItem[i],
label: yItem.label,
color: this.isDarkMode ? yItem.colors_n[2] : yItem.colors_d[2]
});
prevAngle = newAngle;
}
state.pieAngles = angles;
}
yScale = dpi * (dims.h - pTop - pBottom + (mini ? 0 : -4));
yShift = (dims.h - pBottom + (mini ? 0 : dims.t)) * dpi;
var colInd = 0;
for (i = 0; i < ysLen; i++) {
o = mini ? state['om_' + i] : state['o_' + i];
if (o > 0) {
y = optYs[i].y;
var k = o * yScale;
ctx.fillStyle = this.isDarkMode ? ys[i].colors_n[0] : ys[i].colors_d[0];
ctx.globalAlpha = state['f_' + i] * 0.9 + 0.1;
ctx.beginPath();
if (zoomMorph == 0 || !mini) {
//use regular version to skip complex math evaluations
//despite of fact that they produce same result for morph == 0
if (zoomMorph == 0) {
if (i > 0) {
ctx.moveTo(optX[xInd2] * xScale + xShift << 0, hBottom - prevY[xInd2] + overlap << 0);
for (j = xInd2 - 1; j >= xInd1; j--) {
ctx.lineTo(optX[j] * xScale + xShift << 0, hBottom - prevY[j] + overlap << 0);
}
} else {
ctx.moveTo(optX[xInd2] * xScale + xShift << 0, hBottom << 0);
ctx.lineTo(optX[xInd1] * xScale + xShift << 0, hBottom << 0);
}
if (colInd < colsLen - 1 || hasGapsInData) {
for (j = xInd1; j <= xInd2; j++) {
var curY = (yShift - ((y[j] * k / totalPerX[j]) || 0));
var curH = hBottom - curY;
var sy = prevY[j] + curH;
if (sy > yScale) sy = yScale;
ctx.lineTo(optX[j] * xScale + xShift << 0, hBottom - sy << 0);
prevY[j] += curH;
}
} else {
ctx.lineTo(optX[xInd1] * xScale + xShift << 0, hBottom - yScale << 0);
ctx.lineTo(optX[xInd2] * xScale + xShift << 0, hBottom - yScale << 0);
}
} else {
//magic starts here
var calcTrans = function (fromX, fromY, toAngle, toR) {
var sx = 0;
var sy = 0;
if (selectionOffset && fullyVisibleCount > 1) {
sx = Math.cos(angles[colInd].mid) * selectionOffset * 8 * dpi;
sy = -Math.sin(angles[colInd].mid) * selectionOffset * 8 * dpi;
}
if (toR > radius) toR = radius;
var fromAngle = Math.atan2(cy - fromY, fromX - cx);
fromAngle = fromAngle < 0 ? Math.PI * 2 + fromAngle : fromAngle;
var fromR = Math.pow((cy - fromY) * (cy - fromY) + (fromX - cx) * (fromX - cx), 0.5);
if (Math.abs(toAngle - fromAngle) > Math.PI * (colsLen == 1 ? 1.5 : 1)) {
toAngle -= Math.PI * 2;
}
if (toAngle < -Math.PI * 2) {
toAngle -= -Math.PI * 2;
}
var ang = fromAngle + morph * (toAngle - fromAngle);
var r = fromR + morph * (toR - fromR);
var res = [
cx + Math.cos(ang) * r + sx,
cy - Math.sin(ang) * r + sy
];
return res;
}
var additionalSteps = (zoomMorph < 1 ? 4 : angles[colInd].additionalPoints);
var res;
var dist;
var cBot = false, cTop = false, xj;
var selectionOffset = state['pieInd_' + visibleCols[colInd]] || 0;
if (angles[colInd].percentage == 0) {
ctx.globalAlpha = 0;
}
res = calcTrans(optX[xInd2] * xScale + xShift, hBottom - prevY[xInd2], angles[0].st, radius);
ctx.moveTo(res[0], res[1]);
if (colInd > 0) {
for (j = xInd2 - 1; j >= xInd1; j--) {
xj = optX[j] * xScale + xShift;
if (xj == cx) cBot = true;
if (xj >= cx) {
dist = (xj - cx) / (dims.w * dpi / 2);
if (morph == 0) dist = 0;
res = calcTrans(xj, hBottom - prevY[j] + overlap, angles[0].st, radius * dist);
} else {
if (!cBot) {
cBot = true;
var sc = (cx - xj) / (optX[j + 1] * xScale + xShift - xj);
var sy1 = hBottom - prevY[j] + overlap;
var sy2 = hBottom - prevY[j + 1] + overlap;
res = calcTrans(cx, sy1 + sc * (sy2 - sy1), angles[colInd].st, 0);
ctx.lineTo(res[0], res[1]);
}
dist = (cx - xj) / (dims.w * dpi / 2);
res = calcTrans(xj, hBottom - prevY[j] + overlap, angles[colInd].st, radius * dist);
}
ctx.lineTo(res[0], res[1]);
}
} else {
res = calcTrans(
optX[xInd1] * xScale + xShift,
hBottom,
angles[0].st,
radius
);
ctx.lineTo(res[0], res[1]);
}
if (colInd < colsLen - 1) {
for (j = 0; j <= additionalSteps; j++) {
var curY = (yShift - ((y[xInd1] * k / totalPerX[xInd1]) || 0));
var curH = hBottom - curY;
var sy1 = hBottom - prevY[xInd1] + overlap;
var sy2 = hBottom - prevY[xInd1] - curH;
res = calcTrans(
optX[xInd1] * xScale + xShift,
sy1 + (j / additionalSteps) * (sy2 - sy1),
angles[colInd].st + (j / additionalSteps) * (angles[colInd].ed - angles[colInd].st),
radius
);
ctx.lineTo(res[0], res[1]);
}
for (j = xInd1; j <= xInd2; j++) {
var curY = (yShift - ((y[j] * k / totalPerX[j]) || 0));
var curH = hBottom - curY;
xj = optX[j] * xScale + xShift;
if (xj == cx) cTop = true;
if (xj <= cx) {
dist = (cx - xj) / (dims.w * dpi / 2);
var xjprev = xj;
var syprev = hBottom - prevY[j] - curH;
res = calcTrans(xj, syprev, angles[colInd].ed, radius * dist);
} else {
if (!cTop) {
cTop = true;
var sc = (cx - xjprev) / (xj - xjprev);
var sy1 = syprev;
var sy2 = hBottom - prevY[j] - curH;
res = calcTrans(cx, sy1 + sc * (sy2 - sy1), angles[colInd].ed, 0);
ctx.lineTo(res[0], res[1]);
}
dist = (xj - cx) / (dims.w * dpi / 2);
if (morph == 0) dist = 0;
res = calcTrans(xj, hBottom - prevY[j] - curH, angles[0].st, radius * dist);
}
ctx.lineTo(res[0], res[1]);
prevY[j] += curH;
}
if (xj < cx) { //last day, haack
if (!cTop) {
res = calcTrans(cx, sy1, angles[colInd].ed, 0);
ctx.lineTo(res[0], res[1]);
}
}
} else {
for (j = 0; j <= additionalSteps; j++) {
res = calcTrans(
(optX[xInd1] + (j / additionalSteps) * (optX[xInd2] - optX[xInd1])) * xScale + xShift,
0,
angles[colInd].st + (j / additionalSteps) * (angles[0].st - 2 * Math.PI - angles[colInd].st),
radius
);
ctx.lineTo(res[0], res[1]);
}
}
}
} else {
var xw = this.opts.data.mainPeriodLen * xScale;
ctx.moveTo(optX[xInd2] * xScale + xw + xShift, hBottom - prevY[xInd2]);
if (i > 0) {
ctx.lineTo(optX[xInd2] * xScale + xShift, hBottom - prevY[xInd2]);
for (j = xInd2; j >= xInd1 + 1; j--) {
ctx.lineTo(optX[j] * xScale + xShift, hBottom - (prevY[j] + morph * (prevY[j - 1] - prevY[j])) + overlap);
ctx.lineTo(optX[j - 1] * xScale + xShift, hBottom - prevY[j - 1] + overlap);
}
} else {
ctx.lineTo(optX[xInd1] * xScale + xShift, hBottom);
}
if (colInd < colsLen - 1) {
for (j = xInd1; j <= xInd2 - 1; j++) {
var curY = (yShift - ((y[j] * k / totalPerX[j]) || 0));
var curH = hBottom - curY;
if (j == xInd1) {
ctx.lineTo(optX[xInd1] * xScale + xShift, hBottom - prevY[xInd1] - curH);
}
var curY = (yShift - ((y[j + 1] * k / totalPerX[j + 1]) || 0));
var curHNext = hBottom - curY;
var yTo = prevY[j] + curH;
var yFrom = prevY[j + 1] + curHNext;
ctx.lineTo(optX[j + 1] * xScale + xShift, hBottom - (yFrom + morph * (yTo - yFrom)));
ctx.lineTo(optX[j + 1] * xScale + xShift, hBottom - yFrom);
if (j == xInd2 - 1) {
ctx.lineTo(optX[xInd2] * xScale + xw + xShift, hBottom - prevY[xInd2] - curHNext);
}
prevY[j] += curH;
}
} else {
ctx.lineTo(optX[xInd1] * xScale + xShift << 0, hBottom - yScale << 0);
ctx.lineTo(optX[xInd2] * xScale + xShift << 0, hBottom - yScale << 0);
}
prevY[xInd2] += curHNext;
}
ctx.closePath();
ctx.fill();
//texts
if (!mini && zoomMorph > 0 && angles[colInd].percentageText) {
var opacity = Math.pow(morph, this.opts.state.zoomDir == 1 ? 4 : 20) * o * (state['f_' + i] * 0.9 + 0.1);
var fontSize = Math.max(Math.min(angles[colInd].percentage * 2, 26), 10);
var rad = settings.PIE_RADIUS;
var offset = rad * 2 / 3;
var cosVal = Math.cos(angles[colInd].mid);
var sinVal = Math.sin(angles[colInd].mid);
var isOutboard = angles[colInd].percentage < opts.data.pieLabelsPercentages.outboard;
var sx = 0;
var sy = 0;
if (selectionOffset && fullyVisibleCount > 1) {
sx = cosVal * selectionOffset * 8 * dpi;
sy = -sinVal * selectionOffset * 8 * dpi;
}
ctx.fillStyle = 'white';
ctx.textAlign = 'center';
ctx.globalAlpha = opacity;
if (angles[colInd].percentage < opts.data.pieLabelsPercentages.hoverOnly) {
ctx.globalAlpha = selectionOffset * opacity;
}
if (isOutboard) {
var fontSize = Math.max(fontSize, 14);
offset = rad + fontSize / 3 + 13;
ctx.fillStyle = this.isDarkMode ? ys[i].colors_n[0] : ys[i].colors_d[0];
ctx.lineWidth = 1;
ctx.strokeStyle = this.isDarkMode ? ys[i].colors_n[0] : ys[i].colors_d[0];
var lx1 = cx + sx + (cosVal * (rad - 1)) * dpi;
var ly1 = cy + sy - (sinVal * (rad - 1)) * dpi;
var lx2 = cx + sx + (cosVal * (rad + 6 * (1 - selectionOffset) - 1)) * dpi;
var ly2 = cy + sy - (sinVal * (rad + 6 * (1 - selectionOffset) - 1)) * dpi;
ctx.beginPath();
ctx.moveTo(lx1, ly1);
ctx.lineTo(lx2, ly2);
ctx.stroke();
}
var dx = (cosVal * offset) * (fullyVisibleInd == colInd ? textToCenter : 1);
var tx = cx + sx + dx * dpi + (isOutboard ? (fontSize / 4 * angles[colInd].percentageText.length * dx / offset) * dpi : 0);
var ty = cy + sy - (sinVal * offset) * (fullyVisibleInd == colInd ? textToCenter : 1) * dpi;
ctx.font = 'bold ' + fontSize * dpi + 'px ' + opts.settings.FONTS;
ctx.fillText(angles[colInd].percentageText, tx, ty + fontSize * dpi / 2.9);//fontSize * dpi / 2.9 cause text render point is baseline
ctx.globalAlpha = 1;
}
colInd++;
}
}
ctx.globalAlpha = 1;
mini && this.opts.ctx.drawImage(this.$canvas, dims.l * dpi, dims.t * dpi);
}
}
units.TAreas = TAreas;
})();
(function () {
var units = window.Graph.units;
var TAxisX = function (opts) {
this.opts = opts;
this.ctx = opts.ctx;
this.items = {};
this.setAnimation(false);
this.deleteItem = this.deleteItem.bind(this);
}
TAxisX.prototype = {
onResize: function () {
this.setAnimation(false);
},
setDarkMode: function (enabled) {
this.isDarkMode = enabled;
},
setAnimation: function (enabled) {
this.noAnimation = !enabled;
},
hideItem: function (ind, k) {
this.items[ind].tp = 2;
this.opts.animator.add([{
prop: 'ox_' + ind,
state: this.items[ind].state,
end: 0,
duration: this.noAnimation ? 0 : 200 * k,
tween: 'linear',
group: {top: true},
cbEnd: this.deleteItem
}]);
},
deleteItem: function (state) {
delete this.items[state.ind];
},
render: function (opacity) {
var opts = this.opts;
var dpi = opts.settings.dpi;
var x = opts.data.x;
var state = opts.state;
var pRight = opts.settings.PADD[1];
var pLeft = opts.settings.PADD[3];
var animator = opts.animator;
var xLen = x.length;
var dims = this.opts.state.dims.axisX;
var dimsDates = this.opts.state.dims.dates;
var zoomMode = state.zoomMode;
var zoomMorph = state.zoomMorph == undefined ? 0 : state.zoomMorph;
var x1 = Math.floor(units.TUtils.getXIndex(x, state.x1));
var x2 = Math.ceil(units.TUtils.getXIndex(x, state.x2));
var x1End = x1;
var x2End = x2;
var isPointHasWidth = opts.graphStyle == 'bar' || opts.graphStyle == 'step';
//fast calculation of average space occupied by label
var space = (state.zoomMode && opts.data.details ? opts.data.details.maxXTickLength : opts.data.maxXTickLength) * 9;
var offsetForBarGraphMain = isPointHasWidth ? this.opts.data.mainPeriodLen : 0;
var offsetForBarGraphDetail = isPointHasWidth ? this.opts.data.detailPeriodLen : 0;
var offsetForBarGraph = offsetForBarGraphMain + (offsetForBarGraphDetail - offsetForBarGraphMain) * zoomMorph;
var offsetForBarGraphScale = offsetForBarGraphMain * (1 - zoomMorph);
var xStepMain = (dims.w - pRight - pLeft) / Math.round((state.x2 - state.x1 + offsetForBarGraphMain) / this.opts.data.mainPeriodLen);
var xStepDetail = (dims.w - pRight - pLeft) / Math.round((state.x2 - state.x1 + offsetForBarGraphDetail) / this.opts.data.detailPeriodLen);
var xStep = xStepMain + (xStepDetail - xStepMain) * zoomMorph;
var skipEachMain = Math.pow(2, Math.ceil(Math.log2(space / xStepMain)));
var skipEachDetail = Math.pow(2, Math.ceil(Math.log2(space / xStepDetail)));
var lxScale = (dims.w - pLeft - pRight) / (state.x2 - state.x1 + offsetForBarGraphScale);
if (skipEachMain < 1) {
skipEachMain = 1;
}
if (skipEachDetail < 1) {
skipEachDetail = 1;
}
this.ctx.font = 11 * dpi + 'px ' + opts.settings.FONTS;
this.ctx.textAlign = 'center';
this.ctx.fillStyle = this.isDarkMode ? this.opts.data.axis_n.x : this.opts.data.axis_d.x;
//for fast handle move reduce animation duration
var changeSpeed = this.prevXStep ? (this.prevXStep > xStepMain ? this.prevXStep / xStepMain : xStepMain / this.prevXStep) : 1;
var k = 1 / Math.pow(changeSpeed, 5);
if (zoomMode && zoomMorph == 1) {
k /= 2;
}
this.prevXStep = xStepMain;
var x1 = Math.max(x1 - Math.ceil((pLeft + space * 0.5) / xStep), 0);
var x2 = Math.min(x2 + Math.ceil((pRight + space * 0.5) / xStep), x.length - 1);
if (zoomMode) {
var x1AnimItem = this.opts.animator.get('x1');
var x2AnimItem = this.opts.animator.get('x2');
x1End = x1AnimItem ? x1AnimItem.end : this.opts.state['x1'];
x2End = x2AnimItem ? x2AnimItem.end : this.opts.state['x2'];
x1End = Math.floor(units.TUtils.getXIndex(x, x1End));
x2End = Math.floor(units.TUtils.getXIndex(x, x2End));
}
if (zoomMode) {
var tmp1 = Math.max(x[this.opts.state.detailInd1], this.opts.state.xMainMin);
var tmp2 = Math.min(x[this.opts.state.detailInd2], this.opts.state.xMainMax);
var dtOffset = Math.round((tmp2 - tmp1) / this.opts.data.mainPeriodLen) + (isPointHasWidth ? 0 : 1);
}
for (var i = x1; i <= x2; i++) {
var shown = (i % skipEachMain) == 0;
var prefix = 'm';
if (zoomMode) {
if (i < this.opts.state.detailInd1) {
shown = (i % skipEachMain) == 0 && zoomMorph < 1;
} else if (i <= this.opts.state.detailInd2) {
shown = (Math.max(i - this.opts.state.detailInd1, 0) % skipEachDetail) == 0;
prefix = 'd';
} else {
shown = (Math.max(i - (this.opts.state.detailInd2 - this.opts.state.detailInd1 + 1 - dtOffset), 0) % skipEachMain) == 0 && zoomMorph < 1;
}
}
var id = x[i] + prefix;
var item = this.items[id];
if (shown) {
if (!item) { //not exist or removed
item = {
tp: 1,
xi: x[i],
i: i,
state: {
ind: id
}
}
item.state['ox_' + id] = 0;
this.items[id] = item;
animator.add([{
prop: 'ox_' + id,
state: item.state,
end: 1,
duration: this.noAnimation ? 0 : 200 * k,
tween: 'linear',
group: {top: true}
}]);
} else if (item.tp == 2) { //is hiding
item.tp = 1;
animator.add([{
prop: 'ox_' + id,
state: item.state,
end: 1,
duration: this.noAnimation ? 0 : 200 * k,
tween: 'linear',
group: {top: true}
}]);
}
} else {
if (item && item.tp == 1) { //is showing or shown
this.hideItem(id, k);
}
}
if (item && item.state['ox_' + id] > 0) {
var xc = (item.xi - state.x1 + offsetForBarGraph / 2) * lxScale + pLeft;
this.ctx.globalAlpha = item.state['ox_' + id] * opacity;
if (xc + space / 2 >= dims.l && xc - space / 2 <= dims.l + dims.w) {
//first and last labels manual align
var xAligned = (xc + dims.l) * dpi;
this.ctx.fillText(opts.data.datesShort[i], xAligned, (dims.t + 9) * dpi);
}
}
}
//remove the old ones, which is outside the current range
for (i in this.items) {
var item = this.items[i];
if (item.tp == 1 && ((item.xi < state.x1 - pLeft / lxScale) || (item.xi > state.x2 + pRight / lxScale))) {
this.hideItem(i, k);
}
}
this.ctx.globalAlpha = 1;
// scatter with numeric X: do not render date range caption
// tick labels still shown
if (opts.graphStyle === 'scatter' && opts.data.scatter && opts.data.scatter.xType === 'number') {
return;
}
var datesStr;
if (zoomMode && zoomMorph == 1) {
x2End--;
}
if (x2End < x1End) x2End = x1End;
if (opts.data.datesRange[x1End] == opts.data.datesRange[x2End]) {
datesStr = opts.data.datesRange[x1End];
} else {
datesStr = opts.data.datesRange[x1End] + ' – ' + opts.data.datesRange[x2End];
}
var fontSize = 13;
if (dims.w < 375) {
fontSize = 11;
}
this.ctx.font = 'bold ' + fontSize * dpi + 'px ' + opts.settings.FONTS;
this.ctx.textAlign = 'right';
this.ctx.fillStyle = this.isDarkMode ? '#fff' : '#000';
this.ctx.fillText(datesStr, (dimsDates.l + dimsDates.w) * dpi, (dimsDates.t + dimsDates.h - 7) * dpi);
}
}
units.TAxisX = TAxisX;
})();
(function () {
var units = window.Graph.units;
var TAxisY = function (opts) {
this.opts = opts;
this.ctx = opts.ctx;
this.uuid = 1;
this.items = {};
this.setAnimation(false);
this.setForceUpdate(false);
this.deleteItem = this.deleteItem.bind(this);
}
TAxisY.prototype = {
onResize: function () {
this.setAnimation(false);
this.setForceUpdate(false);
},
setDarkMode: function (enabled) {
this.isDarkMode = enabled;
},
setAnimation: function (enabled) {
this.noAnimation = !enabled;
},
setForceUpdate: function (enabled) {
this.forceUpdate = enabled;
},
deleteItem: function (state) {
delete this.items[state.id];
},
render: function (opacity) {
var calcDataLeft;
var calcDataRight;
if (this.opts.pairY) {
calcDataLeft = this.calcAxisData('y1_0', 'y2_0');
calcDataRight = this.calcAxisData('y1_1', 'y2_1');
//left axis is main, if both need animation priority goes to left
if ((calcDataRight.needAnimation && !calcDataLeft.needAnimation) || this.opts.state['o_0'] < 1) {
this.updateAxisState('y1_1', 'y2_1', 'numRight', calcDataRight, calcDataLeft, calcDataRight);
} else {
this.updateAxisState('y1_0', 'y2_0', 'numLeft', calcDataLeft, calcDataLeft, calcDataRight);
}
} else {
calcDataLeft = this.calcAxisData('y1', 'y2');
this.updateAxisState('y1', 'y2', 'numLeft', calcDataLeft, calcDataLeft, calcDataLeft);
}