Subversion Repositories Applications.papyrus

Rev

Rev 1318 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1318 alexandre_ 1
/*
2
	Copyright (c) 2004-2006, The Dojo Foundation
3
	All Rights Reserved.
4
 
5
	Licensed under the Academic Free License version 2.1 or above OR the
6
	modified BSD license. For more information on Dojo licensing, see:
7
 
8
		http://dojotoolkit.org/community/licensing.shtml
9
*/
10
 
1422 alexandre_ 11
 
12
 
1318 alexandre_ 13
dojo.provide("dojo.charting.Series");
14
dojo.require("dojo.lang.common");
15
dojo.require("dojo.charting.Plotters");
16
dojo.charting.Series = function (kwArgs) {
17
	var args = kwArgs || {length:1};
18
	this.dataSource = args.dataSource || null;
19
	this.bindings = {};
20
	this.color = args.color;
21
	this.label = args.label;
22
	if (args.bindings) {
23
		for (var p in args.bindings) {
24
			this.addBinding(p, args.bindings[p]);
25
		}
26
	}
27
};
28
dojo.extend(dojo.charting.Series, {bind:function (src, bindings) {
29
	this.dataSource = src;
30
	this.bindings = bindings;
31
}, addBinding:function (name, binding) {
32
	this.bindings[name] = binding;
33
}, evaluate:function (kwArgs) {
34
	var ret = [];
35
	var a = this.dataSource.getData();
36
	var l = a.length;
37
	var start = 0;
38
	var end = l;
39
	if (kwArgs) {
40
		if (kwArgs.between) {
41
			for (var i = 0; i < l; i++) {
42
				var fld = this.dataSource.getField(a[i], kwArgs.between.field);
43
				if (fld >= kwArgs.between.low && fld <= kwArgs.between.high) {
44
					var o = {src:a[i], series:this};
45
					for (var p in this.bindings) {
46
						o[p] = this.dataSource.getField(a[i], this.bindings[p]);
47
					}
48
					ret.push(o);
49
				}
50
			}
51
		} else {
52
			if (kwArgs.from || kwArgs.length) {
53
				if (kwArgs.from) {
54
					start = Math.max(kwArgs.from, 0);
55
					if (kwArgs.to) {
56
						end = Math.min(kwArgs.to, end);
57
					}
58
				} else {
59
					if (kwArgs.length < 0) {
60
						start = Math.max((end + length), 0);
61
					} else {
62
						end = Math.min((start + length), end);
63
					}
64
				}
65
				for (var i = start; i < end; i++) {
66
					var o = {src:a[i], series:this};
67
					for (var p in this.bindings) {
68
						o[p] = this.dataSource.getField(a[i], this.bindings[p]);
69
					}
70
					ret.push(o);
71
				}
72
			}
73
		}
74
	} else {
75
		for (var i = start; i < end; i++) {
76
			var o = {src:a[i], series:this};
77
			for (var p in this.bindings) {
78
				o[p] = this.dataSource.getField(a[i], this.bindings[p]);
79
			}
80
			ret.push(o);
81
		}
82
	}
83
	if (ret.length > 0 && typeof (ret[0].x) != "undefined") {
84
		ret.sort(function (a, b) {
85
			if (a.x > b.x) {
86
				return 1;
87
			}
88
			if (a.x < b.x) {
89
				return -1;
90
			}
91
			return 0;
92
		});
93
	}
94
	return ret;
95
}, trends:{createRange:function (values, len) {
96
	var idx = values.length - 1;
97
	var length = (len || values.length);
98
	return {"index":idx, "length":length, "start":Math.max(idx - length, 0)};
99
}, mean:function (values, len) {
100
	var range = this.createRange(values, len);
101
	if (range.index < 0) {
102
		return 0;
103
	}
104
	var total = 0;
105
	var count = 0;
106
	for (var i = range.index; i >= range.start; i--) {
107
		total += values[i].y;
108
		count++;
109
	}
110
	total /= Math.max(count, 1);
111
	return total;
112
}, variance:function (values, len) {
113
	var range = this.createRange(values, len);
114
	if (range.index < 0) {
115
		return 0;
116
	}
117
	var total = 0;
118
	var square = 0;
119
	var count = 0;
120
	for (var i = range.index; i >= range.start; i--) {
121
		total += values[i].y;
122
		square += Math.pow(values[i].y, 2);
123
		count++;
124
	}
125
	return (square / count) - Math.pow(total / count, 2);
126
}, standardDeviation:function (values, len) {
127
	return Math.sqrt(this.getVariance(values, len));
128
}, max:function (values, len) {
129
	var range = this.createRange(values, len);
130
	if (range.index < 0) {
131
		return 0;
132
	}
133
	var max = Number.MIN_VALUE;
134
	for (var i = range.index; i >= range.start; i--) {
135
		max = Math.max(values[i].y, max);
136
	}
137
	return max;
138
}, min:function (values, len) {
139
	var range = this.createRange(values, len);
140
	if (range.index < 0) {
141
		return 0;
142
	}
143
	var min = Number.MAX_VALUE;
144
	for (var i = range.index; i >= range.start; i--) {
145
		min = Math.min(values[i].y, min);
146
	}
147
	return min;
148
}, median:function (values, len) {
149
	var range = this.createRange(values, len);
150
	if (range.index < 0) {
151
		return 0;
152
	}
153
	var a = [];
154
	for (var i = range.index; i >= range.start; i--) {
155
		var b = false;
156
		for (var j = 0; j < a.length; j++) {
157
			if (values[i].y == a[j]) {
158
				b = true;
159
				break;
160
			}
161
		}
162
		if (!b) {
163
			a.push(values[i].y);
164
		}
165
	}
166
	a.sort();
167
	if (a.length > 0) {
168
		return a[Math.ceil(a.length / 2)];
169
	}
170
	return 0;
171
}, mode:function (values, len) {
172
	var range = this.createRange(values, len);
173
	if (range.index < 0) {
174
		return 0;
175
	}
176
	var o = {};
177
	var ret = 0;
178
	var median = Number.MIN_VALUE;
179
	for (var i = range.index; i >= range.start; i--) {
180
		if (!o[values[i].y]) {
181
			o[values[i].y] = 1;
182
		} else {
183
			o[values[i].y]++;
184
		}
185
	}
186
	for (var p in o) {
187
		if (median < o[p]) {
188
			median = o[p];
189
			ret = p;
190
		}
191
	}
192
	return ret;
193
}}});
194