echarts.js 24.9 KB
Newer Older
L
tweak  
lang 已提交
1 2 3 4 5 6 7 8 9 10
/*!
 * ECharts, a javascript interactive chart library.
 *
 * Copyright (c) 2015, Baidu Inc.
 * All rights reserved.
 *
 * LICENSE
 * https://github.com/ecomfe/echarts/blob/master/LICENSE.txt
 */

L
lang 已提交
11
/**
L
lang 已提交
12
 * @module echarts
L
lang 已提交
13
 */
L
lang 已提交
14 15
define(function (require) {

L
lang 已提交
16
    var GlobalModel = require('./model/Global');
L
lang 已提交
17
    var ExtensionAPI = require('./ExtensionAPI');
L
lang 已提交
18
    var CoordinateSystemManager = require('./CoordinateSystem');
L
lang 已提交
19

L
Update  
lang 已提交
20 21 22 23 24 25
    var ComponentModel = require('./model/Component');
    var SeriesModel = require('./model/Series');

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

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

L
lang 已提交
28
    var zrender = require('zrender');
L
lang 已提交
29
    var zrUtil = require('zrender/core/util');
L
lang 已提交
30 31
    var colorTool = require('zrender/tool/color');
    var env = require('zrender/core/env');
L
lang 已提交
32
    var Eventful = require('zrender/mixin/Eventful');
L
lang 已提交
33

34 35
    var each = zrUtil.each;

36 37
    var VISUAL_CODING_STAGES = ['echarts', 'chart', 'component'];

L
lang 已提交
38
    // TODO Transform first or filter first
L
lang 已提交
39 40
    var PROCESSOR_STAGES = ['transform', 'filter', 'statistic'];

L
lang 已提交
41 42 43 44 45 46 47
    /**
     * @module echarts~MessageCenter
     */
    function MessageCenter() {
        Eventful.call(this);
    }
    zrUtil.mixin(MessageCenter, Eventful);
L
lang 已提交
48 49 50
    /**
     * @module echarts~ECharts
     */
L
lang 已提交
51
    function ECharts (dom, theme, opts) {
L
lang 已提交
52
        opts = opts || {};
L
lang 已提交
53

L
lang 已提交
54 55 56 57 58
        if (theme) {
            each(optionPreprocessorFuncs, function (preProcess) {
                preProcess(theme);
            });
        }
L
lang 已提交
59 60 61 62 63 64 65 66 67
        /**
         * @type {string}
         */
        this.id;
        /**
         * Group id
         * @type {string}
         */
        this.group;
L
lang 已提交
68 69 70 71 72
        /**
         * @type {HTMLDomElement}
         * @private
         */
        this._dom = dom;
L
lang 已提交
73 74 75 76
        /**
         * @type {module:zrender/ZRender}
         * @private
         */
L
lang 已提交
77 78 79
        this._zr = zrender.init(dom, {
            renderer: opts.renderer || 'canvas'
        });
L
lang 已提交
80

L
lang 已提交
81 82 83 84
        /**
         * @type {Object}
         * @private
         */
L
lang 已提交
85
        this._theme = zrUtil.clone(theme, true);
L
lang 已提交
86

L
lang 已提交
87 88 89 90
        /**
         * @type {Array.<module:echarts/view/Chart>}
         * @private
         */
L
lang 已提交
91
        this._chartsList = [];
L
lang 已提交
92 93 94 95 96

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

L
lang 已提交
99 100 101 102
        /**
         * @type {Array.<module:echarts/view/Component>}
         * @private
         */
L
lang 已提交
103
        this._componentsList = [];
L
lang 已提交
104 105 106 107 108

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

L
lang 已提交
111
        /**
L
lang 已提交
112
         * @type {module:echarts/ExtensionAPI}
L
lang 已提交
113 114
         * @private
         */
115
        this._api = new ExtensionAPI(this);
L
lang 已提交
116

L
lang 已提交
117 118 119 120
        /**
         * @type {module:echarts/CoordinateSystem}
         * @private
         */
L
lang 已提交
121
        this._coordinateSystem = new CoordinateSystemManager();
L
lang 已提交
122

L
lang 已提交
123 124
        Eventful.call(this);

L
lang 已提交
125 126 127 128 129 130
        /**
         * @type {module:echarts~MessageCenter}
         * @private
         */
        this._messageCenter = new MessageCenter();

L
lang 已提交
131 132
        // Init mouse events
        this._initEvents();
L
Resize  
lang 已提交
133 134 135

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

L
tweak  
lang 已提交
138
    var echartsProto = ECharts.prototype;
L
lang 已提交
139

140 141 142
    /**
     * @return {HTMLDomElement}
     */
L
tweak  
lang 已提交
143 144 145
    echartsProto.getDom = function () {
        return this._dom;
    };
L
lang 已提交
146

147 148 149
    /**
     * @return {module:zrender~ZRender}
     */
L
tweak  
lang 已提交
150 151 152
    echartsProto.getZr = function () {
        return this._zr;
    };
L
lang 已提交
153

154 155 156 157 158 159
    /**
     * @param {Object} option
     * @param {boolean} notMerge
     * @param {boolean} [notRefreshImmediately=false]
     */
    echartsProto.setOption = function (option, notMerge, notRefreshImmediately) {
L
tweak  
lang 已提交
160 161
        // PENDING
        option = zrUtil.clone(option, true);
162

L
tweak  
lang 已提交
163 164 165
        each(optionPreprocessorFuncs, function (preProcess) {
            preProcess(option);
        });
L
lang 已提交
166

L
tweak  
lang 已提交
167 168 169 170 171 172 173 174 175
        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 已提交
176

177
        prepareView.call(this, 'component', ecModel);
L
lang 已提交
178

179
        prepareView.call(this, 'chart', ecModel);
L
lang 已提交
180

181
        updateMethods.update.call(this);
L
lang 已提交
182

183
        !notRefreshImmediately && this._zr.refreshImmediately();
L
tweak  
lang 已提交
184
    };
L
lang 已提交
185

L
Tweak  
lang 已提交
186 187 188 189 190 191
    /**
     * @DEPRECATED
     */
    echartsProto.setTheme = function () {
        console.log('ECharts#setTheme() is DEPRECATED in ECharts 3.0');
    };
L
tweak  
lang 已提交
192 193 194 195 196 197
    /**
     * @return {module:echarts/model/Global}
     */
    echartsProto.getModel = function () {
        return this._model;
    };
L
lang 已提交
198

L
tweak  
lang 已提交
199 200 201 202 203 204
    /**
     * @return {number}
     */
    echartsProto.getWidth = function () {
        return this._zr.getWidth();
    };
L
lang 已提交
205

L
tweak  
lang 已提交
206 207 208 209 210 211
    /**
     * @return {number}
     */
    echartsProto.getHeight = function () {
        return this._zr.getHeight();
    };
L
lang 已提交
212

213

214 215 216 217 218 219 220 221
    var updateMethods = {

        /**
         * @param {Object} payload
         * @private
         */
        update: function (payload) {
            console.time && console.time('update');
L
lang 已提交
222

223
            var ecModel = this._model;
L
lang 已提交
224

225 226 227 228 229
            ecModel.restoreData();

            // 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 已提交
230

231
            processData.call(this, ecModel);
L
lang 已提交
232

233
            stackSeriesData.call(this, ecModel);
L
lang 已提交
234

235
            this._coordinateSystem.update(ecModel, this._api);
L
lang 已提交
236

237
            doLayout.call(this, ecModel, payload);
238

239
            doVisualCoding.call(this, ecModel, payload);
240

241
            doRender.call(this, ecModel, payload);
242

243 244 245 246 247 248 249 250 251
            // 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 已提交
252
            }
253
            backgroundColor && (this._dom.style.backgroundColor = backgroundColor);
L
lang 已提交
254

255 256
            console.time && console.timeEnd('update');
        },
257

258 259 260 261 262 263 264
        // PENDING
        /**
         * @param {Object} payload
         * @private
         */
        updateView: function (payload) {
            var ecModel = this._model;
265

266
            doLayout.call(this, ecModel, payload);
267

268
            doVisualCoding.call(this, ecModel, payload);
269

270 271
            invokeUpdateMethod.call(this, 'updateView', ecModel, payload);
        },
272

273 274 275 276 277 278
        /**
         * @param {Object} payload
         * @private
         */
        updateVisual: function (payload) {
            var ecModel = this._model;
279

280
            doVisualCoding.call(this, ecModel, payload);
281

282 283
            invokeUpdateMethod.call(this, 'updateVisual', ecModel, payload);
        },
284

285 286 287 288 289 290
        /**
         * @param {Object} payload
         * @private
         */
        updateLayout: function (payload) {
            var ecModel = this._model;
291

292
            doLayout.call(this, ecModel, payload);
293

294 295
            invokeUpdateMethod.call(this, 'updateLayout', ecModel, payload);
        },
L
lang 已提交
296

297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
        /**
         * @param {Object} payload
         * @private
         */
        highlight: function (payload) {
            toggleHighlight.call(this, 'highlight', payload);
        },

        /**
         * @param {Object} payload
         * @private
         */
        downplay: function (payload) {
            toggleHighlight.call(this, 'downplay', payload);
        }
312 313 314 315 316 317 318

    };

    /**
     * @param {Object} payload
     * @private
     */
319
    function toggleHighlight(method, payload) {
320
        var ecModel = this._model;
321 322 323 324

        ecModel.eachComponent(
            {mainType: 'series', query: payload},
            function (seriesModel) {
P
tweak  
pah100 已提交
325
                var chartView = this._chartsMap[seriesModel.id];
326 327 328 329 330 331
                if (chartView) {
                    chartView[method](seriesModel, ecModel, this._api, payload);
                }
            },
            this
        );
332
    }
333

L
Resize  
lang 已提交
334 335 336
    /**
     * Resize the chart
     */
L
tweak  
lang 已提交
337
    echartsProto.resize = function () {
L
Resize  
lang 已提交
338
        this._zr.resize();
L
lang 已提交
339
        updateMethods.update.call(this);
L
tweak  
lang 已提交
340
    };
P
pah100 已提交
341

L
lang 已提交
342
    /**
L
Resize  
lang 已提交
343 344
     * @param {Object} eventObj
     * @return {Object}
L
lang 已提交
345 346 347 348 349 350 351
     */
    echartsProto.makeActionFromEvent = function (eventObj) {
        var payload = zrUtil.extend({}, eventObj);
        payload.type = eventActionMap[eventObj.type];
        return payload;
    };

L
tweak  
lang 已提交
352 353 354 355
    /**
     * @pubilc
     * @param {Object} payload
     * @param {string} [payload.type] Action type
P
pah100 已提交
356
     * @param {boolean} [silent=false] Whether trigger event.
L
tweak  
lang 已提交
357 358
     * @param {number} [payload.from] From uid
     */
L
lang 已提交
359
    echartsProto.dispatchAction = function (payload, silent) {
L
tweak  
lang 已提交
360 361
        var actionWrap = actions[payload.type];
        if (actionWrap) {
L
lang 已提交
362 363
            var actionInfo = actionWrap.actionInfo;
            var updateMethod = actionInfo.update || 'update';
L
tweak  
lang 已提交
364
            actionWrap.action(payload, this._model);
365
            updateMethod !== 'none' && updateMethods[updateMethod].call(this, payload);
L
lang 已提交
366

P
pah100 已提交
367 368 369 370 371
            if (!silent) {
                // Emit event outside
                // Convert type to eventType
                var eventObj = zrUtil.extend({}, payload);
                eventObj.type = actionInfo.event || eventObj.type;
L
lang 已提交
372
                this._messageCenter.trigger(eventObj.type, eventObj);
P
pah100 已提交
373
            }
L
tweak  
lang 已提交
374 375
        }
    };
376

L
tweak  
lang 已提交
377 378 379 380
    /**
     * @param {string} methodName
     * @private
     */
381
    function invokeUpdateMethod(methodName, ecModel, payload) {
382
        var api = this._api;
L
lang 已提交
383

L
tweak  
lang 已提交
384 385 386 387
        // Update all components
        each(this._componentsList, function (component) {
            var componentModel = component.__model;
            component[methodName](componentModel, ecModel, api, payload);
388

L
tweak  
lang 已提交
389 390
            updateZ(componentModel, component);
        }, this);
L
lang 已提交
391

L
tweak  
lang 已提交
392 393
        // Upate all charts
        ecModel.eachSeries(function (seriesModel, idx) {
P
tweak  
pah100 已提交
394
            var chart = this._chartsMap[seriesModel.id];
L
tweak  
lang 已提交
395
            chart[methodName](seriesModel, ecModel, api, payload);
396

L
tweak  
lang 已提交
397 398
            updateZ(seriesModel, chart);
        }, this);
399

400
    }
L
lang 已提交
401

L
lang 已提交
402
    /**
L
Tweak  
lang 已提交
403
     * Prepare view instances of charts and components
L
lang 已提交
404 405 406
     * @param  {module:echarts/model/Global} ecModel
     * @private
     */
407
    function prepareView(type, ecModel) {
L
Tweak  
lang 已提交
408 409 410
        var isComponent = type === 'component';
        var viewList = isComponent ? this._componentsList : this._chartsList;
        var viewMap = isComponent ? this._componentsMap : this._chartsMap;
L
tweak  
lang 已提交
411
        var zr = this._zr;
L
lang 已提交
412

L
Tweak  
lang 已提交
413 414
        for (var i = 0; i < viewList.length; i++) {
            viewList[i].__keepAlive = false;
L
tweak  
lang 已提交
415
        }
L
lang 已提交
416

L
Tweak  
lang 已提交
417 418 419 420
        ecModel[isComponent ? 'eachComponent' : 'eachSeries'](function (componentType, model) {
            if (isComponent) {
                if (componentType === 'series') {
                    return;
L
lang 已提交
421
                }
422
            }
L
tweak  
lang 已提交
423
            else {
L
Tweak  
lang 已提交
424
                model = componentType;
L
tweak  
lang 已提交
425 426
            }

P
tweak  
pah100 已提交
427
            var view = viewMap[model.id];
L
Tweak  
lang 已提交
428 429 430
            if (!view) {
                var classType = ComponentModel.parseClassType(model.type);
                var Clazz = isComponent
L
tweak  
lang 已提交
431
                    ? ComponentView.getClass(classType.main, classType.sub)
L
Tweak  
lang 已提交
432
                    : ChartView.getClass(classType.sub);
L
tweak  
lang 已提交
433
                if (Clazz) {
L
Tweak  
lang 已提交
434 435
                    view = new Clazz();
                    view.init(ecModel, this._api);
P
tweak  
pah100 已提交
436
                    viewMap[model.id] = view;
L
Tweak  
lang 已提交
437 438 439 440 441
                    viewList.push(view);
                    zr.add(view.group);
                }
                else {
                    // Error
L
lang 已提交
442
                }
443
            }
L
Tweak  
lang 已提交
444 445

            view.__keepAlive = true;
P
tweak  
pah100 已提交
446
            view.__id = model.id;
L
Tweak  
lang 已提交
447
            view.__model = model;
L
tweak  
lang 已提交
448 449
        }, this);

L
Tweak  
lang 已提交
450 451 452 453 454 455 456
        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 已提交
457 458 459 460 461
            }
            else {
                i++;
            }
        }
462 463
    }

