source: Dev/trunk/src/client/dojox/charting/DataSeries.js @ 532

Last change on this file since 532 was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

File size: 5.0 KB
Line 
1define(["dojo/_base/lang", "dojo/_base/declare", "dojo/_base/array", "dojo/_base/connect", "dojox/lang/functional"],
2        function(Lang, declare, ArrayUtil, connect, df){
3
4        return declare("dojox.charting.DataSeries", null, {
5                constructor: function(store, kwArgs, value){
6                        // summary:
7                        //              Series adapter for dojo.data stores.
8                        // store: Object
9                        //              A dojo.data store object.
10                        // kwArgs: Object
11                        //              A store-specific keyword parameters used for fetching items.
12                        //              See dojo/data/api/Read.fetch().
13                        // value: Function|Object|String
14                        //              Function, which takes a store, and an object handle, and
15                        //              produces an output possibly inspecting the store's item. Or
16                        //              a dictionary object, which tells what names to extract from
17                        //              an object and how to map them to an output. Or a string, which
18                        //              is a numeric field name to use for plotting. If undefined, null
19                        //              or empty string (the default), "value" field is extracted.
20                        this.store = store;
21                        this.kwArgs = kwArgs;
22       
23                        if(value){
24                                if(Lang.isFunction(value)){
25                                        this.value = value;
26                                }else if(Lang.isObject(value)){
27                                        this.value = Lang.hitch(this, "_dictValue",
28                                                df.keys(value), value);
29                                }else{
30                                        this.value = Lang.hitch(this, "_fieldValue", value);
31                                }
32                        }else{
33                                this.value = Lang.hitch(this, "_defaultValue");
34                        }
35       
36                        this.data = [];
37       
38                        this._events = [];
39       
40                        if(this.store.getFeatures()["dojo.data.api.Notification"]){
41                                this._events.push(
42                                        connect.connect(this.store, "onNew", this, "_onStoreNew"),
43                                        connect.connect(this.store, "onDelete", this, "_onStoreDelete"),
44                                        connect.connect(this.store, "onSet", this, "_onStoreSet")
45                                );
46                        }
47
48                        this._initialRendering = true;
49                        this.fetch();
50                },
51       
52                destroy: function(){
53                        // summary:
54                        //              Clean up before GC.
55                        ArrayUtil.forEach(this._events, connect.disconnect);
56                },
57       
58                setSeriesObject: function(series){
59                        // summary:
60                        //              Sets a dojox.charting.Series object we will be working with.
61                        // series: dojox.charting.Series
62                        //              Our interface to the chart.
63                        this.series = series;
64                },
65       
66                // value transformers
67       
68                _dictValue: function(keys, dict, store, item){
69                        var o = {};
70                        ArrayUtil.forEach(keys, function(key){
71                                o[key] = store.getValue(item, dict[key]);
72                        });
73                        return o;
74                },
75       
76                _fieldValue: function(field, store, item){
77                        return store.getValue(item, field);
78                },
79       
80                _defaultValue: function(store, item){
81                        return store.getValue(item, "value");
82                },
83       
84                // store fetch loop
85       
86                fetch: function(){
87                        // summary:
88                        //              Fetches data from the store and updates a chart.
89                        if(!this._inFlight){
90                                this._inFlight = true;
91                                var kwArgs = Lang.delegate(this.kwArgs);
92                                kwArgs.onComplete = Lang.hitch(this, "_onFetchComplete");
93                                kwArgs.onError = Lang.hitch(this, "onFetchError");
94                                this.store.fetch(kwArgs);
95                        }
96                },
97       
98                _onFetchComplete: function(items, request){
99                        this.items = items;
100                        this._buildItemMap();
101                        this.data = ArrayUtil.map(this.items, function(item){
102                                return this.value(this.store, item);
103                        }, this);
104                        this._pushDataChanges();
105                        this._inFlight = false;
106                },
107       
108                onFetchError: function(errorData, request){
109                        // summary:
110                        //              As stub to process fetch errors. Provide so user can attach to
111                        //              it with dojo.connect(). See dojo/data/api/Read fetch() for
112                        //              details: onError property.
113                        this._inFlight = false;
114                },
115       
116                _buildItemMap: function(){
117                        if(this.store.getFeatures()["dojo.data.api.Identity"]){
118                                var itemMap = {};
119                                ArrayUtil.forEach(this.items, function(item, index){
120                                        itemMap[this.store.getIdentity(item)] = index;
121                                }, this);
122                                this.itemMap = itemMap;
123                        }
124                },
125       
126                _pushDataChanges: function(){
127                        if(this.series){
128                                this.series.chart.updateSeries(this.series.name, this, this._initialRendering);
129                                this._initialRendering = false;
130                                this.series.chart.delayedRender();
131                        }
132                },
133       
134                // store notification handlers
135       
136                _onStoreNew: function(){
137                        // the only thing we can do is to re-fetch items
138                        this.fetch();
139                },
140       
141                _onStoreDelete: function(item){
142                        // we cannot do anything with deleted item, the only way is to compare
143                        // items for equality
144                        if(this.items){
145                                var flag = ArrayUtil.some(this.items, function(it, index){
146                                        if(it === item){
147                                                this.items.splice(index, 1);
148                                                this._buildItemMap();
149                                                this.data.splice(index, 1);
150                                                return true;
151                                        }
152                                        return false;
153                                }, this);
154                                if(flag){
155                                        this._pushDataChanges();
156                                }
157                        }
158                },
159       
160                _onStoreSet: function(item){
161                        if(this.itemMap){
162                                // we can use our handy item map, if the store supports Identity
163                                var id = this.store.getIdentity(item), index = this.itemMap[id];
164                                if(typeof index == "number"){
165                                        this.data[index] = this.value(this.store, this.items[index]);
166                                        this._pushDataChanges();
167                                }
168                        }else{
169                                // otherwise we have to rely on item's equality
170                                if(this.items){
171                                        var flag = ArrayUtil.some(this.items, function(it, index){
172                                                if(it === item){
173                                                        this.data[index] = this.value(this.store, it);
174                                                        return true;
175                                                }
176                                                return false;
177                                        }, this);
178                                        if(flag){
179                                                this._pushDataChanges();
180                                        }
181                                }
182                        }
183                }
184        });
185});
Note: See TracBrowser for help on using the repository browser.