echarts.js 42.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Enable DEV mode when using source code without build. which has no __DEV__ variable
// In build process 'typeof __DEV__' will be replace with 'boolean'
// So this code will be removed or disabled anyway after built.
if (typeof __DEV__ === 'undefined') {
    // In browser
    if (typeof window !== 'undefined') {
        window.__DEV__ = true;
    }
    // In node
    else if (typeof global !== 'undefined') {
        global.__DEV__ = true;
    }
}

L
tweak  
lang 已提交
15 16 17 18 19 20 21 22 23 24
/*!
 * 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 已提交
25
/**
L
lang 已提交
26
 * @module echarts
L
lang 已提交
27
 */
L
lang 已提交
28 29
define(function (require) {

30 31
    var env = require('zrender/core/env');

L
lang 已提交
32
    var GlobalModel = require('./model/Global');
L
lang 已提交
33
    var ExtensionAPI = require('./ExtensionAPI');
L
lang 已提交
34
    var CoordinateSystemManager = require('./CoordinateSystem');
P
pah100 已提交
35
    var OptionManager = require('./model/OptionManager');
L
lang 已提交
36

L
Update  
lang 已提交
37 38 39 40 41
    var ComponentModel = require('./model/Component');
    var SeriesModel = require('./model/Series');

    var ComponentView = require('./view/Component');
    var ChartView = require('./view/Chart');
L
lang 已提交
42
    var graphic = require('./util/graphic');
L
Update  
lang 已提交
43

L
lang 已提交
44
    var zrender = require('zrender');
L
lang 已提交
45
    var zrUtil = require('zrender/core/util');
L
lang 已提交
46
    var colorTool = require('zrender/tool/color');
L
lang 已提交
47
    var Eventful = require('zrender/mixin/Eventful');
L
lang 已提交
48
    var timsort = require('zrender/core/timsort');
L
lang 已提交
49

50 51
    var each = zrUtil.each;

52 53
    var PRIORITY_PROCESSOR_FILTER = 1000;
    var PRIORITY_PROCESSOR_STATISTIC = 5000;
54

55 56 57 58 59

    var PRIORITY_VISUAL_LAYOUT = 1000;
    var PRIORITY_VISUAL_GLOBAL = 2000;
    var PRIORITY_VISUAL_CHART = 3000;
    var PRIORITY_VISUAL_COMPONENT = 4000;
P
pah100 已提交
60
    var PRIORITY_VISUAL_BRUSH = 5000;
L
lang 已提交
61

62 63 64 65 66 67 68 69 70 71 72
    // Main process have three entries: `setOption`, `dispatchAction` and `resize`,
    // where they must not be invoked nestedly, except the only case: invoke
    // dispatchAction with updateMethod "none" in main process.
    // This flag is used to carry out this rule.
    // All events will be triggered out side main process (i.e. when !this[IN_MAIN_PROCESS]).
    var IN_MAIN_PROCESS = '__flag_in_main_process';
    // Only final events can be "program triggered", that is, trigger by `setOption`,
    // `dispatchAciton` or `resize`. This flag is used to avoid dead lock when calling
    // those method in final events listener.
    var IN_FINAL_EVENTS = '__flag_in_final_event';

73 74 75 76 77 78
    function createRegisterEventWithLowercaseName(method) {
        return function (eventName, handler, context) {
            // Event name is all lowercase
            eventName = eventName && eventName.toLowerCase();
            Eventful.prototype[method].call(this, eventName, handler, context);
        };
L
lang 已提交
79
    }
L
lang 已提交
80 81 82 83 84 85
    /**
     * @module echarts~MessageCenter
     */
    function MessageCenter() {
        Eventful.call(this);
    }
86 87 88
    MessageCenter.prototype.on = createRegisterEventWithLowercaseName('on');
    MessageCenter.prototype.off = createRegisterEventWithLowercaseName('off');
    MessageCenter.prototype.one = createRegisterEventWithLowercaseName('one');
L
lang 已提交
89
    zrUtil.mixin(MessageCenter, Eventful);
L
lang 已提交
90 91 92
    /**
     * @module echarts~ECharts
     */
L
lang 已提交
93
    function ECharts (dom, theme, opts) {
L
lang 已提交
94
        opts = opts || {};
L
lang 已提交
95

L
lang 已提交
96 97 98 99 100
        // Get theme by name
        if (typeof theme === 'string') {
            theme = themeStorage[theme];
        }

L
lang 已提交
101 102 103 104 105 106 107 108 109
        /**
         * @type {string}
         */
        this.id;
        /**
         * Group id
         * @type {string}
         */
        this.group;
L
lang 已提交
110 111 112 113 114
        /**
         * @type {HTMLDomElement}
         * @private
         */
        this._dom = dom;
L
lang 已提交
115 116 117 118
        /**
         * @type {module:zrender/ZRender}
         * @private
         */
L
lang 已提交
119
        this._zr = zrender.init(dom, {
120 121
            renderer: opts.renderer || 'canvas',
            devicePixelRatio: opts.devicePixelRatio
L
lang 已提交
122
        });
L
lang 已提交
123

L
lang 已提交
124 125 126 127
        /**
         * @type {Object}
         * @private
         */
L
lang 已提交
128
        this._theme = zrUtil.clone(theme);
L
lang 已提交
129

L
lang 已提交
130 131 132 133
        /**
         * @type {Array.<module:echarts/view/Chart>}
         * @private
         */
L
lang 已提交
134
        this._chartsViews = [];
L
lang 已提交
135 136 137 138 139

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

L
lang 已提交
142 143 144 145
        /**
         * @type {Array.<module:echarts/view/Component>}
         * @private
         */
L
lang 已提交
146
        this._componentsViews = [];
L
lang 已提交
147 148 149 150 151

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

L
lang 已提交
154
        /**
L
lang 已提交
155
         * @type {module:echarts/ExtensionAPI}
L
lang 已提交
156 157
         * @private
         */
158
        this._api = new ExtensionAPI(this);
L
lang 已提交
159

L
lang 已提交
160 161 162 163
        /**
         * @type {module:echarts/CoordinateSystem}
         * @private
         */
164
        this._coordSysMgr = new CoordinateSystemManager();
L
lang 已提交
165

P
pah100 已提交
166 167 168
        /**
         * @type {Array.<Object>}
         */
169
        this._finalEvents = [];
P
pah100 已提交
170

L
lang 已提交
171 172
        Eventful.call(this);

L
lang 已提交
173 174 175 176 177 178
        /**
         * @type {module:echarts~MessageCenter}
         * @private
         */
        this._messageCenter = new MessageCenter();

L
lang 已提交
179 180
        // Init mouse events
        this._initEvents();
L
Resize  
lang 已提交
181 182 183

        // In case some people write `window.onresize = chart.resize`
        this.resize = zrUtil.bind(this.resize, this);
184 185 186 187 188 189


        // Sort on demand
        function prioritySortFunc(a, b) {
            return a.prio - b.prio;
        }
L
lang 已提交
190 191
        timsort(visualFuncs, prioritySortFunc);
        timsort(dataProcessorFuncs, prioritySortFunc);
L
lang 已提交
192
    }
L
lang 已提交
193

L
tweak  
lang 已提交
194
    var echartsProto = ECharts.prototype;
L
lang 已提交
195

196 197 198
    /**
     * @return {HTMLDomElement}
     */
L
tweak  
lang 已提交
199 200 201
    echartsProto.getDom = function () {
        return this._dom;
    };
L
lang 已提交
202

203 204 205
    /**
     * @return {module:zrender~ZRender}
     */
L
tweak  
lang 已提交
206 207 208
    echartsProto.getZr = function () {
        return this._zr;
    };
L
lang 已提交
209

210 211 212
    /**
     * @param {Object} option
     * @param {boolean} notMerge
P
pah100 已提交
213
     * @param {boolean} [notRefreshImmediately=false] Useful when setOption frequently.
214
     */
215 216 217 218 219 220 221 222 223
    echartsProto.setOption = function (option, notMerge, notRefreshImmediately) {
        if (__DEV__) {
            zrUtil.assert(!this[IN_MAIN_PROCESS], '`setOption` should not be called during main process.');
        }

        this[IN_MAIN_PROCESS] = 1;

        this._finalEvents = [];

P
pah100 已提交
224
        if (!this._model || notMerge) {
P
pah100 已提交
225 226 227
            this._model = new GlobalModel(
                null, null, this._theme, new OptionManager(this._api)
            );
L
tweak  
lang 已提交
228
        }
L
lang 已提交
229

P
pah100 已提交
230
        this._model.setOption(option, optionPreprocessorFuncs);
P
pah100 已提交
231 232

        updateMethods.prepareAndUpdate.call(this);
P
pah100 已提交
233 234

        !notRefreshImmediately && this._zr.refreshImmediately();
235 236 237 238

        this[IN_MAIN_PROCESS] = 0;

        triggerFinalEvents.call(this);
P
pah100 已提交
239 240
    };

L
Tweak  
lang 已提交
241 242 243 244 245 246
    /**
     * @DEPRECATED
     */
    echartsProto.setTheme = function () {
        console.log('ECharts#setTheme() is DEPRECATED in ECharts 3.0');
    };
P
pah100 已提交
247

L
tweak  
lang 已提交
248 249 250 251 252 253
    /**
     * @return {module:echarts/model/Global}
     */
    echartsProto.getModel = function () {
        return this._model;
    };
L
lang 已提交
254

L
lang 已提交
255 256 257 258
    /**
     * @return {Object}
     */
    echartsProto.getOption = function () {
259
        return this._model.getOption();
L
lang 已提交
260 261
    };

L
tweak  
lang 已提交
262 263 264 265 266 267
    /**
     * @return {number}
     */
    echartsProto.getWidth = function () {
        return this._zr.getWidth();
    };
L
lang 已提交
268

L
tweak  
lang 已提交
269 270 271 272 273 274
    /**
     * @return {number}
     */
    echartsProto.getHeight = function () {
        return this._zr.getHeight();
    };
L
lang 已提交
275

L
lang 已提交
276 277 278 279 280 281 282 283 284 285
    /**
     * Get canvas which has all thing rendered
     * @param {Object} opts
     * @param {string} [opts.backgroundColor]
     */
    echartsProto.getRenderedCanvas = function (opts) {
        if (!env.canvasSupported) {
            return;
        }
        opts = opts || {};
286
        opts.pixelRatio = opts.pixelRatio || 1;
L
lang 已提交
287
        opts.backgroundColor = opts.backgroundColor
288
            || this._model.get('backgroundColor');
L
lang 已提交
289 290 291 292 293 294 295 296 297 298 299 300
        var zr = this._zr;
        var list = zr.storage.getDisplayList();
        // Stop animations
        zrUtil.each(list, function (el) {
            el.stopAnimation(true);
        });
        return zr.painter.getRenderedCanvas(opts);
    };
    /**
     * @return {string}
     * @param {Object} opts
     * @param {string} [opts.type='png']
301
     * @param {string} [opts.pixelRatio=1]
L
lang 已提交
302 303 304
     * @param {string} [opts.backgroundColor]
     */
    echartsProto.getDataURL = function (opts) {
305 306
        opts = opts || {};
        var excludeComponents = opts.excludeComponents;
307 308 309
        var ecModel = this._model;
        var excludesComponentViews = [];
        var self = this;
310 311

        each(excludeComponents, function (componentType) {
312 313 314 315 316 317 318 319 320 321 322 323
            ecModel.eachComponent({
                mainType: componentType
            }, function (component) {
                var view = self._componentsMap[component.__viewId];
                if (!view.group.ignore) {
                    excludesComponentViews.push(view);
                    view.group.ignore = true;
                }
            });
        });

        var url = this.getRenderedCanvas(opts).toDataURL(
L
lang 已提交
324 325
            'image/' + (opts && opts.type || 'png')
        );
326 327 328 329 330

        each(excludesComponentViews, function (view) {
            view.group.ignore = false;
        });
        return url;
L
lang 已提交
331 332 333 334 335 336 337
    };


    /**
     * @return {string}
     * @param {Object} opts
     * @param {string} [opts.type='png']
338
     * @param {string} [opts.pixelRatio=1]
L
lang 已提交
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
     * @param {string} [opts.backgroundColor]
     */
    echartsProto.getConnectedDataURL = function (opts) {
        if (!env.canvasSupported) {
            return;
        }
        var groupId = this.group;
        var mathMin = Math.min;
        var mathMax = Math.max;
        var MAX_NUMBER = Infinity;
        if (connectedGroups[groupId]) {
            var left = MAX_NUMBER;
            var top = MAX_NUMBER;
            var right = -MAX_NUMBER;
            var bottom = -MAX_NUMBER;
            var canvasList = [];
355
            var dpr = (opts && opts.pixelRatio) || 1;
L
lang 已提交
356 357 358
            for (var id in instances) {
                var chart = instances[id];
                if (chart.group === groupId) {
359 360 361
                    var canvas = chart.getRenderedCanvas(
                        zrUtil.clone(opts)
                    );
L
lang 已提交
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
                    var boundingRect = chart.getDom().getBoundingClientRect();
                    left = mathMin(boundingRect.left, left);
                    top = mathMin(boundingRect.top, top);
                    right = mathMax(boundingRect.right, right);
                    bottom = mathMax(boundingRect.bottom, bottom);
                    canvasList.push({
                        dom: canvas,
                        left: boundingRect.left,
                        top: boundingRect.top
                    });
                }
            }

            left *= dpr;
            top *= dpr;
            right *= dpr;
            bottom *= dpr;
            var width = right - left;
            var height = bottom - top;
            var targetCanvas = zrUtil.createCanvas();
            targetCanvas.width = width;
            targetCanvas.height = height;
            var zr = zrender.init(targetCanvas);

            each(canvasList, function (item) {
                var img = new graphic.Image({
                    style: {
                        x: item.left * dpr - left,
                        y: item.top * dpr - top,
                        image: item.dom
                    }
                });
                zr.add(img);
            });
            zr.refreshImmediately();

            return targetCanvas.toDataURL('image/' + (opts && opts.type || 'png'));
        }
        else {
            return this.getDataURL(opts);
        }
    };
404

405 406 407 408 409 410 411
    var updateMethods = {

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

414
            var ecModel = this._model;
415 416
            var api = this._api;
            var coordSysMgr = this._coordSysMgr;
417 418 419 420
            // update before setOption
            if (!ecModel) {
                return;
            }
L
lang 已提交
421

422
            // Fixme First time update ?
423 424 425 426 427
            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 已提交
428

429 430 431 432 433
            // Create new coordinate system each update
            // In LineView may save the old coordinate system and use it to get the orignal point
            coordSysMgr.create(this._model, this._api);

            processData.call(this, ecModel, api);
L
lang 已提交
434

435
            stackSeriesData.call(this, ecModel);
L
lang 已提交
436

437
            coordSysMgr.update(ecModel, api);
L
lang 已提交
438

L
lang 已提交
439
            doVisualEncoding.call(this, ecModel, payload);
440

441
            doRender.call(this, ecModel, payload);
442

443
            // Set background
L
lang 已提交
444
            var backgroundColor = ecModel.get('backgroundColor') || 'transparent';
445

L
lang 已提交
446
            var painter = this._zr.painter;
447
            // TODO all use clearColor ?
L
lang 已提交
448
            if (painter.isSingleCanvas && painter.isSingleCanvas()) {
L
lang 已提交
449 450 451 452 453
                this._zr.configLayer(0, {
                    clearColor: backgroundColor
                });
            }
            else {
L
lang 已提交
454 455 456 457 458 459 460 461 462
                // In IE8
                if (!env.canvasSupported) {
                    var colorArr = colorTool.parse(backgroundColor);
                    backgroundColor = colorTool.stringify(colorArr, 'rgb');
                    if (colorArr[3] === 0) {
                        backgroundColor = 'transparent';
                    }
                }
                backgroundColor = backgroundColor;
463
                this._dom.style.backgroundColor = backgroundColor;
L
lang 已提交
464
            }
L
lang 已提交
465

466
            // console.time && console.timeEnd('update');
467
        },
468

469 470 471 472 473 474 475
        // PENDING
        /**
         * @param {Object} payload
         * @private
         */
        updateView: function (payload) {
            var ecModel = this._model;
476

477 478 479 480 481
            // update before setOption
            if (!ecModel) {
                return;
            }

P
pah100 已提交
482 483 484 485
            ecModel.eachSeries(function (seriesModel) {
                seriesModel.getData().clearAllVisual();
            });

486
            doVisualEncoding.call(this, ecModel, payload);
487

488 489
            invokeUpdateMethod.call(this, 'updateView', ecModel, payload);
        },
490

491 492 493 494 495 496
        /**
         * @param {Object} payload
         * @private
         */
        updateVisual: function (payload) {
            var ecModel = this._model;
497

498 499 500 501 502
            // update before setOption
            if (!ecModel) {
                return;
            }

P
pah100 已提交
503 504 505 506
            ecModel.eachSeries(function (seriesModel) {
                seriesModel.getData().clearAllVisual();
            });

507
            doVisualEncoding.call(this, ecModel, payload);
508

509 510
            invokeUpdateMethod.call(this, 'updateVisual', ecModel, payload);
        },
511

512 513 514 515 516 517
        /**
         * @param {Object} payload
         * @private
         */
        updateLayout: function (payload) {
            var ecModel = this._model;
518

519 520 521 522 523
            // update before setOption
            if (!ecModel) {
                return;
            }

L
lang 已提交
524
            doLayout.call(this, ecModel, payload);
525

526 527
            invokeUpdateMethod.call(this, 'updateLayout', ecModel, payload);
        },
L
lang 已提交
528

529 530 531 532 533 534 535 536 537 538 539 540 541 542
        /**
         * @param {Object} payload
         * @private
         */
        highlight: function (payload) {
            toggleHighlight.call(this, 'highlight', payload);
        },

        /**
         * @param {Object} payload
         * @private
         */
        downplay: function (payload) {
            toggleHighlight.call(this, 'downplay', payload);
P
pah100 已提交
543 544 545 546
        },

        /**
         * @param {Object} payload
P
pah100 已提交
547
         * @private
P
pah100 已提交
548
         */
P
pah100 已提交
549
        prepareAndUpdate: function (payload) {
P
pah100 已提交
550
            var ecModel = this._model;
551

P
pah100 已提交
552 553 554
            prepareView.call(this, 'component', ecModel);

            prepareView.call(this, 'chart', ecModel);
P
pah100 已提交
555

P
pah100 已提交
556
            updateMethods.update.call(this, payload);
P
pah100 已提交
557
        }
558 559 560 561 562 563
    };

    /**
     * @param {Object} payload
     * @private
     */
564
    function toggleHighlight(method, payload) {
565
        var ecModel = this._model;
566

567 568 569 570 571
        // dispatchAction before setOption
        if (!ecModel) {
            return;
        }

572 573
        ecModel.eachComponent(
            {mainType: 'series', query: payload},
L
lang 已提交
574
            function (seriesModel, index) {
L
lang 已提交
575
                var chartView = this._chartsMap[seriesModel.__viewId];
L
lang 已提交
576
                if (chartView && chartView.__alive) {
P
pah100 已提交
577
                    chartView[method](
L
lang 已提交
578
                        seriesModel, ecModel, this._api, payload
P
pah100 已提交
579
                    );
580 581 582 583
                }
            },
            this
        );
584
    }
585

L
Resize  
lang 已提交
586 587 588
    /**
     * Resize the chart
     */
L
tweak  
lang 已提交
589
    echartsProto.resize = function () {
590 591 592 593 594
        if (__DEV__) {
            zrUtil.assert(!this[IN_MAIN_PROCESS], '`resize` should not be called during main process.');
        }

        this[IN_MAIN_PROCESS] = 1;
P
pah100 已提交
595

596 597 598
        this._finalEvents = [];

        this._zr.resize();
P
pah100 已提交
599

P
pah100 已提交
600 601
        var optionChanged = this._model && this._model.resetOption('media');
        updateMethods[optionChanged ? 'prepareAndUpdate' : 'update'].call(this);
L
lang 已提交
602 603 604

        // Resize loading effect
        this._loadingFX && this._loadingFX.resize();
605 606 607 608

        this[IN_MAIN_PROCESS] = 0;

        triggerFinalEvents.call(this);
L
lang 已提交
609 610 611 612 613 614 615 616 617 618 619 620 621
    };

    var defaultLoadingEffect = require('./loading/default');
    /**
     * Show loading effect
     * @param  {string} [name='default']
     * @param  {Object} [cfg]
     */
    echartsProto.showLoading = function (name, cfg) {
        if (zrUtil.isObject(name)) {
            cfg = name;
            name = 'default';
        }
L
lang 已提交
622
        this.hideLoading();
L
lang 已提交
623
        var el = defaultLoadingEffect(this._api, cfg);
L
lang 已提交
624
        var zr = this._zr;
L
lang 已提交
625
        this._loadingFX = el;
L
lang 已提交
626 627

        zr.add(el);
L
lang 已提交
628 629 630 631 632 633
    };

    /**
     * Hide loading effect
     */
    echartsProto.hideLoading = function () {
L
lang 已提交
634
        this._loadingFX && this._zr.remove(this._loadingFX);
L
lang 已提交
635
        this._loadingFX = null;
L
tweak  
lang 已提交
636
    };
P
pah100 已提交
637

L
lang 已提交
638
    /**
L
Resize  
lang 已提交
639 640
     * @param {Object} eventObj
     * @return {Object}
L
lang 已提交
641 642 643 644 645 646 647
     */
    echartsProto.makeActionFromEvent = function (eventObj) {
        var payload = zrUtil.extend({}, eventObj);
        payload.type = eventActionMap[eventObj.type];
        return payload;
    };

648 649 650 651 652 653 654 655
    function triggerFinalEvents() {
        if (!this[IN_FINAL_EVENTS]) { // Avoid dead lock.
            this[IN_FINAL_EVENTS] = 1;
            each(this._finalEvents, function (eventObj) {
                this.trigger(eventObj.type, eventObj);
            }, this);
            this[IN_FINAL_EVENTS] = 0;
        }
P
pah100 已提交
656 657
    }

L
tweak  
lang 已提交
658 659 660 661
    /**
     * @pubilc
     * @param {Object} payload
     * @param {string} [payload.type] Action type
P
pah100 已提交
662
     * @param {boolean} [silent=false] Whether trigger event.
L
tweak  
lang 已提交
663
     */
L
lang 已提交
664
    echartsProto.dispatchAction = function (payload, silent) {
L
tweak  
lang 已提交
665
        var actionWrap = actions[payload.type];
666 667 668
        if (!actionWrap) {
            return;
        }
L
lang 已提交
669

670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
        var actionInfo = actionWrap.actionInfo;
        var updateMethod = actionInfo.update || 'update';

        if (__DEV__) {
            zrUtil.assert(
                updateMethod === 'none' || !this[IN_MAIN_PROCESS],
                '`dispatchAction` should not be called during main process.'
                    + 'unless updateMathod is "none".'
            );
        }

        var isFinalEvent = updateMethod === 'none' && this[IN_MAIN_PROCESS];
        if (!isFinalEvent) {
            this[IN_MAIN_PROCESS] = 1;
            this._finalEvents = [];
        }
L
lang 已提交
686

687 688 689 690 691 692 693 694 695 696 697
        var payloads = [payload];
        var batched = false;
        // Batch action
        if (payload.batch) {
            batched = true;
            payloads = zrUtil.map(payload.batch, function (item) {
                item = zrUtil.defaults(zrUtil.extend({}, item), payload);
                item.batch = null;
                return item;
            });
        }
P
pah100 已提交
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
        var eventObjBatch = [];
        var eventObj;
        var isHighlightOrDownplay = payload.type === 'highlight' || payload.type === 'downplay';
        for (var i = 0; i < payloads.length; i++) {
            var batchItem = payloads[i];
            // Action can specify the event by return it.
            eventObj = actionWrap.action(batchItem, this._model);
            // Emit event outside
            eventObj = eventObj || zrUtil.extend({}, batchItem);
            // Convert type to eventType
            eventObj.type = actionInfo.event || eventObj.type;
            eventObjBatch.push(eventObj);

            // Highlight and downplay are special.
            isHighlightOrDownplay && updateMethods[updateMethod].call(this, batchItem);
        }

        (updateMethod !== 'none' && !isHighlightOrDownplay)
            && updateMethods[updateMethod].call(this, payload);

        // Follow the rule of action batch
        if (batched) {
            eventObj = {
                type: actionInfo.event || payload.type,
                batch: eventObjBatch
            };
        }
        else {
            eventObj = eventObjBatch[0];
        }

        if (!isFinalEvent) {
            this[IN_MAIN_PROCESS] = 0;
L
lang 已提交
732
            if (!silent) {
L
lang 已提交
733
                this._messageCenter.trigger(eventObj.type, eventObj);
734
                triggerFinalEvents.call(this);
P
pah100 已提交
735
            }
736 737 738
        }
        else {
            this._finalEvents.push(eventObj);
L
tweak  
lang 已提交
739 740
        }
    };
741

L
lang 已提交
742 743 744 745
    /**
     * Register event
     * @method
     */
746 747 748
    echartsProto.on = createRegisterEventWithLowercaseName('on');
    echartsProto.off = createRegisterEventWithLowercaseName('off');
    echartsProto.one = createRegisterEventWithLowercaseName('one');
L
lang 已提交
749

L
tweak  
lang 已提交
750 751 752 753
    /**
     * @param {string} methodName
     * @private
     */
754
    function invokeUpdateMethod(methodName, ecModel, payload) {
755
        var api = this._api;
L
lang 已提交
756

L
tweak  
lang 已提交
757
        // Update all components
L
lang 已提交
758
        each(this._componentsViews, function (component) {
L
tweak  
lang 已提交
759 760
            var componentModel = component.__model;
            component[methodName](componentModel, ecModel, api, payload);
761

L
tweak  
lang 已提交
762 763
            updateZ(componentModel, component);
        }, this);
L
lang 已提交
764

L
tweak  
lang 已提交
765 766
        // Upate all charts
        ecModel.eachSeries(function (seriesModel, idx) {
L
lang 已提交
767
            var chart = this._chartsMap[seriesModel.__viewId];
L
tweak  
lang 已提交
768
            chart[methodName](seriesModel, ecModel, api, payload);
769

L
tweak  
lang 已提交
770 771
            updateZ(seriesModel, chart);
        }, this);
772

773
    }
L
lang 已提交
774

L
lang 已提交
775
    /**
L
Tweak  
lang 已提交
776
     * Prepare view instances of charts and components
L
lang 已提交
777 778 779
     * @param  {module:echarts/model/Global} ecModel
     * @private
     */
780
    function prepareView(type, ecModel) {
L
Tweak  
lang 已提交
781
        var isComponent = type === 'component';
L
lang 已提交
782
        var viewList = isComponent ? this._componentsViews : this._chartsViews;
L
Tweak  
lang 已提交
783
        var viewMap = isComponent ? this._componentsMap : this._chartsMap;
L
tweak  
lang 已提交
784
        var zr = this._zr;
L
lang 已提交
785

L
Tweak  
lang 已提交
786
        for (var i = 0; i < viewList.length; i++) {
L
lang 已提交
787
            viewList[i].__alive = false;
L
tweak  
lang 已提交
788
        }
L
lang 已提交
789

L
Tweak  
lang 已提交
790 791 792 793
        ecModel[isComponent ? 'eachComponent' : 'eachSeries'](function (componentType, model) {
            if (isComponent) {
                if (componentType === 'series') {
                    return;
L
lang 已提交
794
                }
795
            }
L
tweak  
lang 已提交
796
            else {
L
Tweak  
lang 已提交
797
                model = componentType;
L
tweak  
lang 已提交
798 799
            }

800
            // Consider: id same and type changed.
L
lang 已提交
801 802
            var viewId = model.id + '_' + model.type;
            var view = viewMap[viewId];
L
Tweak  
lang 已提交
803 804 805
            if (!view) {
                var classType = ComponentModel.parseClassType(model.type);
                var Clazz = isComponent
L
tweak  
lang 已提交
806
                    ? ComponentView.getClass(classType.main, classType.sub)
L
Tweak  
lang 已提交
807
                    : ChartView.getClass(classType.sub);
L
tweak  
lang 已提交
808
                if (Clazz) {
L
Tweak  
lang 已提交
809 810
                    view = new Clazz();
                    view.init(ecModel, this._api);
L
lang 已提交
811
                    viewMap[viewId] = view;
L
Tweak  
lang 已提交
812 813 814 815 816
                    viewList.push(view);
                    zr.add(view.group);
                }
                else {
                    // Error
L
lang 已提交
817
                    return;
L
lang 已提交
818
                }
819
            }
L
Tweak  
lang 已提交
820

L
lang 已提交
821
            model.__viewId = viewId;
L
lang 已提交
822
            view.__alive = true;
L
lang 已提交
823
            view.__id = viewId;
L
Tweak  
lang 已提交
824
            view.__model = model;
L
tweak  
lang 已提交
825 826
        }, this);

L
Tweak  
lang 已提交
827 828
        for (var i = 0; i < viewList.length;) {
            var view = viewList[i];
L
lang 已提交
829
            if (!view.__alive) {
L
Tweak  
lang 已提交
830
                zr.remove(view.group);
L
lang 已提交
831
                view.dispose(ecModel, this._api);
L
Tweak  
lang 已提交
832 833
                viewList.splice(i, 1);
                delete viewMap[view.__id];
L
tweak  
lang 已提交
834 835 836 837 838
            }
            else {
                i++;
            }
        }
839 840
    }

L
tweak  
lang 已提交
841 842 843 844 845 846
    /**
     * Processor data in each series
     *
     * @param {module:echarts/model/Global} ecModel
     * @private
     */
847
    function processData(ecModel, api) {
848 849
        each(dataProcessorFuncs, function (process) {
            process.func(ecModel, api);
L
tweak  
lang 已提交
850
        });
851
    }
L
lang 已提交
852

L
tweak  
lang 已提交
853 854 855
    /**
     * @private
     */
856
    function stackSeriesData(ecModel) {
L
tweak  
lang 已提交
857 858 859 860 861 862 863 864
        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 已提交
865
                }
L
tweak  
lang 已提交
866 867 868
                stackedDataMap[stack] = data;
            }
        });
