echarts.js 26.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');
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 220
    var updateMethods = {

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

222
            var ecModel = this._model;
L
lang 已提交
223

224 225 226 227 228
            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 已提交
229

230
            processData.call(this, ecModel);
L
lang 已提交
231

232
            stackSeriesData.call(this, ecModel);
L
lang 已提交
233

234
            this._coordinateSystem.update(ecModel, this._api);
L
lang 已提交
235

236
            doLayout.call(this, ecModel, payload);
237

238
            doVisualCoding.call(this, ecModel, payload);
239

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

242 243 244 245 246 247 248 249 250
            // 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 已提交
251
            }
L
lang 已提交
252 253 254 255 256 257 258 259
            if (env.node) {
                this._zr.configLayer(0, {
                    clearColor: backgroundColor
                });
            }
            else {
                backgroundColor && (this._dom.style.backgroundColor = backgroundColor);
            }
L
lang 已提交
260

261 262
            console.time && console.timeEnd('update');
        },
263

264 265 266 267 268 269 270
        // PENDING
        /**
         * @param {Object} payload
         * @private
         */
        updateView: function (payload) {
            var ecModel = this._model;
271

272
            doLayout.call(this, ecModel, payload);
273

274
            doVisualCoding.call(this, ecModel, payload);
275

276 277
            invokeUpdateMethod.call(this, 'updateView', ecModel, payload);
        },
278

279 280 281 282 283 284
        /**
         * @param {Object} payload
         * @private
         */
        updateVisual: function (payload) {
            var ecModel = this._model;
285

286
            doVisualCoding.call(this, ecModel, payload);
287

288 289
            invokeUpdateMethod.call(this, 'updateVisual', ecModel, payload);
        },
290

291 292 293 294 295 296
        /**
         * @param {Object} payload
         * @private
         */
        updateLayout: function (payload) {
            var ecModel = this._model;
297

298
            doLayout.call(this, ecModel, payload);
299

300 301
            invokeUpdateMethod.call(this, 'updateLayout', ecModel, payload);
        },
L
lang 已提交
302

303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
        /**
         * @param {Object} payload
         * @private
         */
        highlight: function (payload) {
            toggleHighlight.call(this, 'highlight', payload);
        },

        /**
         * @param {Object} payload
         * @private
         */
        downplay: function (payload) {
            toggleHighlight.call(this, 'downplay', payload);
        }
318 319 320 321 322 323 324

    };

    /**
     * @param {Object} payload
     * @private
     */
325
    function toggleHighlight(method, payload) {
326
        var ecModel = this._model;
327 328 329

        ecModel.eachComponent(
            {mainType: 'series', query: payload},
P
pah100 已提交
330
            function (seriesModel, index, payloadInfo) {
P
tweak  
pah100 已提交
331
                var chartView = this._chartsMap[seriesModel.id];
332
                if (chartView) {
P
pah100 已提交
333 334 335
                    chartView[method](
                        seriesModel, ecModel, this._api, payloadInfo
                    );
336 337 338 339
                }
            },
            this
        );
340
    }
341

L
Resize  
lang 已提交
342 343 344
    /**
     * Resize the chart
     */
L
tweak  
lang 已提交
345
    echartsProto.resize = function () {
L
Resize  
lang 已提交
346
        this._zr.resize();
L
lang 已提交
347
        updateMethods.update.call(this);
L
lang 已提交
348 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

        // 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);
        this._loadingFX = el;
        this._zr.add(el);
    };

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

L
lang 已提交
377
    /**
L
Resize  
lang 已提交
378 379
     * @param {Object} eventObj
     * @return {Object}
L
lang 已提交
380 381 382 383 384 385 386
     */
    echartsProto.makeActionFromEvent = function (eventObj) {
        var payload = zrUtil.extend({}, eventObj);
        payload.type = eventActionMap[eventObj.type];
        return payload;
    };

L
tweak  
lang 已提交
387 388 389 390
    /**
     * @pubilc
     * @param {Object} payload
     * @param {string} [payload.type] Action type
P
pah100 已提交
391
     * @param {boolean} [silent=false] Whether trigger event.
L
tweak  
lang 已提交
392 393
     * @param {number} [payload.from] From uid
     */
