echarts.js 13.8 KB
Newer Older
L
lang 已提交
1
/**
L
tweak  
lang 已提交
2
 * TODO visualCoding 的优先级
L
lang 已提交
3
 *      setTheme
4
 *      axis position 统一处理
L
lang 已提交
5
 *      规范 Symbol 配置和绘制, customPath
L
lang 已提交
6
 */
L
lang 已提交
7 8
define(function (require) {

L
lang 已提交
9
    var GlobalModel = require('./model/Global');
L
lang 已提交
10
    var ExtensionAPI = require('./ExtensionAPI');
L
lang 已提交
11
    var CoordinateSystemManager = require('./CoordinateSystem');
L
lang 已提交
12

L
Update  
lang 已提交
13 14 15 16 17 18
    var ComponentModel = require('./model/Component');
    var SeriesModel = require('./model/Series');

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

L
lang 已提交
19
    var zrender = require('zrender');
L
lang 已提交
20
    var zrUtil = require('zrender/core/util');
L
lang 已提交
21

L
lang 已提交
22 23 24 25 26 27
    /**
     * @inner
     */
    function getSeriesId(series, seriesIndex) {
        return series.type + '_' + (series.name ||  seriesIndex);
    }
L
lang 已提交
28 29 30
    /**
     * @module echarts~ECharts
     */
L
lang 已提交
31 32
    var ECharts = function (dom, theme, opts) {
        opts = opts || {};
L
lang 已提交
33

L
lang 已提交
34 35 36 37
        /**
         * @type {module:zrender/ZRender}
         * @private
         */
L
lang 已提交
38 39 40
        this._zr = zrender.init(dom, {
            renderer: opts.renderer || 'canvas'
        });
L
lang 已提交
41

L
lang 已提交
42 43 44 45
        /**
         * @type {Object}
         * @private
         */
L
lang 已提交
46
        this._theme = zrUtil.clone(theme);
L
lang 已提交
47

L
lang 已提交
48 49 50 51
        /**
         * @type {Array.<module:echarts/view/Chart>}
         * @private
         */
L
lang 已提交
52
        this._chartsList = [];
L
lang 已提交
53 54 55 56 57

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

L
lang 已提交
60 61 62 63
        /**
         * @type {Array.<module:echarts/view/Component>}
         * @private
         */
L
lang 已提交
64
        this._componentsList = [];
L
lang 已提交
65 66 67 68 69

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

L
lang 已提交
72
        /**
L
lang 已提交
73
         * @type {module:echarts/ExtensionAPI}
L
lang 已提交
74 75
         * @private
         */
L
lang 已提交
76
        this._extensionAPI = new ExtensionAPI(this);
L
lang 已提交
77

L
lang 已提交
78 79 80 81
        /**
         * @type {module:echarts/CoordinateSystem}
         * @private
         */
L
lang 已提交
82
        this._coordinateSystem = new CoordinateSystemManager();
L
lang 已提交
83

L
lang 已提交
84 85 86 87 88
        /**
         * Layout instances
         * @type {Array}
         * @private
         */
L
lang 已提交
89 90 91
        this._layouts = zrUtil.map(layoutClasses, function (Layout) {
            return new Layout();
        });
L
lang 已提交
92 93 94 95 96 97 98 99 100 101 102 103

        /**
         * @type {boolean}
         * @private
         */
        this._needsUpdate = false;

        this._zr.animation.on('frame', function () {
            if (this._needsUpdate) {
                this.updateImmediately();
            }
        }, this);
L
lang 已提交
104 105 106 107
    };

    ECharts.prototype = {

L
lang 已提交
108 109 110 111
        getZr: function () {
            return this._zr;
        },

L
lang 已提交
112
        setOption: function (option, notMerge) {
L
lang 已提交
113
            option = zrUtil.clone(option, true);
L
lang 已提交
114

L
lang 已提交
115 116 117 118 119 120 121 122
            var ecModel = this._model;
            if (! ecModel || notMerge) {
                ecModel = new GlobalModel(option, null, this._theme);
                this._model = ecModel;
            }
            else {
                ecModel.mergeOption(option);
            }
L
lang 已提交
123

L
lang 已提交
124
            this._prepareComponents(ecModel);
L
lang 已提交
125

L
lang 已提交
126
            this._prepareCharts(ecModel);
L
lang 已提交
127 128

            this.updateImmediately();
L
lang 已提交
129 130
        },

L
lang 已提交
131 132 133 134
        setTheme: function (theme) {

        },

L
lang 已提交
135 136 137 138
        getCoordinateSystem: function (type, idx) {
            return this._coordinateSystem.get(type, idx);
        },

L
lang 已提交
139 140 141
        /**
         * @return {number}
         */
L
lang 已提交
142 143 144
        getWidth: function () {
            return this._zr.getWidth();
        },
L
lang 已提交
145

L
lang 已提交
146 147 148
        /**
         * @return {number}
         */
L
lang 已提交
149 150 151 152 153
        getHeight: function () {
            return this._zr.getHeight();
        },

        update: function () {
L
lang 已提交
154
            this._needsUpdate = true;
L
lang 已提交
155 156
        },

P
pah100 已提交
157
        updateImmediately: function (event) {
158 159
            // console.time('update');

L
lang 已提交
160 161
            var ecModel = this._model;

P
pah100 已提交
162
            ecModel.restoreData();
L
lang 已提交
163

P
pah100 已提交
164 165 166 167
            // 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.

L
lang 已提交
168
            this._processData(ecModel);
L
lang 已提交
169

L
lang 已提交
170 171
            this._stackSeriesData(ecModel);

L
lang 已提交
172
            this._coordinateSystem.update(ecModel, this._extensionAPI);
L
lang 已提交
173

L
lang 已提交
174 175
            this._doVisualCoding(ecModel);

P
pah100 已提交
176
            this._doLayout(ecModel, event);
L
lang 已提交
177

P
pah100 已提交
178
            this._doRender(ecModel, event);
L
lang 已提交
179 180

            this._needsUpdate = false;
181 182

            // console.timeEnd('update');
L
lang 已提交
183 184
        },

L
lang 已提交
185
        resize: function () {
L
lang 已提交
186
            // var ecModel = this._model;
L
lang 已提交
187

L
lang 已提交
188
            // this._coordinateSystem.resize(ecModel, this._extensionAPI);
L
lang 已提交
189

L
lang 已提交
190
            // this._doVisualCoding(ecModel);
L
lang 已提交
191

L
lang 已提交
192 193 194 195 196
            // this._doLayout(ecModel);

            // this._doRender(ecModel);

            this.updateImmediately();
L
lang 已提交
197 198
        },

P
pah100 已提交
199 200 201 202 203 204 205 206 207 208 209 210 211 212
        /**
         * @pubilc
         * @param {Object} event
         * @param {string} [event.type] Event type
         * @param {number} [event.from] From uid
         */
        dispatch: function (event) {
            var action = actions[event.type];
            if (action) {
                action(event, this._model);
                this.updateImmediately(event);
            }
        },

L
lang 已提交
213
        _prepareCharts: function (ecModel) {
L
lang 已提交
214

L
lang 已提交
215 216
            var chartsList = this._chartsList;
            var chartsMap = this._chartsMap;
L
lang 已提交
217
            var zr = this._zr;
L
lang 已提交
218 219 220 221 222 223 224 225 226

            for (var i = 0; i < chartsList.length; i++) {
                chartsList[i].__keepAlive = false;
            }

            ecModel.eachSeries(function (seriesModel, idx) {
                var id = getSeriesId(seriesModel.option, idx);

                var chart = chartsMap[id];
L
lang 已提交
227
                if (! chart) {
228 229 230
                    chart = ChartView.create(
                        ComponentModel.parseComponentType(seriesModel.type).sub
                    );
L
lang 已提交
231
                    if (chart) {
L
lang 已提交
232
                        chart.init(this._extensionAPI);
L
lang 已提交
233 234
                        chartsMap[id] = chart;
                        chartsList.push(chart);
L
lang 已提交
235
                        zr.add(chart.group);
L
lang 已提交
236 237 238 239 240 241
                    }
                    else {
                        // Error
                    }
                }

L
lang 已提交
242 243
                chart.__keepAlive = true;
                chart.__id = id;
L
lang 已提交
244 245
            }, this);

L
lang 已提交
246 247 248
            for (var i = 0; i < chartsList.length;) {
                var chart = chartsList[i];
                if (! chart.__keepAlive) {
L
lang 已提交
249
                    zr.remove(chart.group);
L
lang 已提交
250
                    chart.dispose();
L
lang 已提交
251 252
                    chartsList.splice(i, 1);
                    delete chartsMap[chart.__id];
L
lang 已提交
253 254 255 256
                }
                else {
                    i++;
                }
257
            }
L
lang 已提交
258 259
        },

L
lang 已提交
260
        _prepareComponents: function (ecModel) {
L
lang 已提交
261 262 263 264 265 266 267 268

            var componentsMap = this._componentsMap;
            var componentsList = this._componentsList;

            for (var i = 0; i < componentsList.length; i++) {
                componentsList[i].__keepAlive = true;
            }

L
Update  
lang 已提交
269
            ComponentView.eachAvailableComponent(function (componentType) {
L
lang 已提交
270

L
lang 已提交
271 272 273
                ecModel.eachComponent(componentType, function (componentModel, idx) {
                    var id = componentType + '_' + idx;
                    var component = componentsMap[id];
L
lang 已提交
274 275
                    if (! component) {
                        // Create and add component
L
Update  
lang 已提交
276
                        component = ComponentView.create(componentType, componentModel);
L
lang 已提交
277
                        component.init(this._extensionAPI);
L
lang 已提交
278
                        componentsMap[id] = component;
L
lang 已提交
279
                        componentsList.push(component);
L
lang 已提交
280 281

                        this._zr.add(component.group);
L
lang 已提交
282
                    }
L
lang 已提交
283 284 285 286 287 288 289 290 291 292 293 294 295 296
                    component.__id = id;
                    component.__keepAlive = true;
                    // Used in rendering
                    component.__model = componentModel;
                }, this);
            }, this);

            for (var i = 0; i < componentsList.length;) {
                var component = componentsList[i];
                if (! component.__keepAlive) {
                    this._zr.remove(component.group);
                    component.dispose();
                    componentsList.splice(i, 1);
                    delete componentsMap[component.__id];
L
lang 已提交
297 298
                }
                else {
L
lang 已提交
299
                    i++;
L
lang 已提交
300
                }
301
            }
L
lang 已提交
302 303
        },

L
lang 已提交
304 305 306 307 308 309
        /**
         * Processor data in each series
         *
         * @param {module:echarts/model/Global} ecModel
         * @private
         */
L
lang 已提交
310
        _processData: function (ecModel) {
L
lang 已提交
311
            zrUtil.each(dataProcessorFuncs, function (processor) {
L
lang 已提交
312 313
                processor(ecModel);
            });
L
lang 已提交
314 315
        },

L
lang 已提交
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
        /**
         * @private
         */
        _stackSeriesData: function (ecModel) {
            var stackedDataMap = {};
            ecModel.eachSeries(function (series) {
                var stack = series.get('stack');
                var data = series.getData();
                if (stack && data.type === 'list' && data.dimensions.length === 1) {
                    var previousStack = stackedDataMap[stack];
                    if (previousStack) {
                        data.each(function (dataItem, idx) {
                            dataItem.stackedOn = previousStack.at(idx);
                        });
                    }
                    stackedDataMap[stack] = data;
                }
            });
        },

L
lang 已提交
336 337 338 339 340 341
        /**
         * Layout before each chart render there series after visual coding and data processing
         *
         * @param {module:echarts/model/Global} ecModel
         * @private
         */
P
pah100 已提交
342
        _doLayout: function (ecModel, event) {
L
lang 已提交
343
            zrUtil.each(this._layouts, function (layout) {
P
merge  
pah100 已提交
344
                layout.update(ecModel, event);
345 346
            });
            zrUtil.each(layoutFuncs, function (layout) {
P
pah100 已提交
347
                layout(ecModel, event);
L
lang 已提交
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
            });
        },

        /**
         * Code visual infomation from data after data processing
         *
         * @param {module:echarts/model/Global} ecModel
         * @private
         */
        _doVisualCoding: function (ecModel) {
            zrUtil.each(visualCodingFuncs, function (visualCoding) {
                visualCoding(ecModel);
            });
        },

        /**
         * Render each chart and component
         */
P
pah100 已提交
366
        _doRender: function (ecModel, event) {
L
lang 已提交
367
            var api = this._extensionAPI;
L
lang 已提交
368
            // Render all components
L
lang 已提交
369
            zrUtil.each(this._componentsList, function (component) {
P
pah100 已提交
370
                component.render(component.__model, ecModel, api, event);
L
lang 已提交
371
            }, this);
P
pah100 已提交
372

L
lang 已提交
373
            zrUtil.each(this._chartsList, function (chart) {
374
                chart.__keepAlive = false;
L
lang 已提交
375
            }, this);
P
pah100 已提交
376

L
lang 已提交
377
            // Render all charts
L
lang 已提交
378 379
            ecModel.eachSeries(function (seriesModel, idx) {
                var id = getSeriesId(seriesModel.option, idx);
L
lang 已提交
380
                var chart = this._chartsMap[id];
381
                chart.__keepAlive = true;
P
pah100 已提交
382
                chart.render(seriesModel, ecModel, api, event);
L
lang 已提交
383
            }, this);
P
pah100 已提交
384

385 386 387 388 389 390
            // Remove groups of charts
            zrUtil.each(this._chartsList, function (chart) {
                if (!chart.__keepAlive) {
                    chart.remove();
                }
            }, this);
L
lang 已提交
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
        },

        dispose: function () {
            zrUtil.each(this._components, function (component) {
                component.dispose();
            });
            zrUtil.each(this._charts, function (chart) {
                chart.dispose();
            });

            this.zr.dispose();
        }
    };


L
lang 已提交
406
    var dataProcessorFuncs = [];
L
lang 已提交
407

P
pah100 已提交
408 409
    var actions = [];

L
lang 已提交
410 411
    var layoutClasses = [];

412 413
    var layoutFuncs = [];

L
lang 已提交
414 415
    var visualCodingFuncs = [];

L
lang 已提交
416 417 418 419 420
    /**
     * @module echarts
     */
    var echarts = {

L
lang 已提交
421 422
        init: function (dom, theme, opts) {
            return new ECharts(dom, theme, opts);
L
lang 已提交
423 424
        },

L
lang 已提交
425 426 427 428 429 430
        /**
         * @param {Function}
         */
        registerProcessor: function (processorFunc) {
            if (zrUtil.indexOf(dataProcessorFuncs, processorFunc) < 0) {
                dataProcessorFuncs.push(processorFunc);
L
lang 已提交
431
            }
L
lang 已提交
432 433
        },

P
pah100 已提交
434 435 436 437 438 439 440 441 442
        /**
         * @param {Function}
         */
        registerAction: function (actionName, action) {
            if (!actions[actionName]) {
                actions[actionName] = action;
            }
        },

L
lang 已提交
443 444 445 446
        /**
         * @param {string} type
         * @param {*} CoordinateSystem
         */
L
lang 已提交
447
        registerCoordinateSystem: function (type, CoordinateSystem) {
L
lang 已提交
448 449 450
            CoordinateSystemManager.register(type, CoordinateSystem);
        },

L
lang 已提交
451 452 453
        /**
         * @param {*} layout
         */
454
        registerLayout: function (layout, isFactory) {
L
tweak  
lang 已提交
455
            // PENDING All functions ?
456 457 458 459 460 461 462 463 464
            if (isFactory) {
                if (zrUtil.indexOf(layoutClasses, layout) < 0) {
                    layoutClasses.push(layout);
                }
            }
            else {
                if (zrUtil.indexOf(layoutFuncs, layout) < 0) {
                    layoutFuncs.push(layout);
                }
L
lang 已提交
465 466 467
            }
        },

L
lang 已提交
468 469
        registerVisualCoding: function (visualCodingFunc) {
            visualCodingFuncs.push(visualCodingFunc);
L
Update  
lang 已提交
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
        },

        /**
         * @param {Object} opts
         */
        extendChartView: function (opts) {
            return ChartView.extend(opts);
        },

        /**
         * @param {Object} opts
         */
        extendComponentModel: function (opts) {
            return ComponentModel.extend(opts);
        },

P
pah100 已提交
486 487 488 489 490 491 492
        /**
         * @param {Object} opts
         */
        extendSeriesModel: function (opts) {
            return SeriesModel.extend(opts);
        },

L
Update  
lang 已提交
493 494 495 496 497
        /**
         * @param {Object} opts
         */
        extendComponentView: function (opts) {
            return ComponentView.extend(opts);
L
lang 已提交
498
        }
L
lang 已提交
499 500
    };

L
lang 已提交
501 502
    echarts.registerVisualCoding(require('./visual/defaultColor'));

L
lang 已提交
503 504
    return echarts;
});