-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathjs_chart_lite.js
1286 lines (1164 loc) · 56.9 KB
/
js_chart_lite.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
/*
----------------------- js_chart ver. 1.2 -----------------------
(c) 2019 SpeedBit, reg. Czestochowa, Poland
--------------------------------------------------------------------
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// -------------------------- js_chart ----------------------------
class js_chart {
constructor(container, Ydata, Ystyle, Xdesc, XaxisTxt, YaxisTxt) {
// chart defaults parameters
this.onmouseclick= true; // mouse click event on/off
this.onmousemove = true; // mose move event on/off
this.ontouchmove = true; // touch move event on/off
this.onmousedown = true; // mouse down event on/off
this.onmouseup = true; // mouse up event on/off
this.onkeydown = true; // key down event on/off
this.onkeyup = true; // key up event on/off
this.oncontext = true; // block context menu event on/off (turn off context memu)
this.axisXtxt = "x"; // axis X description
this.axisYtxt = "y"; // axis Y description
this.Ymax = 0; // Xmin or 0 for auto
this.Ymin = 0; // Ymin or 0 for auto
this.decimalX = 0; // decimal point X-axis
this.decimalY = 0; // decimal point Y-axis
// margins
this.margv = 5; // vertical margin
this.margh = 5; // horizontal margin
// canvas background
this.canvasbkcol = "rgba(250, 250, 170, 0.4)"; // canvas background color
this.canvasfrw = 2; // canvas frame width
this.canvasfrcol = "rgba(0, 0, 255, 1)"; // canvas frame color
// markers
this.marsize = 8; // size of marker
this.marw = 2; // width of marker
this.drawaxis = true; // draw axis ?
this.drawarrow = true; // draw axis arrows ?
// axes & arrows & description
this.al = 18; // arrow length
this.aw = 9; // arrow width
this.axcol = "rgba(0,0,0,1)"; // axis color
this.axw = 2; // axis width
this.axisdesc = true; // draw description of axis ?
this.axdesccol = "rgba(0,100,200,1)"; // axis description color
this.drawdesc = true; // draw values ?
this.dgroup = true; // use group digits
this.drawmark = true; // draw markers ?
this.descol = "rgba(0,0,255,1)"; // description / markers color
this.descfpx = 15; // font size for description
this.descfontmod = " italic "; // font modifier
this.descfont = "px Courier New"; // description font
this.rotdescX = true; // rotate the x-axis description 90 degrees
this.drw0x = true; // draw zero value X-axis
this.drw0y = true; // draw zero value Y-axis
this.hmarshift = false; // move X markers by 1/2 size (for bars it's better)
this.addmaxmarg = 0.05; // additional margin from max value to border. 0 = none, 0,01 = 1% of max value;
// mesh
this.drawmesh = true; // draw mesh
this.meshframe = true; // draw mesh frame
this.mshcol = "rgb(200,200,200, 0.7)"; // mesh color
this.meshlw = 1; // mesh line width
// chart zone (for test)
this.chartzone = false; // draw the graph zone
this.chartzonecol= "rgba(120, 120, 120, 1)"; // chart zone frame color
// draw zone (for test)
this.drawzone = false; // draw zone ?
this.drawzfrm = true; // draw zone frame ?
this.drwfracol = "rgba(150, 0, 0, 0.5)"; // draw zone frame color
this.drwfilcol = "rgba(250,250,250, 0.3)"; // draw zone fill color
// cross & hint style
this.crXYline = true; // cross
this.crXjump = true; // jump on X markers
this.crYjumpM = false; // jump on Y markers
this.crYjumpP = true; // jump on Y values
this.crHint = true; // show hint
this.crpointhint = true; // show arc hint
this.crpointfill = true; // show arc hint
this.crlinetomouse= false;// draw line to mouse cursor
this.crlinetosmallhint= true; // draw line to small rectangle hint (onclick)
// cross lines
this.crlinecol = "rgb(0,0,255)"; // color of cross lines
this.crlinewidth = 1; // width of cross lines
this.crXlinedash = [7, 7]; // dash of cross lines X
this.crYlinedash = [7, 7]; // dash of cross line Y
// hint text
this.smallhint = true; // small hint
this.hintwithctrl= true; // hint only when ctrl key is pressed
this.hintfillcol = "rgba(150, 200, 150, 0.7)"; // hint rectangle fill color
this.hintfillhitcol= "rgba(200, 200, 200, 1)"; // hint rectangle fill color when point is hit
this.hintframewidth= 3; // hint rectangle line width
this.hintrectcol = "rgba(50, 50, 50, 0.7)"; // hint rectangle line color
this.hintfpx = 11;
this.hintfontmod = " italic "; // hint font modifier
this.hintfont = "px Courier New"; // hint font
this.hinttxtcol = "rgb(0, 0, 50)"; // hint text color
// hint data point
this.hintpointw = 5; // hint point width
this.hintlinecol = "rgba(50, 50, 50, 1)"; // hint frame color
this.hintpointfill = "rgba(50, 50, 150, 0.5)";// hint frame fill color
this.hintpointcolfromdraw = true; // hint point fill color from draw
this.hintpointlwdth = 1; // hint point line width
this.hintcolfrwdth = 0.5; // hint color square frame line width
this.hintlinetoMcol = "rgba(250, 0, 0, 1)"; // hint line to mouse color
this.hintlinetoMwidth= 0.5; // hint line to mouse width
this.hintmaxalpha = false; // all hints fill with parameter alpha = 1
// hint shadow
this.hintdatashadow = true; // hint data shadow ? (only if any bar chart exists)
this.hintshadowcol = "rgba(150, 150, 150, 0.2)"; // hint data shadow color
// default line graph
this.linecol = "rgba(250, 0, 0, 0.6)"; // chart line color
this.linewidth = 2; // char line width
this.linepoints = false; // points on chart line ?
this.linepointsize= 4; // points size
this.linepointcol = "rgba(250, 0, 0, 0.7)"; // points color
// default area graph
this.arealine = "rgb(250, 0, 250)"; // area chart line color
this.areafill = "rgba(250, 0, 250, 0.3)";// area chart fill color
this.areawidth = 2; // area line width
this.areapoints = false; // points on area line ?
this.areapointsize= 5; // area point size
this.areapointcol = "rgb(250, 0, 250)"; // area point color
// default bezier curve for line and area graph
this.beziercurve = true; // bezier curve or normal line ?
this.beziercnst = 3; // bezier coefficient
this.bezierlvloff = 3; // global level auto off bezier curve. if data points number for one X markers > bezierlvloff
// then bezier curve will be off. Value -1 block this feature. Optimal value is 3
// default bar graph
this.barline = "rgb(0, 250, 250)"; // bar line color
this.barfill = "rgba(0, 250, 250, 0.3)"; // bar fill color
this.barlw = 1; // bar line width
this.barpoints = false; // points on bars ?
this.barpointsize= 5; // bar point size
this.barpointcol = "rgba(0, 250, 250, 0.3)"; // bar point color
this.barperc = 0.75; // percent filling bar markers
//
this.bigmax = 10000; // above this number, all data will be converted to the decimal power
// for a partial chart (from the scope of data)
this.allmaxmin = true; // false = auto from scope, true = auto from all data (if Ymax or Ymin != 0 => these values will be constans)
// mouse multi click - on/off points for all draws (mouse rigth key)
this.multimsdown = true; // on/off points for all draws
this.multimsdowncnt = 3; // number cliks for points switch
this.multimstout = 1000; // timeout for points switch
this.multimsdowncol = "rgba(0, 0, 0, 0.15)"; // color for points switch
// shedow when select zoom
this.hintselectshadow = true; // on/off select shadow
this.hintselectcol = "rgba(0, 0, 0, 0.3)"; // select shadow color
// shift chart left/rigth by moouse left click (if zoomed)
this.mouseclickLR = true; // on/off shift
this.LRsize = 1/5; // click area (left and right) - fraction of the entire chart area 1/5 = 20%
this.hintzoomcol = "rgba(0, 250, 0, 0.15)"; // color of the shift click area
this.undozoompx = 20; // the number of pixels by which the mouse must be moved to the left to undo the magnification
//--- internal ------------------------------------------------------------
// place for the legend
this.islegend = false;
this.legmarg = 5; // margin of legend to chart
this.legpos = 0; // where is the legend ?
this.legstr = []; // place for legend strings
this.legtop = 0; // place for the legend on the top
this.legleft = 0; // place for the legend on the left
this.legbottom = 0; // place for the legend on the bottom
this.legright = 0; // place for the legend on the right
this.legframew = 1; // legend rectangle line width
this.legframecol = "rgba(250, 250, 250, 0.7)"; // color of the legend frame
this.legfillcol = "rgba(150, 150, 150, 0.5)"; // color of the legend bacground
this.legpx = 12; // legend font size
this.legfontmod = " italic "; // legend font modifier
this.legfont = "px Courier New"; // legend font name
this.legtxtcol = "rgba( 0, 250, 250, 1)"; // legend text color
// this must be for start
if (typeof container == "undefined") return -1; // no container
if (typeof Ydata == "undefined") return -2; // no data
if (typeof Ystyle == "undefined") return -3; // no style
// get data reference
this.data = Ydata;
this.style = Ystyle;
this.desc = Xdesc;
// axis text
if (typeof XaxisTxt != "undefined") this.axisXtxt = XaxisTxt;
if (typeof YaxisTxt != "undefined") this.axisYtxt = YaxisTxt;
// varables and calculations
this.container = container; // main container
this.ctx ; // main context
this.ctxl2 ; // cross & hint context
this.canvas; // main canvas
this.layer2; // cross & hint canvas
this.canvas = document.createElement("canvas"); this.canvas.id = "canvas";
this.layer2 = document.createElement("canvas"); this.layer2.id = "layer2";
this.canvas.style.zindex= 1;
this.layer2.style.zindex= 2;
this.canvas.style.order = "1";
this.layer2.style.order = "2";
this.layer2.style.position = "absolute";
this.canvas.style.position = "absolute";
document.getElementById(container).appendChild(this.canvas);
document.getElementById(container).appendChild(this.layer2);
this.ctx = this.canvas.getContext("2d");
this.ctxl2 = this.layer2.getContext("2d");
this.alldatalength = 0;
// zoom data
this.datalength = 0;
this.from = 0;
this.zoom = false;
// find the data length and maximum values
if ( Array.isArray(this.data) ) {
for (let i=0; i < this.data.length; i++ )
if ( (typeof this.data[i] != "undefined") &&
(this.alldatalength < this.data[i].length)) this.alldatalength = this.data[i].length;
}
this.datalength = this.alldatalength;
// zoom data
this.to = this.alldatalength;
this.eventsactivated = false; // events invoked only once!
this.pointclicked = false; // mouse point clicked
// chart data
this.xdiv = 0;
this.wght = 0; // calculated weight of Y data
this.barcnt = 0; // count bar charts
this.barnr = 0; // bar number for draw
this.marhpx = 0;
this.zlvl = 0;
this.lvlv = this.margh + this.marsize * 2 + this.legleft; // level vertical axis
this.dcorr = 1; // data corrector
this.aYtxt = "Y";
// axis data
this.lft = 0; // left margin
this.top = 0; // top margin
this.rgt = 0; // right margin
this.bot = 0; // bottom margin
this.zerox = 0; // level zero
this.drt = 0; // draw top
this.drb = 0; // draw bottom
this.drl = 0; // draw left
this.drr = 0; // draw right
this.drv = 0; // draw height
this.allpoints = false; //
} // constructor
update(Ydata, Ystyle, Xdesc, XaxisTxt, YaxisTxt) {
if (typeof Ydata != "undefined") this.data = Ydata ; // new data
if (typeof Ystyle != "undefined") this.style = Ystyle ; // new style
if (typeof Xdesc != "undefined") this.desc = Xdesc ; // new desc
if (typeof XaxisTxt != "undefined") this.axisXtxt = XaxisTxt; // new X axis text
if (typeof YaxisTxt != "undefined") this.axisYtxt = YaxisTxt; // new Y axis text
this.alldatalength = 0;
// find the length and maximum values of new data
if ( Array.isArray(this.data) ) {
for (let i = 0; i < this.data.length; i++ )
if ( (typeof this.data[i] != "undefined") &&
(this.alldatalength < this.data[i].length)) this.alldatalength = this.data[i].length;
}
if (this.datalength > this.alldatalength) { // new data is shorter
this.from = 0;
this.to = this.alldatalength;
this.zoom = false;
}
self.allpoints = false; // new draw..
this.draw();
}
left() {
if (!this.zoom) return; // no zoom
if (this.from == 0) return; // not possible
this.from--;
this.to--;
this.draw();
}
right() {
if (!this.zoom) return; // no zoom
if (this.to >= this.alldatalength) return; // not possible
this.from++;
this.to++;
this.draw();
}
draw_FromTo(from, to) {
if (( from < 0) || (from >= this.alldatalength) ) return -1;
if (( to < 0) || (to > this.alldatalength) ) return -1;
if (( from > to) || (from == to ) ) return -1;
this.from = from;
this.to = to;
this.zoom = true;
if ((this.from == 0) && (this.to == this.alldatalength)) this.zoom = false;
this.draw();
}
draw_FromCount(from, cnt) {
if (( from < 0) || (from > this.alldatalength - 2 ) ) return -1; // params error
if ( cnt <= 1) return -1;
if ( ( from + cnt ) > this.alldatalength ) cnt = this.alldatalength - from;
this.from = from;
this.to = from + cnt;
this.zoom = true;
if ((this.from == 0) && (this.to == this.alldatalength)) this.zoom = false;
this.draw();
}
undozoom() {
this.from = 0;
this.to = this.alldatalength;
this.datalength = this.alldatalength;
this.zoom = false;
this.draw();
}
clear() {
// main canvas & layer2 clear
this.ctx.clearRect (0, 0, this.ctx.canvas.width , this.ctx.canvas.height );
this.ctxl2.clearRect(0, 0, this.ctxl2.canvas.width, this.ctxl2.canvas.height);
}
redraw() { this.draw(); }
draw() {
// set canvas position and dimmensions the same as parent dimmensions
let cv = document.getElementById(this.container);
this.canvas.style.left = 0;
this.canvas.style.top = 0;
this.canvas.width = cv.offsetWidth ;
this.canvas.height = cv.offsetHeight;
this.layer2.style.left = 0;
this.layer2.style.top = 0;
this.layer2.width = cv.offsetWidth ;
this.layer2.height = cv.offsetHeight;
this.zlvl = 0;
this.lvlv = this.margh + this.marsize * 2 + this.legleft; // level vertical axis
this.zlvl = this.drb - this.zerox; // level zero horizontal axis
this.lft = this.margh + this.legleft; // left margin
this.top = this.margv + this.legtop ; // top margin
this.rgt = this.ctx.canvas.clientWidth - this.margh - this.legright ; // right margin
this.bot = this.ctx.canvas.clientHeight - this.margv - this.legbottom; // bottom margin
this.zerox = 0; // level zero
this.drt = this.top + this.al * 1.5 * this.drawaxis + !this.drawaxis * this.al * 0.3; // draw top
this.drt = this.top + (this.drawaxis ? this.al * 1.5 : this.descfpx / 2); // draw top
this.drb = this.bot - this.marsize; // draw bottom
this.drl = this.lvlv; // draw left
this.drr = this.rgt - (this.drawaxis ? this.al * 1.5 : this.descfpx / 2); // draw right
this.drv = this.drb - this.drt; // draw height
this.make_chart();
}
// internal, main function of the chart engine
make_chart() {
let self = this;
//---------------------------------------------
let minv = 0; // min of Y data
let maxv = 0; // max of Y data
//---------------------------------------------
let rangev = { minv:0, maxv:0}; // range of data - min & max
let marpldata = { cnt:0, factor:1, weight:1} // markers data plus
let marmidata = { cnt:0, factor:1, weight:1} // markers data minus
let marxdata = { cnt:0, div:1} // markers data X
let maxhm = 0;
let marh = 0;
function getMinMax(arr) {
if (typeof arr == "undefined") return { "min":0, "max":0 };
let min = 0;
let max = 0;
let start = 0;
let stop = arr.length;
if ( self.zoom && !self.allmaxmin )
{
start = self.from;
stop = self.to;
}
for (let i = start; i < stop; i++) {
min = arr[i] < min ? arr[i] : min;
max = arr[i] > max ? arr[i] : max;
}
return { "min":min, "max":max };
}
function getMaxMarkerValue(maxvalue, marcntmax) {
if ((marcntmax == 0) || (maxvalue==0)) return { "cnt":1, "factor":0, "weight": 0, "value": 0}
maxvalue = Math.abs(maxvalue);
marcntmax = Math.abs(marcntmax);
let weight = 1;
let factor = 0;
let marcntact = 0;
if (maxvalue > 1) {
do {
factor = 0.1 ; marcntact = Math.abs( maxvalue / (factor * weight) ); if (marcntact <= marcntmax) break;
factor = 0.2 ; marcntact = Math.abs( maxvalue / (factor * weight) ); if (marcntact <= marcntmax) break;
factor = 0.25; marcntact = Math.abs( maxvalue / (factor * weight) ); if (marcntact <= marcntmax) break;
factor = 0.5 ; marcntact = Math.abs( maxvalue / (factor * weight) ); if (marcntact <= marcntmax) break;
factor = 1.0 ; marcntact = Math.abs( maxvalue / (factor * weight) ); if (marcntact <= marcntmax) break;
factor = 2.0 ; marcntact = Math.abs( maxvalue / (factor * weight) ); if (marcntact <= marcntmax) break;
factor = 2.5 ; marcntact = Math.abs( maxvalue / (factor * weight) ); if (marcntact <= marcntmax) break;
factor = 5.0 ; marcntact = Math.abs( maxvalue / (factor * weight) ); if (marcntact <= marcntmax) break;
//factor = 7.5; marcntact = ( maxvalue / (factor * weight) ); if (marcntact <= marcntmax) break;
weight = weight * 10;
} while (marcntact > marcntmax);
}
else { // maxvalue < 1
do {
factor = 8.0; marcntact = Math.abs( (maxvalue * weight) / factor ); if (marcntact >= marcntmax) break;
factor = 7.5; marcntact = Math.abs( (maxvalue * weight) / factor ); if (marcntact >= marcntmax) break;
factor = 6.0; marcntact = Math.abs( (maxvalue * weight) / factor ); if (marcntact >= marcntmax) break;
factor = 5.0; marcntact = Math.abs( (maxvalue * weight) / factor ); if (marcntact >= marcntmax) break;
factor = 2.5; marcntact = Math.abs( (maxvalue * weight) / factor ); if (marcntact >= marcntmax) break;
factor = 2.0; marcntact = Math.abs( (maxvalue * weight) / factor ); if (marcntact >= marcntmax) break;
factor = 1.0; marcntact = Math.abs( (maxvalue * weight) / factor ); if (marcntact >= marcntmax) break;
weight = weight * 10;
} while (marcntact < marcntmax);
// previous values are good
if (Math.abs( (maxvalue * weight) / factor ) > marcntmax) {
if (factor == 8.0) { factor = 1.0; weight = weight / 10; }
if (factor == 7.5) { factor = 8.0; }
if (factor == 7.0) { factor = 7.5; }
if (factor == 5.0) { factor = 7.0; }
if (factor == 2.5) { factor = 5.0; }
if (factor == 2.0) { factor = 2.5; }
if (factor == 1.0) { factor = 2.0; }
marcntact = Math.abs( (maxvalue * weight) / factor );
}
weight = 1 / weight;
}
return { "cnt":marcntact, "factor":factor, "weight": weight, "value": (factor * weight) }
}
function getXmarkersDiv(data_cnt, maxhm) {
let marcnt = data_cnt;
let div = 1;
let weight = 1;
do {
div = 1; marcnt = Math.abs( data_cnt / (div * weight) ); if (marcnt <= maxhm) break;
div = 2; marcnt = Math.abs( data_cnt / (div * weight) ); if (marcnt <= maxhm) break;
div = 5; marcnt = Math.abs( data_cnt / (div * weight) ); if (marcnt <= maxhm) break;
weight = weight * 10;
} while (marcnt > maxhm);
return { "cnt": marcnt, "div": div * weight }
}
function getStyle(style, name, defvalue) {
let s;
if (typeof style == "undefined") return defvalue;
for (let i = 0; i < style.length; i++ ) {
if (typeof style[i] == "undefined") continue;
s = style[i].split("=");
if (s[0] == name) return s[1];
}
return defvalue;
}
function changeRGBAalpha(rgba, alpha) {
if (typeof rgba == "undefined") return rgba;
if ( !rgba.includes("rgba") ) return rgba;
let str = rgba.split(",");
str[3] = alpha + ")";
return str[0] + "," + str[1] + "," + str[2] + "," + str[3];
}
//--- GRAPHS ---------------------------------------------------------------------------------
// line graph
function do_line_graph(data, style) {
if (typeof data == "undefined") return; // if no data then exit
let w1 = 0;
let w2 = 0;
if (maxv != 0) { w1 = Math.abs( (self.zlvl - self.drt ) / maxv ); } else w1 = 0;
if (minv != 0) { w2 = Math.abs( (self.drb - self.zlvl) / minv ); } else w2 = 0;
self.wght = Math.max(w1, w2);
let loclinecolor = getStyle(style, "linecolor" , self.linecol );
let loclinewidth = getStyle(style, "linewidth" , self.linewidth );
let loclinepoints = getStyle(style, "points" , self.linepoints ).toString() == "true";
let locpointsize = getStyle(style, "pointsize" , self.linepointsize);
let locpointcolor = getStyle(style, "pointcolor" , self.linepointcol );
let locbeziercurve = getStyle(style, "beziercurve", self.beziercurve ).toString() == "true";
let locbezierconst = getStyle(style, "beziercnst" , self.beziercnst );
if (getStyle(style, "pointcolor" , null) == null) locpointcolor = changeRGBAalpha(loclinecolor, 0.3);
// Chart line
self.ctx.strokeStyle = loclinecolor;
self.ctx.fillStyle = loclinecolor;
self.ctx.lineWidth = loclinewidth;
let lastdef = false;
let x = 0; let x1 = 0;
let y = 0; let y1 = 0;
for (let i = 0; i < self.datalength; i++) {
let x0def = (typeof data[i + 0 + self.from * self.zoom] != "undefined") && (data[i + 0 + self.from * self.zoom] != null);
let x1def = (typeof data[i + 1 + self.from * self.zoom] != "undefined") && (data[i + 1 + self.from * self.zoom] != null);
if (i == self.datalength - 1) x1def = false;
x = self.lvlv + ((i * self.marhpx) / self.xdiv) + self.hmarshift * (self.marhpx / 2);
if (x0def) y = self.zlvl - (data[i + self.from * self.zoom] * self.wght * self.dcorr);
else y = self.zlvl;
// first data
if (x0def && !lastdef) {
self.ctx.beginPath();
if (x1def) {
self.ctx.moveTo(x, y);
self.ctx.lineTo(x, y);
}
else {
self.ctx.save;
self.ctx.strokeStyle = changeRGBAalpha(locpointcolor, 1);
self.ctx.fillStyle = locpointcolor;
self.ctx.beginPath();
self.ctx.arc(self.lvlv + (i * self.marhpx) / self.xdiv + self.hmarshift * (self.marhpx / 2), self.zlvl - data[i + self.from * self.zoom] * self.wght * self.dcorr, locpointsize, 0, 2 * Math.PI);
self.ctx.stroke();
self.ctx.fill();
self.ctx.restore;
}
}
// middle data
if (x0def && x1def) {
if (locbeziercurve) { //bezier curve
x1 = self.lvlv + ( ( (i + 1) * self.marhpx) / self.xdiv) + self.hmarshift * (self.marhpx / 2);
y1 = self.zlvl - (data[i + 1 + self.from * self.zoom] * self.wght * self.dcorr);
self.ctx.bezierCurveTo(x + self.marhpx / locbezierconst, y, x1 - self.marhpx / locbezierconst, y1, x1, y1);
}
else
self.ctx.lineTo(x, y);
}
// last data
if (x0def && !x1def) {
self.ctx.lineTo(x, y);
self.ctx.stroke();
self.ctx.closePath();
self.ctx.beginPath();
}
// true last data
if (x0def && !lastdef && !x1def ) {
if (i+1 < self.datalength) {
self.ctx.lineTo(x + self.marhpx , y);
}
else {
self.ctx.save;
self.ctx.strokeStyle = changeRGBAalpha(locpointcolor, 1);
self.ctx.fillStyle = locpointcolor;
self.ctx.beginPath();
self.ctx.arc(self.lvlv + (i * self.marhpx) / self.xdiv + self.hmarshift * (self.marhpx / 2), self.zlvl - data[i + self.from * self.zoom] * self.wght * self.dcorr, locpointsize, 0, 2 * Math.PI);
self.ctx.stroke();
self.ctx.fill();
self.ctx.restore;
}
}
lastdef = x0def;
}
// data big points
if (loclinepoints || self.allpoints) {
self.ctx.beginPath();
self.ctx.strokeStyle = changeRGBAalpha(locpointcolor, 1);
self.ctx.fillStyle = locpointcolor;
self.ctx.arc(self.lvlv + self.hmarshift * (self.marhpx / 2), self.zlvl - data[0 + self.from * self.zoom] * self.wght * self.dcorr , locpointsize, 0, 2 * Math.PI);
self.ctx.stroke();
self.ctx.fill();
for (let i = 1; i < self.datalength; i++) {
self.ctx.beginPath();
self.ctx.arc(self.lvlv + (i * self.marhpx) / self.xdiv + self.hmarshift * (self.marhpx / 2), self.zlvl - data[i + self.from * self.zoom] * self.wght * self.dcorr, locpointsize, 0, 2 * Math.PI);
self.ctx.stroke();
self.ctx.fill();
}
}
}
// bar graph
function do_bar_graph(data, style) {
if (typeof data == "undefined") return; // if no data then exit
let w1 = 0;
let w2 = 0;
if (maxv != 0) { w1 = Math.abs( (self.zlvl - self.drt ) / maxv ); } else w1 = 0;
if (minv != 0) { w2 = Math.abs( (self.drb - self.zlvl) / minv ); } else w2 = 0;
self.wght = Math.max(w1, w2);
// Chart bar
self.ctx.strokeStyle = getStyle(style, "linecolor", self.barline);
self.ctx.fillStyle = getStyle(style, "fillcolor", self.barfill);
self.ctx.lineWidth = getStyle(style, "linewidth", self.barlw );
let locpoints = getStyle(style, "points" , self.barpoints ).toString() == "true";
let locpointsize = getStyle(style, "pointsize" , self.barpointsize);
let locpointcolor= getStyle(style, "pointcolor" , self.barpointcol );
if (getStyle(style, "pointcolor" , null) == null) locpointcolor = changeRGBAalpha(self.ctx.fillStyle, 0.3);
self.ctx.beginPath();
let xdef = false; // is data?
let wdh = self.marhpx * self.barperc; // place for all bars
let wob = wdh / self.barcnt; // width of one bar
let x = 0;
let y = 0;
for (let i = 0; i < self.datalength; i++) {
xdef = (typeof data[i + self.from * self.zoom] != "undefined") && (data[i + self.from * self.zoom] != null);
if (xdef) {
x = self.lvlv + ((i * self.marhpx) / self.xdiv) + (wob * self.barnr) + self.hmarshift * ((self.marhpx / 2) - (wdh / 2));
y = self.zlvl - (data[i + self.from * self.zoom] * self.wght * self.dcorr);
// draw rectangle
self.ctx.rect(x , y, wob, self.zlvl - y);
}
}
self.ctx.stroke();
self.ctx.fill();
self.ctx.closePath();
self.barnr++; // bar counter ++
// data big points
if (locpoints || self.allpoints) {
self.ctx.beginPath();
self.ctx.strokeStyle = changeRGBAalpha(locpointcolor, 1);
self.ctx.fillStyle = locpointcolor;
//self.ctx.arc(self.lvlv + self.hmarshift * (self.marhpx / 2), self.zlvl - data[0 + self.from * self.zoom] * self.wght * self.dcorr, locpointsize, 0, 2 * Math.PI);
self.ctx.stroke();
self.ctx.fill();
for (let i = 0; i < self.datalength; i++) {
self.ctx.beginPath();
x = self.lvlv + ((i * self.marhpx) / self.xdiv) + (wob * self.barnr) + self.hmarshift * ((self.marhpx / 2) - (wdh / 2) - wob/2);
y = self.zlvl - (data[i + self.from * self.zoom] * self.wght * self.dcorr);
self.ctx.arc(x, y, locpointsize, 0, 2 * Math.PI);
self.ctx.stroke();
self.ctx.fill();
}
}
}
// area graph
function do_area_graph(data, style) {
if (typeof data == "undefined") return; // if no data then exit
let w1 = 0;
let w2 = 0;
if (maxv!=0) { w1 = Math.abs( (self.zlvl - self.drt ) / maxv ); } else w1 = 0;
if (minv!=0) { w2 = Math.abs( (self.drb - self.zlvl) / minv ); } else w2 = 0;
self.wght = Math.max(w1, w2);
let loclinecolor = getStyle(style, "linecolor" , self.arealine );
let loclinewidth = getStyle(style, "linewidth" , self.areawidth );
let locfillcolor = getStyle(style, "fillcolor" , self.areafill );
let locpoints = getStyle(style, "points" , self.areapoints ).toString() == "true";
let locpointsize = getStyle(style, "pointsize" , self.areapointsize);
let locpointcolor = getStyle(style, "pointcolor" , self.areapointcol );
let locbeziercurve = getStyle(style, "beziercurve", self.beziercurve ).toString() == "true";
let locbezierconst = getStyle(style, "beziercnst" , self.beziercnst );
self.ctx.strokeStyle = loclinecolor;
self.ctx.fillStyle = locfillcolor;
self.ctx.lineWidth = loclinewidth;
if (getStyle(style, "pointcolor" , null) == null) locpointcolor = changeRGBAalpha(locfillcolor, 0.3);
let lastdef = false;
let x = 0; let x1 = 0;
let y = 0; let y1 = 0;
for (let i = 0; i < self.datalength; i++) {
let x0def = (typeof data[i + 0 + self.from * self.zoom] != "undefined") && (data[i + 0 + self.from * self.zoom] != null);
let x1def = (typeof data[i + 1 + self.from * self.zoom] != "undefined") && (data[i + 1 + self.from * self.zoom] != null);
if (i == self.datalength - 1) x1def = false;
x = self.lvlv + ((i * self.marhpx) / self.xdiv) + self.hmarshift * (self.marhpx / 2);
y = 0;;
if (x0def) y = self.zlvl - (data[i + self.from * self.zoom] * self.wght * self.dcorr);
else y = self.zlvl;
// first data
if (x0def && !lastdef) {
self.ctx.beginPath();
self.ctx.moveTo(x, self.zlvl);
self.ctx.lineTo(x, y);
}
// middle data
if (x0def && x1def) {
if (locbeziercurve) { //bezier curve
x1 = self.lvlv + ( ( (i + 1) * self.marhpx) / self.xdiv) + self.hmarshift * (self.marhpx / 2);
y1 = self.zlvl - (data[i + 1 + self.from * self.zoom] * self.wght * self.dcorr);
self.ctx.bezierCurveTo(x + self.marhpx / locbezierconst, y, x1 - self.marhpx / locbezierconst, y1, x1, y1);
}
else
self.ctx.lineTo(x, y);
}
// last data
if (x0def && !x1def) {
self.ctx.lineTo(x, y);
self.ctx.lineTo(x, self.zlvl);
self.ctx.stroke();
self.ctx.fill();
self.ctx.closePath();
self.ctx.beginPath();
}
lastdef = x0def;
}
// data big points
if (locpoints || self.allpoints) {
self.ctx.beginPath();
self.ctx.strokeStyle = changeRGBAalpha(locpointcolor, 1);
self.ctx.fillStyle = locpointcolor;
self.ctx.arc(self.lvlv + self.hmarshift * (self.marhpx / 2), self.zlvl - data[0 + self.from * self.zoom] * self.wght * self.dcorr , locpointsize, 0, 2 * Math.PI);
self.ctx.stroke();
self.ctx.fill();
for (let i = 1; i < self.datalength; i++) {
self.ctx.beginPath();
self.ctx.arc(self.lvlv + (i * self.marhpx) / self.xdiv + self.hmarshift * (self.marhpx / 2), self.zlvl - data[i + self.from * self.zoom] * self.wght * self.dcorr, locpointsize, 0, 2 * Math.PI);
self.ctx.stroke();
self.ctx.fill();
}
}
}
// stairs graph
function do_stairs_graph(data, style) {
if (typeof data == "undefined") return; // if no data then exit
let w1 = 0;
let w2 = 0;
if (maxv != 0) { w1 = Math.abs( (self.zlvl - self.drt ) / maxv ); } else w1 = 0;
if (minv != 0) { w2 = Math.abs( (self.drb - self.zlvl) / minv ); } else w2 = 0;
self.wght = Math.max(w1, w2);
let loclinecolor = getStyle(style, "linecolor" , self.linecol );
let loclinewidth = getStyle(style, "linewidth" , self.linewidth );
let loclinepoints = getStyle(style, "points" , self.linepoints ).toString() == "true";
let locpointsize = getStyle(style, "pointsize" , self.linepointsize);
let locpointcolor = getStyle(style, "pointcolor" , self.linepointcol );
if (getStyle(style, "pointcolor" , null) == null) locpointcolor = changeRGBAalpha(loclinecolor, 0.3);
// Chart line
self.ctx.strokeStyle = loclinecolor;
self.ctx.fillStyle = loclinecolor;
self.ctx.lineWidth = loclinewidth;
let lastdef = false;
let x = 0; let x1 = 0;
let y = 0; let y1 = 0;
for (let i = 0; i < self.datalength; i++) {
let x0def = (typeof data[i + 0 + self.from * self.zoom] != "undefined") && (data[i + 0 + self.from * self.zoom] != null);
let x1def = (typeof data[i + 1 + self.from * self.zoom] != "undefined") && (data[i + 1 + self.from * self.zoom] != null);
if (i == self.datalength - 1) x1def = false;
x = self.lvlv + ((i * self.marhpx) / self.xdiv) + self.hmarshift * (self.marhpx / 2);
if (x0def) y = self.zlvl - (data[i + self.from * self.zoom] * self.wght * self.dcorr);
else y = self.zlvl;
if (x1def) y1 = self.zlvl - (data[i + 1 + self.from * self.zoom] * self.wght * self.dcorr); else y1=y;
// first data
if (x0def && !lastdef && x1def) {
self.ctx.beginPath();
self.ctx.moveTo(x, y);
self.ctx.lineTo(x + self.marhpx , y);
}
// middle data
if (x0def && x1def) {
self.ctx.lineTo(x + self.marhpx , y);
self.ctx.lineTo(x + self.marhpx , y1);
}
// last data
if (x0def && !x1def ) {
if (i+1 < self.datalength) {
self.ctx.moveTo(x, y1);
self.ctx.lineTo(x + self.marhpx , y1);
}
self.ctx.stroke();
//self.ctx.fill();
self.ctx.closePath();
self.ctx.beginPath();
}
// true last data
if (x0def && !lastdef && !x1def ) {
if (i+1 < self.datalength) {
self.ctx.lineTo(x + self.marhpx , y);
}
else {
self.ctx.save;
self.ctx.strokeStyle = changeRGBAalpha(locpointcolor, 1);
self.ctx.fillStyle = locpointcolor;
self.ctx.beginPath();
self.ctx.arc(self.lvlv + (i * self.marhpx) / self.xdiv + self.hmarshift * (self.marhpx / 2), self.zlvl - data[i + self.from * self.zoom] * self.wght * self.dcorr, locpointsize, 0, 2 * Math.PI);
self.ctx.stroke();
self.ctx.fill();
self.ctx.restore;
}
}
lastdef = x0def;
}
// data big points
if (loclinepoints || self.allpoints) {
self.ctx.beginPath();
self.ctx.strokeStyle = changeRGBAalpha(locpointcolor, 1);
self.ctx.fillStyle = locpointcolor;
self.ctx.arc(self.lvlv + self.hmarshift * (self.marhpx / 2), self.zlvl - data[0 + self.from * self.zoom] * self.wght * self.dcorr , locpointsize, 0, 2 * Math.PI);
self.ctx.stroke();
self.ctx.fill();
for (let i = 1; i < self.datalength; i++) {
self.ctx.beginPath();
self.ctx.arc(self.lvlv + (i * self.marhpx) / self.xdiv + self.hmarshift * (self.marhpx / 2), self.zlvl - data[i + self.from * self.zoom] * self.wght * self.dcorr, locpointsize, 0, 2 * Math.PI);
self.ctx.stroke();
self.ctx.fill();
}
}
}
// make all charts ...
function do_graph() {
if (typeof self.style == "undefined") return;
for (let i = 0; i < self.data.length; i++) {
if (typeof self.style[i] == "undefined") continue;
}
for (let i = 0; i < self.data.length; i++) {
if (typeof self.style[i] == "undefined") continue;
if (getStyle(self.style[i], "type", "") == "line" ) do_line_graph (self.data[i], self.style[i]); else
if (getStyle(self.style[i], "type", "") == "area" ) do_area_graph (self.data[i], self.style[i]); else
if (getStyle(self.style[i], "type", "") == "bar" ) do_bar_graph (self.data[i], self.style[i]); else
if (getStyle(self.style[i], "type", "") == "stairs") do_stairs_graph(self.data[i], self.style[i]); else
;
}
}
function isBar (i) { return getStyle(self.style[i], "type", "") == "bar" ; }
function isArea(i) { return getStyle(self.style[i], "type", "") == "area"; }
function isLine(i) { return getStyle(self.style[i], "type", "") == "line"; }
// the beginning of the main procedure
this.datalength = this.alldatalength;
this.wght = 0; // calculated weight of Y data
this.barcnt = 0; // count bar charts
this.barnr = 0; // bar number for draw
this.marhpx = 0; // number px for X marker
// let start calculate ...
if ( Array.isArray(this.data) ) {
for (let i=0; i < this.data.length; i++ ) {
// for bars it's better
if ( isBar(i) ) { self.barcnt++; self.hmarshift = true; }
// get max & min of data array
rangev = getMinMax(this.data[i]); // get min & max of data
if (rangev.min < minv) minv = rangev.min; // max of data
if (rangev.max > maxv) maxv = rangev.max; // min of data
}
}
// additional margin
maxv += maxv * this.addmaxmarg;
minv += minv * this.addmaxmarg;
if (this.Ymax > 0) maxv = this.Ymax; // max Y constant value
if (this.Ymin < 0) minv = this.Ymin; // min Y constant value
// decimal points 0 .. 5
if ((this.decimalX < 0 ) || (this.decimalX > 5 )) this.decimalX = 0;
if ((this.decimalY < 0 ) || (this.decimalY > 5 )) this.decimalY = 0;
// initialize description X-axis table if not exists
if ((typeof this.desc == "undefined") || (this.desc == null) || (this.desc == 0) || (this.desc.length == 0) ) {
this.desc = [];
for (let i = 0; i < this.datalength; i++) this.desc.push( (i + 1) );
}
// main canvas clear
this.ctx.beginPath();
this.ctx.clearRect(0, 0, this.ctx.canvas.width, this.ctx.canvas.height);
// canvas fill color & width
this.ctx.fillStyle = this.canvasbkcol;
this.ctx.lineWidth = this.canvasfrw;
this.ctx.fillRect(0, 0, this.ctx.canvas.width, this.ctx.canvas.height);
this.ctx.stroke();
this.ctx.closePath();
this.ctx.strokeStyle = this.canvasfrcol;
if (this.drawzfrm) {
this.ctx.beginPath();
this.ctx.rect(0, 0, this.ctx.canvas.width, this.ctx.canvas.height);
this.ctx.stroke();
this.ctx.closePath();
}
// canvas start parameters
this.ctx.lineWidth = 1;
this.ctx.font = this.descfontmod + this.descfpx + this.descfont;
this.ctx.lineCap = "round";
this.ctx.lineJoin = "miter";
this.ctx.miterLimit = 1;
this.dcorr = 1;
this.aYtxt = self.axisYtxt.toString();
// if the data plus is very small, calculate the multiplier and show it on the Y axis.
let tm = Math.abs( Math.max( Math.abs(maxv), Math.abs(minv) ) );
if ( ( tm * Math.pow(10, this.decimalY ) ) < 1) {
let tw = 1;
let ti = 0;
if (tm > 0)
while (tm * tw < 10) {
tw *= 10; ti++;
if (ti > 10) break; // STOP: 10 it is very much! (10^10)
}
if (ti > 0) {
this.aYtxt = ("*(10^-" + ti + ")") + self.axisYtxt.toString();
minv *= tw;
maxv *= tw;
this.dcorr = tw;
}
if (this.decimalY == 0) this.decimalY = 2; //ti;
}
// if the data plus is very big, calculate the divider and show it on the Y axis.
tm = Math.abs( Math.max( Math.abs(maxv), Math.abs(minv) ) );
if ( tm > this.bigmax) {
let tw = 1;
let ti = 0;
while ( (tm / tw) > this.bigmax) {
tw = tw * 10; ti++;
if (ti > 10) break; // STOP: 10 it is very much! (10^10)
}
if (ti > 0) {
this.aYtxt = ("*(10^" + ti + ")") + self.axisYtxt.toString();
minv = minv / tw;
maxv = maxv / tw;
this.dcorr = 1 / tw;
}
this.decimalY = 0;
}
// for zoom
if (!this.zoom) this.datalength = this.alldatalength;
else this.datalength = this.to - this.from;
// level zero shift (from min, max) only if Y+ & Y- data exists, else 0 or (drb-drt)
if ( (maxv >= 0) && (minv == 0) ) this.zerox = 0; // only Y+
if ( (minv < 0) && (maxv == 0) ) this.zerox = this.drb - this.drt; // only Y-
if ( (maxv > 0) && (minv < 0) ) this.zerox = Math.abs( ( minv/(maxv - minv) ) * this.drv ) // Y+ & Y-
this.zlvl = this.drb - this.zerox; // level zero horizontal axis
if (this.datalength==0) this.zlvl = this.drb; // if no data then zero level
// check if the description of the x-axis is on the canvas
let marwxdesc = 0;
let maxw = 0;
// find max width of X-axis description
for (let i = this.from * this.zoom; i < this.desc.length; i++) {
maxw = this.ctx.measureText( this.desc[i].toLocaleString(undefined, {useGrouping: this.dgroup, minimumFractionDigits: this.decimalX, maximumFractionDigits:this.decimalX}) ).width;
if (maxw > marwxdesc) marwxdesc = maxw;
}
// if descritions X-axis heigth > room we have to change the drawing area and move X-axis up
if ((this.drb - this.zlvl) < marwxdesc) {
//this.top = this.margv + marwxdesc - this.zerox; // new bottom margin
//this.bot = this.ctx.canvas.clientHeight - this.margv - marwxdesc; // new bottom coordinate
this.drb = this.bot - this.marsize / 2 - marwxdesc - this.descfpx / 2; // new bottom draw coordinate
this.drv = this.drb - this.drt; // new draw height
// new level zero shift
if ((maxv > 0) && (minv < 0)) this.zerox = Math.abs( ( minv/(maxv - minv) ) * this.drv ) // Y+ & Y-
else if (maxv >= 0) this.zerox = 0; // only Y+
else if (minv < 0) this.zerox = this.drb - this.drt; // only Y-
this.zlvl = this.drb - this.zerox; // new level zero horizontal axis
}
// count max markers (from font pixels)
let marpl = Math.round( Math.abs( (this.zlvl - this.drt) / this.descfpx ) ); // max markers plus max counter