869
    }
L
lang 已提交
870

L
tweak  
lang 已提交
871
    /**
872
     * Layout before each chart render there series, special visual encoding stage
L
tweak  
lang 已提交
873 874 875 876
     *
     * @param {module:echarts/model/Global} ecModel
     * @private
     */
L
lang 已提交
877 878
    function doLayout(ecModel, payload) {
        var api = this._api;
879 880 881 882
        each(visualFuncs, function (visual) {
            if (visual.isLayout) {
                visual.func(ecModel, api, payload);
            }
L
tweak  
lang 已提交
883
        });
884
    }
L
lang 已提交
885

L
tweak  
lang 已提交
886
    /**
887
     * Encode visual infomation from data after data processing
L
tweak  
lang 已提交
888 889 890 891
     *
     * @param {module:echarts/model/Global} ecModel
     * @private
     */
L
lang 已提交
892 893
    function doVisualEncoding(ecModel, payload) {
        var api = this._api;
L
lang 已提交
894 895 896 897
        ecModel.clearColorPalette();
        ecModel.eachSeries(function (seriesModel) {
            seriesModel.clearColorPalette();
        });
898 899
        each(visualFuncs, function (visual) {
            visual.func(ecModel, api, payload);
L
tweak  
lang 已提交
900
        });
901
    }
