forked from apache/echarts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
echarts.js
1761 lines (1543 loc) · 64.6 KB
/
echarts.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
/*!
* ECharts, a javascript interactive chart library.
*
* Copyright (c) 2015, Baidu Inc.
* All rights reserved.
*
* LICENSE
* https://github.com/ecomfe/echarts/blob/master/LICENSE.txt
*/
/**
* echarts
*
* @desc echarts基于Canvas,纯Javascript图表库,提供直观,生动,可交互,可个性化定制的数据统计图表。
* @author Kener (@Kener-林峰, [email protected])
*
*/
define(function (require) {
var ecConfig = require('./config');
var zrUtil = require('zrender/tool/util');
var zrEvent = require('zrender/tool/event');
var self = {};
var _canvasSupported = require('zrender/tool/env').canvasSupported;
var _idBase = new Date() - 0;
var _instances = {}; // ECharts实例map索引
var DOM_ATTRIBUTE_KEY = '_echarts_instance_';
self.version = '2.2.7';
self.dependencies = {
zrender: '2.1.1'
};
/**
* 入口方法
*/
self.init = function (dom, theme) {
var zrender = require('zrender');
if ((zrender.version.replace('.', '') - 0) < (self.dependencies.zrender.replace('.', '') - 0)) {
console.error(
'ZRender ' + zrender.version
+ ' is too old for ECharts ' + self.version
+ '. Current version need ZRender '
+ self.dependencies.zrender + '+'
);
}
dom = dom instanceof Array ? dom[0] : dom;
// dom与echarts实例映射索引
var key = dom.getAttribute(DOM_ATTRIBUTE_KEY);
if (!key) {
key = _idBase++;
dom.setAttribute(DOM_ATTRIBUTE_KEY, key);
}
if (_instances[key]) {
// 同一个dom上多次init,自动释放已有实例
_instances[key].dispose();
}
_instances[key] = new Echarts(dom);
_instances[key].id = key;
_instances[key].canvasSupported = _canvasSupported;
_instances[key].setTheme(theme);
return _instances[key];
};
/**
* 通过id获得ECharts实例,id可在实例化后读取
*/
self.getInstanceById = function (key) {
return _instances[key];
};
/**
* 消息中心
*/
function MessageCenter() {
zrEvent.Dispatcher.call(this);
}
zrUtil.merge(MessageCenter.prototype, zrEvent.Dispatcher.prototype, true);
/**
* 基于zrender实现Echarts接口层
* @param {HtmlElement} dom 必要
*/
function Echarts(dom) {
// Fxxk IE11 for breaking initialization without a warrant;
// Just set something to let it be!
// by kener 2015-01-09
dom.innerHTML = '';
this._themeConfig = {}; // zrUtil.clone(ecConfig);
this.dom = dom;
// this._zr;
// this._option; // curOption clone
// this._optionRestore; // for restore;
// this._island;
// this._toolbox;
// this._timeline;
// this._refreshInside; // 内部刷新标志位
this._connected = false;
this._status = { // 用于图表间通信
dragIn: false,
dragOut: false,
needRefresh: false
};
this._curEventType = false; // 破循环信号灯
this._chartList = []; // 图表实例
this._messageCenter = new MessageCenter();
this._messageCenterOutSide = new MessageCenter(); // Echarts层的外部消息中心,做Echarts层的消息转发
// resize方法经常被绑定到window.resize上,闭包一个this
this.resize = this.resize();
// 初始化::构造函数
this._init();
}
/**
* ZRender EVENT
*
* @inner
* @const
* @type {Object}
*/
var ZR_EVENT = require('zrender/config').EVENT;
/**
* 要绑定监听的zrender事件列表
*
* @const
* @inner
* @type {Array}
*/
var ZR_EVENT_LISTENS = [
'CLICK', 'DBLCLICK', 'MOUSEOVER', 'MOUSEOUT',
'DRAGSTART', 'DRAGEND', 'DRAGENTER', 'DRAGOVER', 'DRAGLEAVE', 'DROP'
];
/**
* 对echarts的实例中的chartList属性成员,逐个进行方法调用,遍历顺序为逆序
* 由于在事件触发的默认行为处理中,多次用到相同逻辑,所以抽象了该方法
* 由于所有的调用场景里,最多只有两个参数,基于性能和体积考虑,这里就不使用call或者apply了
*
* @inner
* @param {ECharts} ecInstance ECharts实例
* @param {string} methodName 要调用的方法名
* @param {*} arg0 调用参数1
* @param {*} arg1 调用参数2
* @param {*} arg2 调用参数3
*/
function callChartListMethodReverse(ecInstance, methodName, arg0, arg1, arg2) {
var chartList = ecInstance._chartList;
var len = chartList.length;
while (len--) {
var chart = chartList[len];
if (typeof chart[methodName] === 'function') {
chart[methodName](arg0, arg1, arg2);
}
}
}
Echarts.prototype = {
/**
* 初始化::构造函数
*/
_init: function () {
var self = this;
var _zr = require('zrender').init(this.dom);
this._zr = _zr;
// wrap: n,e,d,t for name event data this
this._messageCenter.dispatch = function(type, event, eventPackage, that) {
eventPackage = eventPackage || {};
eventPackage.type = type;
eventPackage.event = event;
self._messageCenter.dispatchWithContext(type, eventPackage, that);
self._messageCenterOutSide.dispatchWithContext(type, eventPackage, that);
// 如下注掉的代码,@see: https://github.com/ecomfe/echarts-discuss/issues/3
// if (type != 'HOVER' && type != 'MOUSEOUT') { // 频繁事件直接抛出
// setTimeout(function(){
// self._messageCenterOutSide.dispatchWithContext(
// type, eventPackage, that
// );
// },50);
// }
// else {
// self._messageCenterOutSide.dispatchWithContext(
// type, eventPackage, that
// );
// }
};
this._onevent = function(param){
return self.__onevent(param);
};
for (var e in ecConfig.EVENT) {
if (e != 'CLICK' && e != 'DBLCLICK'
&& e != 'HOVER' && e != 'MOUSEOUT' && e != 'MAP_ROAM'
) {
this._messageCenter.bind(ecConfig.EVENT[e], this._onevent, this);
}
}
var eventBehaviors = {};
this._onzrevent = function (param) {
return self[eventBehaviors[ param.type ]](param);
};
// 挂载关心的事件
for (var i = 0, len = ZR_EVENT_LISTENS.length; i < len; i++) {
var eventName = ZR_EVENT_LISTENS[i];
var eventValue = ZR_EVENT[eventName];
eventBehaviors[eventValue] = '_on' + eventName.toLowerCase();
_zr.on(eventValue, this._onzrevent);
}
this.chart = {}; // 图表索引
this.component = {}; // 组件索引
// 内置图表
// 孤岛
var Island = require('./chart/island');
this._island = new Island(this._themeConfig, this._messageCenter, _zr, {}, this);
this.chart.island = this._island;
// 内置通用组件
// 工具箱
var Toolbox = require('./component/toolbox');
this._toolbox = new Toolbox(this._themeConfig, this._messageCenter, _zr, {}, this);
this.component.toolbox = this._toolbox;
var componentLibrary = require('./component');
componentLibrary.define('title', require('./component/title'));
componentLibrary.define('tooltip', require('./component/tooltip'));
componentLibrary.define('legend', require('./component/legend'));
if (_zr.getWidth() === 0 || _zr.getHeight() === 0) {
console.error('Dom’s width & height should be ready before init.');
}
},
/**
* ECharts事件处理中心
*/
__onevent: function (param){
param.__echartsId = param.__echartsId || this.id;
// 来自其他联动图表的事件
var fromMyself = (param.__echartsId === this.id);
if (!this._curEventType) {
this._curEventType = param.type;
}
switch (param.type) {
case ecConfig.EVENT.LEGEND_SELECTED :
this._onlegendSelected(param);
break;
case ecConfig.EVENT.DATA_ZOOM :
if (!fromMyself) {
var dz = this.component.dataZoom;
if (dz) {
dz.silence(true);
dz.absoluteZoom(param.zoom);
dz.silence(false);
}
}
this._ondataZoom(param);
break;
case ecConfig.EVENT.DATA_RANGE :
fromMyself && this._ondataRange(param);
break;
case ecConfig.EVENT.MAGIC_TYPE_CHANGED :
if (!fromMyself) {
var tb = this.component.toolbox;
if (tb) {
tb.silence(true);
tb.setMagicType(param.magicType);
tb.silence(false);
}
}
this._onmagicTypeChanged(param);
break;
case ecConfig.EVENT.DATA_VIEW_CHANGED :
fromMyself && this._ondataViewChanged(param);
break;
case ecConfig.EVENT.TOOLTIP_HOVER :
fromMyself && this._tooltipHover(param);
break;
case ecConfig.EVENT.RESTORE :
this._onrestore();
break;
case ecConfig.EVENT.REFRESH :
fromMyself && this._onrefresh(param);
break;
// 鼠标同步
case ecConfig.EVENT.TOOLTIP_IN_GRID :
case ecConfig.EVENT.TOOLTIP_OUT_GRID :
if (!fromMyself) {
// 只处理来自外部的鼠标同步
var grid = this.component.grid;
if (grid) {
this._zr.trigger(
'mousemove',
{
connectTrigger: true,
zrenderX: grid.getX() + param.x * grid.getWidth(),
zrenderY: grid.getY() + param.y * grid.getHeight()
}
);
}
}
else if (this._connected) {
// 来自自己,并且存在多图联动,空间坐标映射修改参数分发
var grid = this.component.grid;
if (grid) {
param.x = (param.event.zrenderX - grid.getX()) / grid.getWidth();
param.y = (param.event.zrenderY - grid.getY()) / grid.getHeight();
}
}
break;
/*
case ecConfig.EVENT.RESIZE :
case ecConfig.EVENT.DATA_CHANGED :
case ecConfig.EVENT.PIE_SELECTED :
case ecConfig.EVENT.MAP_SELECTED :
break;
*/
}
// 多图联动,只做自己的一级事件分发,避免级联事件循环
if (this._connected && fromMyself && this._curEventType === param.type) {
for (var c in this._connected) {
this._connected[c].connectedEventHandler(param);
}
// 分发完毕后复位
this._curEventType = null;
}
if (!fromMyself || (!this._connected && fromMyself)) { // 处理了完联动事件复位
this._curEventType = null;
}
},
/**
* 点击事件,响应zrender事件,包装后分发到Echarts层
*/
_onclick: function (param) {
callChartListMethodReverse(this, 'onclick', param);
if (param.target) {
var ecData = this._eventPackage(param.target);
if (ecData && ecData.seriesIndex != null) {
this._messageCenter.dispatch(
ecConfig.EVENT.CLICK,
param.event,
ecData,
this
);
}
}
},
/**
* 双击事件,响应zrender事件,包装后分发到Echarts层
*/
_ondblclick: function (param) {
callChartListMethodReverse(this, 'ondblclick', param);
if (param.target) {
var ecData = this._eventPackage(param.target);
if (ecData && ecData.seriesIndex != null) {
this._messageCenter.dispatch(
ecConfig.EVENT.DBLCLICK,
param.event,
ecData,
this
);
}
}
},
/**
* 鼠标移入事件,响应zrender事件,包装后分发到Echarts层
*/
_onmouseover: function (param) {
if (param.target) {
var ecData = this._eventPackage(param.target);
if (ecData && ecData.seriesIndex != null) {
this._messageCenter.dispatch(
ecConfig.EVENT.HOVER,
param.event,
ecData,
this
);
}
}
},
/**
* 鼠标移出事件,响应zrender事件,包装后分发到Echarts层
*/
_onmouseout: function (param) {
if (param.target) {
var ecData = this._eventPackage(param.target);
if (ecData && ecData.seriesIndex != null) {
this._messageCenter.dispatch(
ecConfig.EVENT.MOUSEOUT,
param.event,
ecData,
this
);
}
}
},
/**
* dragstart回调,可计算特性实现
*/
_ondragstart: function (param) {
// 复位用于图表间通信拖拽标识
this._status = {
dragIn: false,
dragOut: false,
needRefresh: false
};
callChartListMethodReverse(this, 'ondragstart', param);
},
/**
* dragging回调,可计算特性实现
*/
_ondragenter: function (param) {
callChartListMethodReverse(this, 'ondragenter', param);
},
/**
* dragstart回调,可计算特性实现
*/
_ondragover: function (param) {
callChartListMethodReverse(this, 'ondragover', param);
},
/**
* dragstart回调,可计算特性实现
*/
_ondragleave: function (param) {
callChartListMethodReverse(this, 'ondragleave', param);
},
/**
* dragstart回调,可计算特性实现
*/
_ondrop: function (param) {
callChartListMethodReverse(this, 'ondrop', param, this._status);
this._island.ondrop(param, this._status);
},
/**
* dragdone回调 ,可计算特性实现
*/
_ondragend: function (param) {
callChartListMethodReverse(this, 'ondragend', param, this._status);
this._timeline && this._timeline.ondragend(param, this._status);
this._island.ondragend(param, this._status);
// 发生过重计算
if (this._status.needRefresh) {
this._syncBackupData(this._option);
var messageCenter = this._messageCenter;
messageCenter.dispatch(
ecConfig.EVENT.DATA_CHANGED,
param.event,
this._eventPackage(param.target),
this
);
messageCenter.dispatch(ecConfig.EVENT.REFRESH, null, null, this);
}
},
/**
* 图例选择响应
*/
_onlegendSelected: function (param) {
// 用于图表间通信
this._status.needRefresh = false;
callChartListMethodReverse(this, 'onlegendSelected', param, this._status);
if (this._status.needRefresh) {
this._messageCenter.dispatch(ecConfig.EVENT.REFRESH, null, null, this);
}
},
/**
* 数据区域缩放响应
*/
_ondataZoom: function (param) {
// 用于图表间通信
this._status.needRefresh = false;
callChartListMethodReverse(this, 'ondataZoom', param, this._status);
if (this._status.needRefresh) {
this._messageCenter.dispatch(ecConfig.EVENT.REFRESH, null, null, this);
}
},
/**
* 值域漫游响应
*/
_ondataRange: function (param) {
this._clearEffect();
// 用于图表间通信
this._status.needRefresh = false;
callChartListMethodReverse(this, 'ondataRange', param, this._status);
// 没有相互影响,直接刷新即可
if (this._status.needRefresh) {
this._zr.refreshNextFrame();
}
},
/**
* 动态类型切换响应
*/
_onmagicTypeChanged: function () {
this._clearEffect();
this._render(this._toolbox.getMagicOption());
},
/**
* 数据视图修改响应
*/
_ondataViewChanged: function (param) {
this._syncBackupData(param.option);
this._messageCenter.dispatch(
ecConfig.EVENT.DATA_CHANGED,
null,
param,
this
);
this._messageCenter.dispatch(ecConfig.EVENT.REFRESH, null, null, this);
},
/**
* tooltip与图表间通信
*/
_tooltipHover: function (param) {
var tipShape = [];
callChartListMethodReverse(this, 'ontooltipHover', param, tipShape);
},
/**
* 还原
*/
_onrestore: function () {
this.restore();
},
/**
* 刷新
*/
_onrefresh: function (param) {
this._refreshInside = true;
this.refresh(param);
this._refreshInside = false;
},
/**
* 数据修改后的反向同步dataZoom持有的备份数据
*/
_syncBackupData: function (curOption) {
this.component.dataZoom && this.component.dataZoom.syncBackupData(curOption);
},
/**
* 打包Echarts层的事件附件
*/
_eventPackage: function (target) {
if (target) {
var ecData = require('./util/ecData');
var seriesIndex = ecData.get(target, 'seriesIndex');
var dataIndex = ecData.get(target, 'dataIndex');
dataIndex = seriesIndex != -1 && this.component.dataZoom
? this.component.dataZoom.getRealDataIndex(
seriesIndex,
dataIndex
)
: dataIndex;
return {
seriesIndex: seriesIndex,
seriesName: (ecData.get(target, 'series') || {}).name,
dataIndex: dataIndex,
data: ecData.get(target, 'data'),
name: ecData.get(target, 'name'),
value: ecData.get(target, 'value'),
special: ecData.get(target, 'special')
};
}
return;
},
_noDataCheck: function(magicOption) {
var series = magicOption.series;
for (var i = 0, l = series.length; i < l; i++) {
if (series[i].type == ecConfig.CHART_TYPE_MAP
|| (series[i].data && series[i].data.length > 0)
|| (series[i].markPoint && series[i].markPoint.data && series[i].markPoint.data.length > 0)
|| (series[i].markLine && series[i].markLine.data && series[i].markLine.data.length > 0)
|| (series[i].nodes && series[i].nodes.length > 0)
|| (series[i].links && series[i].links.length > 0)
|| (series[i].matrix && series[i].matrix.length > 0)
|| (series[i].eventList && series[i].eventList.length > 0)
) {
return false; // 存在任意数据则为非空数据
}
}
var loadOption = (this._option && this._option.noDataLoadingOption)
|| this._themeConfig.noDataLoadingOption
|| ecConfig.noDataLoadingOption
|| {
text: (this._option && this._option.noDataText)
|| this._themeConfig.noDataText
|| ecConfig.noDataText,
effect: (this._option && this._option.noDataEffect)
|| this._themeConfig.noDataEffect
|| ecConfig.noDataEffect
};
// 空数据
this.clear();
this.showLoading(loadOption);
return true;
},
/**
* 图表渲染
*/
_render: function (magicOption) {
this._mergeGlobalConifg(magicOption);
if (this._noDataCheck(magicOption)) {
return;
}
var bgColor = magicOption.backgroundColor;
if (bgColor) {
if (!_canvasSupported
&& bgColor.indexOf('rgba') != -1
) {
// IE6~8对RGBA的处理,filter会带来其他颜色的影响
var cList = bgColor.split(',');
this.dom.style.filter = 'alpha(opacity=' +
cList[3].substring(0, cList[3].lastIndexOf(')')) * 100
+ ')';
cList.length = 3;
cList[0] = cList[0].replace('a', '');
this.dom.style.backgroundColor = cList.join(',') + ')';
}
else {
this.dom.style.backgroundColor = bgColor;
}
}
this._zr.clearAnimation();
this._chartList = [];
var chartLibrary = require('./chart');
var componentLibrary = require('./component');
if (magicOption.xAxis || magicOption.yAxis) {
magicOption.grid = magicOption.grid || {};
magicOption.dataZoom = magicOption.dataZoom || {};
}
var componentList = [
'title', 'legend', 'tooltip', 'dataRange', 'roamController',
'grid', 'dataZoom', 'xAxis', 'yAxis', 'polar'
];
var ComponentClass;
var componentType;
var component;
for (var i = 0, l = componentList.length; i < l; i++) {
componentType = componentList[i];
component = this.component[componentType];
if (magicOption[componentType]) {
if (component) {
component.refresh && component.refresh(magicOption);
}
else {
ComponentClass = componentLibrary.get(
/^[xy]Axis$/.test(componentType) ? 'axis' : componentType
);
component = new ComponentClass(
this._themeConfig, this._messageCenter, this._zr,
magicOption, this, componentType
);
this.component[componentType] = component;
}
this._chartList.push(component);
}
else if (component) {
component.dispose();
this.component[componentType] = null;
delete this.component[componentType];
}
}
var ChartClass;
var chartType;
var chart;
var chartMap = {}; // 记录已经初始化的图表
for (var i = 0, l = magicOption.series.length; i < l; i++) {
chartType = magicOption.series[i].type;
if (!chartType) {
console.error('series[' + i + '] chart type has not been defined.');
continue;
}
if (!chartMap[chartType]) {
chartMap[chartType] = true;
ChartClass = chartLibrary.get(chartType);
if (ChartClass) {
if (this.chart[chartType]) {
chart = this.chart[chartType];
chart.refresh(magicOption);
}
else {
chart = new ChartClass(
this._themeConfig, this._messageCenter, this._zr,
magicOption, this
);
}
this._chartList.push(chart);
this.chart[chartType] = chart;
}
else {
console.error(chartType + ' has not been required.');
}
}
}
// 已有实例但新option不带这类图表的实例释放
for (chartType in this.chart) {
if (chartType != ecConfig.CHART_TYPE_ISLAND && !chartMap[chartType]) {
this.chart[chartType].dispose();
this.chart[chartType] = null;
delete this.chart[chartType];
}
}
this.component.grid && this.component.grid.refixAxisShape(this.component);
this._island.refresh(magicOption);
this._toolbox.refresh(magicOption);
magicOption.animation && !magicOption.renderAsImage
? this._zr.refresh()
: this._zr.render();
var imgId = 'IMG' + this.id;
var img = document.getElementById(imgId);
if (magicOption.renderAsImage && _canvasSupported) {
// IE8- 不支持图片渲染形式
if (img) {
// 已经渲染过则更新显示
img.src = this.getDataURL(magicOption.renderAsImage);
}
else {
// 没有渲染过插入img dom
img = this.getImage(magicOption.renderAsImage);
img.id = imgId;
img.style.position = 'absolute';
img.style.left = 0;
img.style.top = 0;
this.dom.firstChild.appendChild(img);
}
this.un();
this._zr.un();
this._disposeChartList();
this._zr.clear();
}
else if (img) {
// 删除可能存在的img
img.parentNode.removeChild(img);
}
img = null;
this._option = magicOption;
},
/**
* 还原
*/
restore: function () {
this._clearEffect();
this._option = zrUtil.clone(this._optionRestore);
this._disposeChartList();
this._island.clear();
this._toolbox.reset(this._option, true);
this._render(this._option);
},
/**
* 刷新
* @param {Object=} param,可选参数,用于附带option,内部同步用,外部不建议带入数据修改,无法同步
*/
refresh: function (param) {
this._clearEffect();
param = param || {};
var magicOption = param.option;
// 外部调用的refresh且有option带入
if (!this._refreshInside && magicOption) {
// 做简单的差异合并去同步内部持有的数据克隆,不建议带入数据
// 开启数据区域缩放、拖拽重计算、数据视图可编辑模式情况下,当用户产生了数据变化后无法同步
// 如有带入option存在数据变化,请重新setOption
magicOption = this.getOption();
zrUtil.merge(magicOption, param.option, true);
zrUtil.merge(this._optionRestore, param.option, true);
this._toolbox.reset(magicOption);
}
this._island.refresh(magicOption);
this._toolbox.refresh(magicOption);
// 停止动画
this._zr.clearAnimation();
// 先来后到,安顺序刷新各种图表,图表内部refresh优化检查magicOption,无需更新则不更新~
for (var i = 0, l = this._chartList.length; i < l; i++) {
this._chartList[i].refresh && this._chartList[i].refresh(magicOption);
}
this.component.grid && this.component.grid.refixAxisShape(this.component);
this._zr.refresh();
},
/**
* 释放图表实例
*/
_disposeChartList: function () {
this._clearEffect();
// 停止动画
this._zr.clearAnimation();
var len = this._chartList.length;
while (len--) {
var chart = this._chartList[len];
if (chart) {
var chartType = chart.type;
this.chart[chartType] && delete this.chart[chartType];
this.component[chartType] && delete this.component[chartType];
chart.dispose && chart.dispose();
}
}
this._chartList = [];
},
/**
* 非图表全局属性merge~~
*/
_mergeGlobalConifg: function (magicOption) {
var mergeList = [
// 背景颜色
'backgroundColor',
// 拖拽重计算相关
'calculable', 'calculableColor', 'calculableHolderColor',
// 孤岛显示连接符
'nameConnector', 'valueConnector',
// 动画相关
'animation', 'animationThreshold',
'animationDuration', 'animationDurationUpdate',
'animationEasing', 'addDataAnimation',
// 默认标志图形类型列表
'symbolList',
// 降低图表内元素拖拽敏感度,单位ms,不建议外部干预
'DRAG_ENABLE_TIME'
];
var len = mergeList.length;
while (len--) {
var mergeItem = mergeList[len];
if (magicOption[mergeItem] == null) {
magicOption[mergeItem] = this._themeConfig[mergeItem] != null
? this._themeConfig[mergeItem]
: ecConfig[mergeItem];
}
}
// 数值系列的颜色列表,不传则采用内置颜色,可配数组,借用zrender实例注入,会有冲突风险,先这样
var themeColor = magicOption.color;
if (!(themeColor && themeColor.length)) {
themeColor = this._themeConfig.color || ecConfig.color;
}
this._zr.getColor = function (idx) {
var zrColor = require('zrender/tool/color');
return zrColor.getColor(idx, themeColor);
};
if (!_canvasSupported) {
// 不支持Canvas的强制关闭动画
magicOption.animation = false;
magicOption.addDataAnimation = false;
}
},
/**
* 万能接口,配置图表实例任何可配置选项,多次调用时option选项做merge处理
* @param {Object} option
* @param {boolean=} notMerge 多次调用时option选项是默认是合并(merge)的,
* 如果不需求,可以通过notMerger参数为true阻止与上次option的合并
*/
setOption: function (option, notMerge) {
if (!option.timeline) {
return this._setOption(option, notMerge);
}
else {
return this._setTimelineOption(option);
}
},
/**
* 万能接口,配置图表实例任何可配置选项,多次调用时option选项做merge处理
* @param {Object} option
* @param {boolean=} notMerge 多次调用时option选项是默认是合并(merge)的,
* 如果不需求,可以通过notMerger参数为true阻止与上次option的合并
* @param {boolean=} 默认false。keepTimeLine 表示从timeline组件调用而来,
* 表示当前行为是timeline的数据切换,保持timeline,
* 反之销毁timeline。 详见Issue #1601
*/
_setOption: function (option, notMerge, keepTimeLine) {
if (!notMerge && this._option) {
this._option = zrUtil.merge(
this.getOption(),
zrUtil.clone(option),
true
);
}
else {
this._option = zrUtil.clone(option);
!keepTimeLine && this._timeline && this._timeline.dispose();
}
this._optionRestore = zrUtil.clone(this._option);
if (!this._option.series || this._option.series.length === 0) {
this._zr.clear();
return;
}
if (this.component.dataZoom // 存在dataZoom控件
&& (this._option.dataZoom // 并且新option也存在
|| (this._option.toolbox
&& this._option.toolbox.feature
&& this._option.toolbox.feature.dataZoom
&& this._option.toolbox.feature.dataZoom.show
)
)
) {
// dataZoom同步数据
this.component.dataZoom.syncOption(this._option);
}
this._toolbox.reset(this._option);
this._render(this._option);
return this;
},
/**
* 返回内部持有的当前显示option克隆
*/
getOption: function () {
var magicOption = zrUtil.clone(this._option);
var self = this;
function restoreOption(prop) {
var restoreSource = self._optionRestore[prop];