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

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

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

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

    var ComponentView = require('./view/Component');
    var ChartView = require('./view/Chart');

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

32 33
    var each = zrUtil.each;

34 35
    var VISUAL_CODING_STAGES = ['echarts', 'chart', 'component'];

L
lang 已提交
36
    // TODO Transform first or filter first
L
lang 已提交
37 38
    var PROCESSOR_STAGES = ['transform', 'filter', 'statistic'];

L
lang 已提交
39 40 41 42 43 44 45
    /**
     * @module echarts~MessageCenter
     */
    function MessageCenter() {
        Eventful.call(this);
    }
    zrUtil.mixin(MessageCenter, Eventful);
L
lang 已提交
46 47 48
    /**
     * @module echarts~ECharts
     */
L
lang 已提交
49
    function ECharts (dom, theme, opts) {
L
lang 已提交
50
        opts = opts || {};
L
lang 已提交
51

L
lang 已提交
52 53 54 55 56
        if (theme) {
            each(optionPreprocessorFuncs, function (preProcess) {
                preProcess(theme);
            });
        }
L
lang 已提交
57 58 59 60 61 62 63 64 65
        /**
         * @type {string}
         */
        this.id;
        /**
         * Group id
         * @type {string}
         */
        this.group;
L
lang 已提交
66 67 68 69 70
        /**
         * @type {HTMLDomElement}
         * @private
         */
        this._dom = dom;
L
lang 已提交
71 72 73 74
        /**
         * @type {module:zrender/ZRender}
         * @private
         */
L
lang 已提交
75
        this._zr = zrender.init(dom, {
76 77
            renderer: opts.renderer || 'canvas',
            devicePixelRatio: opts.devicePixelRatio
L
lang 已提交
78
        });
L
lang 已提交
79

L
lang 已提交
80 81 82 83
        /**
         * @type {Object}
         * @private
         */
L
lang 已提交
84
        this._theme = zrUtil.clone(theme, true);
L
lang 已提交
85

L
lang 已提交
86 87 88 89
        /**
         * @type {Array.<module:echarts/view/Chart>}
         * @private
         */
L
lang 已提交
90
        this._chartsList = [];
L
lang 已提交
91 92 93 94 95

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

L
lang 已提交
98 99 100 101
        /**
         * @type {Array.<module:echarts/view/Component>}
         * @private
         */
L
lang 已提交
102
        this._componentsList = [];
L
lang 已提交
103 104 105 106 107

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

L
lang 已提交
110
        /**
L
lang 已提交
111
         * @type {module:echarts/ExtensionAPI}
L
lang 已提交
112 113
         * @private
         */
114
        this._api = new ExtensionAPI(this);
L
lang 已提交
115

L
lang 已提交
116 117 118 119
        /**
         * @type {module:echarts/CoordinateSystem}
         * @private
         */
L
lang 已提交
120
        this._coordinateSystem = new CoordinateSystemManager();
L
lang 已提交
121

L
lang 已提交
122 123
        Eventful.call(this);

L
lang 已提交
124 125 126 127 128 129
        /**
         * @type {module:echarts~MessageCenter}
         * @private
         */
        this._messageCenter = new MessageCenter();

L
lang 已提交
130 131
        // Init mouse events
        this._initEvents();
L
Resize  
lang 已提交
132 133 134

        // In case some people write `window.onresize = chart.resize`
        this.resize = zrUtil.bind(this.resize, this);
L
lang 已提交
135
    }
L
lang 已提交
136

L
tweak  
lang 已提交
137
    var echartsProto = ECharts.prototype;
L
lang 已提交
138

139 140 141
    /**
     * @return {HTMLDomElement}
     */
L
tweak  
lang 已提交
142 143 144
    echartsProto.getDom = function () {
        return this._dom;
    };
L
lang 已提交
145

146 147 148
    /**
     * @return {module:zrender~ZRender}
     */
L
tweak  
lang 已提交
149 150 151
    echartsProto.getZr = function () {
        return this._zr;
    };
L
lang 已提交
152