L
lang 已提交
394
    echartsProto.dispatchAction = function (payload, silent) {
L
tweak  
lang 已提交
395 396
        var actionWrap = actions[payload.type];
        if (actionWrap) {
L
lang 已提交
397 398
            var actionInfo = actionWrap.actionInfo;
            var updateMethod = actionInfo.update || 'update';
L
tweak  
lang 已提交
399
            actionWrap.action(payload, this._model);
400
            updateMethod !== 'none' && updateMethods[updateMethod].call(this, payload);
L
lang 已提交
401

P
pah100 已提交
402 403 404 405 406
            if (!silent) {
                // Emit event outside
                // Convert type to eventType
                var eventObj = zrUtil.extend({}, payload);
                eventObj.type = actionInfo.event || eventObj.type;
L
lang 已提交
407
                this._messageCenter.trigger(eventObj.type, eventObj);
P
pah100 已提交
408
            }
L
tweak  
lang 已提交
409 410
        }
    };
411

L
tweak  
lang 已提交
412 413 414 415
    /**
     * @param {string} methodName
     * @private
     */
416
    function invokeUpdateMethod(methodName, ecModel, payload) {
417
        var api = this._api;
L
lang 已提交
418

L
tweak  
lang 已提交
419 420 421 422
        // Update all components
        each(this._componentsList, function (component) {
            var componentModel = component.__model;
            component[methodName](componentModel, ecModel, api, payload);
423

L
tweak  
lang 已提交
424 425
            updateZ(componentModel, component);
        }, this);
L
lang 已提交
426

L
tweak  
lang 已提交
427 428
        // Upate all charts
        ecModel.eachSeries(function (seriesModel, idx) {
P
tweak  
pah100 已提交
429
            var chart = this._chartsMap[seriesModel.id];
L
tweak  
lang 已提交
430
            chart[methodName](seriesModel, ecModel, api, payload);
431

L
tweak  
lang 已提交
432 433
            updateZ(seriesModel, chart);
        }, this);
434

435
    }
L
lang 已提交
436

L
lang 已提交
437
    /**
L
Tweak  
lang 已提交
438
     * Prepare view instances of charts and components
L
lang 已提交
439 440 441
     * @param  {module:echarts/model/Global} ecModel
     * @private
     */
442
    function prepareView(type, ecModel) {
L
Tweak  
lang 已提交
443 444 445
        var isComponent = type === 'component';
        var viewList = isComponent ? this._componentsList : this._chartsList;
        var viewMap = isComponent ? this._componentsMap : this._chartsMap;
L
tweak  
lang 已提交
446
        var zr = this._zr;
L
lang 已提交
447

L
Tweak  
lang 已提交
448 449
        for (var i = 0; i < viewList.length; i++) {
            viewList[i].__keepAlive = false;
L
tweak  
lang 已提交
450
        }
L
lang 已提交
451

L
Tweak  
lang 已提交
452 453 454 455
        ecModel[isComponent ? 'eachComponent' : 'eachSeries'](function (componentType, model) {
            if (isComponent) {
                if (componentType === 'series') {
                    return;
L
lang 已提交
456
                }
457
            }
L
tweak  
lang 已提交
458
            else {
L
Tweak  
lang 已提交
459
                model = componentType;
L
tweak  
lang 已提交
460 461
            }

P
tweak  
pah100 已提交
462
            var view = viewMap[model.id];
L
Tweak  
lang 已提交
463 464 465
            if (!view) {
                var classType = ComponentModel.parseClassType(model.type);
                var Clazz = isComponent
L
tweak  
lang 已提交
466
                    ? ComponentView.getClass(classType.main, classType.sub)
L
Tweak  
lang 已提交
467
                    : ChartView.getClass(classType.sub);
L
tweak  
lang 已提交
468
                if (Clazz) {
L
Tweak  
lang 已提交
469 470
                    view = new Clazz();
                    view.init(ecModel, this._api);
P
tweak  
pah100 已提交
471
                    viewMap[model.id] = view;
L
Tweak  
lang 已提交
472 473 474 475 476
                    viewList.push(view);
                    zr.add(view.group);
                }
                else {
                    // Error
L
lang 已提交
477
                    return;
L
lang 已提交
478
                }
479
            }
L
Tweak  
lang 已提交
480 481

            view.__keepAlive = true;
P
tweak  
pah100 已提交
482
            view.__id = model.id;
L
Tweak  
lang 已提交
483
            view.__model = model;
L
tweak  
lang 已提交
484 485
        }, this);

L
Tweak  
lang 已提交
486 487 488 489 490 491 492
        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 已提交
493 494 495 496 497
            }
            else {
                i++;
            }
        }
