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

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

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

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

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

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

34 35
    var each = zrUtil.each;

36 37
    var PRIORITY_PROCESSOR_FILTER = 1000;
    var PRIORITY_PROCESSOR_STATISTIC = 5000;
38

39 40 41 42 43

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

46 47 48 49 50 51
    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 已提交
52
    }
L
lang 已提交
53 54 55 56 57 58
    /**
     * @module echarts~MessageCenter
     */
    function MessageCenter() {
        Eventful.call(this);
    }
59 60 61
    MessageCenter.prototype.on = createRegisterEventWithLowercaseName('on');
    MessageCenter.prototype.off = createRegisterEventWithLowercaseName('off');
    MessageCenter.prototype.one = createRegisterEventWithLowercaseName('one');
L
lang 已提交
62
    zrUtil.mixin(MessageCenter, Eventful);
L
lang 已提交
63 64 65
    /**
     * @module echarts~ECharts
     */
L
lang 已提交
66
    function ECharts (dom, theme, opts) {
L
lang 已提交
67
        opts = opts || {};
L
lang 已提交
68

L
lang 已提交
69 70 71 72 73
        // Get theme by name
        if (typeof theme === 'string') {
            theme = themeStorage[theme];
        }

L
lang 已提交
74 75 76 77 78 79 80 81 82
        /**
         * @type {string}
         */
        this.id;
        /**
         * Group id
         * @type {string}
         */
        this.group;
L
lang 已提交
83 84 85 86 87
        /**
         * @type {HTMLDomElement}
         * @private
         */
        this._dom = dom;
L
lang 已提交
88 89 90 91
        /**
         * @type {module:zrender/ZRender}
         * @private
         */
L
lang 已提交
92
        this._zr = zrender.init(dom, {
93 94
            renderer: opts.renderer || 'canvas',
            devicePixelRatio: opts.devicePixelRatio
L
lang 已提交
95
        });
L
lang 已提交
96

L
lang 已提交
97 98 99 100
        /**
         * @type {Object}
         * @private
         */
L
lang 已提交
101
        this._theme = zrUtil.clone(theme);
L
lang 已提交
102

L
lang 已提交
103 104 105 106
        /**
         * @type {Array.<module:echarts/view/Chart>}
         * @private
         */
L
lang 已提交
107
        this._chartsViews = [];
L
lang 已提交
108 109 110 111 112

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

L
lang 已提交
115 116 117 118
        /**
         * @type {Array.<module:echarts/view/Component>}
         * @private
         */
L
lang 已提交
119
        this._componentsViews = [];
L
lang 已提交
120 121 122 123 124

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

L
lang 已提交
127
        /**
L
lang 已提交
128
         * @type {module:echarts/ExtensionAPI}
L
lang 已提交
129 130
         * @private
         */
131
        this._api = new ExtensionAPI(this);
L
lang 已提交
132

L
lang 已提交
133 134 135 136
        /**
         * @type {module:echarts/CoordinateSystem}
         * @private
         */
137
        this._coordSysMgr = new CoordinateSystemManager();
L
lang 已提交
138

L
lang 已提交
139 140
        Eventful.call(this);

L
lang 已提交
141 142 143 144 145 146
        /**
         * @type {module:echarts~MessageCenter}
         * @private
         */
        this._messageCenter = new MessageCenter();

L
lang 已提交
147 148
        // Init mouse events
        this._initEvents();
L
Resize  
lang 已提交
149 150 151

        // In case some people write `window.onresize = chart.resize`
        this.resize = zrUtil.bind(this.resize, this);
152 153 154 155 156 157 158 159


        // Sort on demand
        function prioritySortFunc(a, b) {
            return a.prio - b.prio;
        }
        visualFuncs.sort(prioritySortFunc);
        dataProcessorFuncs.sort(prioritySortFunc);
L
lang 已提交
160
    }
L
lang 已提交
161

L
tweak  
lang 已提交
162
    var echartsProto = ECharts.prototype;
L
lang 已提交
163

164 165 166
    /**
     * @return {HTMLDomElement}
     */
L
tweak  
lang 已提交
167 168 169
    echartsProto.getDom = function () {
        return this._dom;
    };
L
lang 已提交
170

171 172 173
    /**
     * @return {module:zrender~ZRender}
     */
L
tweak  
lang 已提交
174 175 176
    echartsProto.getZr = function () {
        return this._zr;
    };
L
lang 已提交
177

