echarts.js 6.0 KB
Newer Older
L
lang 已提交
1 2 3
define(function (require) {

    var config = require('./config');
L
lang 已提交
4
    var GlobalModel = require('./model/Global');
L
lang 已提交
5 6 7
    var zrUtil = require('zrender/core/util');
    var Chart = require('./chart/Chart');
    var Component = require('./component/Component');
L
lang 已提交
8
    var ExtensionAPI = require('./ExtensionAPI');
L
lang 已提交
9
    var CoordinateSystemManager = require('./CoordinateSystem');
L
lang 已提交
10 11 12

    var zrender = require('zrender');

L
lang 已提交
13
    var processors = [];
L
lang 已提交
14 15 16 17 18 19

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

L
lang 已提交
20
        this._zr = zrender.init(dom);
L
lang 已提交
21 22 23 24 25 26 27 28 29 30 31 32

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

        this._theme = theme;

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

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

L
lang 已提交
33
        this._extensionAPI = new ExtensionAPI(this);
L
lang 已提交
34

L
lang 已提交
35
        this._coordinateSystem = new CoordinateSystemManager();
L
lang 已提交
36 37 38 39
    };

    ECharts.prototype = {

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

L
lang 已提交
44 45 46 47
        setOption: function (rawOption, merge) {
            rawOption = zrUtil.clone(rawOption);
            zrUtil.merge(rawOption, this._theme);

L
lang 已提交
48
            var ecModel = new GlobalModel(rawOption);
L
lang 已提交
49 50

            // Add series index
L
lang 已提交
51
            ecModel.eachSeries(function (series, seriesIndex) {
L
lang 已提交
52 53 54
                series.seriesIndex = seriesIndex;
            });

L
lang 已提交
55
            this._model = ecModel;
L
lang 已提交
56

L
lang 已提交
57
            this._prepareComponents(ecModel);
L
lang 已提交
58

L
lang 已提交
59
            this._prepareCharts(ecModel);
L
lang 已提交
60 61

            this.updateImmediately();
L
lang 已提交
62 63
        },

L
lang 已提交
64 65 66 67
        getCoordinateSystem: function (type, idx) {
            return this._coordinateSystem.get(type, idx);
        },

L
lang 已提交
68 69 70 71
        update: function () {

        },

L
lang 已提交
72
        updateImmediately: function () {
L
lang 已提交
73
            this._model.restore();
L
lang 已提交
74

L
lang 已提交
75
            this._processData(this._model);
L
lang 已提交
76

L
lang 已提交
77
            this._coordinateSystem.update(this._model);
L
lang 已提交
78

L
lang 已提交
79
            this._doRender(this._model);
L
lang 已提交
80 81
        },

L
lang 已提交
82
        resize: function () {
L
lang 已提交
83
            this._coordinateSystem.resize(this._model, this._extensionAPI);
L
lang 已提交
84 85
        },

L
lang 已提交
86
        _prepareCharts: function (ecModel) {
L
lang 已提交
87
            var chartUsedMap = {};
L
lang 已提交
88
            zrUtil.each(ecModel.get('series'), function (series, idx) {
L
lang 已提交
89 90 91 92 93 94 95
                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 已提交
96
                        chart.init(this._extensionAPI);
L
lang 已提交
97
                        this._chartsMap[id] = chart;
L
lang 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
                        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 已提交
121
        _prepareComponents: function (ecModel) {
L
lang 已提交
122 123 124 125
            Component.eachAvailableComponent(function (componentType) {
                var componentsMap = this._componentsMap;
                var componentsList = this._componentsList;

L
lang 已提交
126
                var componentOption = ecModel.get(componentType);
L
lang 已提交
127 128 129 130 131
                var component = componentsMap[componentType];
                if (componentOption) {
                    if (! component) {
                        // Create and add component
                        component = Component.create(componentType, componentOption);
L
lang 已提交
132
                        component.init(this._extensionAPI);
L
lang 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
                        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 已提交
148 149 150 151
        _processData: function (ecModel) {
            zrUtil.each(processors, function (processor) {
                processor(ecModel);
            });
L
lang 已提交
152 153
        },

L
lang 已提交
154
        _doRender: function (optionModel, stateModel) {
L
lang 已提交
155
            var api = this._extensionAPI;
L
lang 已提交
156 157
            // Render all components
            zrUtil.each(this._components, function (component) {
L
lang 已提交
158 159
                component.render(optionModel, stateModel, api);
            }, this);
L
lang 已提交
160
            // Render all charts
L
lang 已提交
161 162 163 164
            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 已提交
165 166 167 168
                this.zr.addElement(group);
            }, this);
            // TODO
            // Remove group of unused chart
L
lang 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
        },

        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 已提交
193
        registreProcessor: function (Processor) {
L
lang 已提交
194 195 196 197 198 199

        },

        registerCoordinateSystem: function (type, CoordinateSystem) {

        }
L
lang 已提交
200 201 202
    };


L
lang 已提交
203
    echarts.registreProcessor(require('./processor/AxisDefault'));
L
lang 已提交
204

L
lang 已提交
205
    echarts.registreProcessor(require('./processor/SeriesFilter'));
L
lang 已提交
206 207 208

    return echarts;
});