echarts.js 25.8 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
            }
L
lang 已提交
253 254 255 256 257 258 259 260
            if (env.node) {
                this._zr.configLayer(0, {
                    clearColor: backgroundColor
                });
            }
            else {
                backgroundColor && (this._dom.style.backgroundColor = backgroundColor);
            }
L
lang 已提交
261

262 263
            console.time && console.timeEnd('update');
        },
264

265 266 267 268 269 270 271
        // PENDING
        /**
         * @param {Object} payload
         * @private
         */
        updateView: function (payload) {
            var ecModel = this._model;
272

273
            doLayout.call(this, ecModel, payload);
274

275
            doVisualCoding.call(this, ecModel, payload);
276

277 278
            invokeUpdateMethod.call(this, 'updateView', ecModel, payload);
        },
279

280 281 282 283 284 285
        /**
         * @param {Object} payload
         * @private
         */
        updateVisual: function (payload) {
            var ecModel = this._model;
286

287
            doVisualCoding.call(this, ecModel, payload);
288

289 290
            invokeUpdateMethod.call(this, 'updateVisual', ecModel, payload);
        },
291

292 293 294 295 296 297
        /**
         * @param {Object} payload
         * @private
         */
        updateLayout: function (payload) {
            var ecModel = this._model;
298

299
            doLayout.call(this, ecModel, payload);
300

301 302
            invokeUpdateMethod.call(this, 'updateLayout', ecModel, payload);
        },
L
lang 已提交
303

304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
        /**
         * @param {Object} payload
         * @private
         */
        highlight: function (payload) {
            toggleHighlight.call(this, 'highlight', payload);
        },

        /**
         * @param {Object} payload
         * @private
         */
        downplay: function (payload) {
            toggleHighlight.call(this, 'downplay', payload);
        }
319 320 321 322 323 324 325

    };

    /**
     * @param {Object} payload
     * @private
     */
326
    function toggleHighlight(method, payload) {
327
        var ecModel = this._model;
328 329 330 331

        ecModel.eachComponent(
            {mainType: 'series', query: payload},
            function (seriesModel) {
P
tweak  
pah100 已提交
332
                var chartView = this._chartsMap[seriesModel.id];
333 334 335 336 337 338
                if (chartView) {
                    chartView[method](seriesModel, ecModel, this._api, payload);
                }
            },
            this
        );
339
    }
340

L
Resize  
lang 已提交
341 342 343
    /**
     * Resize the chart
     */
L
tweak  
lang 已提交
344
    echartsProto.resize = function () {
L
Resize  
lang 已提交
345
        this._zr.resize();
L
lang 已提交
346
        updateMethods.update.call(this);
L
tweak  
lang 已提交
347
    };
P
pah100 已提交
348

L
lang 已提交
349
    /**
L
Resize  
lang 已提交
350 351
     * @param {Object} eventObj
     * @return {Object}
L
lang 已提交
352 353 354 355 356 357 358
     */
    echartsProto.makeActionFromEvent = function (eventObj) {
        var payload = zrUtil.extend({}, eventObj);
        payload.type = eventActionMap[eventObj.type];
        return payload;
    };

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

P
pah100 已提交
374 375 376 377 378
            if (!silent) {
                // Emit event outside
                // Convert type to eventType
                var eventObj = zrUtil.extend({}, payload);
                eventObj.type = actionInfo.event || eventObj.type;
L
lang 已提交
379
                this._messageCenter.trigger(eventObj.type, eventObj);
P
pah100 已提交
380
            }
L
tweak  
lang 已提交
381 382
        }
    };
383

L
tweak  
lang 已提交
384 385 386 387
    /**
     * @param {string} methodName
     * @private
     */