178 179 180
    /**
     * @param {Object} option
     * @param {boolean} notMerge
P
pah100 已提交
181
     * @param {boolean} [notRefreshImmediately=false] Useful when setOption frequently.
182 183
     */
    echartsProto.setOption = function (option, notMerge, notRefreshImmediately) {
P
pah100 已提交
184
        if (!this._model || notMerge) {
P
pah100 已提交
185 186 187
            this._model = new GlobalModel(
                null, null, this._theme, new OptionManager(this._api)
            );
L
tweak  
lang 已提交
188
        }
L
lang 已提交
189

P
pah100 已提交
190
        this._model.setOption(option, optionPreprocessorFuncs);
P
pah100 已提交
191 192

        updateMethods.prepareAndUpdate.call(this);
P
pah100 已提交
193 194 195 196

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

L
Tweak  
lang 已提交
197 198 199 200 201 202
    /**
     * @DEPRECATED
     */
    echartsProto.setTheme = function () {
        console.log('ECharts#setTheme() is DEPRECATED in ECharts 3.0');
    };
P
pah100 已提交
203

L
tweak  
lang 已提交
204 205 206 207 208 209
    /**
     * @return {module:echarts/model/Global}
     */
    echartsProto.getModel = function () {
        return this._model;
    };
L
lang 已提交
210

L
lang 已提交
211 212 213 214
    /**
     * @return {Object}
     */
    echartsProto.getOption = function () {
215
        return this._model.getOption();
L
lang 已提交
216 217
    };

L
tweak  
lang 已提交
218 219 220 221 222 223
    /**
     * @return {number}
     */
    echartsProto.getWidth = function () {
        return this._zr.getWidth();
    };
L
lang 已提交
224

L
tweak  
lang 已提交
225 226 227 228 229 230
    /**
     * @return {number}
     */
    echartsProto.getHeight = function () {
        return this._zr.getHeight();
    };
L
lang 已提交
231

L
lang 已提交
232 233 234 235 236 237 238 239 240 241
    /**
     * Get canvas which has all thing rendered
     * @param {Object} opts
     * @param {string} [opts.backgroundColor]
     */
    echartsProto.getRenderedCanvas = function (opts) {
        if (!env.canvasSupported) {
            return;
        }
        opts = opts || {};
242
        opts.pixelRatio = opts.pixelRatio || 1;
L
lang 已提交
243
        opts.backgroundColor = opts.backgroundColor
244
            || this._model.get('backgroundColor');
L
lang 已提交
245 246 247 248 249 250 251 252 253 254 255 256
        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']
257
     * @param {string} [opts.pixelRatio=1]
L
lang 已提交
258 259 260
     * @param {string} [opts.backgroundColor]
     */
    echartsProto.getDataURL = function (opts) {
261 262
        opts = opts || {};
        var excludeComponents = opts.excludeComponents;
263 264 265
        var ecModel = this._model;
        var excludesComponentViews = [];
        var self = this;
266 267

        each(excludeComponents, function (componentType) {
268 269 270 271 272 273 274 275 276 277 278 279
            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 已提交
280 281
            'image/' + (opts && opts.type || 'png')
        );
282 283 284 285 286

        each(excludesComponentViews, function (view) {
            view.group.ignore = false;
        });
        return url;
L
lang 已提交
287 288 289 290 291 292 293
    };


    /**
     * @return {string}
     * @param {Object} opts
     * @param {string} [opts.type='png']
294
     * @param {string} [opts.pixelRatio=1]
L
lang 已提交
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
     * @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 = [];
311
            var dpr = (opts && opts.pixelRatio) || 1;
L
lang 已提交
312 313 314
            for (var id in instances) {
                var chart = instances[id];
                if (chart.group === groupId) {
315 316 317
                    var canvas = chart.getRenderedCanvas(
                        zrUtil.clone(opts)
                    );
L
lang 已提交
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
                    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);
        }
    };
360

361 362 363 364 365 366 367
    var updateMethods = {

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

370
            var ecModel = this._model;
371 372
            var api = this._api;
            var coordSysMgr = this._coordSysMgr;
373 374 375 376
            // update before setOption
            if (!ecModel) {
                return;
            }
L
lang 已提交
377

378
            // Fixme First time update ?
379 380 381 382 383
            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 已提交
384

385 386 387 388 389
            // 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 已提交
390

391
            stackSeriesData.call(this, ecModel);
L
lang 已提交
392

393
            coordSysMgr.update(ecModel, api);
L
lang 已提交
394

L
lang 已提交
395
            doVisualEncoding.call(this, ecModel, payload);
396

397
            doRender.call(this, ecModel, payload);
398

399
            // Set background
L
lang 已提交
400
            var backgroundColor = ecModel.get('backgroundColor') || 'transparent';
401

L
lang 已提交
402
            var painter = this._zr.painter;
403
            // TODO all use clearColor ?
L
lang 已提交
404
            if (painter.isSingleCanvas && painter.isSingleCanvas()) {
L
lang 已提交
405 406 407 408 409
                this._zr.configLayer(0, {
                    clearColor: backgroundColor
                });
            }
            else {
L
lang 已提交
410 411 412 413 414 415 416 417 418
                // In IE8
                if (!env.canvasSupported) {
                    var colorArr = colorTool.parse(backgroundColor);
                    backgroundColor = colorTool.stringify(colorArr, 'rgb');
                    if (colorArr[3] === 0) {
                        backgroundColor = 'transparent';
                    }
                }
                backgroundColor = backgroundColor;
419
                this._dom.style.backgroundColor = backgroundColor;
L
lang 已提交
420
            }
L
lang 已提交
421

422
            // console.time && console.timeEnd('update');
423
        },
424

425 426 427 428 429 430 431
        // PENDING
        /**
         * @param {Object} payload
         * @private
         */
        updateView: function (payload) {
            var ecModel = this._model;
432

433 434 435 436 437
            // update before setOption
            if (!ecModel) {
                return;
            }

P
pah100 已提交
438 439 440 441
            ecModel.eachSeries(function (seriesModel) {
                seriesModel.getData().clearAllVisual();
            });

442
            doVisualEncoding.call(this, ecModel, payload);
443

444 445
            invokeUpdateMethod.call(this, 'updateView', ecModel, payload);
        },
446

447 448 449 450 451 452
        /**
         * @param {Object} payload
         * @private
         */
        updateVisual: function (payload) {
            var ecModel = this._model;
453

454 455 456 457 458
            // update before setOption
            if (!ecModel) {
                return;
            }

P
pah100 已提交
459 460 461 462
            ecModel.eachSeries(function (seriesModel) {
                seriesModel.getData().clearAllVisual();
            });

463
            doVisualEncoding.call(this, ecModel, payload);
464

465 466
            invokeUpdateMethod.call(this, 'updateVisual', ecModel, payload);
        },
467

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

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

L
lang 已提交
480
            doLayout.call(this, ecModel, payload);
481

482 483
            invokeUpdateMethod.call(this, 'updateLayout', ecModel, payload);
        },
L
lang 已提交
484

485 486 487 488 489 490 491 492 493 494 495 496 497 498
        /**
         * @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 已提交
499 500 501 502
        },

        /**
         * @param {Object} payload
P
pah100 已提交
503
         * @private
P
pah100 已提交
504
         */
P
pah100 已提交
505
        prepareAndUpdate: function (payload) {
P
pah100 已提交
506
            var ecModel = this._model;
507

P
pah100 已提交
508 509 510
            prepareView.call(this, 'component', ecModel);

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

P
pah100 已提交
512
            updateMethods.update.call(this, payload);
P
pah100 已提交
513
        }
514 515 516 517 518 519
    };

    /**
     * @param {Object} payload
     * @private
     */
520
    function toggleHighlight(method, payload) {
521
        var ecModel = this._model;
522

523 524 525 526 527
        // dispatchAction before setOption
        if (!ecModel) {
            return;
        }

528 529
        ecModel.eachComponent(
            {mainType: 'series', query: payload},
L
lang 已提交
530
            function (seriesModel, index) {
L
lang 已提交
531
                var chartView = this._chartsMap[seriesModel.__viewId];
L
lang 已提交
532
                if (chartView && chartView.__alive) {
P
pah100 已提交
533
                    chartView[method](
L
lang 已提交
534
                        seriesModel, ecModel, this._api, payload
P
pah100 已提交
535
                    );
536 537 538 539
                }
            },
            this
        );
540
    }
541

L
Resize  
lang 已提交
542 543 544
    /**
     * Resize the chart
     */
L
tweak  
lang 已提交
545
    echartsProto.resize = function () {
L
Resize  
lang 已提交
546
        this._zr.resize();
P
pah100 已提交
547 548 549

        var optionChanged = this._model && this._model.resetOption('media');
        updateMethods[optionChanged ? 'prepareAndUpdate' : 'update'].call(this);
L
lang 已提交
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565

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

    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 已提交
566
        this.hideLoading();
L
lang 已提交
567
        var el = defaultLoadingEffect(this._api, cfg);
L
lang 已提交
568
        var zr = this._zr;
L
lang 已提交
569
        this._loadingFX = el;
L
lang 已提交
570 571

        zr.add(el);
L
lang 已提交
572 573 574 575 576 577
    };

    /**
     * Hide loading effect
     */
    echartsProto.hideLoading = function () {
L
lang 已提交
578
        this._loadingFX && this._zr.remove(this._loadingFX);
L
lang 已提交
579
        this._loadingFX = null;
L
tweak  
lang 已提交
580
    };
P
pah100 已提交
581

L
lang 已提交
582
    /**
L
Resize  
lang 已提交
583 584
     * @param {Object} eventObj
     * @return {Object}
L
lang 已提交
585 586 587 588 589 590 591
     */
    echartsProto.makeActionFromEvent = function (eventObj) {
        var payload = zrUtil.extend({}, eventObj);
        payload.type = eventActionMap[eventObj.type];
        return payload;
    };

L
tweak  
lang 已提交
592 593 594 595
    /**
     * @pubilc
     * @param {Object} payload
     * @param {string} [payload.type] Action type
P
pah100 已提交
596
     * @param {boolean} [silent=false] Whether trigger event.
L
tweak  
lang 已提交
597
     */
L
lang 已提交
598
    echartsProto.dispatchAction = function (payload, silent) {
L
tweak  
lang 已提交
599 600
        var actionWrap = actions[payload.type];
        if (actionWrap) {
L
lang 已提交
601 602 603
            var actionInfo = actionWrap.actionInfo;
            var updateMethod = actionInfo.update || 'update';

L
lang 已提交
604
            var payloads = [payload];
605
            var batched = false;
L
lang 已提交
606 607
            // Batch action
            if (payload.batch) {
608
                batched = true;
L
lang 已提交
609 610 611 612 613 614 615 616 617 618 619 620 621 622
                payloads = zrUtil.map(payload.batch, function (item) {
                    item = zrUtil.defaults(zrUtil.extend({}, item), payload);
                    item.batch = null;
                    return item;
                });
            }

            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);
P
pah100 已提交
623
                // Emit event outside
L
lang 已提交
624
                eventObj = eventObj || zrUtil.extend({}, batchItem);
P
pah100 已提交
625 626
                // Convert type to eventType
                eventObj.type = actionInfo.event || eventObj.type;
L
lang 已提交
627 628
                eventObjBatch.push(eventObj);

L
lang 已提交
629
                // Highlight and downplay are special.
L
lang 已提交
630 631 632 633 634
                isHighlightOrDownplay && updateMethods[updateMethod].call(this, batchItem);
            }

            (updateMethod !== 'none' && !isHighlightOrDownplay)
                && updateMethods[updateMethod].call(this, payload);
P
pah100 已提交
635

L
lang 已提交
636
            if (!silent) {
637 638
                // Follow the rule of action batch
                if (batched) {
L
lang 已提交
639
                    eventObj = {
P
pah100 已提交
640
                        type: actionInfo.event || payload.type,
L
lang 已提交
641 642 643 644 645 646
                        batch: eventObjBatch
                    };
                }
                else {
                    eventObj = eventObjBatch[0];
                }
L
lang 已提交
647
                this._messageCenter.trigger(eventObj.type, eventObj);
P
pah100 已提交
648
            }
L
tweak  
lang 已提交
649 650
        }
    };
