echarts.js 41.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
    // 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';

69 70 71 72 73 74
    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 已提交
75
    }
L
lang 已提交
76 77 78 79 80 81
    /**
     * @module echarts~MessageCenter
     */
    function MessageCenter() {
        Eventful.call(this);
    }
82 83 84
    MessageCenter.prototype.on = createRegisterEventWithLowercaseName('on');
    MessageCenter.prototype.off = createRegisterEventWithLowercaseName('off');
    MessageCenter.prototype.one = createRegisterEventWithLowercaseName('one');
L
lang 已提交
85
    zrUtil.mixin(MessageCenter, Eventful);
L
lang 已提交
86 87 88
    /**
     * @module echarts~ECharts
     */
L
lang 已提交
89
    function ECharts (dom, theme, opts) {
L
lang 已提交
90
        opts = opts || {};
L
lang 已提交
91

L
lang 已提交
92 93 94 95 96
        // Get theme by name
        if (typeof theme === 'string') {
            theme = themeStorage[theme];
        }

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

L
lang 已提交
120 121 122 123
        /**
         * @type {Object}
         * @private
         */
L
lang 已提交
124
        this._theme = zrUtil.clone(theme);
L
lang 已提交
125

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

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

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

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

L
lang 已提交
150
        /**
L
lang 已提交
151
         * @type {module:echarts/ExtensionAPI}
L
lang 已提交
152 153
         * @private
         */
154
        this._api = new ExtensionAPI(this);
L
lang 已提交
155

L
lang 已提交
156 157 158 159
        /**
         * @type {module:echarts/CoordinateSystem}
         * @private
         */
160
        this._coordSysMgr = new CoordinateSystemManager();
L
lang 已提交
161

L
lang 已提交
162 163
        Eventful.call(this);

L
lang 已提交
164 165 166 167 168 169
        /**
         * @type {module:echarts~MessageCenter}
         * @private
         */
        this._messageCenter = new MessageCenter();

L
lang 已提交
170 171
        // Init mouse events
        this._initEvents();
L
Resize  
lang 已提交
172 173 174

        // In case some people write `window.onresize = chart.resize`
        this.resize = zrUtil.bind(this.resize, this);
175 176 177 178 179 180


        // Sort on demand
        function prioritySortFunc(a, b) {
            return a.prio - b.prio;
        }
L
lang 已提交
181 182
        timsort(visualFuncs, prioritySortFunc);
        timsort(dataProcessorFuncs, prioritySortFunc);
L
lang 已提交
183
    }
L
lang 已提交
184

L
tweak  
lang 已提交
185
    var echartsProto = ECharts.prototype;
L
lang 已提交
186

187 188 189
    /**
     * @return {HTMLDomElement}
     */
L
tweak  
lang 已提交
190 191 192
    echartsProto.getDom = function () {
        return this._dom;
    };
L
lang 已提交
193

194 195 196
    /**
     * @return {module:zrender~ZRender}
     */
L
tweak  
lang 已提交
197 198 199
    echartsProto.getZr = function () {
        return this._zr;
    };
L
lang 已提交
200

201 202 203
    /**
     * @param {Object} option
     * @param {boolean} notMerge
P
pah100 已提交
204
     * @param {boolean} [notRefreshImmediately=false] Useful when setOption frequently.
205
     */
206 207 208 209 210
    echartsProto.setOption = function (option, notMerge, notRefreshImmediately) {
        if (__DEV__) {
            zrUtil.assert(!this[IN_MAIN_PROCESS], '`setOption` should not be called during main process.');
        }

P
pah100 已提交
211
        this[IN_MAIN_PROCESS] = true;
212

P
pah100 已提交
213
        if (!this._model || notMerge) {
P
pah100 已提交
214 215 216
            this._model = new GlobalModel(
                null, null, this._theme, new OptionManager(this._api)
            );
L
tweak  
lang 已提交
217
        }
L
lang 已提交
218

P
pah100 已提交
219
        this._model.setOption(option, optionPreprocessorFuncs);
P
pah100 已提交
220 221

        updateMethods.prepareAndUpdate.call(this);
P
pah100 已提交
222 223

        !notRefreshImmediately && this._zr.refreshImmediately();
224

P
pah100 已提交
225
        this[IN_MAIN_PROCESS] = false;
P
pah100 已提交
226 227
    };

L
Tweak  
lang 已提交
228 229 230 231 232 233
    /**
     * @DEPRECATED
     */
    echartsProto.setTheme = function () {
        console.log('ECharts#setTheme() is DEPRECATED in ECharts 3.0');
    };
P
pah100 已提交
234

L
tweak  
lang 已提交
235 236 237 238 239 240
    /**
     * @return {module:echarts/model/Global}
     */
    echartsProto.getModel = function () {
        return this._model;
    };
L
lang 已提交
241

L
lang 已提交
242 243 244 245
    /**
     * @return {Object}
     */
    echartsProto.getOption = function () {
246
        return this._model.getOption();
L
lang 已提交
247 248
    };