153 154 155 156 157 158
    /**
     * @param {Object} option
     * @param {boolean} notMerge
     * @param {boolean} [notRefreshImmediately=false]
     */
    echartsProto.setOption = function (option, notMerge, notRefreshImmediately) {
L
tweak  
lang 已提交
159 160
        // PENDING
        option = zrUtil.clone(option, true);
161

L
tweak  
lang 已提交
162 163 164
        each(optionPreprocessorFuncs, function (preProcess) {
            preProcess(option);
        });
L
lang 已提交
165

L
tweak  
lang 已提交
166 167 168 169 170 171 172 173 174
        var ecModel = this._model;
        if (!ecModel || notMerge) {
            ecModel = new GlobalModel(option, null, this._theme);
            this._model = ecModel;
        }
        else {
            ecModel.restoreData();
            ecModel.mergeOption(option);
        }
L
lang 已提交
175

176
        prepareView.call(this, 'component', ecModel);
L
lang 已提交
177

178
        prepareView.call(this, 'chart', ecModel);
L
lang 已提交
179

180
        updateMethods.update.call(this);
L
lang 已提交
181

182
        !notRefreshImmediately && this._zr.refreshImmediately();
L
tweak  
lang 已提交
183
    };
L
lang 已提交
184

L
Tweak  
lang 已提交
185 186 187 188 189 190
    /**
     * @DEPRECATED
     */
    echartsProto.setTheme = function () {
        console.log('ECharts#setTheme() is DEPRECATED in ECharts 3.0');
    };
L
tweak  
lang 已提交
191 192 193 194 195 196
    /**
     * @return {module:echarts/model/Global}
     */
    echartsProto.getModel = function () {
        return this._model;
    };
L
lang 已提交
197

L
tweak  
lang 已提交
198 199 200 201 202 203
    /**
     * @return {number}
     */
    echartsProto.getWidth = function () {
        return this._zr.getWidth();
    };
L
lang 已提交
204

L
tweak  
lang 已提交
205 206 207 208 209 210
    /**
     * @return {number}
     */
    echartsProto.getHeight = function () {
        return this._zr.getHeight();
    };
L
lang 已提交
211

212

213 214 215 216 217 218 219
    var updateMethods = {

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

222
            var ecModel = this._model;
223 224 225 226
            // update before setOption
            if (!ecModel) {
                return;
            }
L
lang 已提交
227

228 229 230 231 232
            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 已提交
233

234
            processData.call(this, ecModel);
L
lang 已提交
235

236
            stackSeriesData.call(this, ecModel);
L
lang 已提交
237

238
            this._coordinateSystem.update(ecModel, this._api);
L
lang 已提交
239

240
            doLayout.call(this, ecModel, payload);
241

242
            doVisualCoding.call(this, ecModel, payload);
243

244
            doRender.call(this, ecModel, payload);
245

246 247 248 249 250 251 252 253 254
            // Set background
            var backgroundColor = ecModel.get('backgroundColor');
            // In IE8
            if (!env.canvasSupported) {
                var colorArr = colorTool.parse(backgroundColor);
                backgroundColor = colorTool.stringify(colorArr, 'rgb');
                if (colorArr[3] === 0) {
                    backgroundColor = 'transparent';
                }
L
lang 已提交
255
            }
L
lang 已提交
256 257 258 259 260 261 262 263
            if (env.node) {
                this._zr.configLayer(0, {
                    clearColor: backgroundColor
                });
            }
            else {
                backgroundColor && (this._dom.style.backgroundColor = backgroundColor);
            }
L
lang 已提交
264

265
            // console.time && console.timeEnd('update');
266
        },
267

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

276 277 278 279 280
            // update before setOption
            if (!ecModel) {
                return;
            }

281
            doLayout.call(this, ecModel, payload);
282

283
            doVisualCoding.call(this, ecModel, payload);
284

285 286
            invokeUpdateMethod.call(this, 'updateView', ecModel, payload);
        },
287

288 289 290 291 292 293
        /**
         * @param {Object} payload
         * @private
         */
        updateVisual: function (payload) {
            var ecModel = this._model;
294

295 296 297 298 299
            // update before setOption
            if (!ecModel) {
                return;
            }

300
            doVisualCoding.call(this, ecModel, payload);
301

302 303
            invokeUpdateMethod.call(this, 'updateVisual', ecModel, payload);
        },