L
lang 已提交
902

L
tweak  
lang 已提交
903 904 905 906
    /**
     * Render each chart and component
     * @private
     */
907
    function doRender(ecModel, payload) {
908
        var api = this._api;
L
tweak  
lang 已提交
909
        // Render all components
L
lang 已提交
910 911 912
        each(this._componentsViews, function (componentView) {
            var componentModel = componentView.__model;
            componentView.render(componentModel, ecModel, api, payload);
L
tweak  
lang 已提交
913

L
lang 已提交
914
            updateZ(componentModel, componentView);
L
tweak  
lang 已提交
915 916
        }, this);

L
lang 已提交
917
        each(this._chartsViews, function (chart) {
L
lang 已提交
918
            chart.__alive = false;
L
tweak  
lang 已提交
919 920
        }, this);

L
lang 已提交
921
        var elCountAll = 0;
L
tweak  
lang 已提交
922 923
        // Render all charts
        ecModel.eachSeries(function (seriesModel, idx) {
L
lang 已提交
924
            var chartView = this._chartsMap[seriesModel.__viewId];
L
lang 已提交
925
            chartView.__alive = true;
L
lang 已提交
926
            chartView.render(seriesModel, ecModel, api, payload);
L
tweak  
lang 已提交
927

L
lang 已提交
928 929
            chartView.group.silent = !!seriesModel.get('silent');

L
lang 已提交
930
            updateZ(seriesModel, chartView);
931 932 933

            // Progressive configuration
            var elCount = 0;
L
lang 已提交
934
            chartView.group.traverse(function (el) {
L
Tweak  
lang 已提交
935
                if (el.type !== 'group' && !el.ignore) {
L
lang 已提交
936 937 938
                    elCount++;
                }
            });
L
lang 已提交
939 940
            elCountAll += elCount;

L
Tweak  
lang 已提交
941
            var frameDrawNum = +seriesModel.get('progressive');
L
lang 已提交
942
            var needProgressive = elCount > seriesModel.get('progressiveThreshold') && frameDrawNum && !env.node;
943 944
            if (needProgressive) {
                chartView.group.traverse(function (el) {
L
lang 已提交
945
                    // FIXME marker and other components
946 947
                    if (el.type !== 'group') {
                        el.progressive = needProgressive ?
L
Tweak  
lang 已提交
948
                            Math.floor(elCount++ / frameDrawNum) : -1;
949 950 951 952 953 954
                        if (needProgressive) {
                            el.stopAnimation(true);
                        }
                    }
                });
            }
L
tweak  
lang 已提交
955 956
        }, this);

L
lang 已提交
957 958 959 960 961 962 963 964
        // If use hover layer
        if (elCountAll > ecModel.get('hoverLayerThreshold') && !env.node) {
            this._zr.storage.traverse(function (el) {
                if (el.type !== 'group') {
                    el.useHoverLayer = true;
                }
            });
        }
L
lang 已提交
965
        // Remove groups of unrendered charts
L
lang 已提交
966
        each(this._chartsViews, function (chart) {
L
lang 已提交
967
            if (!chart.__alive) {
L
tweak  
lang 已提交
968 969 970
                chart.remove(ecModel, api);
            }
        }, this);
971
    }