L
tweak  
lang 已提交
249 250 251 252 253 254
    /**
     * @return {number}
     */
    echartsProto.getWidth = function () {
        return this._zr.getWidth();
    };
L
lang 已提交
255

L
tweak  
lang 已提交
256 257 258 259 260 261
    /**
     * @return {number}
     */
    echartsProto.getHeight = function () {
        return this._zr.getHeight();
    };
L
lang 已提交
262

L
lang 已提交
263 264 265 266 267 268 269 270 271 272
    /**
     * Get canvas which has all thing rendered
     * @param {Object} opts
     * @param {string} [opts.backgroundColor]
     */
    echartsProto.getRenderedCanvas = function (opts) {
        if (!env.canvasSupported) {
            return;
        }
        opts = opts || {};
273
        opts.pixelRatio = opts.pixelRatio || 1;
L
lang 已提交
274
        opts.backgroundColor = opts.backgroundColor
275
            || this._model.get('backgroundColor');
L
lang 已提交
276 277 278 279 280 281 282 283 284 285 286 287
        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']
288
     * @param {string} [opts.pixelRatio=1]
L
lang 已提交
289 290 291
     * @param {string} [opts.backgroundColor]
     */
    echartsProto.getDataURL = function (opts) {
292 293
        opts = opts || {};
        var excludeComponents = opts.excludeComponents;
294 295 296
        var ecModel = this._model;
        var excludesComponentViews = [];
        var self = this;
297 298

        each(excludeComponents, function (componentType) {
299 300 301 302 303 304 305 306 307 308 309 310
            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 已提交
311 312
            'image/' + (opts && opts.type || 'png')
        );
313 314 315 316 317

        each(excludesComponentViews, function (view) {
            view.group.ignore = false;
        });
        return url;
L
lang 已提交
318 319 320 321 322 323 324
    };


    /**
     * @return {string}
     * @param {Object} opts
     * @param {string} [opts.type='png']
325
     * @param {string} [opts.pixelRatio=1]
L
lang 已提交
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
     * @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 = [];
342
            var dpr = (opts && opts.pixelRatio) || 1;
L
lang 已提交
343 344 345
            for (var id in instances) {
                var chart = instances[id];
                if (chart.group === groupId) {
346 347 348
                    var canvas = chart.getRenderedCanvas(
                        zrUtil.clone(opts)
                    );
L
lang 已提交
349 350 351 352 353 354 355 356 357 358 359 360 361 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
                    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);
        }
    };
391

392 393 394 395 396 397 398
    var updateMethods = {

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

401
            var ecModel = this._model;
402 403
            var api = this._api;
            var coordSysMgr = this._coordSysMgr;
404 405 406 407
            // update before setOption
            if (!ecModel) {
                return;
            }
L
lang 已提交
408

409
            // Fixme First time update ?
410 411 412 413 414
            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 已提交
415

416 417 418 419 420
            // 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 已提交
421

422
            stackSeriesData.call(this, ecModel);
L
lang 已提交
423

424
            coordSysMgr.update(ecModel, api);
L
lang 已提交
425

L
lang 已提交
426
            doVisualEncoding.call(this, ecModel, payload);
427

428
            doRender.call(this, ecModel, payload);
429

430
            // Set background
L
lang 已提交
431
            var backgroundColor = ecModel.get('backgroundColor') || 'transparent';
432

L
lang 已提交
433
            var painter = this._zr.painter;
434
            // TODO all use clearColor ?
L
lang 已提交
435
            if (painter.isSingleCanvas && painter.isSingleCanvas()) {
L
lang 已提交
436 437 438 439 440
                this._zr.configLayer(0, {
                    clearColor: backgroundColor
                });
            }
            else {
L
lang 已提交
441 442 443 444 445 446 447 448 449
                // In IE8
                if (!env.canvasSupported) {
                    var colorArr = colorTool.parse(backgroundColor);
                    backgroundColor = colorTool.stringify(colorArr, 'rgb');
                    if (colorArr[3] === 0) {
                        backgroundColor = 'transparent';
                    }
                }
                backgroundColor = backgroundColor;
450
                this._dom.style.backgroundColor = backgroundColor;
L
lang 已提交
451
            }
L
lang 已提交
452

453
            // console.time && console.timeEnd('update');
454
        },
455

456 457 458 459 460 461 462
        // PENDING
        /**
         * @param {Object} payload
         * @private
         */
        updateView: function (payload) {
            var ecModel = this._model;
463

464 465 466 467 468
            // update before setOption
            if (!ecModel) {
                return;
            }

P
pah100 已提交
469 470 471 472
            ecModel.eachSeries(function (seriesModel) {
                seriesModel.getData().clearAllVisual();
            });

473
            doVisualEncoding.call(this, ecModel, payload);
474

475 476
            invokeUpdateMethod.call(this, 'updateView', ecModel, payload);
        },
477

478 479 480 481 482 483
        /**
         * @param {Object} payload
         * @private
         */
        updateVisual: function (payload) {
            var ecModel = this._model;
484

485 486 487 488 489
            // update before setOption
            if (!ecModel) {
                return;
            }

P
pah100 已提交
490 491 492 493
            ecModel.eachSeries(function (seriesModel) {
                seriesModel.getData().clearAllVisual();
            });

494
            doVisualEncoding.call(this, ecModel, payload);
495

496 497
            invokeUpdateMethod.call(this, 'updateVisual', ecModel, payload);
        },
498

499 500 501 502 503 504
        /**
         * @param {Object} payload
         * @private
         */
        updateLayout: function (payload) {
            var ecModel = this._model;
505

506 507 508 509 510
            // update before setOption
            if (!ecModel) {
                return;
            }

L
lang 已提交
511
            doLayout.call(this, ecModel, payload);
512

513 514
            invokeUpdateMethod.call(this, 'updateLayout', ecModel, payload);
        },
L
lang 已提交
515

516 517 518 519 520 521 522 523 524 525 526 527 528 529
        /**
         * @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 已提交
530 531 532 533
        },

        /**
         * @param {Object} payload
P
pah100 已提交
534
         * @private
P
pah100 已提交
535
         */
P
pah100 已提交
536
        prepareAndUpdate: function (payload) {
P
pah100 已提交
537
            var ecModel = this._model;
538

P
pah100 已提交
539 540 541
            prepareView.call(this, 'component', ecModel);

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

P
pah100 已提交
543
            updateMethods.update.call(this, payload);
P
pah100 已提交
544
        }
545 546 547 548 549 550
    };

    /**
     * @param {Object} payload
     * @private
     */
551
    function toggleHighlight(method, payload) {
552
        var ecModel = this._model;
553

554 555 556 557 558
        // dispatchAction before setOption
        if (!ecModel) {
            return;
        }

559 560
        ecModel.eachComponent(
            {mainType: 'series', query: payload},
L
lang 已提交
561
            function (seriesModel, index) {
L
lang 已提交
562
                var chartView = this._chartsMap[seriesModel.__viewId];
L
lang 已提交
563
                if (chartView && chartView.__alive) {
P
pah100 已提交
564
                    chartView[method](
L
lang 已提交
565
                        seriesModel, ecModel, this._api, payload
P
pah100 已提交
566
                    );
567 568 569 570
                }
            },
            this
        );
571
    }
572

L
Resize  
lang 已提交
573 574 575
    /**
     * Resize the chart
     */
L
tweak  
lang 已提交
576
    echartsProto.resize = function () {
577 578 579 580
        if (__DEV__) {
            zrUtil.assert(!this[IN_MAIN_PROCESS], '`resize` should not be called during main process.');
        }

P
pah100 已提交
581
        this[IN_MAIN_PROCESS] = true;
P
pah100 已提交
582

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

P
pah100 已提交
585 586
        var optionChanged = this._model && this._model.resetOption('media');
        updateMethods[optionChanged ? 'prepareAndUpdate' : 'update'].call(this);
L
lang 已提交
587 588 589

        // Resize loading effect
        this._loadingFX && this._loadingFX.resize();
590

P
pah100 已提交
591
        this[IN_MAIN_PROCESS] = false;
L
lang 已提交
592 593 594 595 596 597 598 599 600 601 602 603 604
    };

    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 已提交
605
        this.hideLoading();
L
lang 已提交
606
        var el = defaultLoadingEffect(this._api, cfg);
L
lang 已提交
607
        var zr = this._zr;
L
lang 已提交
608
        this._loadingFX = el;
L
lang 已提交
609 610

        zr.add(el);
L
lang 已提交
611 612 613 614 615 616
    };

    /**
     * Hide loading effect
     */
    echartsProto.hideLoading = function () {
L
lang 已提交
617
        this._loadingFX && this._zr.remove(this._loadingFX);
L
lang 已提交
618
        this._loadingFX = null;
L
tweak  
lang 已提交
619
    };
