echarts.js 23.9 KB
Newer Older
L
lang 已提交
1
/**
L
lang 已提交
2
 * @module echarts
L
lang 已提交
3
 */
L
lang 已提交
4 5
define(function (require) {

L
lang 已提交
6
    var GlobalModel = require('./model/Global');
L
lang 已提交
7
    var ExtensionAPI = require('./ExtensionAPI');
L
lang 已提交
8
    var CoordinateSystemManager = require('./CoordinateSystem');
L
lang 已提交
9

L
Update  
lang 已提交
10 11 12 13 14 15
    var ComponentModel = require('./model/Component');
    var SeriesModel = require('./model/Series');

    var ComponentView = require('./view/Component');
    var ChartView = require('./view/Chart');

L
lang 已提交
16
    // var Scale = require('./scale/Scale');
L
lang 已提交
17

L
lang 已提交
18
    var zrender = require('zrender');
L
lang 已提交
19
    var zrUtil = require('zrender/core/util');
L
lang 已提交
20 21
    var colorTool = require('zrender/tool/color');
    var env = require('zrender/core/env');
L
lang 已提交
22
    var Eventful = require('zrender/mixin/Eventful');
L
lang 已提交
23

24 25
    var each = zrUtil.each;

26 27
    var VISUAL_CODING_STAGES = ['echarts', 'chart', 'component'];

L
lang 已提交
28
    // TODO Transform first or filter first
L
lang 已提交
29 30
    var PROCESSOR_STAGES = ['transform', 'filter', 'statistic'];

L
lang 已提交
31 32 33
    /**
     * @module echarts~ECharts
     */
L
lang 已提交
34
    function ECharts (dom, theme, opts) {
L
lang 已提交
35
        opts = opts || {};
L
lang 已提交
36

L
lang 已提交
37 38 39 40 41
        if (theme) {
            each(optionPreprocessorFuncs, function (preProcess) {
                preProcess(theme);
            });
        }
L
lang 已提交
42 43 44 45 46 47 48 49 50
        /**
         * @type {string}
         */
        this.id;
        /**
         * Group id
         * @type {string}
         */
        this.group;
L
lang 已提交
51 52 53 54 55
        /**
         * @type {HTMLDomElement}
         * @private
         */
        this._dom = dom;
L
lang 已提交
56 57 58 59
        /**
         * @type {module:zrender/ZRender}
         * @private
         */
L
lang 已提交
60 61 62
        this._zr = zrender.init(dom, {
            renderer: opts.renderer || 'canvas'
        });
L
lang 已提交
63

L
lang 已提交
64 65 66 67
        /**
         * @type {Object}
         * @private
         */
L
lang 已提交
68
        this._theme = zrUtil.clone(theme, true);
L
lang 已提交
69

L
lang 已提交
70 71 72 73
        /**
         * @type {Array.<module:echarts/view/Chart>}
         * @private
         */
L
lang 已提交
74
        this._chartsList = [];
L
lang 已提交
75 76 77 78 79

        /**
         * @type {Object.<string, module:echarts/view/Chart>}
         * @private
         */
L
lang 已提交
80 81
        this._chartsMap = {};

L
lang 已提交
82 83 84 85
        /**
         * @type {Array.<module:echarts/view/Component>}
         * @private
         */
L
lang 已提交
86
        this._componentsList = [];
L
lang 已提交
87 88 89 90 91

        /**
         * @type {Object.<string, module:echarts/view/Component>}
         * @private
         */
L
lang 已提交
92 93
        this._componentsMap = {};

L
lang 已提交
94
        /**
L
lang 已提交
95
         * @type {module:echarts/ExtensionAPI}
L
lang 已提交
96 97
         * @private
         */
98
        this._api = new ExtensionAPI(this);
L
lang 已提交
99

L
lang 已提交
100 101 102 103
        /**
         * @type {module:echarts/CoordinateSystem}
         * @private
         */
L
lang 已提交
104
        this._coordinateSystem = new CoordinateSystemManager();
L
lang 已提交
105

L
lang 已提交
106 107 108 109
        Eventful.call(this);

        // Init mouse events
        this._initEvents();
L
Resize  
lang 已提交
110 111 112

        // In case some people write `window.onresize = chart.resize`
        this.resize = zrUtil.bind(this.resize, this);
L
lang 已提交
113
    }
L
lang 已提交
114

L
tweak  
lang 已提交
115
    var echartsProto = ECharts.prototype;
L
lang 已提交
116

117 118 119
    /**
     * @return {HTMLDomElement}
     */
L
tweak  
lang 已提交
120 121 122
    echartsProto.getDom = function () {
        return this._dom;
    };
L
lang 已提交
123

124 125 126
    /**
     * @return {module:zrender~ZRender}
     */
L
tweak  
lang 已提交
127 128 129
    echartsProto.getZr = function () {
        return this._zr;
    };
L
lang 已提交
130

131 132 133 134 135 136
    /**
     * @param {Object} option
     * @param {boolean} notMerge
     * @param {boolean} [notRefreshImmediately=false]
     */
    echartsProto.setOption = function (option, notMerge, notRefreshImmediately) {
L
tweak  
lang 已提交
137 138
        // PENDING
        option = zrUtil.clone(option, true);
139

L
tweak  
lang 已提交
140 141 142
        each(optionPreprocessorFuncs, function (preProcess) {
            preProcess(option);
        });
L
lang 已提交
143

L
tweak  
lang 已提交
144 145 146 147 148 149 150 151 152
        var ecModel = this._model;
        if (!ecModel || notMerge) {
            ecModel = new GlobalModel(option, null, this._theme);
            this._model = ecModel;
        }
        else {
            ecModel.restoreData();
            ecModel.mergeOption(option);
        }
L
lang 已提交
153

L
Tweak  
lang 已提交
154
        this._prepareView('component', ecModel);
L
lang 已提交
155

L
Tweak  
lang 已提交
156
        this._prepareView('chart', ecModel);
L
lang 已提交
157

L
tweak  
lang 已提交
158
        this._update();
L
lang 已提交
159

160
        !notRefreshImmediately && this._zr.refreshImmediately();
L
tweak  
lang 已提交
161
    };
L
lang 已提交
162

L
Tweak  
lang 已提交
163 164 165 166 167 168
    /**
     * @DEPRECATED
     */
    echartsProto.setTheme = function () {
        console.log('ECharts#setTheme() is DEPRECATED in ECharts 3.0');
    };
L
tweak  
lang 已提交
169 170 171 172 173 174
    /**
     * @return {module:echarts/model/Global}
     */
    echartsProto.getModel = function () {
        return this._model;
    };
L
lang 已提交
175

L
tweak  
lang 已提交
176 177 178 179 180 181
    /**
     * @return {number}
     */
    echartsProto.getWidth = function () {
        return this._zr.getWidth();
    };
L
lang 已提交
182

L
tweak  
lang 已提交
183 184 185 186 187 188
    /**
     * @return {number}
     */
    echartsProto.getHeight = function () {
        return this._zr.getHeight();
    };
L
lang 已提交
189

L
tweak  
lang 已提交
190 191
    /**
     * @param {Object} payload
L
tweak  
lang 已提交
192
     * @private
L
tweak  
lang 已提交
193
     */
L
tweak  
lang 已提交
194
    echartsProto._update = function (payload) {
L
lang 已提交
195
        console.time && console.time('update');
196

L
tweak  
lang 已提交
197
        var ecModel = this._model;
L
lang 已提交
198

L
tweak  
lang 已提交
199
        ecModel.restoreData();
L
lang 已提交
200

L
tweak  
lang 已提交
201 202 203
        // TODO
        // Save total ecModel here for undo/redo (after restoring data and before processing data).
        // Undo (restoration of total ecModel) can be carried out in 'action' or outside API call.
P
pah100 已提交
204

L
tweak  
lang 已提交
205
        this._processData(ecModel);
L
lang 已提交
206

L
tweak  
lang 已提交
207
        this._stackSeriesData(ecModel);
L
lang 已提交
208

209
        this._coordinateSystem.update(ecModel, this._api);
L
lang 已提交
210

L
tweak  
lang 已提交
211
        this._doLayout(ecModel, payload);
212

L
tweak  
lang 已提交
213
        this._doVisualCoding(ecModel, payload);
214

L
tweak  
lang 已提交
215
        this._doRender(ecModel, payload);
216

L
tweak  
lang 已提交
217 218 219 220 221 222 223 224
        // Set background
        var backgroundColor = ecModel.get('backgroundColor');
        // In IE8
        if (!env.canvasSupported) {
            var colorArr = colorTool.parse(backgroundColor);
            backgroundColor = colorTool.stringify(colorArr, 'rgb');
            if (colorArr[3] === 0) {
                backgroundColor = 'transparent';
L
lang 已提交
225
            }
L
tweak  
lang 已提交
226 227
        }
        backgroundColor && (this._dom.style.backgroundColor = backgroundColor);
L
lang 已提交
228

L
lang 已提交
229
        console.time && console.timeEnd('update');
L
tweak  
lang 已提交
230
    };
231

L
tweak  
lang 已提交
232 233 234
    // PENDING
    /**
     * @param {Object} payload
L
tweak  
lang 已提交
235
     * @private
L
tweak  
lang 已提交
236
     */
L
tweak  
lang 已提交
237
    echartsProto._updateView = function (payload) {
L
tweak  
lang 已提交
238
        var ecModel = this._model;
239

L
tweak  
lang 已提交
240
        this._doLayout(ecModel, payload);
241

L
tweak  
lang 已提交
242
        this._doVisualCoding(ecModel, payload);
243

L
tweak  
lang 已提交
244 245
        this._invokeUpdateMethod('updateView', ecModel, payload);
    };
246

L
tweak  
lang 已提交
247 248
    /**
     * @param {Object} payload
L
tweak  
lang 已提交
249
     * @private
L
tweak  
lang 已提交
250
     */
L
tweak  
lang 已提交
251
    echartsProto._updateVisual = function (payload) {
L
tweak  
lang 已提交
252
        var ecModel = this._model;
253

L
tweak  
lang 已提交
254
        this._doVisualCoding(ecModel, payload);
255

L
tweak  
lang 已提交
256 257
        this._invokeUpdateMethod('updateVisual', ecModel, payload);
    };
258

L
tweak  
lang 已提交
259 260
    /**
     * @param {Object} payload
L
tweak  
lang 已提交
261
     * @private
L
tweak  
lang 已提交
262
     */
L
tweak  
lang 已提交
263
    echartsProto._updateLayout = function (payload) {
L
tweak  
lang 已提交
264
        var ecModel = this._model;
265

L
tweak  
lang 已提交
266
        this._doLayout(ecModel, payload);
267

L
tweak  
lang 已提交
268 269
        this._invokeUpdateMethod('updateLayout', ecModel, payload);
    };
L
lang 已提交
270

271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
    /**
     * @param {Object} payload
     * @private
     */
    echartsProto._highlight = function (payload) {
        this._toggleHighlight('highlight', payload);
    };

    /**
     * @param {Object} payload
     * @private
     */
    echartsProto._downplay = function (payload) {
        this._toggleHighlight('downplay', payload);
    };

    /**
     * @param {Object} payload
     * @private
     */
    echartsProto._toggleHighlight = function (method, payload) {
        var ecModel = this._model;
293 294 295 296 297 298 299 300 301 302 303

        ecModel.eachComponent(
            {mainType: 'series', query: payload},
            function (seriesModel) {
                var chartView = this._chartsMap[seriesModel.getId()];
                if (chartView) {
                    chartView[method](seriesModel, ecModel, this._api, payload);
                }
            },
            this
        );
304 305
    };

L
Resize  
lang 已提交
306 307 308
    /**
     * Resize the chart
     */
L
tweak  
lang 已提交
309
    echartsProto.resize = function () {
L
Resize  
lang 已提交
310
        this._zr.resize();
L
tweak  
lang 已提交
311
        this._update();
L
tweak  
lang 已提交
312
    };
P
pah100 已提交
313

L
lang 已提交
314
    /**
L
Resize  
lang 已提交
315 316
     * @param {Object} eventObj
     * @return {Object}
L
lang 已提交
317 318 319 320 321 322 323
     */
    echartsProto.makeActionFromEvent = function (eventObj) {
        var payload = zrUtil.extend({}, eventObj);
        payload.type = eventActionMap[eventObj.type];
        return payload;
    };

L
tweak  
lang 已提交
324 325 326 327
    /**
     * @pubilc
     * @param {Object} payload
     * @param {string} [payload.type] Action type
P
pah100 已提交
328
     * @param {boolean} [silent=false] Whether trigger event.
L
tweak  
lang 已提交
329 330
     * @param {number} [payload.from] From uid
     */
P
pah100 已提交
331
    echartsProto.dispatch = function (payload, silent) {
L
tweak  
lang 已提交
332 333
        var actionWrap = actions[payload.type];
        if (actionWrap) {
L
lang 已提交
334 335
            var actionInfo = actionWrap.actionInfo;
            var updateMethod = actionInfo.update || 'update';
L
tweak  
lang 已提交
336
            actionWrap.action(payload, this._model);
L
tweak  
lang 已提交
337
            updateMethod !== 'none' && this['_' + updateMethod](payload);
L
lang 已提交
338

P
pah100 已提交
339 340 341 342 343 344 345
            if (!silent) {
                // Emit event outside
                // Convert type to eventType
                var eventObj = zrUtil.extend({}, payload);
                eventObj.type = actionInfo.event || eventObj.type;
                this.trigger(eventObj.type, eventObj);
            }
L
tweak  
lang 已提交
346 347
        }
    };
348

L
tweak  
lang 已提交
349 350 351 352 353
    /**
     * @param {string} methodName
     * @private
     */
    echartsProto._invokeUpdateMethod = function (methodName, ecModel, payload) {
354
        var api = this._api;
L
lang 已提交
355

L
tweak  
lang 已提交
356 357 358 359
        // Update all components
        each(this._componentsList, function (component) {
            var componentModel = component.__model;
            component[methodName](componentModel, ecModel, api, payload);
360

L
tweak  
lang 已提交
361 362
            updateZ(componentModel, component);
        }, this);
L
lang 已提交
363

L
tweak  
lang 已提交
364 365
        // Upate all charts
        ecModel.eachSeries(function (seriesModel, idx) {
366
            var chart = this._chartsMap[seriesModel.getId()];
L
tweak  
lang 已提交
367
            chart[methodName](seriesModel, ecModel, api, payload);
368

L
tweak  
lang 已提交
369 370
            updateZ(seriesModel, chart);
        }, this);
371

L
tweak  
lang 已提交
372
    };
L
lang 已提交
373

L
lang 已提交
374
    /**
L
Tweak  
lang 已提交
375
     * Prepare view instances of charts and components
L
lang 已提交
376 377 378
     * @param  {module:echarts/model/Global} ecModel
     * @private
     */
L
Tweak  
lang 已提交
379 380 381 382
    echartsProto._prepareView = function (type, ecModel) {
        var isComponent = type === 'component';
        var viewList = isComponent ? this._componentsList : this._chartsList;
        var viewMap = isComponent ? this._componentsMap : this._chartsMap;
L
tweak  
lang 已提交
383
        var zr = this._zr;
L
lang 已提交
384

L
Tweak  
lang 已提交
385 386
        for (var i = 0; i < viewList.length; i++) {
            viewList[i].__keepAlive = false;
L
tweak  
lang 已提交
387
        }
L
lang 已提交
388

L
Tweak  
lang 已提交
389 390 391 392
        ecModel[isComponent ? 'eachComponent' : 'eachSeries'](function (componentType, model) {
            if (isComponent) {
                if (componentType === 'series') {
                    return;
L
lang 已提交
393
                }
394
            }
L
tweak  
lang 已提交
395
            else {
L
Tweak  
lang 已提交
396
                model = componentType;
L
tweak  
lang 已提交
397 398
            }

L
Tweak  
lang 已提交
399
            var id = model.getId();
L
tweak  
lang 已提交
400

L
Tweak  
lang 已提交
401 402 403 404
            var view = viewMap[id];
            if (!view) {
                var classType = ComponentModel.parseClassType(model.type);
                var Clazz = isComponent
L
tweak  
lang 已提交
405
                    ? ComponentView.getClass(classType.main, classType.sub)
L
Tweak  
lang 已提交
406
                    : ChartView.getClass(classType.sub);
L
tweak  
lang 已提交
407
                if (Clazz) {
L
Tweak  
lang 已提交
408 409 410 411 412 413 414 415
                    view = new Clazz();
                    view.init(ecModel, this._api);
                    viewMap[id] = view;
                    viewList.push(view);
                    zr.add(view.group);
                }
                else {
                    // Error
L
lang 已提交
416
                }
417
            }
L
Tweak  
lang 已提交
418 419 420 421

            view.__keepAlive = true;
            view.__id = id;
            view.__model = model;
L
tweak  
lang 已提交
422 423
        }, this);

L
Tweak  
lang 已提交
424 425 426 427 428 429 430
        for (var i = 0; i < viewList.length;) {
            var view = viewList[i];
            if (!view.__keepAlive) {
                zr.remove(view.group);
                view.dispose(this._api);
                viewList.splice(i, 1);
                delete viewMap[view.__id];
L
tweak  
lang 已提交
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
            }
            else {
                i++;
            }
        }
    };
    /**
     * Processor data in each series
     *
     * @param {module:echarts/model/Global} ecModel
     * @private
     */
    echartsProto._processData = function (ecModel) {
        each(PROCESSOR_STAGES, function (stage) {
            each(dataProcessorFuncs[stage] || [], function (process) {
                process(ecModel);
L
lang 已提交
447
            });
L
tweak  
lang 已提交
448 449
        });
    };
L
lang 已提交
450

L
tweak  
lang 已提交
451 452 453 454 455 456 457 458 459 460 461 462
    /**
     * @private
     */
    echartsProto._stackSeriesData = function (ecModel) {
        var stackedDataMap = {};
        ecModel.eachSeries(function (series) {
            var stack = series.get('stack');
            var data = series.getData();
            if (stack && data.type === 'list') {
                var previousStack = stackedDataMap[stack];
                if (previousStack) {
                    data.stackedOn = previousStack;
L
lang 已提交
463
                }
L
tweak  
lang 已提交
464 465 466 467
                stackedDataMap[stack] = data;
            }
        });
    };
L
lang 已提交
468

L
tweak  
lang 已提交
469
    /**
L
lang 已提交
470
     * Layout before each chart render there series, after visual coding and data processing
L
tweak  
lang 已提交
471 472 473 474 475
     *
     * @param {module:echarts/model/Global} ecModel
     * @private
     */
    echartsProto._doLayout = function (ecModel, payload) {
476
        var api = this._api;
L
tweak  
lang 已提交
477 478 479 480
        each(layoutFuncs, function (layout) {
            layout(ecModel, api, payload);
        });
    };
L
lang 已提交
481

L
tweak  
lang 已提交
482 483 484 485 486 487 488 489 490 491
    /**
     * Code visual infomation from data after data processing
     *
     * @param {module:echarts/model/Global} ecModel
     * @private
     */
    echartsProto._doVisualCoding = function (ecModel, payload) {
        each(VISUAL_CODING_STAGES, function (stage) {
            each(visualCodingFuncs[stage] || [], function (visualCoding) {
                visualCoding(ecModel, payload);
L
lang 已提交
492
            });
L
tweak  
lang 已提交
493 494
        });
    };
L
lang 已提交
495

L
tweak  
lang 已提交
496 497 498 499 500
    /**
     * Render each chart and component
     * @private
     */
    echartsProto._doRender = function (ecModel, payload) {
501
        var api = this._api;
L
tweak  
lang 已提交
502 503 504 505 506 507 508 509 510 511 512 513 514 515
        // Render all components
        each(this._componentsList, function (component) {
            var componentModel = component.__model;
            component.render(componentModel, ecModel, api, payload);

            updateZ(componentModel, component);
        }, this);

        each(this._chartsList, function (chart) {
            chart.__keepAlive = false;
        }, this);

        // Render all charts
        ecModel.eachSeries(function (seriesModel, idx) {
516
            var chart = this._chartsMap[seriesModel.getId()];
L
tweak  
lang 已提交
517 518 519 520 521 522
            chart.__keepAlive = true;
            chart.render(seriesModel, ecModel, api, payload);

            updateZ(seriesModel, chart);
        }, this);

L
lang 已提交
523
        // Remove groups of unrendered charts
L
tweak  
lang 已提交
524 525 526 527 528 529
        each(this._chartsList, function (chart) {
            if (!chart.__keepAlive) {
                chart.remove(ecModel, api);
            }
        }, this);
    };
L
lang 已提交
530

L
lang 已提交
531 532 533 534 535 536 537 538 539 540 541 542 543
    var MOUSE_EVENT_NAMES = [
        'click', 'dblclick', 'mouseover', 'mouseout', 'globalout'
    ];
    /**
     * @private
     */
    echartsProto._initEvents = function () {
        var zr = this._zr;
        each(MOUSE_EVENT_NAMES, function (eveName) {
            zr.on(eveName, function (e) {
                var ecModel = this.getModel();
                var el = e.target;
                if (el && el.dataIndex != null) {
544
                    var hostModel = el.hostModel || ecModel.getSeriesByIndex(el.seriesIndex);
L
lang 已提交
545 546 547 548 549 550 551 552 553
                    var params = hostModel && hostModel.getDataParams(el.dataIndex) || {};
                    params.event = e;
                    params.type = eveName;
                    this.trigger(eveName, params);
                }
            }, this);
        }, this);
    };

L
lang 已提交
554 555 556 557 558 559 560 561 562
    /**
     * @return {boolean]
     */
    echartsProto.isDisposed = function () {
        return this._disposed;
    };
    /**
     * Dispose instance
     */
L
tweak  
lang 已提交
563
    echartsProto.dispose = function () {
L
lang 已提交
564 565
        this._disposed = true;

L
tweak  
lang 已提交
566 567 568 569 570 571
        each(this._components, function (component) {
            component.dispose();
        });
        each(this._charts, function (chart) {
            chart.dispose();
        });
L
lang 已提交
572

L
Tweak  
lang 已提交
573
        this._zr.dispose();
L
lang 已提交
574 575

        instances[this.id] = null;
L
lang 已提交
576 577
    };

L
lang 已提交
578 579
    zrUtil.mixin(ECharts, Eventful);

L
lang 已提交
580 581 582 583 584 585 586 587 588 589 590 591 592 593
    /**
     * @param {module:echarts/model/Series|module:echarts/model/Component} model
     * @param {module:echarts/view/Component|module:echarts/view/Chart} view
     * @return {string}
     */
    function updateZ(model, view) {
        var z = model.get('z');
        var zlevel = model.get('zlevel');
        // Set z and zlevel
        view.group.traverse(function (el) {
            z != null && (el.z = z);
            zlevel != null && (el.zlevel = zlevel);
        });
    }
L
lang 已提交
594 595 596 597
    /**
     * @type {Array.<Function>}
     * @inner
     */
P
pah100 已提交
598 599
    var actions = [];

L
lang 已提交
600
    /**
L
lang 已提交
601
     * Map eventType to actionType
L
lang 已提交
602 603 604 605
     * @type {Object}
     */
    var eventActionMap = {};

L
lang 已提交
606 607 608 609
    /**
     * @type {Array.<Function>}
     * @inner
     */
610 611
    var layoutFuncs = [];

L
lang 已提交
612 613 614 615 616 617 618
    /**
     * Data processor functions of each stage
     * @type {Array.<Object.<string, Function>>}
     * @inner
     */
    var dataProcessorFuncs = {};

619 620 621 622 623 624
    /**
     * @type {Array.<Function>}
     * @inner
     */
    var optionPreprocessorFuncs = [];

L
lang 已提交
625 626 627 628 629
    /**
     * Visual coding functions of each stage
     * @type {Array.<Object.<string, Function>>}
     * @inner
     */
630
    var visualCodingFuncs = {};
L
lang 已提交
631

L
lang 已提交
632 633 634 635 636 637
    var instances = {};
    var connectedGroups = {};

    var idBase = new Date() - 0;
    var groupIdBase = new Date() - 0;
    var DOM_ATTRIBUTE_KEY = '_echarts_instance_';
L
lang 已提交
638
    /**
L
lang 已提交
639
     * @alias module:echarts
L
lang 已提交
640
     */
L
lang 已提交
641 642 643 644 645 646 647 648 649
    var echarts = {
        /**
         * @type {number}
         */
        version: '3.0.0',
        dependencies: {
            zrender: '3.0.0'
        }
    };
L
lang 已提交
650

L
tweak  
lang 已提交
651 652 653 654 655 656
    /**
     * @param {HTMLDomElement} dom
     * @param {Object} [theme]
     * @param {Object} opts
     */
    echarts.init = function (dom, theme, opts) {
L
lang 已提交
657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
        // Check version
        if ((zrender.version.replace('.', '') - 0) < (echarts.dependencies.zrender.replace('.', '') - 0)) {
            console.error(
                'ZRender ' + zrender.version
                + ' is too old for ECharts ' + echarts.version
                + '. Current version need ZRender '
                + echarts.dependencies.zrender + '+'
            );
        }

        var chart = new ECharts(dom, theme, opts);
        chart.id = idBase++;
        instances[chart.id] = chart;

        // Connecting
        zrUtil.each(eventActionMap, function (actionType, eventType) {
            chart.on(eventType, function (event) {
                if (connectedGroups[chart.group]) {
                    chart.__connectedActionDispatching = true;
                    for (var id in instances) {
L
tweak  
lang 已提交
677
                        var action = chart.makeActionFromEvent(event);
L
lang 已提交
678 679
                        var otherChart = instances[id];
                        if (otherChart !== chart && otherChart.group === chart.group) {
L
tweak  
lang 已提交
680 681 682
                            if (!otherChart.__connectedActionDispatching) {
                                otherChart.dispatch(action);
                            }
L
lang 已提交
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
                        }
                    }
                    chart.__connectedActionDispatching = false;
                }
            });
        });

        return chart;
    };

    /**
     * @return {string|Array.<module:echarts~ECharts>} groupId
     */
    echarts.connect = function (groupId) {
        // Is array of charts
        if (zrUtil.isArray(groupId)) {
            var charts = groupId;
            groupId = null;
            // If any chart has group
            zrUtil.each(charts, function (chart) {
                if (chart.group != null) {
                    groupId = chart.group;
                }
            });
            groupId = groupId || groupIdBase++;
            zrUtil.each(charts, function (chart) {
                chart.group = groupId;
            });
        }
        connectedGroups[groupId] = true;
        return groupId;
    };

    /**
     * @return {string} groupId
     */
    echarts.disConnect = function (groupId) {
        connectedGroups[groupId] = false;
    };

    /**
     * Dispose a chart instance
     * @param  {module:echarts~ECharts|HTMLDomElement|string} chart
     */
    echarts.dispose = function (chart) {
        if (zrUtil.isDom(chart)) {
            chart = echarts.getInstanceByDom(chart);
        }
        else if (typeof chart === 'string') {
            chart = instances[chart];
        }
        if ((chart instanceof ECharts) && !chart.isDisposed()) {
            chart.dispose();
        }
    };

    /**
     * @param  {HTMLDomElement} dom
     * @return {echarts~ECharts}
     */
    echarts.getInstanceByDom = function (dom) {
        var key = dom.getAttribute(DOM_ATTRIBUTE_KEY);
        return instances[key];
    };
    /**
     * @param {string} key
     * @return {echarts~ECharts}
     */
    echarts.getInstanceById = function (key) {
        return instances[key];
L
tweak  
lang 已提交
753
    };
L
lang 已提交
754

L
tweak  
lang 已提交
755 756 757 758 759 760 761
    /**
     * Register option preprocessor
     * @param {Function} preprocessorFunc
     */
    echarts.registerPreprocessor = function (preprocessorFunc) {
        optionPreprocessorFuncs.push(preprocessorFunc);
    };
762

L
tweak  
lang 已提交
763 764 765 766 767 768 769 770 771 772 773
    /**
     * @param {string} stage
     * @param {Function} processorFunc
     */
    echarts.registerProcessor = function (stage, processorFunc) {
        if (zrUtil.indexOf(PROCESSOR_STAGES, stage) < 0) {
            throw new Error('stage should be one of ' + PROCESSOR_STAGES);
        }
        var funcs = dataProcessorFuncs[stage] || (dataProcessorFuncs[stage] = []);
        funcs.push(processorFunc);
    };
L
lang 已提交
774

L
tweak  
lang 已提交
775 776 777 778 779 780 781 782 783 784 785
    /**
     * Usage:
     * registerAction('someAction', 'someEvent', function () { ... });
     * registerAction('someAction', function () { ... });
     * registerAction(
     *     {type: 'someAction', event: 'someEvent', update: 'updateView'},
     *     function () { ... }
     * );
     *
     * @param {(string|Object)} actionInfo
     * @param {string} actionInfo.type
L
lang 已提交
786 787 788 789
     * @param {string} [actionInfo.event]
     * @param {string} [actionInfo.update]
     * @param {string} [eventName]
     * @param {Function} action
L
tweak  
lang 已提交
790
     */
L
lang 已提交
791 792 793 794 795
    echarts.registerAction = function (actionInfo, eventName, action) {
        if (typeof eventName === 'function') {
            action = eventName;
            eventName = '';
        }
L
tweak  
lang 已提交
796 797
        var actionType = zrUtil.isObject(actionInfo)
            ? actionInfo.type
L
lang 已提交
798 799 800
            : ([actionInfo, actionInfo = {
                event: eventName
            }][0]);
L
lang 已提交
801 802

        actionInfo.event = actionInfo.event || actionType;
L
lang 已提交
803
        eventName = actionInfo.event;
804

L
tweak  
lang 已提交
805 806 807
        if (!actions[actionType]) {
            actions[actionType] = {action: action, actionInfo: actionInfo};
        }
L
lang 已提交
808
        eventActionMap[eventName] = actionType;
L
tweak  
lang 已提交
809
    };
P
pah100 已提交
810

L
tweak  
lang 已提交
811 812 813 814 815 816 817
    /**
     * @param {string} type
     * @param {*} CoordinateSystem
     */
    echarts.registerCoordinateSystem = function (type, CoordinateSystem) {
        CoordinateSystemManager.register(type, CoordinateSystem);
    };
L
lang 已提交
818

L
tweak  
lang 已提交
819 820 821
    /**
     * @param {*} layout
     */
L
lang 已提交
822
    echarts.registerLayout = function (layout) {
L
tweak  
lang 已提交
823
        // PENDING All functions ?
824 825
        if (zrUtil.indexOf(layoutFuncs, layout) < 0) {
            layoutFuncs.push(layout);
L
tweak  
lang 已提交
826 827
        }
    };
L
lang 已提交
828

L
tweak  
lang 已提交
829 830 831 832 833 834 835 836 837 838 839
    /**
     * @param {string} stage
     * @param {Function} visualCodingFunc
     */
    echarts.registerVisualCoding = function (stage, visualCodingFunc) {
        if (zrUtil.indexOf(VISUAL_CODING_STAGES, stage) < 0) {
            throw new Error('stage should be one of ' + VISUAL_CODING_STAGES);
        }
        var funcs = visualCodingFuncs[stage] || (visualCodingFuncs[stage] = []);
        funcs.push(visualCodingFunc);
    };
L
Update  
lang 已提交
840

L
tweak  
lang 已提交
841
    /**
L
lang 已提交
842
     * @param {echarts/scale/*} scale
L
tweak  
lang 已提交
843
     */
L
Tweak  
lang 已提交
844 845 846
    // echarts.extendScale = function (opts) {
    //     return Scale.extend(opts);
    // };
L
lang 已提交
847

L
tweak  
lang 已提交
848 849 850 851 852 853
    /**
     * @param {Object} opts
     */
    echarts.extendChartView = function (opts) {
        return ChartView.extend(opts);
    };
L
Update  
lang 已提交
854

L
tweak  
lang 已提交
855 856 857 858 859 860
    /**
     * @param {Object} opts
     */
    echarts.extendComponentModel = function (opts) {
        return ComponentModel.extend(opts);
    };
L
Update  
lang 已提交
861

L
tweak  
lang 已提交
862 863 864 865 866 867
    /**
     * @param {Object} opts
     */
    echarts.extendSeriesModel = function (opts) {
        return SeriesModel.extend(opts);
    };
P
pah100 已提交
868

L
tweak  
lang 已提交
869 870 871 872 873
    /**
     * @param {Object} opts
     */
    echarts.extendComponentView = function (opts) {
        return ComponentView.extend(opts);
L
lang 已提交
874 875
    };

L
lang 已提交
876
    echarts.registerVisualCoding('echarts', require('./visual/seriesColor'));
877 878
    echarts.registerPreprocessor(require('./preprocessor/backwardCompat'));

879 880 881 882 883 884 885 886 887 888 889 890
    // Default action
    echarts.registerAction({
        type: 'highlight',
        event: 'highlight',
        update: 'highlight'
    }, zrUtil.noop);
    echarts.registerAction({
        type: 'downplay',
        event: 'downplay',
        update: 'downplay'
    }, zrUtil.noop);

L
lang 已提交
891 892
    return echarts;
});