echarts.js 6.6 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 12 13 14 15 16

    var zrender = require('zrender');

    /**
     * @module echarts~ECharts
     */
    var ECharts = function (dom, theme) {

L
lang 已提交
17
        this._zr = zrender.init(dom);
L
lang 已提交
18 19 20 21 22 23 24 25 26

        this._theme = theme;

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

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

L
lang 已提交
27
        this._extensionAPI = new ExtensionAPI(this);
L
lang 已提交
28

L
lang 已提交
29
        this._coordinateSystem = new CoordinateSystemManager();
L
lang 已提交
30 31 32 33

        this._layouts = zrUtil.map(layoutClasses, function (Layout) {
            return new Layout();
        });
L
lang 已提交
34 35 36 37
    };

    ECharts.prototype = {

L
lang 已提交
38 39 40 41
        getZr: function () {
            return this._zr;
        },

L
lang 已提交
42
        setOption: function (option, merge) {
L
lang 已提交
43
            option = zrUtil.clone(option, true);
L
lang 已提交
44

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

L
lang 已提交
47
            this._model = ecModel;
L
lang 已提交
48

L
lang 已提交
49
            this._prepareComponents(ecModel);
L
lang 已提交
50

L
lang 已提交
51
            this._prepareCharts(ecModel);
L
lang 已提交
52 53

            this.updateImmediately();
L
lang 已提交
54 55
        },

L
lang 已提交
56 57 58 59
        getCoordinateSystem: function (type, idx) {
            return this._coordinateSystem.get(type, idx);
        },

L
lang 已提交
60 61 62
        getWidth: function () {
            return this._zr.getWidth();
        },
L
lang 已提交
63

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

        update: function () {
L
lang 已提交
69 70
        },

L
lang 已提交
71
        updateImmediately: function () {
L
lang 已提交
72 73 74
            var ecModel = this._model;

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

L
lang 已提交
76
            this._processData(ecModel);
L
lang 已提交
77

L
lang 已提交
78
            this._coordinateSystem.update(ecModel, this._extensionAPI);
L
lang 已提交
79

L
lang 已提交
80 81 82
            this._doLayout(ecModel);

            this._doRender(ecModel);
L
lang 已提交
83 84
        },

L
lang 已提交
85
        resize: function () {
L
lang 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98
            var ecModel = this._model;

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

            this._doLayout(ecModel);

            this._doRender(ecModel);
        },

        _doLayout: function (model) {
            zrUtil.each(this._layouts, function (layout) {
                layout.run(model);
            });
L
lang 已提交
99 100
        },

L
lang 已提交
101
        _prepareCharts: function (ecModel) {
L
lang 已提交
102
            var chartUsedMap = {};
L
lang 已提交
103
            zrUtil.each(ecModel.get('series'), function (series, idx) {
L
lang 已提交
104 105 106 107 108 109 110
                var id = series.type + '_' + (series.name || idx);
                chartUsedMap[id] = true;

                var chart = this._chartsMap[id];
                if (! chart) {
                    chart = Chart.create(series);
                    if (chart) {
L
lang 已提交
111
                        chart.init(this._extensionAPI);
L
lang 已提交
112
                        this._chartsMap[id] = chart;
L
lang 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
                        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 已提交
136
        _prepareComponents: function (ecModel) {
L
lang 已提交
137 138 139 140
            Component.eachAvailableComponent(function (componentType) {
                var componentsMap = this._componentsMap;
                var componentsList = this._componentsList;

L
lang 已提交
141
                var componentOption = ecModel.get(componentType);
L
lang 已提交
142 143 144 145 146
                var component = componentsMap[componentType];
                if (componentOption) {
                    if (! component) {
                        // Create and add component
                        component = Component.create(componentType, componentOption);
L
lang 已提交
147
                        component.init(this._extensionAPI);
L
lang 已提交
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
                        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 已提交
163
        _processData: function (ecModel) {
L
lang 已提交
164
            zrUtil.each(processorList, function (processor) {
L
lang 已提交
165 166
                processor(ecModel);
            });
L
lang 已提交
167 168
        },

L
lang 已提交
169
        _doRender: function (optionModel, stateModel) {
L
lang 已提交
170
            var api = this._extensionAPI;
L
lang 已提交
171 172
            // Render all components
            zrUtil.each(this._components, function (component) {
L
lang 已提交
173 174
                component.render(optionModel, stateModel, api);
            }, this);
L
lang 已提交
175
            // Render all charts
L
lang 已提交
176 177 178 179
            optionModel.eachSeries(function (seriesModel, idx) {
                var id = seriesModel.get('type') + '_' + (seriesModel.get('name') || idx);
                var chart = this._chartsMap[id];
                var group = chart.render(seriesModel, optionModel, api);
L
lang 已提交
180
                this._zr.add(group);
L
lang 已提交
181 182 183
            }, this);
            // TODO
            // Remove group of unused chart
L
lang 已提交
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
        },

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

            this.zr.dispose();
        }
    };


L
lang 已提交
199 200 201 202
    var processorList = [];

    var layoutClasses = [];

L
lang 已提交
203 204 205 206 207 208 209 210 211
    /**
     * @module echarts
     */
    var echarts = {

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

L
lang 已提交
212 213 214 215
        registerProcessor: function (processor) {
            if (zrUtil.indexOf(processorList, processor) < 0) {
                processorList.push(processor);
            }
L
lang 已提交
216 217 218
        },

        registerCoordinateSystem: function (type, CoordinateSystem) {
L
lang 已提交
219 220 221 222 223 224 225 226 227 228
            CoordinateSystemManager.register(type, CoordinateSystem);
        },

        registerLayout: function (layout) {
            if (zrUtil.indexOf(layoutClasses, layout) < 0) {
                layoutClasses.push(layout);
            }
        },

        registerVisualCoding: function () {
L
lang 已提交
229 230

        }
L
lang 已提交
231 232 233 234
    };

    return echarts;
});