P
pah100 已提交
620

L
lang 已提交
621
    /**
L
Resize  
lang 已提交
622 623
     * @param {Object} eventObj
     * @return {Object}
L
lang 已提交
624 625 626 627 628 629 630
     */
    echartsProto.makeActionFromEvent = function (eventObj) {
        var payload = zrUtil.extend({}, eventObj);
        payload.type = eventActionMap[eventObj.type];
        return payload;
    };

L
tweak  
lang 已提交
631 632 633 634
    /**
     * @pubilc
     * @param {Object} payload
     * @param {string} [payload.type] Action type
P
pah100 已提交
635
     * @param {boolean} [silent=false] Whether trigger event.
L
tweak  
lang 已提交
636
     */
L
lang 已提交
637
    echartsProto.dispatchAction = function (payload, silent) {
L
tweak  
lang 已提交
638
        var actionWrap = actions[payload.type];
639 640 641
        if (!actionWrap) {
            return;
        }
L
lang 已提交
642

643 644 645 646 647
        var actionInfo = actionWrap.actionInfo;
        var updateMethod = actionInfo.update || 'update';

        if (__DEV__) {
            zrUtil.assert(
648
                !this[IN_MAIN_PROCESS],
649
                '`dispatchAction` should not be called during main process.'
650
                + 'unless updateMathod is "none".'
651 652 653
            );
        }

654
        this[IN_MAIN_PROCESS] = true;
L
lang 已提交
655

656 657 658 659 660 661 662 663 664 665 666
        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 已提交
667

668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698
        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];
        }

