summaryrefslogtreecommitdiff
path: root/js/dojo-1.6/dojox/charting/StoreSeries.js
blob: a1e4cf3f7b988d5882f7a5bac43a7d613f5b62d7 (plain)
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
/*
	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
*/


if(!dojo._hasResource["dojox.charting.StoreSeries"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
dojo._hasResource["dojox.charting.StoreSeries"] = true;
dojo.provide("dojox.charting.StoreSeries");

dojo.declare("dojox.charting.StoreSeries", null, {
	constructor: function(store, kwArgs, value){
		//	summary:
		//		Series adapter for dojo object stores (dojo.store).
		//	store: Object:
		//		A dojo object store.
		//	kwArgs: Object:
		//		A store-specific keyword parameters used for querying objects.
		//		See dojo.store docs
		//	value: Function|Object|String|Null:
		//		Function, which takes 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(typeof value == "function"){
				this.value = value;
			}else if(typeof value == "object"){
				this.value = function(object){
					var o = {};
					for(var key in value){
						o[key] = object[value[key]];
					}
					return o;
				};
			}else{
				this.value = function(object){
					return object[value];
				};
			}
		}else{
			this.value = function(object){
				return object.value;
			};
		}

		this.data = [];

		this.fetch();
	},

	destroy: function(){
		//	summary:
		//		Clean up before GC.
		if(this.observeHandle){
			this.observeHandle.dismiss();
		}
	},

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

	// store fetch loop

	fetch: function(){
		//	summary:
		//		Fetches data from the store and updates a chart.
		var objects = this.objects = [];
		var self = this;
		if(this.observeHandle){
			this.observeHandle.dismiss();
		}
		var results = this.store.query(this.kwArgs.query, this.kwArgs);
		dojo.when(results, function(objects){
			self.objects = objects;
			update();
		});
		if(results.observe){
			this.observeHandle = results.observe(update, true);
		}
		function update(){
			self.data = dojo.map(self.objects, function(object){
				return self.value(object, self.store);
			});
			self._pushDataChanges();
		}
	},

	_pushDataChanges: function(){
		if(this.series){
			this.series.chart.updateSeries(this.series.name, this);
			this.series.chart.delayedRender();
		}
	}

});

}