echarts.js 6.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 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
        // Create processors
        this._processors = zrUtil.map(startupProcessorClasses, function (Processor) {
L
lang 已提交
27 28 29
            var processor = new Processor();
            processor.init();
            return processor;
L
lang 已提交
30 31
        });

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

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

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

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

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

L
lang 已提交
47 48 49 50
    };

    ECharts.prototype = {

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

L
lang 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67
        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 已提交
68 69 70 71 72 73 74 75 76
            this._prepareComponents(option);

            this._prepareCharts(option);

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

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

        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 () {

        },

L
lang 已提交
99
        updateImmediately: function () {
L
lang 已提交
100 101 102
            // TODO Performance
            var option = this._originalOption.clone();

L
lang 已提交
103
            this._processOption(option, this._state);
L
lang 已提交
104

L
lang 已提交
105
            this._doRender(option);
L
lang 已提交
106 107 108 109 110 111 112 113 114 115 116 117
        },

        _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 已提交
118
                        chart.init(this._extensionAPI);
L
lang 已提交
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 147 148 149 150 151 152 153
                        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 已提交
154
                        component.init(this._extensionAPI);
L
lang 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
                        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 已提交
170
        _processOption: function (option, globalState) {
L
lang 已提交
171
            zrUtil.each(this._processors, function (processor) {
L
lang 已提交
172 173
                processor.syncState(globalState);
                processor.process(option);
L
lang 已提交
174 175 176
            });
        },

L
lang 已提交
177 178
        _doRender: function (option) {
            var api = this._extensionAPI;
L
lang 已提交
179 180
            // Render all components
            zrUtil.each(this._components, function (component) {
L
lang 已提交
181
                component.render(option, api);
L
lang 已提交
182 183 184
            });
            // Render all charts
            zrUtil.each(this._charts, function (chart) {
L
lang 已提交
185 186 187 188 189 190
                var group = chart.render(option, api);
                this.zr.addElement(group);
            }, this);

            // TODO
            // Remove group of unused chart
L
lang 已提交
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 216 217 218 219 220 221 222
        },

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