388
    function invokeUpdateMethod(methodName, ecModel, payload) {
389
        var api = this._api;
L
lang 已提交
390

L
tweak  
lang 已提交
391 392 393 394
        // Update all components
        each(this._componentsList, function (component) {
            var componentModel = component.__model;
            component[methodName](componentModel, ecModel, api, payload);
395

L
tweak  
lang 已提交
396 397
            updateZ(componentModel, component);
        }, this);
L
lang 已提交
398

L
tweak  
lang 已提交
399 400
        // Upate all charts
        ecModel.eachSeries(function (seriesModel, idx) {
P
tweak  
pah100 已提交
401
            var chart = this._chartsMap[seriesModel.id];
L
tweak  
lang 已提交
402
            chart[methodName](seriesModel, ecModel, api, payload);
403

L
tweak  
lang 已提交
404 405
            updateZ(seriesModel, chart);
        }, this);
406

407
    }
L
lang 已提交
408

L
lang 已提交
409
    /**
L
Tweak  
lang 已提交
410
     * Prepare view instances of charts and components
L
lang 已提交
411 412 413
     * @param  {module:echarts/model/Global} ecModel
     * @private
     */
414
    function prepareView(type, ecModel) {
L
Tweak  
lang 已提交
415 416 417
        var isComponent = type === 'component';
        var viewList = isComponent ? this._componentsList : this._chartsList;
        var viewMap = isComponent ? this._componentsMap : this._chartsMap;
L
tweak  
lang 已提交
418
        var zr = this._zr;
L
lang 已提交
419

L
Tweak  
lang 已提交
420 421
        for (var i = 0; i < viewList.length; i++) {
            viewList[i].__keepAlive = false;
L
tweak  
lang 已提交
422
        }
L
lang 已提交
423

L
Tweak  
lang 已提交
424 425 426 427
        ecModel[isComponent ? 'eachComponent' : 'eachSeries'](function (componentType, model) {
            if (isComponent) {
                if (componentType === 'series') {
                    return;
L
lang 已提交
428
                }
429
            }
L
tweak  
lang 已提交
430
            else {
L
Tweak  
lang 已提交
431
                model = componentType;
L
tweak  
lang 已提交
432 433
            }

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

            view.__keepAlive = true;
P
tweak  
pah100 已提交
454
            view.__id = model.id;
L
Tweak  
lang 已提交
455
            view.__model = model;
L
tweak  
lang 已提交
456 457
        }, this);

L
Tweak  
lang 已提交
458 459 460 461 462 463 464
        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 已提交
465 466 467 468 469
            }
            else {
                i++;
            }
        }
470 471
    }

L
tweak  
lang 已提交
472 473 474 475 476 477
    /**
     * Processor data in each series
     *
     * @param {module:echarts/model/Global} ecModel
     * @private
     */
478
    function processData(ecModel) {
L
tweak  
lang 已提交
479 480 481
        each(PROCESSOR_STAGES, function (stage) {
            each(dataProcessorFuncs[stage] || [], function (process) {
                process(ecModel);
L
lang 已提交
482
            });
L
tweak  
lang 已提交
483
        });
484
    }
L
lang 已提交
485

L
tweak  
lang 已提交
486 487 488
    /**
     * @private
     */
489
    function stackSeriesData(ecModel) {
L
tweak  
lang 已提交
490 491 492 493 494 495 496 497
        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 已提交
498
                }
L
tweak  
lang 已提交
499 500 501
                stackedDataMap[stack] = data;
            }
        });
502
    }
L
lang 已提交
503

L
tweak  
lang 已提交
504
    /**
L
lang 已提交
505
     * Layout before each chart render there series, after visual coding and data processing
L
tweak  
lang 已提交
506 507 508 509
     *
     * @param {module:echarts/model/Global} ecModel
     * @private
     */
510
    function doLayout(ecModel, payload) {
511
        var api = this._api;
L
tweak  
lang 已提交
512 513 514
        each(layoutFuncs, function (layout) {
            layout(ecModel, api, payload);
        });
515
    }
L
lang 已提交
516

L
tweak  
lang 已提交
517 518 519 520 521 522
    /**
     * Code visual infomation from data after data processing
     *
     * @param {module:echarts/model/Global} ecModel
     * @private
     */
523
    function doVisualCoding(ecModel, payload) {
L
tweak  
lang 已提交
524 525 526
        each(VISUAL_CODING_STAGES, function (stage) {
            each(visualCodingFuncs[stage] || [], function (visualCoding) {
                visualCoding(ecModel, payload);
L
lang 已提交
527
            });
L
tweak  
lang 已提交
528
        });
529
    }
