echarts.js 8.0 KB
Newer Older
L
lang 已提交
1 2
define(function (require) {

L
lang 已提交
3
    var GlobalModel = require('./model/Global');
L
lang 已提交
4
    var zrUtil = require('zrender/core/util');
L
lang 已提交
5 6 7
    var Chart = require('./chart/ChartView');
    var Component = require('./component/ComponentView');
    var ExtensionAPI = require('./api/ExtensionAPI');
L
lang 已提交
8
    var CoordinateSystemManager = require('./CoordinateSystem');
L
lang 已提交
9 10 11

    var zrender = require('zrender');

L
lang 已提交
12 13 14 15 16 17
    /**
     * @inner
     */
    function getSeriesId(series, seriesIndex) {
        return series.type + '_' + (series.name ||  seriesIndex);
    }
L
lang 已提交
18 19 20 21 22
    /**
     * @module echarts~ECharts
     */
    var ECharts = function (dom, theme) {

L
lang 已提交
23
        this._zr = zrender.init(dom);
L
lang 已提交
24

L
lang 已提交
25
        this._theme = zrUtil.clone(theme);
L
lang 已提交
26 27 28 29 30 31 32

        this._chartsList = [];
        this._chartsMap = {};

        this._componentsList = [];
        this._componentsMap = {};

L
lang 已提交
33
        this._extensionAPI = new ExtensionAPI(this);
L
lang 已提交
34

L
lang 已提交
35
        this._coordinateSystem = new CoordinateSystemManager();
L
lang 已提交
36 37 38 39

        this._layouts = zrUtil.map(layoutClasses, function (Layout) {
            return new Layout();
        });
L
lang 已提交
40 41 42 43
    };

    ECharts.prototype = {

L
lang 已提交
44 45 46 47
        getZr: function () {
            return this._zr;
        },

L
lang 已提交
48
        setOption: function (option, merge) {
L
lang 已提交
49
            option = zrUtil.clone(option, true);
L
lang 已提交
50

L
lang 已提交
51
            var ecModel = new GlobalModel(option, null, this._theme);
L
lang 已提交
52

L
lang 已提交
53
            this._model = ecModel;
L
lang 已提交
54

L
lang 已提交
55
            this._prepareComponents(ecModel);
L
lang 已提交
56

L
lang 已提交
57
            this._prepareCharts(ecModel);
L
lang 已提交
58 59

            this.updateImmediately();
L
lang 已提交
60 61
        },

L
lang 已提交
62 63 64 65
        getCoordinateSystem: function (type, idx) {
            return this._coordinateSystem.get(type, idx);
        },

L
lang 已提交
66 67 68
        getWidth: function () {
            return this._zr.getWidth();
        },
L
lang 已提交
69

L
lang 已提交
70 71 72 73 74
        getHeight: function () {
            return this._zr.getHeight();
        },

        update: function () {
L
lang 已提交
75 76
        },

L
lang 已提交
77
        updateImmediately: function () {
L
lang 已提交
78 79 80
            var ecModel = this._model;

            ecModel.restore();
L
lang 已提交
81

L
lang 已提交
82
            this._processData(ecModel);
L
lang 已提交
83

L
lang 已提交
84
            this._coordinateSystem.update(ecModel, this._extensionAPI);
L
lang 已提交
85

L
lang 已提交
86 87
            this._doVisualCoding(ecModel);

L
lang 已提交
88 89 90
            this._doLayout(ecModel);

            this._doRender(ecModel);
L
lang 已提交
91 92
        },

L
lang 已提交
93
        resize: function () {
L
lang 已提交
94 95 96 97 98 99 100 101 102
            var ecModel = this._model;

            this._coordinateSystem.resize(ecModel, this._extensionAPI);

            this._doLayout(ecModel);

            this._doRender(ecModel);
        },

L
lang 已提交
103
        _prepareCharts: function (ecModel) {
L
lang 已提交
104
            var chartUsedMap = {};
L
lang 已提交
105
            zrUtil.each(ecModel.get('series'), function (series, idx) {
L
lang 已提交
106
                var id = getSeriesId(series, idx);
L
lang 已提交
107 108 109 110 111 112
                chartUsedMap[id] = true;

                var chart = this._chartsMap[id];
                if (! chart) {
                    chart = Chart.create(series);
                    if (chart) {
L
lang 已提交
113
                        chart.init(this._extensionAPI);
L
lang 已提交
114
                        this._chartsMap[id] = chart;
L
lang 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
                        this._chartsList.push(chart);
                    }
                    else {
                        // Error
                    }
                }

                chart.__id__ = id;
            }, this);

            for (var i = 0; i < this._chartsList.length;) {
                var chart = this._chartsList[i];
                if (! chartUsedMap[chart.__id__]) {
                    chart.dispose();
                    this._chartsList.splice(i, 1);
                    delete this._chartsMap[chart.__id__];
                }
                else {
                    i++;
                }
            };
        },

L
lang 已提交
138
        _prepareComponents: function (ecModel) {
L
lang 已提交
139 140 141 142
            Component.eachAvailableComponent(function (componentType) {
                var componentsMap = this._componentsMap;
                var componentsList = this._componentsList;

L
lang 已提交
143
                var componentOption = ecModel.get(componentType);
L
lang 已提交
144 145 146 147 148
                var component = componentsMap[componentType];
                if (componentOption) {
                    if (! component) {
                        // Create and add component
                        component = Component.create(componentType, componentOption);
L
lang 已提交
149
                        component.init(this._extensionAPI);
L
lang 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
                        componentsMap[componentType] = component;
                        componentsList.push(component);
                    }
                }
                else {
                    if (component) {
                        // Remove and dispose component
                        component.dispose();
                        delete componentsMap[componentType];
                        componentsList.splice(zrUtil.indexOf(componentsList, component));
                    }
                }
            }, this);
        },

L
lang 已提交
165 166 167 168 169 170
        /**
         * Processor data in each series
         *
         * @param {module:echarts/model/Global} ecModel
         * @private
         */
L
lang 已提交
171
        _processData: function (ecModel) {
L
lang 已提交
172
            zrUtil.each(dataProcessorFuncs, function (processor) {
L
lang 已提交
173 174
                processor(ecModel);
            });
L
lang 已提交
175 176
        },

L
lang 已提交
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
        /**
         * Layout before each chart render there series after visual coding and data processing
         *
         * @param {module:echarts/model/Global} ecModel
         * @private
         */
        _doLayout: function (ecModel) {
            zrUtil.each(this._layouts, function (layout) {
                layout.run(ecModel);
            });
        },

        /**
         * 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
         *
         */
        _doRender: function (ecModel, stateModel) {
L
lang 已提交
206
            var api = this._extensionAPI;
L
lang 已提交
207 208
            // Render all components
            zrUtil.each(this._components, function (component) {
L
lang 已提交
209
                component.render(ecModel, stateModel, api);
L
lang 已提交
210
            }, this);
L
lang 已提交
211
            // Render all charts
L
lang 已提交
212 213
            ecModel.eachSeries(function (seriesModel, idx) {
                var id = getSeriesId(seriesModel.option, idx);
L
lang 已提交
214
                var chart = this._chartsMap[id];
L
lang 已提交
215
                var group = chart.render(seriesModel, ecModel, api);
L
lang 已提交
216
                this._zr.add(group);
L
lang 已提交
217 218 219
            }, this);
            // TODO
            // Remove group of unused chart
L
lang 已提交
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
        },

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

            this.zr.dispose();
        }
    };


L
lang 已提交
235
    var dataProcessorFuncs = [];
L
lang 已提交
236 237 238

    var layoutClasses = [];

L
lang 已提交
239 240
    var visualCodingFuncs = [];

L
lang 已提交
241 242 243 244 245 246 247 248 249
    /**
     * @module echarts
     */
    var echarts = {

        init: function (dom, theme) {
            return new ECharts(dom, theme);
        },

L
lang 已提交
250 251 252 253 254 255
        /**
         * @param {Function}
         */
        registerProcessor: function (processorFunc) {
            if (zrUtil.indexOf(dataProcessorFuncs, processorFunc) < 0) {
                dataProcessorFuncs.push(processorFunc);
L
lang 已提交
256
            }
L
lang 已提交
257 258
        },

L
lang 已提交
259 260 261 262
        /**
         * @param {string} type
         * @param {*} CoordinateSystem
         */
L
lang 已提交
263
        registerCoordinateSystem: function (type, CoordinateSystem) {
L
lang 已提交
264 265 266
            CoordinateSystemManager.register(type, CoordinateSystem);
        },

L
lang 已提交
267 268 269
        /**
         * @param {*} layout
         */
L
lang 已提交
270 271 272 273 274 275
        registerLayout: function (layout) {
            if (zrUtil.indexOf(layoutClasses, layout) < 0) {
                layoutClasses.push(layout);
            }
        },

L
lang 已提交
276

L
lang 已提交
277 278
        registerVisualCoding: function (visualCodingFunc) {
            visualCodingFuncs.push(visualCodingFunc);
L
lang 已提交
279
        }
L
lang 已提交
280 281
    };

L
lang 已提交
282 283 284

    echarts.registerVisualCoding(require('./visual/defaultColor'));

L
lang 已提交
285 286
    return echarts;
});