1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
/*
Copyright (c) 2004-2011, The Dojo Foundation All Rights Reserved.
Available via Academic Free License >= 2.1 OR the modified BSD license.
see: http://dojotoolkit.org/license for details
*/
dojo._xdResourceLoaded(function(dojo, dijit, dojox){
return {depends: [["provide", "dojox.charting.DataSeries"],
["require", "dojox.lang.functional"]],
defineResource: function(dojo, dijit, dojox){if(!dojo._hasResource["dojox.charting.DataSeries"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
dojo._hasResource["dojox.charting.DataSeries"] = true;
dojo.provide("dojox.charting.DataSeries");
dojo.require("dojox.lang.functional");
dojo.declare("dojox.charting.DataSeries", null, {
constructor: function(store, kwArgs, value){
// summary:
// Series adapter for dojo.data stores.
// store: Object:
// A dojo.data store object.
// kwArgs: Object:
// A store-specific keyword parameters used for fetching items.
// See dojo.data.api.Read.fetch().
// value: Function|Object|String|Null:
// Function, which takes a store, and an object handle, and
// produces an output possibly inspecting the store's item. Or
// a dictionary object, which tells what names to extract from
// an object and how to map them to an output. Or a string, which
// is a numeric field name to use for plotting. If undefined, null
// or empty string (the default), "value" field is extracted.
this.store = store;
this.kwArgs = kwArgs;
if(value){
if(dojo.isFunction(value)){
this.value = value;
}else if(dojo.isObject(value)){
this.value = dojo.hitch(this, "_dictValue",
dojox.lang.functional.keys(value), value);
}else{
this.value = dojo.hitch(this, "_fieldValue", value);
}
}else{
this.value = dojo.hitch(this, "_defaultValue");
}
this.data = [];
this._events = [];
if(this.store.getFeatures()["dojo.data.api.Notification"]){
this._events.push(
dojo.connect(this.store, "onNew", this, "_onStoreNew"),
dojo.connect(this.store, "onDelete", this, "_onStoreDelete"),
dojo.connect(this.store, "onSet", this, "_onStoreSet")
);
}
this.fetch();
},
destroy: function(){
// summary:
// Clean up before GC.
dojo.forEach(this._events, dojo.disconnect);
},
setSeriesObject: function(series){
// summary:
// Sets a dojox.charting.Series object we will be working with.
// series: dojox.charting.Series:
// Our interface to the chart.
this.series = series;
},
// value transformers
_dictValue: function(keys, dict, store, item){
var o = {};
dojo.forEach(keys, function(key){
o[key] = store.getValue(item, dict[key]);
});
return o;
},
_fieldValue: function(field, store, item){
return store.getValue(item, field);
},
_defaultValue: function(store, item){
return store.getValue(item, "value");
},
// store fetch loop
fetch: function(){
// summary:
// Fetches data from the store and updates a chart.
if(!this._inFlight){
this._inFlight = true;
var kwArgs = dojo.delegate(this.kwArgs);
kwArgs.onComplete = dojo.hitch(this, "_onFetchComplete");
kwArgs.onError = dojo.hitch(this, "onFetchError");
this.store.fetch(kwArgs);
}
},
_onFetchComplete: function(items, request){
this.items = items;
this._buildItemMap();
this.data = dojo.map(this.items, function(item){
return this.value(this.store, item);
}, this);
this._pushDataChanges();
this._inFlight = false;
},
onFetchError: function(errorData, request){
// summary:
// As stub to process fetch errors. Provide so user can attach to
// it with dojo.connect(). See dojo.data.api.Read fetch() for
// details: onError property.
this._inFlight = false;
},
_buildItemMap: function(){
if(this.store.getFeatures()["dojo.data.api.Identity"]){
var itemMap = {};
dojo.forEach(this.items, function(item, index){
itemMap[this.store.getIdentity(item)] = index;
}, this);
this.itemMap = itemMap;
}
},
_pushDataChanges: function(){
if(this.series){
this.series.chart.updateSeries(this.series.name, this);
this.series.chart.delayedRender();
}
},
// store notification handlers
_onStoreNew: function(){
// the only thing we can do is to re-fetch items
this.fetch();
},
_onStoreDelete: function(item){
// we cannot do anything with deleted item, the only way is to compare
// items for equality
if(this.items){
var flag = dojo.some(this.items, function(it, index){
if(it === item){
this.items.splice(index, 1);
this._buildItemMap();
this.data.splice(index, 1);
return true;
}
return false;
}, this);
if(flag){
this._pushDataChanges();
}
}
},
_onStoreSet: function(item){
if(this.itemMap){
// we can use our handy item map, if the store supports Identity
var id = this.store.getIdentity(item), index = this.itemMap[id];
if(typeof index == "number"){
this.data[index] = this.value(this.store, this.items[index]);
this._pushDataChanges();
}
}else{
// otherwise we have to rely on item's equality
if(this.items){
var flag = dojo.some(this.items, function(it, index){
if(it === item){
this.data[index] = this.value(this.store, it);
return true;
}
return false;
}, this);
if(flag){
this._pushDataChanges();
}
}
}
}
});
}
}};});
|