498 499
    }

L
tweak  
lang 已提交
500 501 502 503 504 505
    /**
     * Processor data in each series
     *
     * @param {module:echarts/model/Global} ecModel
     * @private
     */
506
    function processData(ecModel) {
L
tweak  
lang 已提交
507 508 509
        each(PROCESSOR_STAGES, function (stage) {
            each(dataProcessorFuncs[stage] || [], function (process) {
                process(ecModel);
L
lang 已提交
510
            });
L
tweak  
lang 已提交
511
        });
512
    }
L
lang 已提交
513

L
tweak  
lang 已提交
514 515 516
    /**
     * @private
     */
517
    function stackSeriesData(ecModel) {
L
tweak  
lang 已提交
518 519 520 521 522 523 524 525
        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 已提交
526
                }
L
tweak  
lang 已提交
527 528 529
                stackedDataMap[stack] = data;
            }
        });
530
    }
L
lang 已提交
531

L
tweak  
lang 已提交
532
    /**
L
lang 已提交
533
     * Layout before each chart render there series, after visual coding and data processing
L
tweak  
lang 已提交
534 535 536 537
     *
     * @param {module:echarts/model/Global} ecModel
     * @private
     */
538
    function doLayout(ecModel, payload) {
539
        var api = this._api;
L
tweak  
lang 已提交
540 541 542
        each(layoutFuncs, function (layout) {
            layout(ecModel, api, payload);
        });
543
    }
L
lang 已提交
544

L
tweak  
lang 已提交
545 546 547 548 549 550
    /**
     * Code visual infomation from data after data processing
     *
     * @param {module:echarts/model/Global} ecModel
     * @private
     */
551
    function doVisualCoding(ecModel, payload) {
L
tweak  
lang 已提交
552 553 554
        each(VISUAL_CODING_STAGES, function (stage) {
            each(visualCodingFuncs[stage] || [], function (visualCoding) {
                visualCoding(ecModel, payload);
L
lang 已提交
555
            });
L
tweak  
lang 已提交
556
        });
557
    }
L
lang 已提交
558

L
tweak  
lang 已提交
559 560 561 562
    /**
     * Render each chart and component
     * @private
     */
563
    function doRender(ecModel, payload) {
564
        var api = this._api;
L
tweak  
lang 已提交
565 566 567 568 569 570 571 572 573 574 575 576 577 578
        // 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 已提交
579
            var chart = this._chartsMap[seriesModel.id];
L
tweak  
lang 已提交
580 581 582 583 584 585
            chart.__keepAlive = true;
            chart.render(seriesModel, ecModel, api, payload);

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

L
lang 已提交
586
        // Remove groups of unrendered charts
L
tweak  
lang 已提交
587 588 589 590 591
        each(this._chartsList, function (chart) {
            if (!chart.__keepAlive) {
                chart.remove(ecModel, api);
            }
        }, this);
592
    }
L
lang 已提交
593

L
lang 已提交
594 595 596 597 598 599 600 601 602 603 604 605 606
    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) {
607
                    var hostModel = el.hostModel || ecModel.getSeriesByIndex(el.seriesIndex);
L
lang 已提交
608 609 610 611 612 613 614
                    var params = hostModel && hostModel.getDataParams(el.dataIndex) || {};
                    params.event = e;
                    params.type = eveName;
                    this.trigger(eveName, params);
                }
            }, this);
        }, this);
L
lang 已提交
615 616 617 618 619 620

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