651

L
lang 已提交
652 653 654 655
    /**
     * Register event
     * @method
     */
656 657 658
    echartsProto.on = createRegisterEventWithLowercaseName('on');
    echartsProto.off = createRegisterEventWithLowercaseName('off');
    echartsProto.one = createRegisterEventWithLowercaseName('one');
L
lang 已提交
659

L
tweak  
lang 已提交
660 661 662 663
    /**
     * @param {string} methodName
     * @private
     */
664
    function invokeUpdateMethod(methodName, ecModel, payload) {
665
        var api = this._api;
L
lang 已提交
666

L
tweak  
lang 已提交
667
        // Update all components
L
lang 已提交
668
        each(this._componentsViews, function (component) {
L
tweak  
lang 已提交
669 670
            var componentModel = component.__model;
            component[methodName](componentModel, ecModel, api, payload);
671

L
tweak  
lang 已提交
672 673
            updateZ(componentModel, component);
        }, this);
L
lang 已提交
674

L
tweak  
lang 已提交
675 676
        // Upate all charts
        ecModel.eachSeries(function (seriesModel, idx) {
L
lang 已提交
677
            var chart = this._chartsMap[seriesModel.__viewId];
L
tweak  
lang 已提交
678
            chart[methodName](seriesModel, ecModel, api, payload);
679

L
tweak  
lang 已提交
680 681
            updateZ(seriesModel, chart);
        }, this);
682

683
    }