304

305 306 307 308 309 310
        /**
         * @param {Object} payload
         * @private
         */
        updateLayout: function (payload) {
            var ecModel = this._model;
311

312 313 314 315 316
            // update before setOption
            if (!ecModel) {
                return;
            }

317
            doLayout.call(this, ecModel, payload);
318

319 320
            invokeUpdateMethod.call(this, 'updateLayout', ecModel, payload);
        },
L
lang 已提交
321

322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
        /**
         * @param {Object} payload
         * @private
         */
        highlight: function (payload) {
            toggleHighlight.call(this, 'highlight', payload);
        },

        /**
         * @param {Object} payload
         * @private
         */
        downplay: function (payload) {
            toggleHighlight.call(this, 'downplay', payload);
        }
337 338 339 340 341 342 343

    };

    /**
     * @param {Object} payload
     * @private
     */
344
    function toggleHighlight(method, payload) {
345
        var ecModel = this._model;
346

347 348 349 350 351
        // dispatchAction before setOption
        if (!ecModel) {
            return;
        }

352 353
        ecModel.eachComponent(
            {mainType: 'series', query: payload},
P
pah100 已提交
354
            function (seriesModel, index, payloadInfo) {
P
tweak  
pah100 已提交
355
                var chartView = this._chartsMap[seriesModel.id];
356
                if (chartView) {
P
pah100 已提交
357 358 359
                    chartView[method](
                        seriesModel, ecModel, this._api, payloadInfo
                    );
360 361 362 363
                }
            },
            this
        );
364
    }
365

L
Resize  
lang 已提交
366 367 368
    /**
     * Resize the chart
     */
L
tweak  
lang 已提交
369
    echartsProto.resize = function () {
L
Resize  
lang 已提交
370
        this._zr.resize();
L
lang 已提交
371
        updateMethods.update.call(this);
L
lang 已提交
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388

        // 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';
        }
        var el = defaultLoadingEffect(this._api, cfg);
L
lang 已提交
389
        var zr = this._zr;
L
lang 已提交
390
        this._loadingFX = el;
L
lang 已提交
391 392 393

        zr.painter.clear();
        zr.add(el);
L
lang 已提交
394 395 396 397 398 399 400 401
    };

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

L
lang 已提交
404
    /**
L
Resize  
lang 已提交
405 406
     * @param {Object} eventObj
     * @return {Object}
L
lang 已提交
407 408 409 410 411 412 413
     */
    echartsProto.makeActionFromEvent = function (eventObj) {
        var payload = zrUtil.extend({}, eventObj);
        payload.type = eventActionMap[eventObj.type];
        return payload;
    };

L
tweak  
lang 已提交
414 415 416 417
    /**
     * @pubilc
     * @param {Object} payload
     * @param {string} [payload.type] Action type
P
pah100 已提交
418
     * @param {boolean} [silent=false] Whether trigger event.
L
tweak  
lang 已提交
419 420
     * @param {number} [payload.from] From uid
     */
L
lang 已提交
421
    echartsProto.dispatchAction = function (payload, silent) {
L
tweak  
lang 已提交
422 423
        var actionWrap = actions[payload.type];
        if (actionWrap) {
L
lang 已提交
424 425
            var actionInfo = actionWrap.actionInfo;
            var updateMethod = actionInfo.update || 'update';
L
tweak  
lang 已提交
426
            actionWrap.action(payload, this._model);
427
            updateMethod !== 'none' && updateMethods[updateMethod].call(this, payload);
L
lang 已提交
428

P
pah100 已提交
429 430 431 432 433
            if (!silent) {
                // Emit event outside
                // Convert type to eventType
                var eventObj = zrUtil.extend({}, payload);
                eventObj.type = actionInfo.event || eventObj.type;
L
lang 已提交
434
                this._messageCenter.trigger(eventObj.type, eventObj);
P
pah100 已提交
435
            }
L
tweak  
lang 已提交
436 437
        }
    };
438

L
tweak  
lang 已提交
439 440 441 442
    /**
     * @param {string} methodName
     * @private
     */