L
lang 已提交
972

L
lang 已提交
973
    var MOUSE_EVENT_NAMES = [
L
Typo  
lang 已提交
974
        'click', 'dblclick', 'mouseover', 'mouseout', 'mousedown', 'mouseup', 'globalout'
L
lang 已提交
975 976 977 978 979 980
    ];
    /**
     * @private
     */
    echartsProto._initEvents = function () {
        each(MOUSE_EVENT_NAMES, function (eveName) {
981
            this._zr.on(eveName, function (e) {
L
lang 已提交
982 983 984
                var ecModel = this.getModel();
                var el = e.target;
                if (el && el.dataIndex != null) {
L
lang 已提交
985
                    var dataModel = el.dataModel || ecModel.getSeriesByIndex(el.seriesIndex);
986
                    var params = dataModel && dataModel.getDataParams(el.dataIndex, el.dataType) || {};
L
lang 已提交
987 988 989 990
                    params.event = e;
                    params.type = eveName;
                    this.trigger(eveName, params);
                }
L
lang 已提交
991 992 993 994
                // If element has custom eventData of components
                else if (el && el.eventData) {
                    this.trigger(eveName, el.eventData);
                }
L
lang 已提交
995 996
            }, this);
        }, this);
L
lang 已提交
997

L
lang 已提交
998
        each(eventActionMap, function (actionType, eventType) {
L
lang 已提交
999 1000 1001 1002
            this._messageCenter.on(eventType, function (event) {
                this.trigger(eventType, event);
            }, this);
        }, this);
L
lang 已提交
1003 1004
    };