L
lang 已提交
623 624 625 626 627 628 629 630 631
    /**
     * @return {boolean]
     */
    echartsProto.isDisposed = function () {
        return this._disposed;
    };
    /**
     * Dispose instance
     */
L
tweak  
lang 已提交
632
    echartsProto.dispose = function () {
L
lang 已提交
633 634
        this._disposed = true;

L
tweak  
lang 已提交
635 636 637 638 639 640
        each(this._components, function (component) {
            component.dispose();
        });
        each(this._charts, function (chart) {
            chart.dispose();
        });
L
lang 已提交
641

L
Tweak  
lang 已提交
642
        this._zr.dispose();
L
lang 已提交
643 644

        instances[this.id] = null;
L
lang 已提交
645 646
    };

L
lang 已提交
647 648
    zrUtil.mixin(ECharts, Eventful);

L
lang 已提交
649 650 651 652 653 654 655 656 657 658 659 660 661 662
    /**
     * @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 已提交
663 664 665 666
    /**
     * @type {Array.<Function>}
     * @inner
     */
P
pah100 已提交
667 668
    var actions = [];

L
lang 已提交
669
    /**
L
lang 已提交
670
     * Map eventType to actionType
L
lang 已提交
671 672 673 674
     * @type {Object}
     */
    var eventActionMap = {};

L
lang 已提交
675 676 677 678
    /**
     * @type {Array.<Function>}
     * @inner
     */
679 680
    var layoutFuncs = [];

L
lang 已提交
681 682 683 684 685 686 687
    /**
     * Data processor functions of each stage
     * @type {Array.<Object.<string, Function>>}
     * @inner
     */
    var dataProcessorFuncs = {};

688 689 690 691 692 693
    /**
     * @type {Array.<Function>}
     * @inner
     */
    var optionPreprocessorFuncs = [];

L
lang 已提交
694 695 696 697 698
    /**
     * Visual coding functions of each stage
     * @type {Array.<Object.<string, Function>>}
     * @inner
     */
699
    var visualCodingFuncs = {};
L
lang 已提交
700

L
lang 已提交
701 702 703 704 705 706
    var instances = {};
    var connectedGroups = {};

    var idBase = new Date() - 0;
    var groupIdBase = new Date() - 0;
    var DOM_ATTRIBUTE_KEY = '_echarts_instance_';
L
lang 已提交
707
    /**
L
lang 已提交
708
     * @alias module:echarts
L
lang 已提交
709
     */
L
lang 已提交
710 711 712 713 714 715 716 717 718
    var echarts = {
        /**
         * @type {number}
         */
        version: '3.0.0',
        dependencies: {
            zrender: '3.0.0'
        }
    };
L
lang 已提交
719