L
lang 已提交
684

L
lang 已提交
685
    /**
L
Tweak  
lang 已提交
686
     * Prepare view instances of charts and components
L
lang 已提交
687 688 689
     * @param  {module:echarts/model/Global} ecModel
     * @private
     */
690
    function prepareView(type, ecModel) {
L
Tweak  
lang 已提交
691
        var isComponent = type === 'component';
L
lang 已提交
692
        var viewList = isComponent ? this._componentsViews : this._chartsViews;
L
Tweak  
lang 已提交
693
        var viewMap = isComponent ? this._componentsMap : this._chartsMap;
L
tweak  
lang 已提交
694
        var zr = this._zr;
L
lang 已提交
695

L
Tweak  
lang 已提交
696
        for (var i = 0; i < viewList.length; i++) {
L
lang 已提交
697
            viewList[i].__alive = false;
L
tweak  
lang 已提交
698
        }
L
lang 已提交
699

L
Tweak  
lang 已提交
700 701 702 703
        ecModel[isComponent ? 'eachComponent' : 'eachSeries'](function (componentType, model) {
            if (isComponent) {
                if (componentType === 'series') {
                    return;
L
lang 已提交
704
                }
705
            }
L
tweak  
lang 已提交
706
            else {
L
Tweak  
lang 已提交
707
                model = componentType;
L
tweak  
lang 已提交
708 709
            }

710
            // Consider: id same and type changed.
L
lang 已提交
711 712
            var viewId = model.id + '_' + model.type;
            var view = viewMap[viewId];
L
Tweak  
lang 已提交
713 714 715
            if (!view) {
                var classType = ComponentModel.parseClassType(model.type);
                var Clazz = isComponent
L
tweak  
lang 已提交
716
                    ? ComponentView.getClass(classType.main, classType.sub)
L
Tweak  
lang 已提交
717
                    : ChartView.getClass(classType.sub);
L
tweak  
lang 已提交
718
                if (Clazz) {
L
Tweak  
lang 已提交
719 720
                    view = new Clazz();
                    view.init(ecModel, this._api);
L
lang 已提交
721
                    viewMap[viewId] = view;
L
Tweak  
lang 已提交
722 723 724 725 726
                    viewList.push(view);
                    zr.add(view.group);
                }
                else {
                    // Error
L
lang 已提交
727
                    return;
L
lang 已提交
728
                }
729
            }
L
Tweak  
lang 已提交
730

L
lang 已提交
731
            model.__viewId = viewId;
L
lang 已提交
732
            view.__alive = true;
L
lang 已提交
733
            view.__id = viewId;
L
Tweak  
lang 已提交
734
            view.__model = model;
L
tweak  
lang 已提交
735 736
        }, this);

L
Tweak  
lang 已提交
737 738
        for (var i = 0; i < viewList.length;) {
            var view = viewList[i];
L
lang 已提交
739
            if (!view.__alive) {
L
Tweak  
lang 已提交
740
                zr.remove(view.group);
L
lang 已提交
741
                view.dispose(ecModel, this._api);
L
Tweak  
lang 已提交
742 743
                viewList.splice(i, 1);
                delete viewMap[view.__id];
L
tweak  
lang 已提交
744 745 746 747 748
            }
            else {
                i++;
            }
        }
749 750
    }