L
lang 已提交
1005
    /**
L
lang 已提交
1006
     * @return {boolean}
L
lang 已提交
1007 1008 1009 1010
     */
    echartsProto.isDisposed = function () {
        return this._disposed;
    };
L
lang 已提交
1011 1012 1013 1014 1015 1016 1017

    /**
     * Clear
     */
    echartsProto.clear = function () {
        this.setOption({}, true);
    };
L
lang 已提交
1018 1019 1020
    /**
     * Dispose instance
     */
L
tweak  
lang 已提交
1021
    echartsProto.dispose = function () {
L
lang 已提交
1022
        this._disposed = true;
L
lang 已提交
1023
        var api = this._api;
L
lang 已提交
1024
        var ecModel = this._model;
L
lang 已提交
1025

L
lang 已提交
1026
        each(this._componentsViews, function (component) {
L
lang 已提交
1027
            component.dispose(ecModel, api);
L
tweak  
lang 已提交
1028
        });
L
lang 已提交
1029
        each(this._chartsViews, function (chart) {
L
lang 已提交
1030
            chart.dispose(ecModel, api);
L
tweak  
lang 已提交
1031
        });
L
lang 已提交
1032

L
Tweak  
lang 已提交
1033
        this._zr.dispose();
L
lang 已提交
1034

L
lang 已提交
1035
        delete instances[this.id];
L
lang 已提交
1036 1037
    };