699 700 701
        this[IN_MAIN_PROCESS] = false;

        !silent && this._messageCenter.trigger(eventObj.type, eventObj);
L
tweak  
lang 已提交
702
    };
703

L
lang 已提交
704 705 706 707
    /**
     * Register event
     * @method
     */
708 709 710
    echartsProto.on = createRegisterEventWithLowercaseName('on');
    echartsProto.off = createRegisterEventWithLowercaseName('off');
    echartsProto.one = createRegisterEventWithLowercaseName('one');
L
lang 已提交
711

L
tweak  
lang 已提交
712 713 714 715
    /**
     * @param {string} methodName
     * @private
     */
716
    function invokeUpdateMethod(methodName, ecModel, payload) {
717
        var api = this._api;
L
lang 已提交
718

L
tweak  
lang 已提交
719
        // Update all components
L
lang 已提交
720
        each(this._componentsViews, function (component) {
L
tweak  
lang 已提交
721 722
            var componentModel = component.__model;
            component[methodName](componentModel, ecModel, api, payload);
723

L
tweak  
lang 已提交
724 725
            updateZ(componentModel, component);
        }, this);
L
lang 已提交
726

L
tweak  
lang 已提交
727 728
        // Upate all charts
        ecModel.eachSeries(function (seriesModel, idx) {
L
lang 已提交
729
            var chart = this._chartsMap[seriesModel.__viewId];
L
tweak  
lang 已提交
730
            chart[methodName](seriesModel, ecModel, api, payload);
731

L
tweak  
lang 已提交
732 733
            updateZ(seriesModel, chart);
        }, this);
734

735
    }
L
lang 已提交
736

L
lang 已提交
737
    /**
L
Tweak  
lang 已提交
738
     * Prepare view instances of charts and components
L
lang 已提交
739 740 741
     * @param  {module:echarts/model/Global} ecModel
     * @private
     */
742
    function prepareView(type, ecModel) {
L
Tweak  
lang 已提交
743
        var isComponent = type === 'component';
L
lang 已提交
744
        var viewList = isComponent ? this._componentsViews : this._chartsViews;
L
Tweak  
lang 已提交
745
        var viewMap = isComponent ? this._componentsMap : this._chartsMap;
L
tweak  
lang 已提交
746
        var zr = this._zr;
L
lang 已提交
747

L
Tweak  
lang 已提交
748
        for (var i = 0; i < viewList.length; i++) {
L
lang 已提交
749
            viewList[i].__alive = false;
L
tweak  
lang 已提交
750
        }
L
lang 已提交
751

L
Tweak  
lang 已提交
752 753 754 755
        ecModel[isComponent ? 'eachComponent' : 'eachSeries'](function (componentType, model) {
            if (isComponent) {
                if (componentType === 'series') {
                    return;
L
lang 已提交
756
                }
757
            }
L
tweak  
lang 已提交
758
            else {
L
Tweak  
lang 已提交
759
                model = componentType;
L
tweak  
lang 已提交
760 761
            }

762
            // Consider: id same and type changed.
L
lang 已提交
763 764
            var viewId = model.id + '_' + model.type;
            var view = viewMap[viewId];
L
Tweak  
lang 已提交
765 766 767
            if (!view) {
                var classType = ComponentModel.parseClassType(model.type);
                var Clazz = isComponent
L
tweak  
lang 已提交
768
                    ? ComponentView.getClass(classType.main, classType.sub)
L
Tweak  
lang 已提交
769
                    : ChartView.getClass(classType.sub);
L
tweak  
lang 已提交
770
                if (Clazz) {
L
Tweak  
lang 已提交
771 772
                    view = new Clazz();
                    view.init(ecModel, this._api);
L
lang 已提交
773
                    viewMap[viewId] = view;
L
Tweak  
lang 已提交
774 775 776 777 778
                    viewList.push(view);
                    zr.add(view.group);
                }
                else {
                    // Error
L
lang 已提交
779
                    return;
L
lang 已提交
780
                }
781
            }
L
Tweak  
lang 已提交
782

L
lang 已提交
783
            model.__viewId = viewId;
L
lang 已提交
784
            view.__alive = true;
L
lang 已提交
785
            view.__id = viewId;
L
Tweak  
lang 已提交
786
            view.__model = model;
L
tweak  
lang 已提交
787 788
        }, this);

L
Tweak  
lang 已提交
789 790
        for (var i = 0; i < viewList.length;) {
            var view = viewList[i];
L
lang 已提交
791
            if (!view.__alive) {
L
Tweak  
lang 已提交
792
                zr.remove(view.group);
L
lang 已提交
793
                view.dispose(ecModel, this._api);
L
Tweak  
lang 已提交
794 795
                viewList.splice(i, 1);
                delete viewMap[view.__id];
L
tweak  
lang 已提交
796 797 798 799 800
            }
            else {
                i++;
            }
        }
801 802
    }