443
    function invokeUpdateMethod(methodName, ecModel, payload) {
444
        var api = this._api;
L
lang 已提交
445

L
tweak  
lang 已提交
446 447 448 449
        // Update all components
        each(this._componentsList, function (component) {
            var componentModel = component.__model;
            component[methodName](componentModel, ecModel, api, payload);
450

L
tweak  
lang 已提交
451 452
            updateZ(componentModel, component);
        }, this);
L
lang 已提交
453

L
tweak  
lang 已提交
454 455
        // Upate all charts
        ecModel.eachSeries(function (seriesModel, idx) {
P
tweak  
pah100 已提交
456
            var chart = this._chartsMap[seriesModel.id];
L
tweak  
lang 已提交
457
            chart[methodName](seriesModel, ecModel, api, payload);
458

L
tweak  
lang 已提交
459 460
            updateZ(seriesModel, chart);
        }, this);
461

462
    }
L
lang 已提交
463

L
lang 已提交
464
    /**
L
Tweak  
lang 已提交
465
     * Prepare view instances of charts and components
L
lang 已提交
466 467 468
     * @param  {module:echarts/model/Global} ecModel
     * @private
     */
469
    function prepareView(type, ecModel) {
L
Tweak  
lang 已提交
470 471 472
        var isComponent = type === 'component';
        var viewList = isComponent ? this._componentsList : this._chartsList;
        var viewMap = isComponent ? this._componentsMap : this._chartsMap;
L
tweak  
lang 已提交
473
        var zr = this._zr;
L
lang 已提交
474

L
Tweak  
lang 已提交
475 476
        for (var i = 0; i < viewList.length; i++) {
            viewList[i].__keepAlive = false;
L
tweak  
lang 已提交
477
        }
L
lang 已提交
478

L
Tweak  
lang 已提交
479 480 481 482
        ecModel[isComponent ? 'eachComponent' : 'eachSeries'](function (componentType, model) {
            if (isComponent) {
                if (componentType === 'series') {
                    return;
L
lang 已提交
483
                }
484
            }
L
tweak  
lang 已提交
485
            else {
L
Tweak  
lang 已提交
486
                model = componentType;
L
tweak  
lang 已提交
487 488
            }

P
tweak  
pah100 已提交
489
            var view = viewMap[model.id];
L
Tweak  
lang 已提交
490 491 492
            if (!view) {
                var classType = ComponentModel.parseClassType(model.type);
                var Clazz = isComponent
L
tweak  
lang 已提交
493
                    ? ComponentView.getClass(classType.main, classType.sub)
L
Tweak  
lang 已提交
494
                    : ChartView.getClass(classType.sub);
L
tweak  
lang 已提交
495
                if (Clazz) {
L
Tweak  
lang 已提交
496 497
                    view = new Clazz();
                    view.init(ecModel, this._api);
P
tweak  
pah100 已提交
498
                    viewMap[model.id] = view;
L
Tweak  
lang 已提交
499 500 501 502 503
                    viewList.push(view);
                    zr.add(view.group);
                }
                else {
                    // Error
L
lang 已提交
504
                    return;
L
lang 已提交
505
                }
506
            }
L
Tweak  
lang 已提交
507 508

            view.__keepAlive = true;
P
tweak  
pah100 已提交
509
            view.__id = model.id;
L
Tweak  
lang 已提交
510
            view.__model = model;
L
tweak  
lang 已提交
511 512
        }, this);

L
Tweak  
lang 已提交
513 514 515 516 517 518 519
        for (var i = 0; i < viewList.length;) {
            var view = viewList[i];
            if (!view.__keepAlive) {
                zr.remove(view.group);
                view.dispose(this._api);
                viewList.splice(i, 1);
                delete viewMap[view.__id];
L
tweak  
lang 已提交
520 521 522 523 524
            }
            else {
                i++;
            }
        }
525 526
    }

L
tweak  
lang 已提交
527 528 529 530 531 532
    /**
     * Processor data in each series
     *
     * @param {module:echarts/model/Global} ecModel
     * @private
     */
533
    function processData(ecModel) {
L
tweak  
lang 已提交
534 535 536
        each(PROCESSOR_STAGES, function (stage) {
            each(dataProcessorFuncs[stage] || [], function (process) {
                process(ecModel);
L
lang 已提交
537
            });
L
tweak  
lang 已提交
538
        });
539
    }
