echarts.js 24.1 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);
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 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
    /**
     * @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;
        var seriesModel;
        if (payload.seriesIndex) {
            seriesModel = ecModel.getSeriesByIndex(payload.seriesIndex, true);
        }
        else if (payload.seriesName) {
            seriesModel = ecModel.getSeriesByName(payload.seriesName, true);
        }
        if (seriesModel) {
            var chartView = this._chartsMap[seriesModel.getId()];
            if (chartView) {
                chartView[method](seriesModel, ecModel, this._api, payload);
            }
        }
    };

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

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

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

P
pah100 已提交
341 342 343 344 345 346 347
            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 已提交
348 349
        }
    };
350

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

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

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

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

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

L
tweak  
lang 已提交
374
    };
L
lang 已提交
375

L
lang 已提交
376
    /**
L
Tweak  
lang 已提交
377
     * Prepare view instances of charts and components
L
lang 已提交
378 379 380
     * @param  {module:echarts/model/Global} ecModel
     * @private
     */
L
Tweak  
lang 已提交
381 382 383 384
    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 已提交
385
        var zr = this._zr;
L
lang 已提交
386

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

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

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

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

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

L
Tweak  
lang 已提交
426 427 428 429 430 431 432
        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 已提交
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
            }
            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 已提交
449
            });
L
tweak  
lang 已提交
450 451
        });
    };
L
lang 已提交
452

L
tweak  
lang 已提交
453 454 455 456 457 458 459 460 461 462 463 464
    /**
     * @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 已提交
465
                }
L
tweak  
lang 已提交
466 467 468 469
                stackedDataMap[stack] = data;
            }
        });
    };
L
lang 已提交
470

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

L
tweak  
lang 已提交
484 485 486 487 488 489 490 491 492 493
    /**
     * 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 已提交
494
            });
L
tweak  
lang 已提交
495 496
        });
    };
L
lang 已提交
497

L
tweak  
lang 已提交
498 499 500 501 502
    /**
     * Render each chart and component
     * @private
     */
    echartsProto._doRender = function (ecModel, payload) {
503
        var api = this._api;
L
tweak  
lang 已提交
504 505 506 507 508 509 510 511 512 513 514 515 516 517
        // 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) {
518
            var chart = this._chartsMap[seriesModel.getId()];
L
tweak  
lang 已提交
519 520 521 522 523 524
            chart.__keepAlive = true;
            chart.render(seriesModel, ecModel, api, payload);

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

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

L
lang 已提交
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
    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) {
                    var hostModel = el.hostModel || ecModel.getSeriesByIndex(
                        el.seriesIndex, true
                    );
                    var params = hostModel && hostModel.getDataParams(el.dataIndex) || {};
                    params.event = e;
                    params.type = eveName;
                    this.trigger(eveName, params);
                }
            }, this);
        }, this);
    };

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

L
tweak  
lang 已提交
570 571 572 573 574 575
        each(this._components, function (component) {
            component.dispose();
        });
        each(this._charts, function (chart) {
            chart.dispose();
        });
L
lang 已提交
576

L
Tweak  
lang 已提交
577
        this._zr.dispose();
L
lang 已提交
578 579

        instances[this.id] = null;
L
lang 已提交
580 581
    };

L
lang 已提交
582 583
    zrUtil.mixin(ECharts, Eventful);

L
lang 已提交
584 585 586 587 588 589 590 591 592 593 594 595 596 597
    /**
     * @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 已提交
598 599 600 601
    /**
     * @type {Array.<Function>}
     * @inner
     */
P
pah100 已提交
602 603
    var actions = [];

L
lang 已提交
604
    /**
L
lang 已提交
605
     * Map eventType to actionType
L
lang 已提交
606 607 608 609
     * @type {Object}
     */
    var eventActionMap = {};

L
lang 已提交
610 611 612 613
    /**
     * @type {Array.<Function>}
     * @inner
     */
614 615
    var layoutFuncs = [];

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

623 624 625 626 627 628
    /**
     * @type {Array.<Function>}
     * @inner
     */
    var optionPreprocessorFuncs = [];

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

L
lang 已提交
636 637 638 639 640 641
    var instances = {};
    var connectedGroups = {};

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