L
tweak  
lang 已提交
803 804 805 806 807 808
    /**
     * Processor data in each series
     *
     * @param {module:echarts/model/Global} ecModel
     * @private
     */
809
    function processData(ecModel, api) {
810 811
        each(dataProcessorFuncs, function (process) {
            process.func(ecModel, api);
L
tweak  
lang 已提交
812
        });
813
    }
L
lang 已提交
814

L
tweak  
lang 已提交
815 816 817
    /**
     * @private
     */
818
    function stackSeriesData(ecModel) {
L
tweak  
lang 已提交
819 820 821 822 823 824 825 826
        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 已提交
827
                }
L
tweak  
lang 已提交
828 829 830
                stackedDataMap[stack] = data;
            }
        });
831
    }
L
lang 已提交
832

L
tweak  
lang 已提交
833
    /**
834
     * Layout before each chart render there series, special visual encoding stage
L
tweak  
lang 已提交
835 836 837 838
     *
     * @param {module:echarts/model/Global} ecModel
     * @private
     */
L
lang 已提交
839 840
    function doLayout(ecModel, payload) {
        var api = this._api;
841 842 843 844
        each(visualFuncs, function (visual) {
            if (visual.isLayout) {
                visual.func(ecModel, api, payload);
            }
L
tweak  
lang 已提交
845
        });
846
    }
L
lang 已提交
847

L
tweak  
lang 已提交
848
    /**
849
     * Encode visual infomation from data after data processing
L
tweak  
lang 已提交
850 851 852 853
     *
     * @param {module:echarts/model/Global} ecModel
     * @private
     */
L
lang 已提交
854 855
    function doVisualEncoding(ecModel, payload) {
        var api = this._api;
L
lang 已提交
856 857 858 859
        ecModel.clearColorPalette();
        ecModel.eachSeries(function (seriesModel) {
            seriesModel.clearColorPalette();
        });
860 861
        each(visualFuncs, function (visual) {
            visual.func(ecModel, api, payload);
L
tweak  
lang 已提交
862
        });
863
    }
L
lang 已提交
864

L
tweak  
lang 已提交
865 866 867 868
    /**
     * Render each chart and component
     * @private
     */
869
    function doRender(ecModel, payload) {
870
        var api = this._api;
L
tweak  
lang 已提交
871
        // Render all components
L
lang 已提交
872 873 874
        each(this._componentsViews, function (componentView) {
            var componentModel = componentView.__model;
            componentView.render(componentModel, ecModel, api, payload);
L
tweak  
lang 已提交
875

L
lang 已提交
876
            updateZ(componentModel, componentView);
L
tweak  
lang 已提交
877 878
        }, this);

L
lang 已提交
879
        each(this._chartsViews, function (chart) {
L
lang 已提交
880
            chart.__alive = false;
L
tweak  
lang 已提交
881 882
        }, this);

L
lang 已提交
883
        var elCountAll = 0;
L
tweak  
lang 已提交
884 885
        // Render all charts
        ecModel.eachSeries(function (seriesModel, idx) {
L
lang 已提交
886
            var chartView = this._chartsMap[seriesModel.__viewId];
L
lang 已提交
887
            chartView.__alive = true;
L
lang 已提交
888
            chartView.render(seriesModel, ecModel, api, payload);
L
tweak  
lang 已提交
889

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

L
lang 已提交
892
            updateZ(seriesModel, chartView);
893 894 895

            // Progressive configuration
            var elCount = 0;
L
lang 已提交
896
            chartView.group.traverse(function (el) {
L
Tweak  
lang 已提交
897
                if (el.type !== 'group' && !el.ignore) {
L
lang 已提交
898 899 900
                    elCount++;
                }
            });
L
lang 已提交
901 902
            elCountAll += elCount;

L
Tweak  
lang 已提交
903
            var frameDrawNum = +seriesModel.get('progressive');
L
lang 已提交
904
            var needProgressive = elCount > seriesModel.get('progressiveThreshold') && frameDrawNum && !env.node;
905 906
            if (needProgressive) {
                chartView.group.traverse(function (el) {
L
lang 已提交
907
                    // FIXME marker and other components
908 909
                    if (el.type !== 'group') {
                        el.progressive = needProgressive ?
L
Tweak  
lang 已提交
910
                            Math.floor(elCount++ / frameDrawNum) : -1;
911 912 913 914 915 916
                        if (needProgressive) {
                            el.stopAnimation(true);
                        }
                    }
                });
            }
L
tweak  
lang 已提交
917 918
        }, this);

L
lang 已提交
919 920 921 922 923 924 925 926
        // 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 已提交
927
        // Remove groups of unrendered charts
L
lang 已提交
928
        each(this._chartsViews, function (chart) {
L
lang 已提交
929
            if (!chart.__alive) {
L
tweak  
lang 已提交
930 931 932
                chart.remove(ecModel, api);
            }
        }, this);
933
    }
