echarts.js 6.9 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 69
        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 已提交
70 71 72 73 74 75 76 77 78
            this._prepareComponents(option);

            this._prepareCharts(option);

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

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

        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 已提交
97 98 99 100
        getCoordinateSystem: function (type, idx) {
            return this._coordinateSystem.get(type, idx);
        },

L
lang 已提交
101 102 103 104
        update: function () {

        },

L
lang 已提交
105
        updateImmediately: function () {
L
lang 已提交
106 107 108
            // TODO Performance
            var option = this._originalOption.clone();

L
lang 已提交
109 110
            this._coordinateSystem.update(option);

L
lang 已提交
111
            this._processOption(option, this._state);
L
lang 已提交
112

L
lang 已提交
113
            this._doRender(option);
L
lang 已提交
114 115
        },

L
lang 已提交
116 117 118 119
        resize: function () {

        },

L
lang 已提交
120 121 122 123 124 125 126 127 128 129
        _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 已提交
130
                        chart.init(this._extensionAPI);
L
lang 已提交
131 132 133 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
                        this._chartsMap[series.type] = chart;
                        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 已提交
166
                        component.init(this._extensionAPI);
L
lang 已提交
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
                        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 已提交
182
        _processOption: function (option, globalState) {
L
lang 已提交
183
            zrUtil.each(this._processors, function (processor) {
L
lang 已提交
184 185
                processor.syncState(globalState);
                processor.process(option);
L
lang 已提交
186 187 188
            });
        },

L
lang 已提交
189 190
        _doRender: function (option) {
            var api = this._extensionAPI;
L
lang 已提交
191 192
            // Render all components
            zrUtil.each(this._components, function (component) {
L
lang 已提交
193
                component.render(option, api);
L
lang 已提交
194 195 196
            });
            // Render all charts
            zrUtil.each(this._charts, function (chart) {
L
lang 已提交
197 198 199 200 201 202
                var group = chart.render(option, api);
                this.zr.addElement(group);
            }, this);

            // TODO
            // Remove group of unused chart
L
lang 已提交
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
        },

        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 已提交
227 228 229 230 231 232 233
        registerStartupProcessor: function (Processor) {

        },

        registerCoordinateSystem: function (type, CoordinateSystem) {

        }
L
lang 已提交
234 235 236
    };


L
lang 已提交
237 238 239
    echarts.registerStartupProcessor(require('./processor/AxisDefault'));

    echarts.registerStartupProcessor(require('./processor/SeriesFilter'));
L
lang 已提交
240 241 242

    return echarts;
});