L
tweak  
lang 已提交
751 752 753 754 755 756
    /**
     * Processor data in each series
     *
     * @param {module:echarts/model/Global} ecModel
     * @private
     */
757
    function processData(ecModel, api) {
758 759
        each(dataProcessorFuncs, function (process) {
            process.func(ecModel, api);
L
tweak  
lang 已提交
760
        });
761
    }
L
lang 已提交
762

L
tweak  
lang 已提交
763 764 765
    /**
     * @private
     */
766
    function stackSeriesData(ecModel) {
L
tweak  
lang 已提交
767 768 769 770 771 772 773 774
        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 已提交
775
                }
L
tweak  
lang 已提交
776 777 778
                stackedDataMap[stack] = data;
            }
        });
779
    }
L
lang 已提交
780

L
tweak  
lang 已提交
781
    /**
782
     * Layout before each chart render there series, special visual encoding stage
L
tweak  
lang 已提交
783 784 785 786
     *
     * @param {module:echarts/model/Global} ecModel
     * @private
     */
L
lang 已提交
787 788
    function doLayout(ecModel, payload) {
        var api = this._api;
789 790 791 792
        each(visualFuncs, function (visual) {
            if (visual.isLayout) {
                visual.func(ecModel, api, payload);
            }
L
tweak  
lang 已提交
793
        });
794
    }
L
lang 已提交
795

L
tweak  
lang 已提交
796
    /**
797
     * Encode visual infomation from data after data processing
L
tweak  
lang 已提交
798 799 800 801
     *
     * @param {module:echarts/model/Global} ecModel
     * @private
     */
L
lang 已提交
802 803
    function doVisualEncoding(ecModel, payload) {
        var api = this._api;
L
lang 已提交
804 805 806 807
        ecModel.clearColorPalette();
        ecModel.eachSeries(function (seriesModel) {
            seriesModel.clearColorPalette();
        });
808 809
        each(visualFuncs, function (visual) {
            visual.func(ecModel, api, payload);
L
tweak  
lang 已提交
810
        });
811
    }
L
lang 已提交
812

L
tweak  
lang 已提交
813 814 815 816
    /**
     * Render each chart and component
     * @private
     */
817
    function doRender(ecModel, payload) {
818
        var api = this._api;
L
tweak  
lang 已提交
819
        // Render all components
L
lang 已提交
820 821 822
        each(this._componentsViews, function (componentView) {
            var componentModel = componentView.__model;
            componentView.render(componentModel, ecModel, api, payload);
L
tweak  
lang 已提交
823

L
lang 已提交
824
            updateZ(componentModel, componentView);
L
tweak  
lang 已提交
825 826
        }, this);

L
lang 已提交
827
        each(this._chartsViews, function (chart) {
L
lang 已提交
828
            chart.__alive = false;
L
tweak  
lang 已提交
829 830
        }, this);

L
lang 已提交
831
        var elCountAll = 0;
L
tweak  
lang 已提交
832 833
        // Render all charts
        ecModel.eachSeries(function (seriesModel, idx) {
L
lang 已提交
834
            var chartView = this._chartsMap[seriesModel.__viewId];
L
lang 已提交
835
            chartView.__alive = true;
L
lang 已提交
836
            chartView.render(seriesModel, ecModel, api, payload);
L
tweak  
lang 已提交
837

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

L
lang 已提交
840
            updateZ(seriesModel, chartView);
841 842 843

            // Progressive configuration
            var elCount = 0;
L
lang 已提交
844
            chartView.group.traverse(function (el) {
L
Tweak  
lang 已提交
845
                if (el.type !== 'group' && !el.ignore) {
L
lang 已提交
846 847 848
                    elCount++;
                }
            });
L
lang 已提交
849 850
            elCountAll += elCount;

L
Tweak  
lang 已提交
851
            var frameDrawNum = +seriesModel.get('progressive');
L
lang 已提交
852
            var needProgressive = elCount > seriesModel.get('progressiveThreshold') && frameDrawNum && !env.node;
853 854
            if (needProgressive) {
                chartView.group.traverse(function (el) {
L
lang 已提交
855
                    // FIXME marker and other components
856 857
                    if (el.type !== 'group') {
                        el.progressive = needProgressive ?
L
Tweak  
lang 已提交
858
                            Math.floor(elCount++ / frameDrawNum) : -1;
859 860 861 862 863 864
                        if (needProgressive) {
                            el.stopAnimation(true);
                        }
                    }
                });
            }
L
tweak  
lang 已提交
865 866
        }, this);

L
lang 已提交
867 868 869 870 871 872 873 874
        // 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 已提交
875
        // Remove groups of unrendered charts
L
lang 已提交
876
        each(this._chartsViews, function (chart) {
L
lang 已提交
877
            if (!chart.__alive) {
L
tweak  
lang 已提交
878 879 880
                chart.remove(ecModel, api);
            }
        }, this);
881
    }