L
lang 已提交
1038 1039
    zrUtil.mixin(ECharts, Eventful);

L
lang 已提交
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
    /**
     * @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) {
1050 1051 1052 1053
            if (el.type !== 'group') {
                z != null && (el.z = z);
                zlevel != null && (el.zlevel = zlevel);
            }
L
lang 已提交
1054 1055
        });
    }
L
lang 已提交
1056 1057 1058 1059
    /**
     * @type {Array.<Function>}
     * @inner
     */
P
pah100 已提交
1060 1061
    var actions = [];

L
lang 已提交
1062
    /**
L
lang 已提交
1063
     * Map eventType to actionType
L
lang 已提交
1064 1065 1066 1067
     * @type {Object}
     */
    var eventActionMap = {};

L
lang 已提交
1068 1069 1070 1071 1072
    /**
     * Data processor functions of each stage
     * @type {Array.<Object.<string, Function>>}
     * @inner
     */
1073
    var dataProcessorFuncs = [];
L
lang 已提交
1074

1075 1076 1077 1078 1079 1080
    /**
     * @type {Array.<Function>}
     * @inner
     */
    var optionPreprocessorFuncs = [];

L
lang 已提交
1081
    /**
1082
     * Visual encoding functions of each stage
L
lang 已提交
1083 1084 1085
     * @type {Array.<Object.<string, Function>>}
     * @inner
     */