L
tweak  
lang 已提交
464 465 466 467 468 469
    /**
     * Processor data in each series
     *
     * @param {module:echarts/model/Global} ecModel
     * @private
     */
470
    function processData(ecModel) {
L
tweak  
lang 已提交
471 472 473
        each(PROCESSOR_STAGES, function (stage) {
            each(dataProcessorFuncs[stage] || [], function (process) {
                process(ecModel);
L
lang 已提交
474
            });
L
tweak  
lang 已提交
475
        });
476
    }
L
lang 已提交
477

L
tweak  
lang 已提交
478 479 480
    /**
     * @private
     */
481
    function stackSeriesData(ecModel) {
L
tweak  
lang 已提交
482 483 484 485 486 487 488 489
        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 已提交
490
                }
L
tweak  
lang 已提交
491 492 493
                stackedDataMap[stack] = data;
            }
        });
494
    }
L
lang 已提交
495

L
tweak  
lang 已提交
496
    /**
L
lang 已提交
497
     * Layout before each chart render there series, after visual coding and data processing
L
tweak  
lang 已提交
498 499 500 501
     *
     * @param {module:echarts/model/Global} ecModel
     * @private
     */
502
    function doLayout(ecModel, payload) {
503
        var api = this._api;
L
tweak  
lang 已提交
504 505 506
        each(layoutFuncs, function (layout) {
            layout(ecModel, api, payload);
        });
507
    }