L
tweak  
lang 已提交
655 656 657 658 659 660
    /**
     * @param {HTMLDomElement} dom
     * @param {Object} [theme]
     * @param {Object} opts
     */
    echarts.init = function (dom, theme, opts) {
L
lang 已提交
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
        // 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 已提交
681
                        var action = chart.makeActionFromEvent(event);
L
lang 已提交
682 683
                        var otherChart = instances[id];
                        if (otherChart !== chart && otherChart.group === chart.group) {
L
tweak  
lang 已提交
684 685 686
                            if (!otherChart.__connectedActionDispatching) {
                                otherChart.dispatch(action);
                            }
L
lang 已提交
687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
                        }
                    }
                    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 已提交
757
    };
L
lang 已提交
758

L
tweak  
lang 已提交
759 760 761 762 763 764 765
    /**
     * Register option preprocessor
     * @param {Function} preprocessorFunc
     */
    echarts.registerPreprocessor = function (preprocessorFunc) {
        optionPreprocessorFuncs.push(preprocessorFunc);
    };
766

L
tweak  
lang 已提交
767 768 769 770 771 772 773 774 775 776 777
    /**
     * @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 已提交
778

L
tweak  
lang 已提交
779 780 781 782 783 784 785 786 787 788 789
    /**
     * 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 已提交
790 791 792 793
     * @param {string} [actionInfo.event]
     * @param {string} [actionInfo.update]
     * @param {string} [eventName]
     * @param {Function} action
L
tweak  
lang 已提交
794
     */
L
lang 已提交
795 796 797 798 799
    echarts.registerAction = function (actionInfo, eventName, action) {
        if (typeof eventName === 'function') {
            action = eventName;
            eventName = '';
        }
L
tweak  
lang 已提交
800 801
        var actionType = zrUtil.isObject(actionInfo)
            ? actionInfo.type
L
lang 已提交
802 803 804
            : ([actionInfo, actionInfo = {
                event: eventName
            }][0]);
L
lang 已提交
805 806

        actionInfo.event = actionInfo.event || actionType;
L
lang 已提交
807
        eventName = actionInfo.event;
808

L
tweak  
lang 已提交
809 810 811
        if (!actions[actionType]) {
            actions[actionType] = {action: action, actionInfo: actionInfo};
        }
L
lang 已提交
812
        eventActionMap[eventName] = actionType;
L
tweak  
lang 已提交
813
    };
P
pah100 已提交
814

L
tweak  
lang 已提交
815 816 817 818 819 820 821
    /**
     * @param {string} type
     * @param {*} CoordinateSystem
     */
    echarts.registerCoordinateSystem = function (type, CoordinateSystem) {
        CoordinateSystemManager.register(type, CoordinateSystem);
    };
L
lang 已提交
822

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

L
tweak  
lang 已提交
833 834 835 836 837 838 839 840 841 842 843
    /**
     * @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 已提交
844

L
tweak  
lang 已提交
845
    /**
L
lang 已提交
846
     * @param {echarts/scale/*} scale
L
tweak  
lang 已提交
847
     */
L
Tweak  
lang 已提交
848 849 850
    // echarts.extendScale = function (opts) {
    //     return Scale.extend(opts);
    // };
L
lang 已提交
851

L
tweak  
lang 已提交
852 853 854 855 856 857
    /**
     * @param {Object} opts
     */
    echarts.extendChartView = function (opts) {
        return ChartView.extend(opts);
    };
L
Update  
lang 已提交
858

L
tweak  
lang 已提交
859 860 861 862 863 864
    /**
     * @param {Object} opts
     */
    echarts.extendComponentModel = function (opts) {
        return ComponentModel.extend(opts);
    };
L
Update  
lang 已提交
865

L
tweak  
lang 已提交
866 867 868 869 870 871
    /**
     * @param {Object} opts
     */
    echarts.extendSeriesModel = function (opts) {
        return SeriesModel.extend(opts);
    };
P
pah100 已提交
872

L
tweak  
lang 已提交
873 874 875 876 877
    /**
     * @param {Object} opts
     */
    echarts.extendComponentView = function (opts) {
        return ComponentView.extend(opts);
L
lang 已提交
878 879
    };

L
lang 已提交
880
    echarts.registerVisualCoding('echarts', require('./visual/seriesColor'));
881 882
    echarts.registerPreprocessor(require('./preprocessor/backwardCompat'));

883 884 885 886 887 888 889 890 891 892 893 894
    // Default action
    echarts.registerAction({
        type: 'highlight',
        event: 'highlight',
        update: 'highlight'
    }, zrUtil.noop);
    echarts.registerAction({
        type: 'downplay',
        event: 'downplay',
        update: 'downplay'
    }, zrUtil.noop);

L
lang 已提交
895 896
    return echarts;
});