L
lang 已提交
934

L
lang 已提交
935
    var MOUSE_EVENT_NAMES = [
L
Typo  
lang 已提交
936
        'click', 'dblclick', 'mouseover', 'mouseout', 'mousedown', 'mouseup', 'globalout'
L
lang 已提交
937 938 939 940 941 942
    ];
    /**
     * @private
     */
    echartsProto._initEvents = function () {
        each(MOUSE_EVENT_NAMES, function (eveName) {
943
            this._zr.on(eveName, function (e) {
L
lang 已提交
944 945 946
                var ecModel = this.getModel();
                var el = e.target;
                if (el && el.dataIndex != null) {
L
lang 已提交
947
                    var dataModel = el.dataModel || ecModel.getSeriesByIndex(el.seriesIndex);
948
                    var params = dataModel && dataModel.getDataParams(el.dataIndex, el.dataType) || {};
L
lang 已提交
949 950 951 952
                    params.event = e;
                    params.type = eveName;
                    this.trigger(eveName, params);
                }
L
lang 已提交
953 954 955 956
                // If element has custom eventData of components
                else if (el && el.eventData) {
                    this.trigger(eveName, el.eventData);
                }
L
lang 已提交
957 958
            }, this);
        }, this);
L
lang 已提交
959

L
lang 已提交
960
        each(eventActionMap, function (actionType, eventType) {
L
lang 已提交
961 962 963 964
            this._messageCenter.on(eventType, function (event) {
                this.trigger(eventType, event);
            }, this);
        }, this);
L
lang 已提交
965 966
    };

L
lang 已提交
967
    /**
L
lang 已提交
968
     * @return {boolean}
L
lang 已提交
969 970 971 972
     */
    echartsProto.isDisposed = function () {
        return this._disposed;
    };
L
lang 已提交
973 974 975 976 977 978 979

    /**
     * Clear
     */
    echartsProto.clear = function () {
        this.setOption({}, true);
    };
L
lang 已提交
980 981 982
    /**
     * Dispose instance
     */
L
tweak  
lang 已提交
983
    echartsProto.dispose = function () {
984 985 986 987 988 989
        if (this._disposed) {
            if (__DEV__) {
                console.warn('Instance ' + this.id + ' has been disposed');
            }
            return;
        }
L
lang 已提交
990
        this._disposed = true;
991

L
lang 已提交
992
        var api = this._api;
L
lang 已提交
993
        var ecModel = this._model;
L
lang 已提交
994

L
lang 已提交
995
        each(this._componentsViews, function (component) {
L
lang 已提交
996
            component.dispose(ecModel, api);
L
tweak  
lang 已提交
997
        });
L
lang 已提交
998
        each(this._chartsViews, function (chart) {
L
lang 已提交
999
            chart.dispose(ecModel, api);
L
tweak  
lang 已提交
1000
        });
L
lang 已提交
1001

1002
        // Dispose after all views disposed
L
Tweak  
lang 已提交
1003
        this._zr.dispose();
L
lang 已提交
1004

L
lang 已提交
1005
        delete instances[this.id];
L
lang 已提交
1006 1007
    };

L
lang 已提交
1008 1009
    zrUtil.mixin(ECharts, Eventful);

L
lang 已提交
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
    /**
     * @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) {
1020 1021 1022 1023
            if (el.type !== 'group') {
                z != null && (el.z = z);
                zlevel != null && (el.zlevel = zlevel);
            }
L
lang 已提交
1024 1025
        });
    }
L
lang 已提交
1026 1027 1028 1029
    /**
     * @type {Array.<Function>}
     * @inner
     */
P
pah100 已提交
1030 1031
    var actions = [];

L
lang 已提交
1032
    /**
L
lang 已提交
1033
     * Map eventType to actionType
L
lang 已提交
1034 1035 1036 1037
     * @type {Object}
     */
    var eventActionMap = {};

L
lang 已提交
1038 1039 1040 1041 1042
    /**
     * Data processor functions of each stage
     * @type {Array.<Object.<string, Function>>}
     * @inner
     */
1043
    var dataProcessorFuncs = [];
L
lang 已提交
1044

1045 1046 1047 1048 1049 1050
    /**
     * @type {Array.<Function>}
     * @inner
     */
    var optionPreprocessorFuncs = [];

L
lang 已提交
1051
    /**
1052
     * Visual encoding functions of each stage
L
lang 已提交
1053 1054 1055
     * @type {Array.<Object.<string, Function>>}
     * @inner
     */
