-
Notifications
You must be signed in to change notification settings - Fork 4
/
ccchart.js
5268 lines (4863 loc) · 191 KB
/
ccchart.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
// utf-8 width module ccchart.m.cssHybrid http://ccchart.com/
/* eslint-disable */
window.ccchart =
(function(window) {
return {
aboutThis: {
name: 'ccchart',
version: '1.12.088',//for ccy
update: 20180612,
updateMemo: 'http://ccchart.com/update.json',
license: 'MIT',
memo: 'This is a Simple and Realtime JavaScript chart that does not depend on libraries such as jQuery or google APIs.',
demoCurrent: 'http://ccchart.com/',
demoDevelopment: 'http://ccchart.org/',
writer: 'Toshiro Takahashi @toshirot',
see: 'http://www.facebook.com/javascripting',
blog: 'https://web.archive.org/web/20160419064517/http://ngw.jp/~tato/wp/?cat=6',
branched: ''
+ 'This project has branched from the jQchart, that made by Toshiro Takahashi.'
+ 'jQchart is the jQuery plugin. since 2007. '
+ 'http://archive.plugins.jquery.com/project/jQchart',
plugins:[]
},
m: [], //for plugins
init: function init(id, op, callback) {
//ccchartの初期化
this.drawing = false;
var _c;
if (('' + id.nodeName).toLowerCase() === 'canvas') {
this.canvas = id;
this.id = id.id;
} else if(_c = document.getElementById(id)){
this.canvas = _c;
this.id = id;
} else {
throw new Error(''
+ 'Please check the ID. For example, '
+ 'spell, or single-byte and double-byte is different'
);
}
if (typeof this.ids !== 'object') { //1th
this.ids = []; //this.ids[id] canvas element by dom id
this.cxs = []; //this.cxs[id] ctx by dom id
this.coj = this.ops = []; //v1.11.07 renamed from ops to coj this.coj[id] is the 'this' of the id's ccchart oj.
this.gcf = this.gcf || {}; //gloval config options
this.wses = []; //廃止予定
//websocket lists e.g. ccchart.wses['-ccchart-ws-'+id+'-'+url]
this.wsuids = [];
//websocket lists e.g. ccchart.wses['-ccchart-ws-'+uuidv4]
this.wsidoj = {};
//kye is id, value is websocket oj e.g. {'hoge1': wsoj, 'hoge2': wsoj}
this.wsReCnt = []; //WebSocket re connect counter
// e.g. ccchart.wsReCnt['-ccchart-ws-'+id+'-'+url]
this.idsLen = 0;
//this.defaultZindex = 0; //canvasのzIndex renamed from maxZindex is now option
this.moveDly = 0; //move2 delay 初期値
this.moveStack = [];
this.moveStackDly = [];
this.cssgs = []; //CSS group lists
this.cssTooltips = []; //CSS cssTooltips
}
//this.ops[this.id]が既に存在していて
if(this.ops[this.id]){
//configが同じ場合
if(this.ops[this.id].op.config===op.config){
//heatmapの時には軸線描画等をパスする為にフラグをtrueにセットする
this.ops[this.id].secondTime = true;
}
}
if(typeof this._addsFlg === 'undefined'){
this._addsFlg = 0; //undefined is before the init Method or drew the add Method
//0 is set in the init Method
//-1 is set in the add Method
//1 is first chart on use add method
//2 is second chart on use add method
}
for (var i in window.ccchart.m) {
var isModule = !! i;
break;
};
if (isModule){
for (var i in this.m) {
if(this.m[i]){
this.moduleExtend(this, this.m[i], i, this.m[i].update);
}
}
}
this.callback = callback;
this.idsLen++;
var _curr_classNames =this.canvas.getAttribute('class');
var _classNames = _curr_classNames?' '+_curr_classNames:'' ;
if(_classNames.indexOf('-ccchart')===-1){
this.canvas.setAttribute('class', '-ccchart' +_classNames);
}
this.ids[this.id] = this.canvas;
this.ctx =
this.cxs[this.id] =
this.canvas.getContext('2d');
if(!op.data)op.data = op.data || [[""],[""]];
this.coj[this.id] = this.coj[this.id] || [];
//unload reset. doesn't work on chrome
if (window.ccchart['_added_unloadEvent'] !== 'on') {
window.addEventListener('unload', function () {
window.ccchart = null;
});
window.ccchart['_added_unloadEvent'] = 'on';
}
//css-hybridリセット //あとで修正
var hybrid = document.getElementById('-ccchart-css-hybrid');
if(hybrid){
//共通のキャンバスで書き換えるflipやws時にgroupbox配下をリセットする 20130413 thanx nohinaさん
var cssgbox = document.getElementById('-ccchart-css-groupbox');
var cssg = document.getElementById('-ccchart-css-group-'+this.id)
if(cssgbox && cssg) cssgbox.removeChild(cssg);
}
this.loadBranch(op, callback);
return this;
},
preProcessing: function (op, callback) {
//事前セッティング
if (!op) op = {};
if (!this.gcf) this.gcf = {}
if (!op.config) op.config = {};
this.deep = op.config.deep || this.gcf.deep || 'yes';
if(this.deep==='yes')op = this.util.deepJSONCopy(op);//deep copy
this.op = op;
this.display = op.config.display || this.gcf.display || this.canvas.style.display || 'block';
this.canvas.style.display = this.display;
this.width = this.util.setConfigNum(this, 'width', this.op.config.width, this.gcf.width, 600)
this.height = this.util.setConfigNum(this, 'height', this.op.config.height, this.gcf.height, 400)
this.bg = op.config.bg || this.gcf.bg || undefined;
this.bgGradient = op.config.bgGradient || this.gcf.bgGradien || '';
if (this.bgGradient === '' && this.bg === undefined) {
this.bgGradient = {
direction: "vertical", //vertical|horizontal
from: "#687478",
to: "#222"
};
}
//グラフの種類 折れ線(line) または 棒(bar)など swtGraph参照
this.type = op.config.type || this.gcf.type || "line";
//barとstacked チャートのツールチップはちょっと特別ね
if(
(op.config.useToolTip === 'yes' ||this.gcf.useToolTip === 'yes')
&& (this.type === 'bar' || this.type === 'stacked')){
op.config.useMarker = 'css-maru';
//バーチャートのツールチップアンカーの色
this.barTipAnchorColor =
this.op.config.barTipAnchorColor || this.gcf.barTipAnchorColor || 'rgba(0,0,0,0.7)';//'red'
}
//CSSツールチップ
this.useToolTip = op.config.useToolTip || this.gcf.useToolTip || 'no';
//CSSを使うかどうか
this.useCss = op.config.useCss || this.gcf.useCss || 'no';
//データマーカーを描画するか //none|arc|ring|maru|css-ring|css-maru
this.useMarker = op.config.useMarker || this.gcf.useMarker || 'none';
//データマーカーがcss-ringやcss-maruなら自動的にuseCss = 'yes' 除くpie
if(this.useMarker === 'css-ring' || this.useMarker === 'css-maru'){
if(!(this.type === 'pie' )){
this.useCss = 'yes';
this.useToolTip = 'yes';
}
} else {
// css-maruかcss-ringが未指定でもツールチップを使うならuseCss='yes'でcss-maruを使う
if(this.useToolTip === 'yes'){
if(!(this.type === 'pie' )){
this.useCss = 'yes';
}
this.useMarker = 'css-maru';
}
}
//pieにマーカーは使わない
if(this.type === 'pie')this.useMarker = 'none';
//ツールチップカスタマイズ
if(this.useToolTip === 'yes')
this.tmpToolTip = op.config.tmpToolTip || this.gcf.tmpToolTip;
//for drawLine, drawAmplitude
//線幅 デフォルトで2
this.lineWidth =
this.util.setConfigNum(this, 'lineWidth', this.op.config.lineWidth, this.gcf.lineWidth, 2);
//線幅セット
this.lineWidthSet = this.util.setLineWidthSet(this, op);
//マーカーの幅または直径
this.markerWidth = this.util.setConfigNum(this, 'markerWidth', this.op.config.markerWidth, this.gcf.markerWidth)
this.markerWidth = this.util.setConfigNum(this, 'markerWidth', this.markerWidth, this.lineWidth * 2 , 2)
if(this.useCss === 'yes'){
this.bind('scroll', '_adjustcsspos');
this.bind('load', '_adjustcsspos');
this.bind('resize', '_adjustcsspos');
}
//canvasのzIndex
this.defaultZindex = this.util.setConfigNum(this, 'defaultZindex', this.op.config.defaultZindex, this.gcf.defaultZindex, 0)
//チャートデータの列と行を変換するか
this.changeRC = op.config.changeRC || this.gcf.changeRC || 'no';
if(this.changeRC !== 'no'){
op.data = this.util.changeRowsAndCols(op.data);
}
//作業用データ
this.wkdata = op.data || [];
/*[
["Year", 2007, 2008, 2009, 2010, 2011, 2012, 2013],
["Tea", 435, 332, 524, 688, 774, 825, 999],
["Coffee", 600, 335, 584, 333, 457, 788, 900],
["Juice", 60, 435, 456, 352, 567, 678, 1260],
["Oolong", 200, 123, 312, 200, 402, 300, 512]
];*/
//データ配列の1行目を項目名とする(デフォルトはtrue)
// scatter では項目名ではないが、凡例type識別用に使う
this.useFirstToColName =
(op.config.useFirstToColName === false)?false:
((this.gcf.useFirstToColName === false)?false:true);
this.useFirstToColName =
(this.useFirstToColName === false) ?
((this.type === 'scatter') ? true : false) : true;
if(this.type === 'heatmap')this.useFirstToColName = false;
if (this.wkdata.length === 1) this.useFirstToColName = false;
//データ配列の1列目を項目名とする(デフォルトはtrue)
this.useFirstToRowName =
(op.config.useFirstToRowName === false)?false:
((this.gcf.useFirstToRowName === false)?false:true);
this.useFirstToRowName =(this.useFirstToRowName === false) ?
false : true;
//useFirstToColNameがtrueなら
//最初の行をカラム項目名として抜き出す
this.colNames =
(this.useFirstToColName === true) ?
this.wkdata.slice(0, 1)[0] : '';
this.wkdata = this.data =
(this.useFirstToColName === true) ?
this.wkdata.slice(1) : this.wkdata;
//useFirstToRowNameがtrueなら
//各行の最初の列を行タイトルとして抜き出す
this.rowNames = [];
this.data = [];
if (this.useFirstToRowName === true) {
//最初の列が項目列なら
this.colNamesTitle = this.colNames.slice(0, 1)[0] || "";
this.colNames = this.colNames.slice(1);
for (var i = 0; i < this.wkdata.length; i++) {
this.rowNames.push(this.wkdata[i][0]);
this.data.push(this.wkdata[i].slice(1));
}
} else {
//最初の列が項目列でなければ
this.colNamesTitle = "";
if(this.useFirstToColName === true){
//最初の行が項目行なら1列目を取り出さない
} else {
this.colNames = this.colNames.slice(1);
}
for (var i = 0; i < this.wkdata.length; i++) {
this.rowNames.push("");
this.data.push(this.wkdata[i]);
}
}
/* 上記 「データ配列の1行目」と「1列目」を項目名として取り出した結果、
先頭行と列を除いたdataおよび、次のプロパティと配列が作成されます。
X軸ラベルのタイトル ccchart.colNamesTitle //"年度"
X軸ラベルの配列 ccchart.colNames
//[2007, 2008, 2009, 2010, 2011, 2012, 2013]
Y軸ラベルの配列 ccchart.rowNames
//["紅茶", "コーヒー", "ジュース", "ウーロン"]
*/
this.hanreiNames = this.rowNames;
this.useRow0thToHanrei =
op.config.useRow0thToHanrei || this.gcf.useRow0thToHanrei || 'none';
if (this.type === 'scatter' || this.type === 'heatmap') {
this.colNamesTitle = this.rowNames[0] || ''; //X軸タイトル
this.rowNamesTitle = this.rowNames[1] || ''; //Y軸タイトル
}
if (this.type === 'heatmap') {
this.hanreiNames = '';
}
if (this.type === 'scatter') {
if (this.useRow0thToHanrei === 'yes'){
this.hanreiNames = '';
} else {
this.hanreiNames =
this.util.uniq(this.colNames.slice(0));
//hanrei凡例リスト 修正 20130413 Thanx piyoさん
//2014/5/2またslice(1)に戻っていたのでv1.08.5 で 再修正
}
}
//データ行数
this.dataRowLen = this.data.length;
//データ列数
this.dataColLen = this.data[0].length;
//WebSocket受信時の最大データ列数
this.maxWsColLen = this.util.setConfigNum(this, 'maxWsColLen', this.op.config.maxWsColLen, this.gcf.maxWsColLen, 10);
//this.wsKeepAlive = this.op.config.wsKeepAlive || this.gcf.wsKeepAlive || true;
//WebSocket詳細デバッグ用フラグ
this.wsDbg = this.op.config.wsDbg || false;
//WebSocket接続/切断等info出力用フラグ
this.wsInfo = this.op.config.wsInfo || false;
//データのnumber化
var _data =[];
for(var i = 0;i< this.data.length; i++){
_data[i]=[];
for(var j = 0; j<this.data[i].length; j++){
//nullなどは0にし、三桁カンマは除去
_data[i][j] = parseFloat((''+this.data[i][j]).split(',').join('')||0)
}
}
this.data = _data;
//Y軸目盛のパーセント表示を行うかどうか
this.yScalePercent =
this.op.config.yScalePercent || this.gcf.yScalePercent || 'no';
//for drawStacked%
if ((this.type === 'stacked%' || this.type === 'pie')
&& this.op.config.percentVal !='no' )this.op.config.percentVal = 'yes';
this.percentVal =
this.op.config.percentVal || this.gcf.percentVal || 'no';
//for drawTitle, drawSubTitle
//タイトル文字列
this.title = op.config.title || this.gcf.title || "";
this.subtitle =
op.config.subTitle || op.config.subtitle || this.gcf.subTitle || "";
//タイトルをcanvasにセットする
this.canvas.setAttribute('title', this.title);
//凡例を使うかどうか
this.useHanrei =
op.config.useHanrei || op.config.useHanrei || this.gcf.useHanrei || "yes";
if(this.useFirstToRowName === false) this.useHanrei = "no";
if(this.type === 'candle') this.useHanrei = "no";
if(this.type === 'heatmap') this.useHanrei = "no";
//X軸目盛値(下の目盛)を表示するか?
this.useXscale =
this.op.config.useXscale || this.gcf.useXscale || "yes";
//Y軸目盛値(左右の目盛)を表示するか?
this.useYscale =
this.op.config.useYscale || this.gcf.useYscale || "yes";
//チャートのみを表示(タイトル,サブタイトル,X軸Y軸目盛値無し) no|yes
this.onlyChart = op.config.onlyChart || this.gcf.onlyChart || "no";
this.onlyChartWidthTitle =
op.config.onlyChartWidthTitle || this.gcf.onlyChartWidthTitle || "no";
this.paddingDefault = op.config.paddingDefault || this.gcf.paddingDefault || 10;
if (this.onlyChartWidthTitle === 'yes') {
this.paddingBottomDefault =
this.paddingLeftDefault =
this.paddingRightDefault = this.paddingDefault;
this.paddingTopDefault = 90;
this.onlyChart = 'yes';
} else if (this.onlyChart === 'yes') {
this.paddingTopDefault =
this.paddingBottomDefault =
this.paddingLeftDefault =
this.paddingRightDefault = this.paddingDefault;
} else {
this.paddingTopDefault = 90;
if (this.title === '')
this.paddingTopDefault = this.paddingTopDefault - 30;
if (this.subtitle === '')
this.paddingTopDefault = this.paddingTopDefault - 15;
this.paddingBottomDefault = 40;
this.paddingLeftDefault = 110;
if (this.yScalePercent === 'no')
this.paddingLeftDefault = 70;
this.paddingRightDefault = 120;
if(this._addsFlg > 0){
//addメソッド時。通常は this.paddingDefaultで10
this.paddingRightDefault = 160;
}
}
//複合チャートではない場合にXY軸値を使わないときはパディング調整する 複合チャートは大変なのでパス
if(this._addsFlg === 0){
if(this.useXscale !== 'yes'){
this.paddingBottomDefault = this.paddingDefault +20;
}
if(this.useYscale !== 'yes'){
this.paddingLeftDefault = this.paddingDefault +20;
}
}
if(this.useHanrei !== 'yes'){//candle とheatmapは 凡例が無い
this.paddingRightDefault = 40;
if(this._addsFlg > 0){
this.paddingRightDefault = 110;
this.paddingRightDefault = this.paddingLeftDefault;
}
}
if (this.type === 'heatmap') {
this.paddingBottomDefault = 55;
}
//データ値を表示
this.useVal = op.config.useVal || this.gcf.useVal || 'no'; //yes|no
if (this.type === 'stacked%' &&
this.useVal === 'yes') {
this.paddingTopDefault = 90;
}
//for drawAxisX, drawAxisY, etc...
//グラフ領域のパディング
this.paddingTop =
op.config.paddingTop || this.gcf.paddingTop || this.paddingTopDefault;
this.paddingBottom =
op.config.paddingBottom || this.gcf.paddingBottom || this.paddingBottomDefault;
this.paddingLeft =
op.config.paddingLeft || this.gcf.paddingLeft|| this.paddingLeftDefault;
this.paddingRight =
op.config.paddingRight || this.gcf.paddingRight || this.paddingRightDefault;
//グラフ領域の幅
this.chartWidth =
this.width - this.paddingLeft - this.paddingRight;
//グラフ領域の高さ
this.chartHeight =
this.height - this.paddingTop - this.paddingBottom;
//グラフ領域の上端
this.chartTop = this.paddingTop;
//グラフ領域の下端
this.chartBottom = this.height - this.paddingBottom;
//グラフ領域の左端
this.chartLeft = this.paddingLeft;
//グラフ領域の右端
this.chartRight = this.width - this.paddingRight;
//端数処理桁数の指定(整数部または小数第n桁以下を四捨五入で丸めるための桁数 int)
//小数点以下桁数を指定する
this.roundDigit = this.op.config.roundDigit;
if(this.roundDigit === undefined)this.gcf.roundDigit;
if(typeof this.roundDigit === 'number'){ //2, 0, 2.5, -3など
this._useDecimal = 'yes';//2, 0, -3など
this.roundDigit = parseInt(this.roundDigit);//桁数なので整数化
} else {//undefined, null, '', [2], "2"
//configに指定がない場合
if(this.roundDigit === undefined){
if(JSON.stringify(this.data).indexOf('.') !== -1){
this._useDecimal = 'yes'; //configに指定がないけど、少数値が有れば yes。
this.roundDigit = 3;//デフォルト値 0.12345は0.123 1や0.1や0.12 は、1や0.1や0.12。
} else {
this._useDecimal = 'no'; //configに指定がなければ no。
this.roundDigit = 0;
}
} else {
this._useDecimal = 'no'; //configに指定がなければ no。文字列の数字'2'もダメ
}
}
//Y軸の天地を反転する デフォルトは 降順'DESC' 昇順'ASC' line,bezi2,bezi のみ
this.yScaleOrder =
this.op.config.yScaleOrder || this.gcf.yScaleOrder || 'DESC';
//X目盛を小数点有りにするか?
this.xScaleDecimal = this._useDecimal;
//Y目盛を小数点有りにするか?
this.yScaleDecimal = this._useDecimal;
//デフォルトはここではまだundefined
this.minY = this.util.setConfigNum(this, 'minY', this.op.config.minY, this.gcf.minY)
this.maxY = this.util.setConfigNum(this, 'maxY', this.op.config.maxY, this.gcf.maxY)
this.minX = this.util.setConfigNum(this, 'minX', this.op.config.minX, this.gcf.minX)
this.maxX = this.util.setConfigNum(this, 'maxX', this.op.config.maxX, this.gcf.maxX)
//maxY minY maxX minX の初期値は以降で設定
this.maxYDefault = this.maxXDefault = 10;
this.minYDefault = this.minXDefault = 0;
//scatter heatmap時、maxY と maxX minX
if (this.type === 'scatter' || this.type === 'heatmap') {
//maxY minY
if (typeof this.maxY === 'number') {
this.maxY = this.maxY;
} else {
this.maxY = this.util.setConfigNum(this, 'maxY', _getSatterMax(this, 1), this.maxYDefault)//index0 is X, 1 is Y
}
//データ最大値this.maxYの切り上げ処理 デフォルトmaxYの1/10桁
if(this.yScaleDecimal !== 'yes')
_setRoundedUpMax(this, 'maxY', 'roundedUpMaxY');
if (typeof this.minY === 'number') {
this.minY = this.minY;
} else {
this.minY = this.util.setConfigNum(this, 'minY', _getSatterMin(this, 1), this.minYDefault)//index0 is X, 1 is Y
}
//maxX minX
if (typeof this.maxX === 'number') {
this.maxX = this.maxX;
} else {
this.maxX = this.util.setConfigNum(this, 'maxX', _getSatterMax(this, 0), this.maxXDefault)//index0 is X, 1 is Y
}
if(this.xScaleDecimal !== 'yes')
_setRoundedUpMax(this, 'maxX', 'roundedUpMaxX');
if (typeof this.minX === 'number') {
this.minX = this.minX || 0;
} else {
this.minX = this.util.setConfigNum(this, 'minX', _getSatterMin(this, 0), this.minXDefault)//index0 is X, 1 is Y
}
function _getSatterMin(it, index){ //index0 is X, 1 is Y
var wk = it.util.hCopy(it.data[index]); //ハードコピー data[1]はY
return wk.sort(function (b, a) {
return b - a
})[0];
}
function _getSatterMax(it, index){ //index0 is X, 1 is Y
var wk = it.util.hCopy(it.data[index]); //ハードコピー data[1]はY
return wk.sort(function (a, b) {
return b - a
})[0];
}
} else {
//scatter heatmap 以外のタイプ
//Yデータの最大値maxYを求めるfor drawYscale
if (typeof this.maxY === 'number') {
this.maxY = this.maxY;
} else {
this.maxY = this.util.setConfigNum(this, 'maxY', _getMax(this, 'maxY'), this.maxYDefault)
}
//maxY 100230 などの時に110000 など大きくなりすぎるので一時停止2017/3/25
//データ最大値this.maxYの切り上げ処理 デフォルトmaxYの1/10桁
//if(this.yScaleDecimal !== 'yes')
// _setRoundedUpMax(this, 'maxY', 'roundedUpMaxY');
//データの最小値を求める
if (typeof this.minY === 'number') {
this.minY = this.minY;
} else {
this.minY = this.util.setConfigNum(this, 'minY', _getMin(this, 'minY'), this.minYDefault)
}
function _getMin(that, prop){
//データの最小値を求める
if( that.type==='line' ||
that.type==='bar' ||
that.type==='candle' ||
that.type==='bezi2' ||
that.type==='bezi' ||
that.type==='area' ||
that.type==='scatter' ||
that.type==='heatmap' ||
that.type==='ampli'){
return that.util.getMin(that);
} else if(that.type === 'stacked'){
//stackedはマイナス方向の積み上げ最小値
return that.util.getMinSum(that);
//※stacked%のminYは0%, stackedareaはいま自動にしてないけどどうする?
}
}
function _getMax(that, prop){
//データの最大値を求める
if(that.type === 'stacked' ||
that.type === 'stackedarea' ||
that.type === 'stacked%'){
//積み重ねた時の最大値 ※stackedはプラス方向の積み上げ最大値
return that.util.getMaxSum(that);
} else {
return that.util.getMax(that);
}
}
}
//これおかしい? 2017/3/25
function _setRoundedUpMax(that, max, roundedUp) {
//データ最大値this[maxY|maxX]の切り上げ処理 デフォルトmaxYまたはmaxXの1/10桁
if (that.yScalePercent === 'no') {
var _rumy = Math.pow(10, ("" + that[max]).split('.')[0].length - 2);
//デフォルトmaxY|maxXの1/10桁
that[roundedUp] =
(op.config[roundedUp] !== undefined) ?
op.config[roundedUp] : _rumy;
if (that[roundedUp] !== 0) {
that[max] = Math.ceil(
that[max] / that[roundedUp]) * that[roundedUp];
} else {
that[max] = that[max];
}
}
}
//水平目盛り線AxisXの本数
if(typeof this.op.config.axisXLen === 'number'){
this.axisXLen = this.op.config.axisXLen;
} else {
if(typeof this.gcf.axisXLen === 'number'){
this.axisXLen = this.gcf.axisXLen;
} else {
this.axisXLen = 10; //default
//Y目盛を小数点無しで (this.maxY - this.minY) < axisXLen なら
//水平目盛り線の数を (this.maxY - this.minY +1)まで減らす
if(this.yScaleDecimal === 'no'){
//if((this.maxY - this.minY) < this.axisXLen) this.axisXLen = parseInt((this.maxY - this.minY+1));
if((this.maxY - this.minY) < this.axisXLen) this.axisXLen = parseInt((this.maxY - this.minY));
}
}
}
//水平目盛線1本当たりのラベル値
this.yGapValue = (this.maxY - this.minY) / (this.axisXLen);
//yGapValueが端数なら小数1位で_useDecimalを再設定
//if((''+this.yGapValue).indexOf('.') !== -1 && this.roundDigit !== undefined){
//Y目盛を小数点有りで1位にする
// this.yScaleDecimal = this._useDecimal = 'yes';
// this.roundDigit = 1;
//}
//値1当たりの高さ
this.unitH = this.chartHeight / (this.maxY - this.minY);
if(isFinite(this.unitH) === false){
this.unitH = this.chartHeight;
//0で割るとInfinityになってしまうのでY値の変化がない場合のように0ならchartHeightにしておく
}
if (this.type === 'stacked%') this.unitH = this.chartHeight / 100;
//ラベルの値初期値
this.wkYScale = this.minY;
//for drawAxisX
//水平目盛線用
this.yGap = this.chartHeight / (this.axisXLen);
//for drawAxisY
//垂直目盛線用
this.axisYLen = this.dataColLen//本数
this.xGap = (this.chartWidth)/this.axisYLen ;//目盛線の間隔
//X軸の色 Y軸の色
this.xColor = op.config.xColor || this.gcf.xColor || 'rgba(180,180,180,0.3)';
this.yColor = op.config.yColor || this.gcf.yColor || 'rgba(180,180,180,0.3)';
//scatter heatmap時の、axisYLen xGap xGapValue unitW wkXScale
if (this.type === 'scatter' || this.type === 'heatmap') {
//for drawAxisY for scatter
//scatter時の垂直目盛線用
this.axisYLen =
this.op.config.axisYLen || this.gcf.axisYLen || 10; //本数
this.xGap = (this.chartWidth) / this.axisYLen; //目盛線の間隔
//垂直目盛線1本当たりのラベル値
this.xGapValue = parseFloat((this.maxX - this.minX) / this.axisYLen, 10);
//値1当たりの幅
this.unitW = this.chartWidth / (this.maxX - this.minX);
//ラベルの値初期値
this.wkXScale = this.minX;
}
//位置記録用readonly psition leftとtopを返す axisYsはyTitleでその列のタイトルも返す
this.axisYs = [];
//[{left: 70, yTitle: "年月"},{left: 206.66666666666666,yTitle: 2014},{left: 343.3333333333333,yTitle: 2015},...]
this.axisXs = [];
//[{top: 360}, {top: 333},{top: 306}]
//for drawLine, drawHanrei
//カラーセット
this.colorSet = op.config.colorSet || this.gcf.colorSet ||
["red","#FF9114","#3CB000","#00A8A2","#0036C0","#C328FF","#FF34C0",
"#F33","#FB4","#3E3","#0EE","#07E","#C7F","#F7E",
"#F66","#FD7","#3F7","#3EE","#0AE","#CAF","#FAF",
"#F99","#FEA","#3FF","#4FF","#0DF","#DCF","#FDF"];//default 28color
//文字列カラー
this.textColor = op.config.textColor || this.gcf.textColor || false;
this.textColors = op.config.textColors || this.gcf.textColors ||{
"title" : "#ccc",
"subTitle": "#ddd",
"x": "#aaa",
"y": "#aaa",
"hanrei": "#ccc",
"unit": "#aaa",
"memo": "#ccc"
}
if(this.textColors){
this.textColors.all = this.textColors.all || undefined;
} else if(this.gcf.textColors){
this.textColors.all = this.gcf.textColors.all || undefined;
}
if(this.type === 'heatmap'){
this.hm_grad = op.config.hm_grad || this.gcf.hm_grad || undefined;
this.innerCircle =
this.util.setConfigNum(this, 'innerCircle', this.op.config.innerCircle, this.gcf.innerCircle, 1);
this.outerCircle=
this.util.setConfigNum(this, 'outerCircle', this.op.config.outerCircle, this.gcf.outerCircle, 30);
}
//for drawHanrei
//凡例マーカーの形 arc|rect
this.hanreiMarkerStyle =
op.config.hanreiMarkerStyle || this.gcf.hanreiMarkerStyle || 'arc';
//円グラフのドーナツ穴の半径
this.pieHoleRadius =
this.util.setConfigNum(this, 'pieHoleRadius', this.op.config.pieHoleRadius, this.gcf.pieHoleRadius, 40);
//円グラフのドーナツ幅
this.pieRingWidth =
this.util.setConfigNum(this, 'pieRingWidth', this.op.config.pieRingWidth, this.gcf.pieRingWidth, 40);
//棒グラフ用パラメータ
this.barWidth =
this.util.setConfigNum(this, 'barWidth', op.config.barWidth, this.gcf.barWidth, 10);
this.barPadding = (op.config.barPadding!==undefined)?op.config.barPadding:
(this.gcf.barPadding!==undefined)?this.gcf.barPadding:
undefined;
this.barGap = op.config.barGap || this.gcf.barGap || 1;
//単位を表示
this.unit = op.config.unit || this.gcf.unit || '';
//キャンバス回転エフェクトの方向 x|y
this.flipDirection =
op.config.flipDirection || this.gcf.flipDirection || 'x';
//X軸にカスタムXライン-を引き、そのY値を表示する
this.xLines = op.config.xLines || this.gcf.xLines || 'none' ||[{
"color":"rgba(204,153,0,0.7)", //ライン色
"width":"1", //ライン幅
"val":0, //Y値
"vColor":"rgba(204,153,0,0.7)",//値色
"xOffset": 2, //X方向オフセット
"yOffset": 4, //Y方向オフセット
"fillOver": null, //ライン上の塗り色
"fillUnder": null, //ライン下の塗り色
"keep": "no" //最大値か最小値で位置をキープするか?"high" || "low" || (default)"no"
}];
//memo
this._memo = op.config.memo || this.gcf.memo || null;
//image
this._img = op.config.img || this.gcf.img || null;
this.imgAlpha = op.config.imgAlpha || this.gcf.imgAlpha || 1;
//影を付けるかどうか
this.useShadow = op.config.useShadow || this.gcf.useShadow || 'yes';
if(this.useShadow === 'yes'){
this.shadows = op.config.shadows || this.gcf.shadows ||{
"hanrei" : ['#222', 5, 5, 5],
"xline": ['#444', 7, 7, 5],
"line": ['#222', 5, 5, 5],
"bar": ['#222', 5, 5, 5],
"stacked": ['#222', 5, -5, 5],
"stackedarea": ['#222', 5, 5, 5],
"bezi": ['#222', 5, 5, 5],
"bezi2": ['#222', 5, 5, 5],
"scatter": ['#222', 5, 5, 5],
"heatmap": ['#222', 5, 5, 5],
"pie": ['#444', 3, 3, 3]
}
if(this.shadows)
this.shadows.all = this.shadows.all ||
(this.gcf.shadows?this.gcf.shadows.all:undefined) || undefined;
}
//forCSS prifix
this.ua = navigator.userAgent.toLowerCase();
this.prefix = this.ua.match(/webkit/)?'-webkit':
this.ua.match(/firefox/)?'-moz':
this.ua.match(/opera/)?'-o':'-ms';
this.pfx =[];
this.util.setPfx('transform'); //this.pfx['transform']
this.util.setPfx('transform-origin'); //this.pfx['transform-origin']
this.util.setPfx('transition'); //this.pfx['transition']
this.util.setPfx('box-sizing'); //this.pfx['box-sizing']
this.borderWidth =
this.util.setConfigNum(this, 'borderWidth', op.config.borderWidth, this.gcf.borderWidth, 3);
//copy the preProcessing data to ids.
this.cojLen = 0;
for(var i in this){
this.coj[this.id][i] = this[i];
this.cojLen++;
}
this.coj[this.id].targetPos = null;//for adjustCss
if(this.useCss==='yes')
if(this.useCssSetting)this.useCssSetting(op);
this.ondrew = callback || function(){};
if(
this._addsFlg !== -1
)
this._ondrew = this._ondrew_old = function(that){
that.drawing = false;
if (that.useCss === 'yes') that.adjustCss('drew', that.id, that.type);
if (that.ondrew)this.ondrew(that);
};
this.draw(op);
},
get: function (url, fnc, async) {
var that = this;
var async = (async === false) ? false : true;
var req = new XMLHttpRequest();
req.open('GET', url, async);
req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
req.setRequestHeader('Pragma', 'no-cache'); //for IE. 20130423 Thanx 久保田 英晃さん
req.setRequestHeader('Cache-Control', 'no-cache');
req.onreadystatechange = function (evt) {
if (req.readyState == 4 && req.status == 200) {
if (req.responseText === '') return req
var res = decodeURIComponent(req.responseText);
if (fnc) fnc(res);
}
};
req.send();
return req;
},
bind: function (type, fnc) {
var it = this;
var target = this.canvas;
//for iphone android
if (type === 'click' || type === 'touchstart') {
var mbl = this.util.isMobile();
type = (mbl) ? 'touchstart' : 'click';
}
// this is for ccchart.$(function(){});
// and ccchart.bind('load', function () {});
if (typeof this.ids !== 'object') {
target = window;
}
if (fnc !== '_adjustcsspos') {
if (typeof fnc === 'function') {
target.removeEventListener(type, function (e) {
fnc.call(it, e, it.canvas);
});
if (type === 'load') {
fnc.call(it, window.event, it.canvas);
} else {
target.addEventListener(type, function (e) {
fnc.call(it, e, it.canvas);
});
}
if (this.useCss === 'yes' && this.hybridBox) {
this.hybridBox.addEventListener(type, function (e) {
fnc.call(it, e, it.canvas);
});
}
}
}
//for CSS Hybrid AdjustCss
if (this.useCss !== 'yes') {
return this;
} else {
if (type === 'load') {
setAdjustCssEvent(this, window, 'load');
}
if (type === 'resize') {
setAdjustCssEvent(this, window, 'resize');
}
if (type === 'scroll') {
setAdjustCssEvent(this, document, 'scroll');
}
}
function _a() { it.adjustCss(type) }
function setAdjustCssEvent(that, target, type) { //重複登録を防ぐ
if (that['_added_css_' + type + 'Event'] !== 'on') {
target.addEventListener(type, _a);
that['_added_css_' + type + 'Event'] = 'on';
}
}
return this;
},
$: function(func){
if(typeof func === 'function')this.bind('load', func);
},
draw: function (op) {
//ヒートマップの時はWS時などに軸などのチャートベースを再描画しない
if(this.ops[this.id].secondTime && this.type === 'heatmap'){
this.drawHeatmap();
this.ops[this.id].secondTime = false;
return;
}
//描画メソッド
this.drawing = true;
if (this._addsFlg === 0 || this._addsFlg === 1) {
this.canvas.width = this.width;
this.canvas.height = this.height;
this.ctx.scale(1, 1);
this.ctx.translate(0, 0);
this.ctx = this.util.setDefaultCtxProps(this, 'font', "100 12px 'Arial'");
if (this.bgGradient) this.drawGradient();
else if (typeof this.bg === 'string') this.drawBG();
if (this.type !== 'pie') {
if(this._addsFlg === 0){
//add以外の通常処理
this.drawAxisX();//水平目盛線と垂直軸ラベル
this.drawAxisY();
} else if(this._addsFlg === 1){
//add時のfirst chartY軸目盛りは書いておく
this.drawAxisX();//水平目盛線と垂直軸ラベル
}
} else {
if(this._addsFlg === 1){
//add時
this.drawAxisX();//水平目盛線と垂直軸ラベル
this.drawAxisY();
}
}
if (this.onlyChart === 'no' || this.onlyChartWidthTitle === 'yes')
this.drawTitle();
if (this.onlyChart === 'no') this.drawSubTitle();
} else if (this._addsFlg === 2) {
//add時のsecond chartXY軸
this.drawAxisX();//水平目盛線と垂直軸ラベル
this.drawAxisY();//垂直目盛線と水平軸ラベル
}
if (this.onlyChart === 'no' && this.useHanrei !== 'no') this.drawHanrei();
if (typeof this._img === 'object') {
this.drawImg(this._img);
}
if (typeof this._memo === 'object') {
this.drawMemo();
//this.drawImgが同じ座標にあるとそちらが
//コールバック遅延するのでメモが見えなくなる
}
if (this.unit !== '') this.drawUnit(this);
//drawUnitはswtGraphによるondrewの前に書かないとaddメソッドで addsFlgがリセットされてしまう
if (op) this.swtGraph();
if (
this.useMarker !== 'none' &&
!(this.type === 'scatter' || this.type === 'heatmap')
) this.drawMarkers();
if (this.xLines !== 'none') this.drawXLine();
},
swtGraph: function () {
//チャートタイプ分岐
switch(this.type){
case('line') : this.drawLine ();break;
case('bar') : this.drawBar ();break;
case('pie') : this.drawPie();break;
case('bezi') : this.drawbeziLine();break;
case('bezi2') : this.drawbeziLine2();break;
case('stacked') : this.drawStackedBar();break;
case('area') : this.drawArea();break;
case('stackedarea') : this.drawStackedArea();break;
case('stacked%') : this.drawStackedPercent();break;
case('ampli') : this.drawAmplitude();break;
case('scatter') : this.drawScatter();break;
case('heatmap') : this.drawHeatmap();break;
case('candle') : this.drawCandle();break;
default : this.drawBar ();break;
}
},