1086
    var visualFuncs = [];
L
lang 已提交
1087 1088 1089 1090 1091 1092
    /**
     * Theme storage
     * @type {Object.<key, Object>}
     */
    var themeStorage = {};

L
lang 已提交
1093

L
lang 已提交
1094 1095 1096 1097 1098 1099
    var instances = {};
    var connectedGroups = {};

    var idBase = new Date() - 0;
    var groupIdBase = new Date() - 0;
    var DOM_ATTRIBUTE_KEY = '_echarts_instance_';
L
lang 已提交
1100
    /**
L
lang 已提交
1101
     * @alias module:echarts
L
lang 已提交
1102
     */
L
lang 已提交
1103 1104 1105 1106
    var echarts = {
        /**
         * @type {number}
         */
L
lang 已提交
1107
        version: '3.1.10',
L
lang 已提交
1108
        dependencies: {
L
lang 已提交
1109
            zrender: '3.1.0'
L
lang 已提交
1110 1111
        }
    };
L
lang 已提交
1112

L
lang 已提交
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147
    function enableConnect(chart) {

        var STATUS_PENDING = 0;
        var STATUS_UPDATING = 1;
        var STATUS_UPDATED = 2;
        var STATUS_KEY = '__connectUpdateStatus';
        function updateConnectedChartsStatus(charts, status) {
            for (var i = 0; i < charts.length; i++) {
                var otherChart = charts[i];
                otherChart[STATUS_KEY] = status;
            }
        }
        zrUtil.each(eventActionMap, function (actionType, eventType) {
            chart._messageCenter.on(eventType, function (event) {
                if (connectedGroups[chart.group] && chart[STATUS_KEY] !== STATUS_PENDING) {
                    var action = chart.makeActionFromEvent(event);
                    var otherCharts = [];
                    for (var id in instances) {
                        var otherChart = instances[id];
                        if (otherChart !== chart && otherChart.group === chart.group) {
                            otherCharts.push(otherChart);
                        }
                    }
                    updateConnectedChartsStatus(otherCharts, STATUS_PENDING);
                    each(otherCharts, function (otherChart) {
                        if (otherChart[STATUS_KEY] !== STATUS_UPDATING) {
                            otherChart.dispatchAction(action);
                        }
                    });
                    updateConnectedChartsStatus(otherCharts, STATUS_UPDATED);
                }
            });
        });

    }
L
tweak  
lang 已提交
1148 1149 1150 1151 1152 1153
    /**
     * @param {HTMLDomElement} dom
     * @param {Object} [theme]
     * @param {Object} opts
     */
    echarts.init = function (dom, theme, opts) {
1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166
        if (__DEV__) {
            // Check version
            if ((zrender.version.replace('.', '') - 0) < (echarts.dependencies.zrender.replace('.', '') - 0)) {
                throw new Error(
                    'ZRender ' + zrender.version
                    + ' is too old for ECharts ' + echarts.version
                    + '. Current version need ZRender '
                    + echarts.dependencies.zrender + '+'
                );
            }
            if (!dom) {
                throw new Error('Initialize failed: invalid dom.');
            }
L
lang 已提交
1167
            if (zrUtil.isDom(dom) && dom.nodeName.toUpperCase() !== 'CANVAS' && (!dom.clientWidth || !dom.clientHeight)) {
L
lang 已提交
1168
                console.warn('Can\'t get dom width or height');
1169
            }
1170
        }
L
lang 已提交
1171 1172

        var chart = new ECharts(dom, theme, opts);
L
lang 已提交
1173
        chart.id = 'ec_' + idBase++;
L
lang 已提交
1174 1175
        instances[chart.id] = chart;

L
lang 已提交
1176 1177 1178
        dom.setAttribute &&
            dom.setAttribute(DOM_ATTRIBUTE_KEY, chart.id);

L
lang 已提交
1179
        enableConnect(chart);
L
lang 已提交
1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197

        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;
                }
            });
L
lang 已提交
1198
            groupId = groupId || ('g_' + groupIdBase++);
L
lang 已提交
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243
            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 已提交
1244
    };
L
lang 已提交
1245

L
lang 已提交
1246 1247 1248 1249 1250 1251 1252
    /**
     * Register theme
     */
    echarts.registerTheme = function (name, theme) {
        themeStorage[name] = theme;
    };

L
tweak  
lang 已提交
1253 1254 1255 1256 1257 1258 1259
    /**
     * Register option preprocessor
     * @param {Function} preprocessorFunc
     */
    echarts.registerPreprocessor = function (preprocessorFunc) {
        optionPreprocessorFuncs.push(preprocessorFunc);
    };
1260

L
tweak  
lang 已提交
1261
    /**
1262
     * @param {number} [priority=1000]
L
tweak  
lang 已提交
1263 1264
     * @param {Function} processorFunc
     */
1265 1266 1267 1268
    echarts.registerProcessor = function (priority, processorFunc) {
        if (typeof priority === 'function') {
            processorFunc = priority;
            priority = PRIORITY_PROCESSOR_FILTER;
L
tweak  
lang 已提交
1269
        }
1270 1271 1272 1273
        if (__DEV__) {
            if (isNaN(priority)) {
                throw new Error('Unkown processor priority');
            }
1274 1275 1276 1277 1278
        }
        dataProcessorFuncs.push({
            prio: priority,
            func: processorFunc
        });
L
tweak  
lang 已提交
1279
    };
L
lang 已提交
1280

L
tweak  
lang 已提交
1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291
    /**
     * 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 已提交
1292 1293 1294 1295
     * @param {string} [actionInfo.event]
     * @param {string} [actionInfo.update]
     * @param {string} [eventName]
     * @param {Function} action
L
tweak  
lang 已提交
1296
     */