L
lang 已提交
540

L
tweak  
lang 已提交
541 542 543
    /**
     * @private
     */
544
    function stackSeriesData(ecModel) {
L
tweak  
lang 已提交
545 546 547 548 549 550 551 552
        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 已提交
553
                }
L
tweak  
lang 已提交
554 555 556
                stackedDataMap[stack] = data;
            }
        });
557
    }
L
lang 已提交
558

L
tweak  
lang 已提交
559
    /**
L
lang 已提交
560
     * Layout before each chart render there series, after visual coding and data processing
L
tweak  
lang 已提交
561 562 563 564
     *
     * @param {module:echarts/model/Global} ecModel
     * @private
     */
565
    function doLayout(ecModel, payload) {
566
        var api = this._api;
L
tweak  
lang 已提交
567 568 569
        each(layoutFuncs, function (layout) {
            layout(ecModel, api, payload);
        });
570
    }
L
lang 已提交
571

L
tweak  
lang 已提交
572 573 574 575 576 577
    /**
     * Code visual infomation from data after data processing
     *
     * @param {module:echarts/model/Global} ecModel
     * @private
     */
578
    function doVisualCoding(ecModel, payload) {
L
tweak  
lang 已提交
579 580 581
        each(VISUAL_CODING_STAGES, function (stage) {
            each(visualCodingFuncs[stage] || [], function (visualCoding) {
                visualCoding(ecModel, payload);
L
lang 已提交
582
            });
L
tweak  
lang 已提交
583
        });
584
    }
L
lang 已提交
585

L
tweak  
lang 已提交
586 587 588 589
    /**
     * Render each chart and component
     * @private
     */