1056
    var visualFuncs = [];
L
lang 已提交
1057 1058 1059 1060 1061 1062
    /**
     * Theme storage
     * @type {Object.<key, Object>}
     */
    var themeStorage = {};

L
lang 已提交
1063

L
lang 已提交
1064 1065 1066 1067 1068 1069
    var instances = {};
    var connectedGroups = {};

    var idBase = new Date() - 0;
    var groupIdBase = new Date() - 0;
    var DOM_ATTRIBUTE_KEY = '_echarts_instance_';
L
lang 已提交
1070
    /**
L
lang 已提交
1071
     * @alias module:echarts
L
lang 已提交
1072
     */
L
lang 已提交
1073 1074 1075 1076
    var echarts = {
        /**
         * @type {number}
         */
L
lang 已提交
1077
        version: '3.1.10',
L
lang 已提交
1078
        dependencies: {
L
lang 已提交
1079
            zrender: '3.1.0'
L
lang 已提交
1080 1081
        }
    };
L
lang 已提交
1082

L
lang 已提交
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
    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 已提交
1118 1119 1120 1121 1122 1123
    /**
     * @param {HTMLDomElement} dom
     * @param {Object} [theme]
     * @param {Object} opts
     */
    echarts.init = function (dom, theme, opts) {
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
        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 已提交
1137
            if (zrUtil.isDom(dom) && dom.nodeName.toUpperCase() !== 'CANVAS' && (!dom.clientWidth || !dom.clientHeight)) {
L
lang 已提交
1138
                console.warn('Can\'t get dom width or height');
1139
            }
1140
        }
L
lang 已提交
1141 1142

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

L
lang 已提交
1146 1147 1148
        dom.setAttribute &&
            dom.setAttribute(DOM_ATTRIBUTE_KEY, chart.id);

L
lang 已提交
1149
        enableConnect(chart);
L
lang 已提交
1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167

        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 已提交
1168
            groupId = groupId || ('g_' + groupIdBase++);
L
lang 已提交
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213
            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 已提交
1214
    };
L
lang 已提交
1215

L
lang 已提交
1216 1217 1218 1219 1220 1221 1222
    /**
     * Register theme
     */
    echarts.registerTheme = function (name, theme) {
        themeStorage[name] = theme;
    };

L
tweak  
lang 已提交
1223 1224 1225 1226 1227 1228 1229
    /**
     * Register option preprocessor
     * @param {Function} preprocessorFunc
     */
    echarts.registerPreprocessor = function (preprocessorFunc) {
        optionPreprocessorFuncs.push(preprocessorFunc);
    };
1230

L
tweak  
lang 已提交
1231
    /**
1232
     * @param {number} [priority=1000]
L
tweak  
lang 已提交
1233 1234
     * @param {Function} processorFunc
     */
1235 1236 1237 1238
    echarts.registerProcessor = function (priority, processorFunc) {
        if (typeof priority === 'function') {
            processorFunc = priority;
            priority = PRIORITY_PROCESSOR_FILTER;
L
tweak  
lang 已提交
1239
        }
1240 1241 1242 1243
        if (__DEV__) {
            if (isNaN(priority)) {
                throw new Error('Unkown processor priority');
            }
1244 1245 1246 1247 1248
        }
        dataProcessorFuncs.push({
            prio: priority,
            func: processorFunc
        });
L
tweak  
lang 已提交
1249
    };
L
lang 已提交
1250

L
tweak  
lang 已提交
1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261
    /**
     * 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 已提交
1262 1263 1264 1265
     * @param {string} [actionInfo.event]
     * @param {string} [actionInfo.update]
     * @param {string} [eventName]
     * @param {Function} action
L
tweak  
lang 已提交
1266
     */
L
lang 已提交
1267 1268 1269 1270 1271
    echarts.registerAction = function (actionInfo, eventName, action) {
        if (typeof eventName === 'function') {
            action = eventName;
            eventName = '';
        }
L
tweak  
lang 已提交
1272 1273
        var actionType = zrUtil.isObject(actionInfo)
            ? actionInfo.type
L
lang 已提交
1274 1275 1276
            : ([actionInfo, actionInfo = {
                event: eventName
            }][0]);
L
lang 已提交
1277

L
lang 已提交
1278 1279
        // Event name is all lowercase
        actionInfo.event = (actionInfo.event || actionType).toLowerCase();
L
lang 已提交
1280
        eventName = actionInfo.event;
1281

L
tweak  
lang 已提交
1282 1283 1284
        if (!actions[actionType]) {
            actions[actionType] = {action: action, actionInfo: actionInfo};
        }
L
lang 已提交
1285
        eventActionMap[eventName] = actionType;
L
tweak  
lang 已提交
1286
    };
P
pah100 已提交
1287

L
tweak  
lang 已提交
1288 1289 1290 1291 1292 1293 1294
    /**
     * @param {string} type
     * @param {*} CoordinateSystem
     */
    echarts.registerCoordinateSystem = function (type, CoordinateSystem) {
        CoordinateSystemManager.register(type, CoordinateSystem);
    };