L
lang 已提交
1297 1298 1299 1300 1301
    echarts.registerAction = function (actionInfo, eventName, action) {
        if (typeof eventName === 'function') {
            action = eventName;
            eventName = '';
        }
L
tweak  
lang 已提交
1302 1303
        var actionType = zrUtil.isObject(actionInfo)
            ? actionInfo.type
L
lang 已提交
1304 1305 1306
            : ([actionInfo, actionInfo = {
                event: eventName
            }][0]);
L
lang 已提交
1307

L
lang 已提交
1308 1309
        // Event name is all lowercase
        actionInfo.event = (actionInfo.event || actionType).toLowerCase();
L
lang 已提交
1310
        eventName = actionInfo.event;
1311

L
tweak  
lang 已提交
1312 1313 1314
        if (!actions[actionType]) {
            actions[actionType] = {action: action, actionInfo: actionInfo};
        }
L
lang 已提交
1315
        eventActionMap[eventName] = actionType;
L
tweak  
lang 已提交
1316
    };
P
pah100 已提交
1317

L
tweak  
lang 已提交
1318 1319 1320 1321 1322 1323 1324
    /**
     * @param {string} type
     * @param {*} CoordinateSystem
     */
    echarts.registerCoordinateSystem = function (type, CoordinateSystem) {
        CoordinateSystemManager.register(type, CoordinateSystem);
    };
L
lang 已提交
1325

L
tweak  
lang 已提交
1326
    /**
1327 1328 1329 1330 1331 1332
     * Layout is a special stage of visual encoding
     * Most visual encoding like color are common for different chart
     * But each chart has it's own layout algorithm
     *
     * @param {string} [priority=1000]
     * @param {Function} layoutFunc
L
tweak  
lang 已提交
1333
     */
1334 1335 1336 1337 1338
    echarts.registerLayout = function (priority, layoutFunc) {
        if (typeof priority === 'function') {
            layoutFunc = priority;
            priority = PRIORITY_VISUAL_LAYOUT;
        }
1339 1340 1341 1342
        if (__DEV__) {
            if (isNaN(priority)) {
                throw new Error('Unkown layout priority');
            }
L
tweak  
lang 已提交
1343
        }
1344 1345 1346 1347 1348
        visualFuncs.push({
            prio: priority,
            func: layoutFunc,
            isLayout: true
        });
L
tweak  
lang 已提交
1349
    };
L
lang 已提交
1350

L
tweak  
lang 已提交
1351
    /**
1352 1353
     * @param {string} [priority=3000]
     * @param {Function} visualFunc
L
tweak  
lang 已提交
1354
     */
1355 1356 1357 1358
    echarts.registerVisual = function (priority, visualFunc) {
        if (typeof priority === 'function') {
            visualFunc = priority;
            priority = PRIORITY_VISUAL_CHART;
L
tweak  
lang 已提交
1359
        }
1360 1361 1362 1363
        if (__DEV__) {
            if (isNaN(priority)) {
                throw new Error('Unkown visual priority');
            }
1364 1365 1366 1367 1368
        }
        visualFuncs.push({
            prio: priority,
            func: visualFunc
        });
L
tweak  
lang 已提交
1369
    };
L
Update  
lang 已提交
1370

L
tweak  
lang 已提交
1371 1372 1373 1374 1375 1376
    /**
     * @param {Object} opts
     */
    echarts.extendChartView = function (opts) {
        return ChartView.extend(opts);
    };
L
Update  
lang 已提交
1377

L
tweak  
lang 已提交
1378 1379 1380 1381 1382 1383
    /**
     * @param {Object} opts
     */
    echarts.extendComponentModel = function (opts) {
        return ComponentModel.extend(opts);
    };
L
Update  
lang 已提交
1384

L
tweak  
lang 已提交
1385 1386 1387 1388 1389 1390
    /**
     * @param {Object} opts
     */
    echarts.extendSeriesModel = function (opts) {
        return SeriesModel.extend(opts);
    };
P
pah100 已提交
1391

L
tweak  
lang 已提交
1392 1393 1394 1395 1396
    /**
     * @param {Object} opts
     */
    echarts.extendComponentView = function (opts) {
        return ComponentView.extend(opts);
L
lang 已提交
1397 1398
    };

1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
    /**
     * 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;
    };

1419
    echarts.registerVisual(PRIORITY_VISUAL_GLOBAL, zrUtil.curry(
L
lang 已提交
1420 1421
        require('./visual/seriesColor'), '', 'itemStyle'
    ));
1422 1423
    echarts.registerPreprocessor(require('./preprocessor/backwardCompat'));

1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435
    // Default action
    echarts.registerAction({
        type: 'highlight',
        event: 'highlight',
        update: 'highlight'
    }, zrUtil.noop);
    echarts.registerAction({
        type: 'downplay',
        event: 'downplay',
        update: 'downplay'
    }, zrUtil.noop);

P
pah100 已提交
1436 1437 1438 1439

    // --------
    // Exports
    // --------
L
lang 已提交
1440 1441 1442
    //
    echarts.List = require('./data/List');
    echarts.Model = require('./model/Model');
P
pah100 已提交
1443

L
lang 已提交
1444 1445 1446
    echarts.graphic = require('./util/graphic');
    echarts.number = require('./util/number');
    echarts.format = require('./util/format');
L
lang 已提交
1447 1448
    echarts.matrix = require('zrender/core/matrix');
    echarts.vector = require('zrender/core/vector');
L
lang 已提交
1449
    echarts.color = require('zrender/tool/color');
P
pah100 已提交
1450 1451 1452 1453 1454

    echarts.util = {};
    each([
            'map', 'each', 'filter', 'indexOf', 'inherits',
            'reduce', 'filter', 'bind', 'curry', 'isArray',
L
lang 已提交
1455
            'isString', 'isObject', 'isFunction', 'extend', 'defaults'
P
pah100 已提交
1456 1457 1458 1459 1460 1461
        ],
        function (name) {
            echarts.util[name] = zrUtil[name];
        }
    );

1462 1463 1464 1465 1466 1467 1468 1469 1470 1471
    // PRIORITY
    echarts.PRIORITY = {
        PROCESSOR: {
            FILTER: PRIORITY_PROCESSOR_FILTER,
            STATISTIC: PRIORITY_PROCESSOR_STATISTIC
        },
        VISUAL: {
            LAYOUT: PRIORITY_VISUAL_LAYOUT,
            GLOBAL: PRIORITY_VISUAL_GLOBAL,
            CHART: PRIORITY_VISUAL_CHART,
P
pah100 已提交
1472
            COMPONENT: PRIORITY_VISUAL_COMPONENT,
P
pah100 已提交
1473
            BRUSH: PRIORITY_VISUAL_BRUSH
1474 1475 1476
        }
    };

L
lang 已提交
1477 1478
    return echarts;
});