L
lang 已提交
508

L
tweak  
lang 已提交
509 510 511 512 513 514
    /**
     * Code visual infomation from data after data processing
     *
     * @param {module:echarts/model/Global} ecModel
     * @private
     */
515
    function doVisualCoding(ecModel, payload) {
L
tweak  
lang 已提交
516 517 518
        each(VISUAL_CODING_STAGES, function (stage) {
            each(visualCodingFuncs[stage] || [], function (visualCoding) {
                visualCoding(ecModel, payload);
L
lang 已提交
519
            });
L
tweak  
lang 已提交
520
        });
521
    }
L
lang 已提交
522

L
tweak  
lang 已提交
523 524 525 526
    /**
     * Render each chart and component
     * @private
     */
527
    function doRender(ecModel, payload) {
528
        var api = this._api;
L
tweak  
lang 已提交
529 530 531 532 533 534 535 536 537 538 539 540 541 542
        // 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) {
P
tweak  
pah100 已提交
543
            var chart = this._chartsMap[seriesModel.id];
L
tweak  
lang 已提交
544 545 546 547 548 549
            chart.__keepAlive = true;
            chart.render(seriesModel, ecModel, api, payload);

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

L
lang 已提交
550
        // Remove groups of unrendered charts
L
tweak  
lang 已提交
551 552 553 554 555
        each(this._chartsList, function (chart) {
            if (!chart.__keepAlive) {
                chart.remove(ecModel, api);
            }
        }, this);
556
    }
