echarts.js 15.6 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 7
 *
 *      每次 update 只刷新 model 变化的那些 component(需要做依赖收集)
L
lang 已提交
8
 */
L
lang 已提交
9 10
define(function (require) {

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

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

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

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

24 25
    var VISUAL_CODING_STAGES = ['echarts', 'chart', 'component'];

L
lang 已提交
26 27 28
    /**
     * @module echarts~ECharts
     */
L
lang 已提交
29 30
    var ECharts = function (dom, theme, opts) {
        opts = opts || {};
L
lang 已提交
31

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

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

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

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

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

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

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

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

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

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

        this._zr.animation.on('frame', function () {
            if (this._needsUpdate) {
                this.updateImmediately();
            }
        }, this);
L
lang 已提交
107 108 109 110
    };

    ECharts.prototype = {

L
lang 已提交
111 112 113 114
        getDom: function () {
            return this._dom;
        },

L
lang 已提交
115 116 117 118
        getZr: function () {
            return this._zr;
        },

L
lang 已提交
119
        setOption: function (option, notMerge) {
L
lang 已提交
120
            // PENDING
L
lang 已提交
121
            option = zrUtil.clone(option, true);
L
lang 已提交
122

L
lang 已提交
123
            var ecModel = this._model;
L
lang 已提交
124
            if (!ecModel || notMerge) {
P
pah100 已提交
125 126 127
                ecModel = new GlobalModel(
                    option, null, this._theme, this._extensionAPI
                );
L
lang 已提交
128 129 130
                this._model = ecModel;
            }
            else {
P
pah100 已提交
131
                ecModel.restoreData();
L
lang 已提交
132 133
                ecModel.mergeOption(option);
            }
L
lang 已提交
134

L
lang 已提交
135
            this._prepareComponents(ecModel);
L
lang 已提交
136

L
lang 已提交
137
            this._prepareCharts(ecModel);
L
lang 已提交
138 139

            this.updateImmediately();
L
lang 已提交
140 141
        },

L
lang 已提交
142 143 144 145 146 147 148
        setTheme: function (theme) {

        },

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

L
lang 已提交
153 154 155
        /**
         * @return {number}
         */
L
lang 已提交
156 157 158 159 160
        getHeight: function () {
            return this._zr.getHeight();
        },

        update: function () {
L
lang 已提交
161
            this._needsUpdate = true;
L
lang 已提交
162 163
        },

P
pah100 已提交
164
        updateImmediately: function (event) {
L
lang 已提交
165
            console.time('update');
166

L
lang 已提交
167 168
            var ecModel = this._model;

P
pah100 已提交
169
            ecModel.restoreData();
L
lang 已提交
170

P
pah100 已提交
171 172 173 174
            // 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 已提交
175
            this._processData(ecModel);
L
lang 已提交
176

L
lang 已提交
177 178
            this._stackSeriesData(ecModel);

L
lang 已提交
179
            this._coordinateSystem.update(ecModel, this._extensionAPI);
L
lang 已提交
180

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

183 184
            this._doVisualCoding(ecModel);

P
pah100 已提交
185
            this._doRender(ecModel, event);
L
lang 已提交
186 187

            this._needsUpdate = false;
188

L
lang 已提交
189
            console.timeEnd('update');
L
lang 已提交
190 191
        },

L
lang 已提交
192
        resize: function () {
L
lang 已提交
193
            // var ecModel = this._model;
L
lang 已提交
194

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

L
lang 已提交
197
            // this._doVisualCoding(ecModel);
L
lang 已提交
198

L
lang 已提交
199 200 201 202 203
            // this._doLayout(ecModel);

            // this._doRender(ecModel);

            this.updateImmediately();
L
lang 已提交
204 205
        },

P
pah100 已提交
206 207 208 209 210 211 212 213 214 215 216 217 218 219
        /**
         * @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 已提交
220
        _prepareCharts: function (ecModel) {
L
lang 已提交
221

L
lang 已提交
222 223
            var chartsList = this._chartsList;
            var chartsMap = this._chartsMap;
L
lang 已提交
224
            var zr = this._zr;
L
lang 已提交
225 226 227 228 229 230

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

            ecModel.eachSeries(function (seriesModel, idx) {
P
pah100 已提交
231
                var id = seriesModel.uid;
L
lang 已提交
232 233

                var chart = chartsMap[id];
234 235
                if (!chart) {
                    var Clazz = ChartView.getClass(
236 237
                        ComponentModel.parseComponentType(seriesModel.type).sub
                    );
238 239
                    if (Clazz) {
                        chart = new Clazz();
L
lang 已提交
240
                        chart.init(ecModel, this._extensionAPI);
L
lang 已提交
241 242
                        chartsMap[id] = chart;
                        chartsList.push(chart);
L
lang 已提交
243
                        zr.add(chart.group);
L
lang 已提交
244 245 246 247 248 249
                    }
                    else {
                        // Error
                    }
                }

L
lang 已提交
250 251
                chart.__keepAlive = true;
                chart.__id = id;
L
lang 已提交
252 253
            }, this);

L
lang 已提交
254 255 256
            for (var i = 0; i < chartsList.length;) {
                var chart = chartsList[i];
                if (! chart.__keepAlive) {
L
lang 已提交
257
                    zr.remove(chart.group);
L
lang 已提交
258
                    chart.dispose(this._extensionAPI);
L
lang 已提交
259 260
                    chartsList.splice(i, 1);
                    delete chartsMap[chart.__id];
L
lang 已提交
261 262 263 264
                }
                else {
                    i++;
                }
265
            }
L
lang 已提交
266 267
        },

L
lang 已提交
268
        _prepareComponents: function (ecModel) {
L
lang 已提交
269 270 271 272 273 274 275 276

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

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

P
pah100 已提交
277
            ecModel.eachComponent(function (componentType, componentModel) {
278 279 280 281
                if (componentType === 'series') {
                    return;
                }

P
pah100 已提交
282
                var id = componentModel.uid;
283 284 285
                var component = componentsMap[id];
                if (!component) {
                    // Create and add component
P
pah100 已提交
286 287 288
                    var Clazz = ComponentView.getClass(
                        componentType, componentModel.option.type
                    );
L
lang 已提交
289

290 291
                    if (Clazz) {
                        component = new Clazz();
L
lang 已提交
292
                        component.init(ecModel, this._extensionAPI);
L
lang 已提交
293
                        componentsMap[id] = component;
L
lang 已提交
294
                        componentsList.push(component);
L
lang 已提交
295 296

                        this._zr.add(component.group);
L
lang 已提交
297
                    }
298 299 300 301 302
                }
                component.__id = id;
                component.__keepAlive = true;
                // Used in rendering
                component.__model = componentModel;
L
lang 已提交
303 304 305 306 307 308
            }, this);

            for (var i = 0; i < componentsList.length;) {
                var component = componentsList[i];
                if (! component.__keepAlive) {
                    this._zr.remove(component.group);
L
lang 已提交
309
                    component.dispose(this._extensionAPI);
L
lang 已提交
310 311
                    componentsList.splice(i, 1);
                    delete componentsMap[component.__id];
L
lang 已提交
312 313
                }
                else {
L
lang 已提交
314
                    i++;
L
lang 已提交
315
                }
316
            }
L
lang 已提交
317 318
        },

L
lang 已提交
319 320 321 322 323 324
        /**
         * Processor data in each series
         *
         * @param {module:echarts/model/Global} ecModel
         * @private
         */
L
lang 已提交
325
        _processData: function (ecModel) {
L
lang 已提交
326
            zrUtil.each(dataProcessorFuncs, function (processor) {
L
lang 已提交
327 328
                processor(ecModel);
            });
L
lang 已提交
329 330
        },

L
lang 已提交
331 332 333 334 335 336 337 338
        /**
         * @private
         */
        _stackSeriesData: function (ecModel) {
            var stackedDataMap = {};
            ecModel.eachSeries(function (series) {
                var stack = series.get('stack');
                var data = series.getData();
L
lang 已提交
339
                if (stack && data.type === 'list') {
L
lang 已提交
340 341
                    var previousStack = stackedDataMap[stack];
                    if (previousStack) {
L
lang 已提交
342
                        data.stackedOn = previousStack;
L
lang 已提交
343 344 345 346 347 348
                    }
                    stackedDataMap[stack] = data;
                }
            });
        },

L
lang 已提交
349 350 351 352 353 354
        /**
         * Layout before each chart render there series after visual coding and data processing
         *
         * @param {module:echarts/model/Global} ecModel
         * @private
         */
P
pah100 已提交
355
        _doLayout: function (ecModel, event) {
L
Add pie  
lang 已提交
356
            var api = this._extensionAPI;
L
lang 已提交
357
            zrUtil.each(this._layouts, function (layout) {
L
Add pie  
lang 已提交
358
                layout.update(ecModel, api, event);
359 360
            });
            zrUtil.each(layoutFuncs, function (layout) {
L
Add pie  
lang 已提交
361
                layout(ecModel, api, event);
L
lang 已提交
362 363 364 365 366 367 368 369 370 371
            });
        },

        /**
         * Code visual infomation from data after data processing
         *
         * @param {module:echarts/model/Global} ecModel
         * @private
         */
        _doVisualCoding: function (ecModel) {
372 373 374 375
            zrUtil.each(VISUAL_CODING_STAGES, function (stage) {
                zrUtil.each(visualCodingFuncs[stage] || [], function (visualCoding) {
                    visualCoding(ecModel);
                });
L
lang 已提交
376 377 378 379 380 381
            });
        },

        /**
         * Render each chart and component
         */
P
pah100 已提交
382
        _doRender: function (ecModel, event) {
L
lang 已提交
383
            var api = this._extensionAPI;
L
lang 已提交
384
            // Render all components
L
lang 已提交
385
            zrUtil.each(this._componentsList, function (component) {
L
lang 已提交
386 387 388 389 390 391 392
                var componentModel = component.__model;
                component.render(componentModel, ecModel, api, event);

                var z = componentModel.get('z');
                var zlevel = componentModel.get('zlevel');
                // Set z and zlevel
                component.group.traverse(function (el) {
L
lang 已提交
393 394
                    z != null && (el.z = z);
                    zlevel != null && (el.zlevel = zlevel);
L
lang 已提交
395
                });
L
lang 已提交
396
            }, this);
P
pah100 已提交
397

L
lang 已提交
398
            zrUtil.each(this._chartsList, function (chart) {
399
                chart.__keepAlive = false;
L
lang 已提交
400
            }, this);
P
pah100 已提交
401

L
lang 已提交
402
            // Render all charts
L
lang 已提交
403
            ecModel.eachSeries(function (seriesModel, idx) {
P
pah100 已提交
404
                var id = seriesModel.uid;
L
lang 已提交
405
                var chart = this._chartsMap[id];
406
                chart.__keepAlive = true;
P
pah100 已提交
407
                chart.render(seriesModel, ecModel, api, event);
L
lang 已提交
408 409 410 411 412

                var z = seriesModel.get('z');
                var zlevel = seriesModel.get('zlevel');
                // Set z and zlevel
                chart.group.traverse(function (el) {
L
lang 已提交
413 414
                    z != null && (el.z = z);
                    zlevel != null && (el.zlevel = zlevel);
L
lang 已提交
415
                });
L
lang 已提交
416
            }, this);
P
pah100 已提交
417

418 419 420 421 422 423
            // Remove groups of charts
            zrUtil.each(this._chartsList, function (chart) {
                if (!chart.__keepAlive) {
                    chart.remove();
                }
            }, this);
L
lang 已提交
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
        },

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

            this.zr.dispose();
        }
    };


L
lang 已提交
439
    var dataProcessorFuncs = [];
L
lang 已提交
440

P
pah100 已提交
441 442
    var actions = [];

L
lang 已提交
443 444
    var layoutClasses = [];

445 446
    var layoutFuncs = [];

447
    var visualCodingFuncs = {};
L
lang 已提交
448

L
lang 已提交
449 450 451 452 453
    /**
     * @module echarts
     */
    var echarts = {

L
lang 已提交
454 455
        init: function (dom, theme, opts) {
            return new ECharts(dom, theme, opts);
L
lang 已提交
456 457
        },

L
lang 已提交
458 459 460 461 462 463
        /**
         * @param {Function}
         */
        registerProcessor: function (processorFunc) {
            if (zrUtil.indexOf(dataProcessorFuncs, processorFunc) < 0) {
                dataProcessorFuncs.push(processorFunc);
L
lang 已提交
464
            }
L
lang 已提交
465 466
        },

P
pah100 已提交
467
        /**
468 469 470 471 472 473 474
         * Usage:
         * registerAction('someAction', 'someEvent', function () { ... });
         * registerAction('someAction', function () { ... });
         *
         * @param {string} actionName
         * @param {string=} eventName Can be ignored
         * @param {Function=} action
P
pah100 已提交
475
         */
476 477 478 479 480
        registerAction: function (actionName, eventName, action) {
            if (typeof eventName === 'function') {
                action = eventName;
                eventName = null;
            }
P
pah100 已提交
481 482 483
            if (!actions[actionName]) {
                actions[actionName] = action;
            }
484 485
            // TODO
            // event
P
pah100 已提交
486 487
        },

L
lang 已提交
488 489 490 491
        /**
         * @param {string} type
         * @param {*} CoordinateSystem
         */
L
lang 已提交
492
        registerCoordinateSystem: function (type, CoordinateSystem) {
L
lang 已提交
493 494 495
            CoordinateSystemManager.register(type, CoordinateSystem);
        },

L
lang 已提交
496 497 498
        /**
         * @param {*} layout
         */
499
        registerLayout: function (layout, isFactory) {
L
tweak  
lang 已提交
500
            // PENDING All functions ?
501 502 503 504 505 506 507 508 509
            if (isFactory) {
                if (zrUtil.indexOf(layoutClasses, layout) < 0) {
                    layoutClasses.push(layout);
                }
            }
            else {
                if (zrUtil.indexOf(layoutFuncs, layout) < 0) {
                    layoutFuncs.push(layout);
                }
L
lang 已提交
510 511 512
            }
        },

L
lang 已提交
513 514 515 516
        /**
         * @param {string} stage
         * @param {Function} visualCodingFunc
         */
517 518 519 520 521 522
        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 已提交
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
        },

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

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

P
pah100 已提交
539 540 541 542 543 544 545
        /**
         * @param {Object} opts
         */
        extendSeriesModel: function (opts) {
            return SeriesModel.extend(opts);
        },

L
Update  
lang 已提交
546 547 548 549 550
        /**
         * @param {Object} opts
         */
        extendComponentView: function (opts) {
            return ComponentView.extend(opts);
L
lang 已提交
551
        }
L
lang 已提交
552 553
    };

554
    echarts.registerVisualCoding('echarts', require('./visual/defaultColor'));
L
lang 已提交
555

L
lang 已提交
556 557
    return echarts;
});