L
lang 已提交
882

L
lang 已提交
883
    var MOUSE_EVENT_NAMES = [
L
Typo  
lang 已提交
884
        'click', 'dblclick', 'mouseover', 'mouseout', 'mousedown', 'mouseup', 'globalout'
L
lang 已提交
885 886 887 888 889 890
    ];
    /**
     * @private
     */
    echartsProto._initEvents = function () {
        each(MOUSE_EVENT_NAMES, function (eveName) {
891
            this._zr.on(eveName, function (e) {
L
lang 已提交
892 893 894
                var ecModel = this.getModel();
                var el = e.target;
                if (el && el.dataIndex != null) {
L
lang 已提交
895
                    var dataModel = el.dataModel || ecModel.getSeriesByIndex(el.seriesIndex);
896
                    var params = dataModel && dataModel.getDataParams(el.dataIndex, el.dataType) || {};
L
lang 已提交
897 898 899 900
                    params.event = e;
                    params.type = eveName;
                    this.trigger(eveName, params);
                }
L
lang 已提交
901 902 903 904
                // If element has custom eventData of components
                else if (el && el.eventData) {
                    this.trigger(eveName, el.eventData);
                }
L
lang 已提交
905 906
            }, this);
        }, this);
L
lang 已提交
907

L
lang 已提交
908
        each(eventActionMap, function (actionType, eventType) {
L
lang 已提交
909 910 911 912
            this._messageCenter.on(eventType, function (event) {
                this.trigger(eventType, event);
            }, this);
        }, this);
L
lang 已提交
913 914
    };

L
lang 已提交
915
    /**
L
lang 已提交
916
     * @return {boolean}
L
lang 已提交
917 918 919 920
     */
    echartsProto.isDisposed = function () {
        return this._disposed;
    };
L
lang 已提交
921 922 923 924 925 926 927

    /**
     * Clear
     */
    echartsProto.clear = function () {
        this.setOption({}, true);
    };
L
lang 已提交
928 929 930
    /**
     * Dispose instance
     */
L
tweak  
lang 已提交
931
    echartsProto.dispose = function () {
L
lang 已提交
932
        this._disposed = true;
L
lang 已提交
933
        var api = this._api;
L
lang 已提交
934
        var ecModel = this._model;
L
lang 已提交
935

L
lang 已提交
936
        each(this._componentsViews, function (component) {
L
lang 已提交
937
            component.dispose(ecModel, api);
L
tweak  
lang 已提交
938
        });
L
lang 已提交
939
        each(this._chartsViews, function (chart) {
L
lang 已提交
940
            chart.dispose(ecModel, api);
L
tweak  
lang 已提交
941
        });
L
lang 已提交
942

L
Tweak  
lang 已提交
943
        this._zr.dispose();
L
lang 已提交
944

L
lang 已提交
945
        delete instances[this.id];
L
lang 已提交
946 947
    };

L
lang 已提交
948 949
    zrUtil.mixin(ECharts, Eventful);

L
lang 已提交
950 951 952 953 954 955 956 957 958 959
    /**
     * @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) {
960 961 962 963
            if (el.type !== 'group') {
                z != null && (el.z = z);
                zlevel != null && (el.zlevel = zlevel);
            }
L
lang 已提交
964 965
        });
    }
L
lang 已提交
966 967 968 969
    /**
     * @type {Array.<Function>}
     * @inner
     */
P
pah100 已提交
970 971
    var actions = [];

L
lang 已提交
972
    /**
L
lang 已提交
973
     * Map eventType to actionType
L
lang 已提交
974 975 976 977
     * @type {Object}
     */
    var eventActionMap = {};

L
lang 已提交
978 979 980 981 982
    /**
     * Data processor functions of each stage
     * @type {Array.<Object.<string, Function>>}
     * @inner
     */
983
    var dataProcessorFuncs = [];
L
lang 已提交
984

985 986 987 988 989 990
    /**
     * @type {Array.<Function>}
     * @inner
     */
    var optionPreprocessorFuncs = [];