590
    function doRender(ecModel, payload) {
591
        var api = this._api;
L
tweak  
lang 已提交
592 593 594 595 596 597 598 599 600 601 602 603 604 605
        // Render all components
        each(this._componentsList, function (component) {
            var componentModel = component.__model;
            component.render(componentModel, ecModel, api, payload);

            updateZ(componentModel, component);
        }, this);

        each(this._chartsList, function (chart) {
            chart.__keepAlive = false;
        }, this);

        // Render all charts
        ecModel.eachSeries(function (seriesModel, idx) {
P
tweak  
pah100 已提交
606
            var chart = this._chartsMap[seriesModel.id];
L
tweak  
lang 已提交
607 608 609 610 611 612
            chart.__keepAlive = true;
            chart.render(seriesModel, ecModel, api, payload);

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

L
lang 已提交
613
        // Remove groups of unrendered charts
L
tweak  
lang 已提交
614 615 616 617 618
        each(this._chartsList, function (chart) {
            if (!chart.__keepAlive) {
                chart.remove(ecModel, api);
            }
        }, this);
619
    }
L
lang 已提交
620

L
lang 已提交
621 622 623 624 625 626 627 628 629 630 631 632 633
    var MOUSE_EVENT_NAMES = [
        'click', 'dblclick', 'mouseover', 'mouseout', 'globalout'
    ];
    /**
     * @private
     */
    echartsProto._initEvents = function () {
        var zr = this._zr;
        each(MOUSE_EVENT_NAMES, function (eveName) {
            zr.on(eveName, function (e) {
                var ecModel = this.getModel();
                var el = e.target;
                if (el && el.dataIndex != null) {
634
                    var hostModel = el.hostModel || ecModel.getSeriesByIndex(el.seriesIndex);
L
lang 已提交
635 636 637 638 639 640 641
                    var params = hostModel && hostModel.getDataParams(el.dataIndex) || {};
                    params.event = e;
                    params.type = eveName;
                    this.trigger(eveName, params);
                }
            }, this);
        }, this);
L
lang 已提交
642 643 644 645 646 647

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

L
lang 已提交
650 651 652 653 654 655 656 657 658
    /**
     * @return {boolean]
     */
    echartsProto.isDisposed = function () {
        return this._disposed;
    };
    /**
     * Dispose instance
     */
L
tweak  
lang 已提交
659
    echartsProto.dispose = function () {
L
lang 已提交
660 661
        this._disposed = true;

L
tweak  
lang 已提交
662 663 664 665 666 667
        each(this._components, function (component) {
            component.dispose();
        });
        each(this._charts, function (chart) {
            chart.dispose();
        });
L
lang 已提交
668

L
Tweak  
lang 已提交
669
        this._zr.dispose();
L
lang 已提交
670 671

        instances[this.id] = null;
L
lang 已提交
672 673
    };

L
lang 已提交
674 675
    zrUtil.mixin(ECharts, Eventful);

L
lang 已提交
676 677 678 679 680 681 682 683 684 685 686 687 688 689
    /**
     * @param {module:echarts/model/Series|module:echarts/model/Component} model
     * @param {module:echarts/view/Component|module:echarts/view/Chart} view
     * @return {string}
     */
    function updateZ(model, view) {
        var z = model.get('z');
        var zlevel = model.get('zlevel');
        // Set z and zlevel
        view.group.traverse(function (el) {
            z != null && (el.z = z);
            zlevel != null && (el.zlevel = zlevel);
        });
    }
L
lang 已提交
690 691 692 693
    /**
     * @type {Array.<Function>}
     * @inner
     */
P
pah100 已提交
694 695
    var actions = [];

L
lang 已提交
696
    /**
L
lang 已提交
697
     * Map eventType to actionType
L
lang 已提交
698 699 700 701
     * @type {Object}
     */
    var eventActionMap = {};

L
lang 已提交
702 703 704 705
    /**
     * @type {Array.<Function>}
     * @inner
     */
706 707
    var layoutFuncs = [];

L
lang 已提交
708 709 710 711 712 713 714
    /**
     * Data processor functions of each stage
     * @type {Array.<Object.<string, Function>>}
     * @inner
     */
    var dataProcessorFuncs = {};

715 716 717 718 719 720
    /**
     * @type {Array.<Function>}
     * @inner
     */
    var optionPreprocessorFuncs = [];

L
lang 已提交
721 722 723 724 725
    /**
     * Visual coding functions of each stage
     * @type {Array.<Object.<string, Function>>}
     * @inner
     */
726
    var visualCodingFuncs = {};
L
lang 已提交
727

L
lang 已提交
728 729 730 731 732 733
    var instances = {};
    var connectedGroups = {};

    var idBase = new Date() - 0;
    var groupIdBase = new Date() - 0;
    var DOM_ATTRIBUTE_KEY = '_echarts_instance_';
L
lang 已提交
734
    /**
L
lang 已提交
735
     * @alias module:echarts
L
lang 已提交
736
     */
L
lang 已提交
737 738 739 740 741 742 743 744 745
    var echarts = {
        /**
         * @type {number}
         */
        version: '3.0.0',
        dependencies: {
            zrender: '3.0.0'
        }
    };
L
lang 已提交
746

L
tweak  
lang 已提交
747 748 749 750 751 752
    /**
     * @param {HTMLDomElement} dom
     * @param {Object} [theme]
     * @param {Object} opts
     */
    echarts.init = function (dom, theme, opts) {
L
lang 已提交
753 754 755 756 757 758 759 760 761 762 763 764 765 766
        // Check version
        if ((zrender.version.replace('.', '') - 0) < (echarts.dependencies.zrender.replace('.', '') - 0)) {
            console.error(
                'ZRender ' + zrender.version
                + ' is too old for ECharts ' + echarts.version
                + '. Current version need ZRender '
                + echarts.dependencies.zrender + '+'
            );
        }

        var chart = new ECharts(dom, theme, opts);
        chart.id = idBase++;
        instances[chart.id] = chart;

L
lang 已提交
767 768 769
        dom.setAttribute &&
            dom.setAttribute(DOM_ATTRIBUTE_KEY, chart.id);

L
lang 已提交
770 771
        // Connecting
        zrUtil.each(eventActionMap, function (actionType, eventType) {
L
lang 已提交
772 773
            // FIXME
            chart._messageCenter.on(eventType, function (event) {
L
lang 已提交
774 775 776
                if (connectedGroups[chart.group]) {
                    chart.__connectedActionDispatching = true;
                    for (var id in instances) {
L
tweak  
lang 已提交
777
                        var action = chart.makeActionFromEvent(event);
L
lang 已提交
778 779
                        var otherChart = instances[id];
                        if (otherChart !== chart && otherChart.group === chart.group) {
L
tweak  
lang 已提交
780
                            if (!otherChart.__connectedActionDispatching) {
L
lang 已提交
781
                                otherChart.dispatchAction(action);
L
tweak  
lang 已提交
782
                            }
L
lang 已提交
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852
                        }
                    }
                    chart.__connectedActionDispatching = false;
                }
            });
        });

        return chart;
    };

    /**
     * @return {string|Array.<module:echarts~ECharts>} groupId
     */
    echarts.connect = function (groupId) {
        // Is array of charts
        if (zrUtil.isArray(groupId)) {
            var charts = groupId;
            groupId = null;
            // If any chart has group
            zrUtil.each(charts, function (chart) {
                if (chart.group != null) {
                    groupId = chart.group;
                }
            });
            groupId = groupId || groupIdBase++;
            zrUtil.each(charts, function (chart) {
                chart.group = groupId;
            });
        }
        connectedGroups[groupId] = true;
        return groupId;
    };

    /**
     * @return {string} groupId
     */
    echarts.disConnect = function (groupId) {
        connectedGroups[groupId] = false;
    };

    /**
     * Dispose a chart instance
     * @param  {module:echarts~ECharts|HTMLDomElement|string} chart
     */
    echarts.dispose = function (chart) {
        if (zrUtil.isDom(chart)) {
            chart = echarts.getInstanceByDom(chart);
        }
        else if (typeof chart === 'string') {
            chart = instances[chart];
        }
        if ((chart instanceof ECharts) && !chart.isDisposed()) {
            chart.dispose();
        }
    };

    /**
     * @param  {HTMLDomElement} dom
     * @return {echarts~ECharts}
     */
    echarts.getInstanceByDom = function (dom) {
        var key = dom.getAttribute(DOM_ATTRIBUTE_KEY);
        return instances[key];
    };
    /**
     * @param {string} key
     * @return {echarts~ECharts}
     */
    echarts.getInstanceById = function (key) {
        return instances[key];
L
tweak  
lang 已提交
853
    };
L
lang 已提交
854

L
tweak  
lang 已提交
855 856 857 858 859 860 861
    /**
     * Register option preprocessor
     * @param {Function} preprocessorFunc
     */
    echarts.registerPreprocessor = function (preprocessorFunc) {
        optionPreprocessorFuncs.push(preprocessorFunc);
    };
862

L
tweak  
lang 已提交
863 864 865 866 867 868 869 870 871 872 873
    /**
     * @param {string} stage
     * @param {Function} processorFunc
     */
    echarts.registerProcessor = function (stage, processorFunc) {
        if (zrUtil.indexOf(PROCESSOR_STAGES, stage) < 0) {
            throw new Error('stage should be one of ' + PROCESSOR_STAGES);
        }
        var funcs = dataProcessorFuncs[stage] || (dataProcessorFuncs[stage] = []);
        funcs.push(processorFunc);
    };
L
lang 已提交
874

L
tweak  
lang 已提交
875 876 877 878 879 880 881 882 883 884 885
    /**
     * 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 已提交
886 887 888 889
     * @param {string} [actionInfo.event]
     * @param {string} [actionInfo.update]
     * @param {string} [eventName]
     * @param {Function} action
L
tweak  
lang 已提交
890
     */
L
lang 已提交
891 892 893 894 895
    echarts.registerAction = function (actionInfo, eventName, action) {
        if (typeof eventName === 'function') {
            action = eventName;
            eventName = '';
        }
L
tweak  
lang 已提交
896 897
        var actionType = zrUtil.isObject(actionInfo)
            ? actionInfo.type
L
lang 已提交
898 899 900
            : ([actionInfo, actionInfo = {
                event: eventName
            }][0]);
L
lang 已提交
901 902

        actionInfo.event = actionInfo.event || actionType;
L
lang 已提交
903
        eventName = actionInfo.event;
904

L
tweak  
lang 已提交
905 906 907
        if (!actions[actionType]) {
            actions[actionType] = {action: action, actionInfo: actionInfo};
        }
L
lang 已提交
908
        eventActionMap[eventName] = actionType;
L
tweak  
lang 已提交
909
    };
P
pah100 已提交
910

L
tweak  
lang 已提交
911 912 913 914 915 916 917
    /**
     * @param {string} type
     * @param {*} CoordinateSystem
     */
    echarts.registerCoordinateSystem = function (type, CoordinateSystem) {
        CoordinateSystemManager.register(type, CoordinateSystem);
    };
L
lang 已提交
918

L
tweak  
lang 已提交
919 920 921
    /**
     * @param {*} layout
     */
L
lang 已提交
922
    echarts.registerLayout = function (layout) {
L
tweak  
lang 已提交
923
        // PENDING All functions ?
924 925
        if (zrUtil.indexOf(layoutFuncs, layout) < 0) {
            layoutFuncs.push(layout);
L
tweak  
lang 已提交
926 927
        }
    };
L
lang 已提交
928

L
tweak  
lang 已提交
929 930 931 932 933 934 935 936 937 938 939
    /**
     * @param {string} stage
     * @param {Function} visualCodingFunc
     */
    echarts.registerVisualCoding = function (stage, visualCodingFunc) {
        if (zrUtil.indexOf(VISUAL_CODING_STAGES, stage) < 0) {
            throw new Error('stage should be one of ' + VISUAL_CODING_STAGES);
        }
        var funcs = visualCodingFuncs[stage] || (visualCodingFuncs[stage] = []);
        funcs.push(visualCodingFunc);
    };
L
Update  
lang 已提交
940

L
tweak  
lang 已提交
941 942 943 944 945 946
    /**
     * @param {Object} opts
     */
    echarts.extendChartView = function (opts) {
        return ChartView.extend(opts);
    };
L
Update  
lang 已提交
947

L
tweak  
lang 已提交
948 949 950 951 952 953
    /**
     * @param {Object} opts
     */
    echarts.extendComponentModel = function (opts) {
        return ComponentModel.extend(opts);
    };
L
Update  
lang 已提交
954

L
tweak  
lang 已提交
955 956 957 958 959 960
    /**
     * @param {Object} opts
     */
    echarts.extendSeriesModel = function (opts) {
        return SeriesModel.extend(opts);
    };
P
pah100 已提交
961

L
tweak  
lang 已提交
962 963 964 965 966
    /**
     * @param {Object} opts
     */
    echarts.extendComponentView = function (opts) {
        return ComponentView.extend(opts);
L
lang 已提交
967 968
    };

969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988
    /**
     * ZRender need a canvas context to do measureText.
     * But in node environment canvas may be created by node-canvas.
     * So we need to specify how to create a canvas instead of using document.createElement('canvas')
     *
     * Be careful of using it in the browser.
     *
     * @param {Function} creator
     * @example
     *     var Canvas = require('canvas');
     *     var echarts = require('echarts');
     *     echarts.setCanvasCreator(function () {
     *         // Small size is enough.
     *         return new Canvas(32, 32);
     *     });
     */
    echarts.setCanvasCreator = function (creator) {
        zrUtil.createCanvas = creator;
    };

L
lang 已提交
989 990 991
    echarts.registerVisualCoding('echarts', zrUtil.curry(
        require('./visual/seriesColor'), '', 'itemStyle'
    ));
992 993
    echarts.registerPreprocessor(require('./preprocessor/backwardCompat'));

994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
    // Default action
    echarts.registerAction({
        type: 'highlight',
        event: 'highlight',
        update: 'highlight'
    }, zrUtil.noop);
    echarts.registerAction({
        type: 'downplay',
        event: 'downplay',
        update: 'downplay'
    }, zrUtil.noop);

P
pah100 已提交
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025

    // --------
    // Exports
    // --------

    echarts.graphic = require('echarts/util/graphic');
    echarts.number = require('echarts/util/number');
    echarts.format = require('echarts/util/format');

    echarts.util = {};
    each([
            'map', 'each', 'filter', 'indexOf', 'inherits',
            'reduce', 'filter', 'bind', 'curry', 'isArray',
            'isString', 'isObject', 'isFunction', 'extend'
        ],
        function (name) {
            echarts.util[name] = zrUtil[name];
        }
    );

L
lang 已提交
1026 1027
    return echarts;
});