L
lang 已提交
557

L
lang 已提交
558 559 560 561 562 563 564 565 566 567 568 569 570
    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) {
571
                    var hostModel = el.hostModel || ecModel.getSeriesByIndex(el.seriesIndex);
L
lang 已提交
572 573 574 575 576 577 578
                    var params = hostModel && hostModel.getDataParams(el.dataIndex) || {};
                    params.event = e;
                    params.type = eveName;
                    this.trigger(eveName, params);
                }
            }, this);
        }, this);
L
lang 已提交
579 580 581 582 583 584

        zrUtil.each(eventActionMap, function (actionType, eventType) {
            this._messageCenter.on(eventType, function (event) {
                this.trigger(eventType, event);
            }, this);
        }, this);
L
lang 已提交
585 586
    };

L
lang 已提交
587 588 589 590 591 592 593 594 595
    /**
     * @return {boolean]
     */
    echartsProto.isDisposed = function () {
        return this._disposed;
    };
    /**
     * Dispose instance
     */
L
tweak  
lang 已提交
596
    echartsProto.dispose = function () {
L
lang 已提交
597 598
        this._disposed = true;

L
tweak  
lang 已提交
599 600 601 602 603 604
        each(this._components, function (component) {
            component.dispose();
        });
        each(this._charts, function (chart) {
            chart.dispose();
        });
L
lang 已提交
605

L
Tweak  
lang 已提交
606
        this._zr.dispose();
L
lang 已提交
607 608

        instances[this.id] = null;
L
lang 已提交
609 610
    };