L
lang 已提交
991
    /**
992
     * Visual encoding functions of each stage
L
lang 已提交
993 994 995
     * @type {Array.<Object.<string, Function>>}
     * @inner
     */
996
    var visualFuncs = [];
L
lang 已提交
997 998 999 1000 1001 1002
    /**
     * Theme storage
     * @type {Object.<key, Object>}
     */
    var themeStorage = {};

L
lang 已提交
1003

L
lang 已提交
1004 1005 1006 1007 1008 1009
    var instances = {};
    var connectedGroups = {};

    var idBase = new Date() - 0;
    var groupIdBase = new Date() - 0;
    var DOM_ATTRIBUTE_KEY = '_echarts_instance_';
L
lang 已提交
1010
    /**
L
lang 已提交
1011
     * @alias module:echarts
L
lang 已提交
1012
     */
L
lang 已提交
1013 1014 1015 1016
    var echarts = {
        /**
         * @type {number}
         */
L
lang 已提交
1017
        version: '3.1.10',
L
lang 已提交
1018
        dependencies: {
L
lang 已提交
1019
            zrender: '3.1.0'
L
lang 已提交
1020 1021
        }
    };
L
lang 已提交
1022

L
lang 已提交
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
    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 已提交
1058 1059 1060 1061 1062 1063
    /**
     * @param {HTMLDomElement} dom
     * @param {Object} [theme]
     * @param {Object} opts
     */
    echarts.init = function (dom, theme, opts) {
L
lang 已提交
1064 1065
        // Check version
        if ((zrender.version.replace('.', '') - 0) < (echarts.dependencies.zrender.replace('.', '') - 0)) {
1066
            throw new Error(
L
lang 已提交
1067 1068 1069 1070 1071 1072
                'ZRender ' + zrender.version
                + ' is too old for ECharts ' + echarts.version
                + '. Current version need ZRender '
                + echarts.dependencies.zrender + '+'
            );
        }
1073 1074 1075
        if (!dom) {
            throw new Error('Initialize failed: invalid dom.');
        }
L
lang 已提交
1076 1077

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

L
lang 已提交
1081 1082 1083
        dom.setAttribute &&
            dom.setAttribute(DOM_ATTRIBUTE_KEY, chart.id);

L
lang 已提交
1084
        enableConnect(chart);
L
lang 已提交
1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102

        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 已提交
1103
            groupId = groupId || ('g_' + groupIdBase++);
L
lang 已提交
1104 1105 1106 1107 1108 1109 1110 1111 1112 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 1148
            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 已提交
1149
    };
L
lang 已提交
1150

L
lang 已提交
1151 1152 1153 1154 1155 1156 1157
    /**
     * Register theme
     */
    echarts.registerTheme = function (name, theme) {
        themeStorage[name] = theme;
    };

L
tweak  
lang 已提交
1158 1159 1160 1161 1162 1163 1164
    /**
     * Register option preprocessor
     * @param {Function} preprocessorFunc
     */
    echarts.registerPreprocessor = function (preprocessorFunc) {
        optionPreprocessorFuncs.push(preprocessorFunc);
    };
1165

L
tweak  
lang 已提交
1166
    /**
1167
     * @param {number} [priority=1000]
L
tweak  
lang 已提交
1168 1169
     * @param {Function} processorFunc
     */
1170 1171 1172 1173
    echarts.registerProcessor = function (priority, processorFunc) {
        if (typeof priority === 'function') {
            processorFunc = priority;
            priority = PRIORITY_PROCESSOR_FILTER;
L
tweak  
lang 已提交
1174
        }
1175 1176 1177 1178 1179 1180 1181
        if (isNaN(priority)) {
            throw new Error('Unkown processor priority');
        }
        dataProcessorFuncs.push({
            prio: priority,
            func: processorFunc
        });
L
tweak  
lang 已提交
1182
    };
L
lang 已提交
1183

L
tweak  
lang 已提交
1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194
    /**
     * 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 已提交
1195 1196 1197 1198
     * @param {string} [actionInfo.event]
     * @param {string} [actionInfo.update]
     * @param {string} [eventName]
     * @param {Function} action
L
tweak  
lang 已提交
1199
     */
L
lang 已提交
1200 1201 1202 1203 1204
    echarts.registerAction = function (actionInfo, eventName, action) {
        if (typeof eventName === 'function') {
            action = eventName;
            eventName = '';
        }
L
tweak  
lang 已提交
1205 1206
        var actionType = zrUtil.isObject(actionInfo)
            ? actionInfo.type
L
lang 已提交
1207 1208 1209
            : ([actionInfo, actionInfo = {
                event: eventName
            }][0]);
L
lang 已提交
1210

L
lang 已提交
1211 1212
        // Event name is all lowercase
        actionInfo.event = (actionInfo.event || actionType).toLowerCase();
L
lang 已提交
1213
        eventName = actionInfo.event;
1214

L
tweak  
lang 已提交
1215 1216 1217
        if (!actions[actionType]) {
            actions[actionType] = {action: action, actionInfo: actionInfo};
        }
L
lang 已提交
1218
        eventActionMap[eventName] = actionType;
L
tweak  
lang 已提交
1219
    };