L
lang 已提交
530

L
tweak  
lang 已提交
531 532 533 534
    /**
     * Render each chart and component
     * @private
     */
535
    function doRender(ecModel, payload) {
536
        var api = this._api;
L
tweak  
lang 已提交
537 538 539 540 541 542 543 544 545 546 547 548 549 550
        // 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 已提交
551
            var chart = this._chartsMap[seriesModel.id];
L
tweak  
lang 已提交
552 553 554 555 556 557
            chart.__keepAlive = true;
            chart.render(seriesModel, ecModel, api, payload);

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

L
lang 已提交
558
        // Remove groups of unrendered charts
L
tweak  
lang 已提交
559 560 561 562 563
        each(this._chartsList, function (chart) {
            if (!chart.__keepAlive) {
                chart.remove(ecModel, api);
            }
        }, this);
564
    }
L
lang 已提交
565

L
lang 已提交
566 567 568 569 570 571 572 573 574 575 576 577 578
    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) {
579
                    var hostModel = el.hostModel || ecModel.getSeriesByIndex(el.seriesIndex);
L
lang 已提交
580 581 582 583 584 585 586
                    var params = hostModel && hostModel.getDataParams(el.dataIndex) || {};
                    params.event = e;
                    params.type = eveName;
                    this.trigger(eveName, params);
                }
            }, this);
        }, this);
L
lang 已提交
587 588 589 590 591 592

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

L
lang 已提交
595 596 597 598 599 600 601 602 603
    /**
     * @return {boolean]
     */
    echartsProto.isDisposed = function () {
        return this._disposed;
    };
    /**
     * Dispose instance
     */
L
tweak  
lang 已提交
604
    echartsProto.dispose = function () {
L
lang 已提交
605 606
        this._disposed = true;

L
tweak  
lang 已提交
607 608 609 610 611 612
        each(this._components, function (component) {
            component.dispose();
        });
        each(this._charts, function (chart) {
            chart.dispose();
        });
L
lang 已提交
613

L
Tweak  
lang 已提交
614
        this._zr.dispose();
L
lang 已提交
615 616

        instances[this.id] = null;
L
lang 已提交
617 618
    };

L
lang 已提交
619 620
    zrUtil.mixin(ECharts, Eventful);

L
lang 已提交
621 622 623 624 625 626 627 628 629 630 631 632 633 634
    /**
     * @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 已提交
635 636 637 638
    /**
     * @type {Array.<Function>}
     * @inner
     */
P
pah100 已提交
639 640
    var actions = [];

L
lang 已提交
641
    /**
L
lang 已提交
642
     * Map eventType to actionType
L
lang 已提交
643 644 645 646
     * @type {Object}
     */
    var eventActionMap = {};

L
lang 已提交
647 648 649 650
    /**
     * @type {Array.<Function>}
     * @inner
     */
651 652
    var layoutFuncs = [];

L
lang 已提交
653 654 655 656 657 658 659
    /**
     * Data processor functions of each stage
     * @type {Array.<Object.<string, Function>>}
     * @inner
     */
    var dataProcessorFuncs = {};

660 661 662 663 664 665
    /**
     * @type {Array.<Function>}
     * @inner
     */
    var optionPreprocessorFuncs = [];

L
lang 已提交
666 667 668 669 670
    /**
     * Visual coding functions of each stage
     * @type {Array.<Object.<string, Function>>}
     * @inner
     */
671
    var visualCodingFuncs = {};
L
lang 已提交
672

L
lang 已提交
673 674 675 676 677 678
    var instances = {};
    var connectedGroups = {};

    var idBase = new Date() - 0;
    var groupIdBase = new Date() - 0;
    var DOM_ATTRIBUTE_KEY = '_echarts_instance_';
L
lang 已提交
679
    /**
L
lang 已提交
680
     * @alias module:echarts
L
lang 已提交
681
     */
L
lang 已提交
682 683 684 685 686 687 688 689 690
    var echarts = {
        /**
         * @type {number}
         */
        version: '3.0.0',
        dependencies: {
            zrender: '3.0.0'
        }
    };