L
lang 已提交
611 612
    zrUtil.mixin(ECharts, Eventful);

L
lang 已提交
613 614 615 616 617 618 619 620 621 622 623 624 625 626
    /**
     * @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 已提交
627 628 629 630
    /**
     * @type {Array.<Function>}
     * @inner
     */
P
pah100 已提交
631 632
    var actions = [];

L
lang 已提交
633
    /**
L
lang 已提交
634
     * Map eventType to actionType
L
lang 已提交
635 636 637 638
     * @type {Object}
     */
    var eventActionMap = {};

L
lang 已提交
639 640 641 642
    /**
     * @type {Array.<Function>}
     * @inner
     */
643 644
    var layoutFuncs = [];

L
lang 已提交
645 646 647 648 649 650 651
    /**
     * Data processor functions of each stage
     * @type {Array.<Object.<string, Function>>}
     * @inner
     */
    var dataProcessorFuncs = {};

652 653 654 655 656 657
    /**
     * @type {Array.<Function>}
     * @inner
     */
    var optionPreprocessorFuncs = [];

L
lang 已提交
658 659 660 661 662
    /**
     * Visual coding functions of each stage
     * @type {Array.<Object.<string, Function>>}
     * @inner
     */
663
    var visualCodingFuncs = {};
L
lang 已提交
664