L
tweak  
lang 已提交
720 721 722 723 724 725
    /**
     * @param {HTMLDomElement} dom
     * @param {Object} [theme]
     * @param {Object} opts
     */
    echarts.init = function (dom, theme, opts) {
L
lang 已提交
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
        // Check version
        if ((zrender.version.replace('.', '') - 0) < (echarts.dependencies.zrender.replace('.', '') - 0)) {
            console.error(
                'ZRender ' + zrender.version
                + ' is too old for ECharts ' + echarts.version
                + '. Current version need ZRender '
                + echarts.dependencies.zrender + '+'
            );
        }

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

        // Connecting
        zrUtil.each(eventActionMap, function (actionType, eventType) {
L
lang 已提交
742 743
            // FIXME
            chart._messageCenter.on(eventType, function (event) {
L
lang 已提交
744 745 746
                if (connectedGroups[chart.group]) {
                    chart.__connectedActionDispatching = true;
                    for (var id in instances) {
L
tweak  
lang 已提交
747
                        var action = chart.makeActionFromEvent(event);
L
lang 已提交
748 749
                        var otherChart = instances[id];
                        if (otherChart !== chart && otherChart.group === chart.group) {
L
tweak  
lang 已提交
750
                            if (!otherChart.__connectedActionDispatching) {
L
lang 已提交
751
                                otherChart.dispatchAction(action);
L
tweak  
lang 已提交
752
                            }
L
lang 已提交
753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 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
                        }
                    }
                    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 已提交
823
    };
L
lang 已提交
824

L
tweak  
lang 已提交
825 826 827 828 829 830 831
    /**
     * Register option preprocessor
     * @param {Function} preprocessorFunc
     */
    echarts.registerPreprocessor = function (preprocessorFunc) {
        optionPreprocessorFuncs.push(preprocessorFunc);
    };
832

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

L
tweak  
lang 已提交
845 846 847 848 849 850 851 852 853 854 855
    /**
     * 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 已提交
856 857 858 859
     * @param {string} [actionInfo.event]
     * @param {string} [actionInfo.update]
     * @param {string} [eventName]
     * @param {Function} action
L
tweak  
lang 已提交
860
     */
L
lang 已提交
861 862 863 864 865
    echarts.registerAction = function (actionInfo, eventName, action) {
        if (typeof eventName === 'function') {
            action = eventName;
            eventName = '';
        }
L
tweak  
lang 已提交
866 867
        var actionType = zrUtil.isObject(actionInfo)
            ? actionInfo.type
L
lang 已提交
868 869 870
            : ([actionInfo, actionInfo = {
                event: eventName
            }][0]);
L
lang 已提交
871 872

        actionInfo.event = actionInfo.event || actionType;
L
lang 已提交
873
        eventName = actionInfo.event;
874

L
tweak  
lang 已提交
875 876 877
        if (!actions[actionType]) {
            actions[actionType] = {action: action, actionInfo: actionInfo};
        }
L
lang 已提交
878
        eventActionMap[eventName] = actionType;
L
tweak  
lang 已提交
879
    };
P
pah100 已提交
880

L
tweak  
lang 已提交
881 882 883 884 885 886 887
    /**
     * @param {string} type
     * @param {*} CoordinateSystem
     */
    echarts.registerCoordinateSystem = function (type, CoordinateSystem) {
        CoordinateSystemManager.register(type, CoordinateSystem);
    };
L
lang 已提交
888

L
tweak  
lang 已提交
889 890 891
    /**
     * @param {*} layout
     */
L
lang 已提交
892
    echarts.registerLayout = function (layout) {
L
tweak  
lang 已提交
893
        // PENDING All functions ?
894 895
        if (zrUtil.indexOf(layoutFuncs, layout) < 0) {
            layoutFuncs.push(layout);
L
tweak  
lang 已提交
896 897
        }
    };
L
lang 已提交
898

L
tweak  
lang 已提交
899 900 901 902 903 904 905 906 907 908 909
    /**
     * @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 已提交
910

L
tweak  
lang 已提交
911 912 913 914 915 916
    /**
     * @param {Object} opts
     */
    echarts.extendChartView = function (opts) {
        return ChartView.extend(opts);
    };
L
Update  
lang 已提交
917

L
tweak  
lang 已提交
918 919 920 921 922 923
    /**
     * @param {Object} opts
     */
    echarts.extendComponentModel = function (opts) {
        return ComponentModel.extend(opts);
    };
L
Update  
lang 已提交
924

L
tweak  
lang 已提交
925 926 927 928 929 930
    /**
     * @param {Object} opts
     */
    echarts.extendSeriesModel = function (opts) {
        return SeriesModel.extend(opts);
    };
P
pah100 已提交
931

L
tweak  
lang 已提交
932 933 934 935 936
    /**
     * @param {Object} opts
     */
    echarts.extendComponentView = function (opts) {
        return ComponentView.extend(opts);
L
lang 已提交
937 938
    };

939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958
    /**
     * 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 已提交
959 960 961
    echarts.registerVisualCoding('echarts', zrUtil.curry(
        require('./visual/seriesColor'), '', 'itemStyle'
    ));
962 963
    echarts.registerPreprocessor(require('./preprocessor/backwardCompat'));

964 965 966 967 968 969 970 971 972 973 974 975
    // Default action
    echarts.registerAction({
        type: 'highlight',
        event: 'highlight',
        update: 'highlight'
    }, zrUtil.noop);
    echarts.registerAction({
        type: 'downplay',
        event: 'downplay',
        update: 'downplay'
    }, zrUtil.noop);

L
lang 已提交
976 977
    return echarts;
});