L
lang 已提交
1295

L
tweak  
lang 已提交
1296
    /**
1297 1298 1299 1300 1301 1302
     * 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 已提交
1303
     */
1304 1305 1306 1307 1308
    echarts.registerLayout = function (priority, layoutFunc) {
        if (typeof priority === 'function') {
            layoutFunc = priority;
            priority = PRIORITY_VISUAL_LAYOUT;
        }
1309 1310 1311 1312
        if (__DEV__) {
            if (isNaN(priority)) {
                throw new Error('Unkown layout priority');
            }
L
tweak  
lang 已提交
1313
        }
1314 1315 1316 1317 1318
        visualFuncs.push({
            prio: priority,
            func: layoutFunc,
            isLayout: true
        });
L
tweak  
lang 已提交
1319
    };
L
lang 已提交
1320

L
tweak  
lang 已提交
1321
    /**
1322 1323
     * @param {string} [priority=3000]
     * @param {Function} visualFunc
L
tweak  
lang 已提交
1324
     */
1325 1326 1327 1328
    echarts.registerVisual = function (priority, visualFunc) {
        if (typeof priority === 'function') {
            visualFunc = priority;
            priority = PRIORITY_VISUAL_CHART;
L
tweak  
lang 已提交
1329
        }
1330 1331 1332 1333
        if (__DEV__) {
            if (isNaN(priority)) {
                throw new Error('Unkown visual priority');
            }
1334 1335 1336 1337 1338
        }
        visualFuncs.push({
            prio: priority,
            func: visualFunc
        });
L
tweak  
lang 已提交
1339
    };
L
Update  
lang 已提交
1340

L
tweak  
lang 已提交
1341 1342 1343 1344 1345 1346
    /**
     * @param {Object} opts
     */
    echarts.extendChartView = function (opts) {
        return ChartView.extend(opts);
    };
L
Update  
lang 已提交
1347

L
tweak  
lang 已提交
1348 1349 1350 1351 1352 1353
    /**
     * @param {Object} opts
     */
    echarts.extendComponentModel = function (opts) {
        return ComponentModel.extend(opts);
    };
L
Update  
lang 已提交
1354

L
tweak  
lang 已提交
1355 1356 1357 1358 1359 1360
    /**
     * @param {Object} opts
     */
    echarts.extendSeriesModel = function (opts) {
        return SeriesModel.extend(opts);
    };
P
pah100 已提交
1361

L
tweak  
lang 已提交
1362 1363 1364 1365 1366
    /**
     * @param {Object} opts
     */
    echarts.extendComponentView = function (opts) {
        return ComponentView.extend(opts);
L
lang 已提交
1367 1368
    };

1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388
    /**
     * 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;
    };

1389
    echarts.registerVisual(PRIORITY_VISUAL_GLOBAL, zrUtil.curry(
L
lang 已提交
1390 1391
        require('./visual/seriesColor'), '', 'itemStyle'
    ));
1392 1393
    echarts.registerPreprocessor(require('./preprocessor/backwardCompat'));

1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405
    // Default action
    echarts.registerAction({
        type: 'highlight',
        event: 'highlight',
        update: 'highlight'
    }, zrUtil.noop);
    echarts.registerAction({
        type: 'downplay',
        event: 'downplay',
        update: 'downplay'
    }, zrUtil.noop);

P
pah100 已提交
1406 1407 1408 1409

    // --------
    // Exports
    // --------
L
lang 已提交
1410 1411 1412
    //
    echarts.List = require('./data/List');
    echarts.Model = require('./model/Model');
P
pah100 已提交
1413

L
lang 已提交
1414 1415 1416
    echarts.graphic = require('./util/graphic');
    echarts.number = require('./util/number');
    echarts.format = require('./util/format');
L
lang 已提交
1417 1418
    echarts.matrix = require('zrender/core/matrix');
    echarts.vector = require('zrender/core/vector');
L
lang 已提交
1419
    echarts.color = require('zrender/tool/color');
P
pah100 已提交
1420 1421 1422 1423 1424

    echarts.util = {};
    each([
            'map', 'each', 'filter', 'indexOf', 'inherits',
            'reduce', 'filter', 'bind', 'curry', 'isArray',
L
lang 已提交
1425
            'isString', 'isObject', 'isFunction', 'extend', 'defaults'
P
pah100 已提交
1426 1427 1428 1429 1430 1431
        ],
        function (name) {
            echarts.util[name] = zrUtil[name];
        }
    );

1432 1433 1434 1435 1436 1437 1438 1439 1440 1441
    // 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 已提交
1442
            COMPONENT: PRIORITY_VISUAL_COMPONENT,
P
pah100 已提交
1443
            BRUSH: PRIORITY_VISUAL_BRUSH
1444 1445 1446
        }
    };

L
lang 已提交
1447 1448
    return echarts;
});