P
pah100 已提交
1220

L
tweak  
lang 已提交
1221 1222 1223 1224 1225 1226 1227
    /**
     * @param {string} type
     * @param {*} CoordinateSystem
     */
    echarts.registerCoordinateSystem = function (type, CoordinateSystem) {
        CoordinateSystemManager.register(type, CoordinateSystem);
    };
L
lang 已提交
1228

L
tweak  
lang 已提交
1229
    /**
1230 1231 1232 1233 1234 1235
     * 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 已提交
1236
     */
1237 1238 1239 1240 1241 1242 1243
    echarts.registerLayout = function (priority, layoutFunc) {
        if (typeof priority === 'function') {
            layoutFunc = priority;
            priority = PRIORITY_VISUAL_LAYOUT;
        }
        if (isNaN(priority)) {
            throw new Error('Unkown layout priority');
L
tweak  
lang 已提交
1244
        }
1245 1246 1247 1248 1249
        visualFuncs.push({
            prio: priority,
            func: layoutFunc,
            isLayout: true
        });
L
tweak  
lang 已提交
1250
    };
L
lang 已提交
1251

L
tweak  
lang 已提交
1252
    /**
1253 1254
     * @param {string} [priority=3000]
     * @param {Function} visualFunc
L
tweak  
lang 已提交
1255
     */
1256 1257 1258 1259
    echarts.registerVisual = function (priority, visualFunc) {
        if (typeof priority === 'function') {
            visualFunc = priority;
            priority = PRIORITY_VISUAL_CHART;
L
tweak  
lang 已提交
1260
        }
1261 1262 1263 1264 1265 1266 1267
        if (isNaN(priority)) {
            throw new Error('Unkown visual priority');
        }
        visualFuncs.push({
            prio: priority,
            func: visualFunc
        });
L
tweak  
lang 已提交
1268
    };
L
Update  
lang 已提交
1269

L
tweak  
lang 已提交
1270 1271 1272 1273 1274 1275
    /**
     * @param {Object} opts
     */
    echarts.extendChartView = function (opts) {
        return ChartView.extend(opts);
    };
L
Update  
lang 已提交
1276

L
tweak  
lang 已提交
1277 1278 1279 1280 1281 1282
    /**
     * @param {Object} opts
     */
    echarts.extendComponentModel = function (opts) {
        return ComponentModel.extend(opts);
    };
L
Update  
lang 已提交
1283

L
tweak  
lang 已提交
1284 1285 1286 1287 1288 1289
    /**
     * @param {Object} opts
     */
    echarts.extendSeriesModel = function (opts) {
        return SeriesModel.extend(opts);
    };
P
pah100 已提交
1290

L
tweak  
lang 已提交
1291 1292 1293 1294 1295
    /**
     * @param {Object} opts
     */
    echarts.extendComponentView = function (opts) {
        return ComponentView.extend(opts);
L
lang 已提交
1296 1297
    };

1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317
    /**
     * 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;
    };

1318
    echarts.registerVisual(PRIORITY_VISUAL_GLOBAL, zrUtil.curry(
L
lang 已提交
1319 1320
        require('./visual/seriesColor'), '', 'itemStyle'
    ));
1321 1322
    echarts.registerPreprocessor(require('./preprocessor/backwardCompat'));

1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334
    // Default action
    echarts.registerAction({
        type: 'highlight',
        event: 'highlight',
        update: 'highlight'
    }, zrUtil.noop);
    echarts.registerAction({
        type: 'downplay',
        event: 'downplay',
        update: 'downplay'
    }, zrUtil.noop);

P
pah100 已提交
1335 1336 1337 1338

    // --------
    // Exports
    // --------
L
lang 已提交
1339 1340 1341
    //
    echarts.List = require('./data/List');
    echarts.Model = require('./model/Model');
P
pah100 已提交
1342

L
lang 已提交
1343 1344 1345
    echarts.graphic = require('./util/graphic');
    echarts.number = require('./util/number');
    echarts.format = require('./util/format');
L
lang 已提交
1346 1347
    echarts.matrix = require('zrender/core/matrix');
    echarts.vector = require('zrender/core/vector');
L
lang 已提交
1348
    echarts.color = require('zrender/tool/color');
P
pah100 已提交
1349 1350 1351 1352 1353

    echarts.util = {};
    each([
            'map', 'each', 'filter', 'indexOf', 'inherits',
            'reduce', 'filter', 'bind', 'curry', 'isArray',
L
lang 已提交
1354
            'isString', 'isObject', 'isFunction', 'extend', 'defaults'
P
pah100 已提交
1355 1356 1357 1358 1359 1360
        ],
        function (name) {
            echarts.util[name] = zrUtil[name];
        }
    );

1361 1362 1363 1364 1365 1366 1367 1368 1369 1370
    // 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 已提交
1371
            COMPONENT: PRIORITY_VISUAL_COMPONENT,
P
pah100 已提交
1372
            BRUSH: PRIORITY_VISUAL_BRUSH
1373 1374 1375
        }
    };

L
lang 已提交
1376 1377
    return echarts;
});