L
lang 已提交
691

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

L
tweak  
lang 已提交
797 798 799 800 801 802 803
    /**
     * Register option preprocessor
     * @param {Function} preprocessorFunc
     */
    echarts.registerPreprocessor = function (preprocessorFunc) {
        optionPreprocessorFuncs.push(preprocessorFunc);
    };
804

L
tweak  
lang 已提交
805 806 807 808 809 810 811 812 813 814 815
    /**
     * @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 已提交
816

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

        actionInfo.event = actionInfo.event || actionType;
L
lang 已提交
845
        eventName = actionInfo.event;
846

L
tweak  
lang 已提交
847 848 849
        if (!actions[actionType]) {
            actions[actionType] = {action: action, actionInfo: actionInfo};
        }
L
lang 已提交
850
        eventActionMap[eventName] = actionType;
L
tweak  
lang 已提交
851
    };
P
pah100 已提交
852

L
tweak  
lang 已提交
853 854 855 856 857 858 859
    /**
     * @param {string} type
     * @param {*} CoordinateSystem
     */
    echarts.registerCoordinateSystem = function (type, CoordinateSystem) {
        CoordinateSystemManager.register(type, CoordinateSystem);
    };
L
lang 已提交
860

L
tweak  
lang 已提交
861 862 863
    /**
     * @param {*} layout
     */
L
lang 已提交
864
    echarts.registerLayout = function (layout) {
L
tweak  
lang 已提交
865
        // PENDING All functions ?
866 867
        if (zrUtil.indexOf(layoutFuncs, layout) < 0) {
            layoutFuncs.push(layout);
L
tweak  
lang 已提交
868 869
        }
    };
L
lang 已提交
870

L
tweak  
lang 已提交
871 872 873 874 875 876 877 878 879 880 881
    /**
     * @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 已提交
882

L
tweak  
lang 已提交
883
    /**
L
lang 已提交
884
     * @param {echarts/scale/*} scale
L
tweak  
lang 已提交
885
     */
L
Tweak  
lang 已提交
886 887 888
    // echarts.extendScale = function (opts) {
    //     return Scale.extend(opts);
    // };
L
lang 已提交
889

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

L
tweak  
lang 已提交
897 898 899 900 901 902
    /**
     * @param {Object} opts
     */
    echarts.extendComponentModel = function (opts) {
        return ComponentModel.extend(opts);
    };
L
Update  
lang 已提交
903

L
tweak  
lang 已提交
904 905 906 907 908 909
    /**
     * @param {Object} opts
     */
    echarts.extendSeriesModel = function (opts) {
        return SeriesModel.extend(opts);
    };
P
pah100 已提交
910

L
tweak  
lang 已提交
911 912 913 914 915
    /**
     * @param {Object} opts
     */
    echarts.extendComponentView = function (opts) {
        return ComponentView.extend(opts);
L
lang 已提交
916 917
    };

918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937
    /**
     * ZRender need a canvas context to do measureText.
     * But in node environment canvas may be created by node-canvas.
     * So we need to specify how to create a canvas instead of using document.createElement('canvas')
     *
     * Be careful of using it in the browser.
     *
     * @param {Function} creator
     * @example
     *     var Canvas = require('canvas');
     *     var echarts = require('echarts');
     *     echarts.setCanvasCreator(function () {
     *         // Small size is enough.
     *         return new Canvas(32, 32);
     *     });
     */
    echarts.setCanvasCreator = function (creator) {
        zrUtil.createCanvas = creator;
    };

L
lang 已提交
938
    echarts.registerVisualCoding('echarts', require('./visual/seriesColor'));
939 940
    echarts.registerPreprocessor(require('./preprocessor/backwardCompat'));

941 942 943 944 945 946 947 948 949 950 951 952
    // Default action
    echarts.registerAction({
        type: 'highlight',
        event: 'highlight',
        update: 'highlight'
    }, zrUtil.noop);
    echarts.registerAction({
        type: 'downplay',
        event: 'downplay',
        update: 'downplay'
    }, zrUtil.noop);

L
lang 已提交
953 954
    return echarts;
});