L
lang 已提交
665 666 667 668 669 670
    var instances = {};
    var connectedGroups = {};

    var idBase = new Date() - 0;
    var groupIdBase = new Date() - 0;
    var DOM_ATTRIBUTE_KEY = '_echarts_instance_';
L
lang 已提交
671
    /**
L
lang 已提交
672
     * @alias module:echarts
L
lang 已提交
673
     */
L
lang 已提交
674 675 676 677 678 679 680 681 682
    var echarts = {
        /**
         * @type {number}
         */
        version: '3.0.0',
        dependencies: {
            zrender: '3.0.0'
        }
    };
L
lang 已提交
683

L
tweak  
lang 已提交
684 685 686 687 688 689
    /**
     * @param {HTMLDomElement} dom
     * @param {Object} [theme]
     * @param {Object} opts
     */
    echarts.init = function (dom, theme, opts) {
L
lang 已提交
690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705
        // 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) {
L
lang 已提交
706 707
            // FIXME
            chart._messageCenter.on(eventType, function (event) {
L
lang 已提交
708 709 710
                if (connectedGroups[chart.group]) {
                    chart.__connectedActionDispatching = true;
                    for (var id in instances) {
L
tweak  
lang 已提交
711
                        var action = chart.makeActionFromEvent(event);
L
lang 已提交
712 713
                        var otherChart = instances[id];
                        if (otherChart !== chart && otherChart.group === chart.group) {
L
tweak  
lang 已提交
714
                            if (!otherChart.__connectedActionDispatching) {
L
lang 已提交
715
                                otherChart.dispatchAction(action);
L
tweak  
lang 已提交
716
                            }
L
lang 已提交
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
                        }
                    }
                    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 已提交
787
    };
L
lang 已提交
788

L
tweak  
lang 已提交
789 790 791 792 793 794 795
    /**
     * Register option preprocessor
     * @param {Function} preprocessorFunc
     */
    echarts.registerPreprocessor = function (preprocessorFunc) {
        optionPreprocessorFuncs.push(preprocessorFunc);
    };
796

L
tweak  
lang 已提交
797 798 799 800 801 802 803 804 805 806 807
    /**
     * @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 已提交
808

L
tweak  
lang 已提交
809 810 811 812 813 814 815 816 817 818 819
    /**
     * 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 已提交
820 821 822 823
     * @param {string} [actionInfo.event]
     * @param {string} [actionInfo.update]
     * @param {string} [eventName]
     * @param {Function} action
L
tweak  
lang 已提交
824
     */
L
lang 已提交
825 826 827 828 829
    echarts.registerAction = function (actionInfo, eventName, action) {
        if (typeof eventName === 'function') {
            action = eventName;
            eventName = '';
        }
L
tweak  
lang 已提交
830 831
        var actionType = zrUtil.isObject(actionInfo)
            ? actionInfo.type
L
lang 已提交
832 833 834
            : ([actionInfo, actionInfo = {
                event: eventName
            }][0]);
L
lang 已提交
835 836

        actionInfo.event = actionInfo.event || actionType;
L
lang 已提交
837
        eventName = actionInfo.event;
838

L
tweak  
lang 已提交
839 840 841
        if (!actions[actionType]) {
            actions[actionType] = {action: action, actionInfo: actionInfo};
        }
L
lang 已提交
842
        eventActionMap[eventName] = actionType;
L
tweak  
lang 已提交
843
    };
P
pah100 已提交
844

L
tweak  
lang 已提交
845 846 847 848 849 850 851
    /**
     * @param {string} type
     * @param {*} CoordinateSystem
     */
    echarts.registerCoordinateSystem = function (type, CoordinateSystem) {
        CoordinateSystemManager.register(type, CoordinateSystem);
    };
L
lang 已提交
852

L
tweak  
lang 已提交
853 854 855
    /**
     * @param {*} layout
     */
L
lang 已提交
856
    echarts.registerLayout = function (layout) {
L
tweak  
lang 已提交
857
        // PENDING All functions ?
858 859
        if (zrUtil.indexOf(layoutFuncs, layout) < 0) {
            layoutFuncs.push(layout);
L
tweak  
lang 已提交
860 861
        }
    };
L
lang 已提交
862

L
tweak  
lang 已提交
863 864 865 866 867 868 869 870 871 872 873
    /**
     * @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 已提交
874

L
tweak  
lang 已提交
875
    /**
L
lang 已提交
876
     * @param {echarts/scale/*} scale
L
tweak  
lang 已提交
877
     */
L
Tweak  
lang 已提交
878 879 880
    // echarts.extendScale = function (opts) {
    //     return Scale.extend(opts);
    // };
L
lang 已提交
881

L
tweak  
lang 已提交
882 883 884 885 886 887
    /**
     * @param {Object} opts
     */
    echarts.extendChartView = function (opts) {
        return ChartView.extend(opts);
    };
L
Update  
lang 已提交
888

L
tweak  
lang 已提交
889 890 891 892 893 894
    /**
     * @param {Object} opts
     */
    echarts.extendComponentModel = function (opts) {
        return ComponentModel.extend(opts);
    };
L
Update  
lang 已提交
895

L
tweak  
lang 已提交
896 897 898 899 900 901
    /**
     * @param {Object} opts
     */
    echarts.extendSeriesModel = function (opts) {
        return SeriesModel.extend(opts);
    };
P
pah100 已提交
902

L
tweak  
lang 已提交
903 904 905 906 907
    /**
     * @param {Object} opts
     */
    echarts.extendComponentView = function (opts) {
        return ComponentView.extend(opts);
L
lang 已提交
908 909
    };

L
lang 已提交
910
    echarts.registerVisualCoding('echarts', require('./visual/seriesColor'));
911 912
    echarts.registerPreprocessor(require('./preprocessor/backwardCompat'));

913 914 915 916 917 918 919 920 921 922 923 924
    // Default action
    echarts.registerAction({
        type: 'highlight',
        event: 'highlight',
        update: 'highlight'
    }, zrUtil.noop);
    echarts.registerAction({
        type: 'downplay',
        event: 'downplay',
        update: 'downplay'
    }, zrUtil.noop);

L
lang 已提交
925 926
    return echarts;
});