echarts.js 6.3 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 11 12 13 14 15 16 17 18 19

    var zrender = require('zrender');

    var startupProcessorClasses = [];

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

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

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

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

L
lang 已提交
30 31 32 33 34 35 36 37
        this._theme = theme;

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

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

L
lang 已提交
38
        this._extensionAPI = new ExtensionAPI(this);
L
lang 已提交
39 40 41 42
    };

    ECharts.prototype = {

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

L
lang 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60
        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;
            });

            // Pending
            // optionModel as parent ?
            var globalState = new Model({}, option);
L
lang 已提交
61
            this._globalState = globalState;
L
lang 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91

            this._originalOption = option;

            this.updateImmediate();
        },

        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);
        },

        update: function () {

        },

        updateImmediate: function () {
            // TODO Performance
            var option = this._originalOption.clone();

L
lang 已提交
92
            this._processOption(option, this._globalState);
L
lang 已提交
93 94 95 96 97

            this._prepareComponents(option);

            this._prepareCharts(option);

L
lang 已提交
98
            this._doRender(option);
L
lang 已提交
99 100 101 102 103 104 105 106 107 108 109 110
        },

        _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 已提交
111
                        chart.init(this._extensionAPI);
L
lang 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
                        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 已提交
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
        _processOption: function (option, globalState) {
L
lang 已提交
164
            zrUtil.each(this._processors, function (processor) {
L
lang 已提交
165 166 167 168 169 170
                if (! processor.__inited__) {
                    processor.init(option, globalState);
                    processor.__inited__ = true;
                }
                processor.syncState(globalState);
                processor.process(option);
L
lang 已提交
171 172 173
            });
        },

L
lang 已提交
174 175
        _doRender: function (option) {
            var api = this._extensionAPI;
L
lang 已提交
176 177
            // Render all components
            zrUtil.each(this._components, function (component) {
L
lang 已提交
178
                component.render(option, api);
L
lang 已提交
179 180 181
            });
            // Render all charts
            zrUtil.each(this._charts, function (chart) {
L
lang 已提交
182
                chart.render(option, api);
L
lang 已提交
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
            });
        },

        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);
        },

        registerStartupProcessor: function (processorFactory) {}
    };


    echarts.registerProcessor(require('./processor/SeriesFilter'));

    return echarts;
});