echarts.js 7.4 KB
Newer Older
L
lang 已提交
1 2 3 4 5 6 7 8
define(function (require) {

    var config = require('./config');
    var OptionModel = require('./model/Option');
    var Model = require('./model/Model');
    var zrUtil = require('zrender/core/util');
    var Chart = require('./chart/Chart');
    var Component = require('./component/Component');
L
lang 已提交
9
    var ExtensionAPI = require('./ExtensionAPI');
L
lang 已提交
10
    var CoordinateSystemManager = require('./CoordinateSystem');
L
lang 已提交
11 12 13 14 15 16 17 18 19 20

    var zrender = require('zrender');

    var startupProcessorClasses = [];

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

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

        theme = zrUtil.clone(theme || {});
        zrUtil.merge(theme, config);

L
lang 已提交
26 27
        // Create processors
        this._processors = zrUtil.map(startupProcessorClasses, function (Processor) {
L
lang 已提交
28 29 30
            var processor = new Processor();
            processor.init();
            return processor;
L
lang 已提交
31 32
        });

L
lang 已提交
33 34 35 36 37 38 39 40
        this._theme = theme;

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

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

L
lang 已提交
41
        this._extensionAPI = new ExtensionAPI(this);
L
lang 已提交
42 43 44 45 46 47

        // Pending
        // optionModel as parent ?
        var globalState = new Model({});
        this._state = globalState;

L
lang 已提交
48
        this._coordinateSystem = new CoordinateSystemManager();
L
lang 已提交
49 50 51 52
    };

    ECharts.prototype = {

L
lang 已提交
53 54 55 56
        getZr: function () {
            return this._zr;
        },

L
lang 已提交
57 58 59 60 61 62 63 64 65 66 67 68
        setOption: function (rawOption, merge) {
            rawOption = zrUtil.clone(rawOption);
            zrUtil.merge(rawOption, this._theme);

            var option = new OptionModel(rawOption);

            // Add series index
            option.eachSeries(function (series, seriesIndex) {
                series.seriesIndex = seriesIndex;
            });

            this._originalOption = option;
L
lang 已提交
69 70 71
            // Processed option is same with originalOption before processing
            // PENDING
            this._processedOption = option;
L
lang 已提交
72

L
lang 已提交
73 74 75 76 77 78 79 80 81
            this._prepareComponents(option);

            this._prepareCharts(option);

            zrUtil.each(this._processors, function (processor) {
                processor.optionChanged(option);
            });

            this.updateImmediately();
L
lang 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
        },

        addProcessor: function (processor, oneForEachType) {
            var processors = this._processors;
            if (zrUtil.indexOf(processors, processor) >= 0) {
                return;
            };
            if (oneForEachType) {
                if (zrUtil.filter(processors, function (exist) {
                    return exist.type === processor.type;
                }).length) {
                    return;
                }
            }

            processors.push(processor);
        },

L
lang 已提交
100 101 102 103
        getCoordinateSystem: function (type, idx) {
            return this._coordinateSystem.get(type, idx);
        },

L
lang 已提交
104 105 106 107
        update: function () {

        },

L
lang 已提交
108
        updateImmediately: function () {
L
lang 已提交
109
            var processedOption = this._processOption(this._originalOption);
L
lang 已提交
110

L
lang 已提交
111
            this._processedOption = processedOption;
L
lang 已提交
112

L
lang 已提交
113
            this._coordinateSystem.update(processedOption);
L
lang 已提交
114

L
lang 已提交
115
            this._doRender(processedOption);
L
lang 已提交
116 117
        },

L
lang 已提交
118
        resize: function () {
L
lang 已提交
119
            this._coordinateSystem.resize(this._processedOption, this._extensionAPI);
L
lang 已提交
120 121
        },

L
lang 已提交
122 123 124 125 126 127 128 129 130 131
        _prepareCharts: function (option) {
            var chartUsedMap = {};
            zrUtil.each(option.get('series'), function (series, idx) {
                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 已提交
132
                        chart.init(this._extensionAPI);
L
lang 已提交
133
                        this._chartsMap[id] = chart;
L
lang 已提交
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
                        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++;
                }
            };
        },

        _prepareComponents: function (option) {
            Component.eachAvailableComponent(function (componentType) {
                var componentsMap = this._componentsMap;
                var componentsList = this._componentsList;

                var componentOption = option.get(componentType);
                var component = componentsMap[componentType];
                if (componentOption) {
                    if (! component) {
                        // Create and add component
                        component = Component.create(componentType, componentOption);
L
lang 已提交
168
                        component.init(this._extensionAPI);
L
lang 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
                        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 已提交
184 185 186 187
        _processOption: function (option) {
            // TODO Performance
            option = option.clone();

L
lang 已提交
188
            zrUtil.each(this._processors, function (processor) {
L
lang 已提交
189
                processor.syncState(this._state);
L
lang 已提交
190
                processor.process(option);
L
lang 已提交
191 192 193
            }, this);

            return option;
L
lang 已提交
194 195
        },

L
lang 已提交
196
        _doRender: function (optionModel, stateModel) {
L
lang 已提交
197
            var api = this._extensionAPI;
L
lang 已提交
198 199
            // Render all components
            zrUtil.each(this._components, function (component) {
L
lang 已提交
200 201
                component.render(optionModel, stateModel, api);
            }, this);
L
lang 已提交
202
            // Render all charts
L
lang 已提交
203 204 205
            optionModel.eachSeries(function (series, idx) {
                var id = series.type + '_' + (series.name || idx);
            });
L
lang 已提交
206
            zrUtil.each(this._charts, function (chart) {
L
lang 已提交
207
                var group = chart.render(optionModel, api);
L
lang 已提交
208 209 210 211 212
                this.zr.addElement(group);
            }, this);

            // TODO
            // Remove group of unused chart
L
lang 已提交
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
        },

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

            this.zr.dispose();
        }
    };


    /**
     * @module echarts
     */
    var echarts = {

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

L
lang 已提交
237 238 239 240 241 242 243
        registerStartupProcessor: function (Processor) {

        },

        registerCoordinateSystem: function (type, CoordinateSystem) {

        }
L
lang 已提交
244 245 246
    };


L
lang 已提交
247 248 249
    echarts.registerStartupProcessor(require('./processor/AxisDefault'));

    echarts.registerStartupProcessor(require('./processor/SeriesFilter'));
L
lang 已提交
250 251 252

    return echarts;
});