Subversion Repositories Applications.papyrus

Rev

Rev 1422 | 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.widget.Chart");
14
dojo.require("dojo.widget.*");
15
dojo.require("dojo.gfx.color");
16
dojo.require("dojo.gfx.color.hsl");
17
dojo.declare("dojo.widget.Chart", null, function () {
18
	this.series = [];
19
}, {isContainer:false, assignColors:function () {
20
	var hue = 30;
21
	var sat = 120;
22
	var lum = 120;
23
	var steps = Math.round(330 / this.series.length);
24
	for (var i = 0; i < this.series.length; i++) {
25
		var c = dojo.gfx.color.hsl2rgb(hue, sat, lum);
26
		if (!this.series[i].color) {
27
			this.series[i].color = dojo.gfx.color.rgb2hex(c[0], c[1], c[2]);
28
		}
29
		hue += steps;
30
	}
31
}, parseData:function (table) {
32
	var thead = table.getElementsByTagName("thead")[0];
33
	var tbody = table.getElementsByTagName("tbody")[0];
34
	if (!(thead && tbody)) {
35
		dojo.raise("dojo.widget.Chart: supplied table must define a head and a body.");
36
	}
37
	var columns = thead.getElementsByTagName("tr")[0].getElementsByTagName("th");
38
	for (var i = 1; i < columns.length; i++) {
39
		var key = "column" + i;
40
		var label = columns[i].innerHTML;
41
		var plotType = columns[i].getAttribute("plotType") || "line";
42
		var color = columns[i].getAttribute("color");
43
		var ds = new dojo.widget.Chart.DataSeries(key, label, plotType, color);
44
		this.series.push(ds);
45
	}
46
	var rows = tbody.rows;
47
	var xMin = Number.MAX_VALUE, xMax = Number.MIN_VALUE;
48
	var yMin = Number.MAX_VALUE, yMax = Number.MIN_VALUE;
49
	var ignore = ["accesskey", "align", "bgcolor", "class", "colspan", "height", "id", "nowrap", "rowspan", "style", "tabindex", "title", "valign", "width"];
50
	for (var i = 0; i < rows.length; i++) {
51
		var row = rows[i];
52
		var cells = row.cells;
53
		var x = Number.MIN_VALUE;
54
		for (var j = 0; j < cells.length; j++) {
55
			if (j == 0) {
56
				x = parseFloat(cells[j].innerHTML);
57
				xMin = Math.min(xMin, x);
58
				xMax = Math.max(xMax, x);
59
			} else {
60
				var ds = this.series[j - 1];
61
				var y = parseFloat(cells[j].innerHTML);
62
				yMin = Math.min(yMin, y);
63
				yMax = Math.max(yMax, y);
64
				var o = {x:x, value:y};
65
				var attrs = cells[j].attributes;
66
				for (var k = 0; k < attrs.length; k++) {
67
					var attr = attrs.item(k);
68
					var bIgnore = false;
69
					for (var l = 0; l < ignore.length; l++) {
70
						if (attr.nodeName.toLowerCase() == ignore[l]) {
71
							bIgnore = true;
72
							break;
73
						}
74
					}
75
					if (!bIgnore) {
76
						o[attr.nodeName] = attr.nodeValue;
77
					}
78
				}
79
				ds.add(o);
80
			}
81
		}
82
	}
83
	return {x:{min:xMin, max:xMax}, y:{min:yMin, max:yMax}};
84
}});
85
dojo.declare("dojo.widget.Chart.DataSeries", null, function (key, label, plotType, color) {
86
	this.id = "DataSeries" + dojo.widget.Chart.DataSeries.count++;
87
	this.key = key;
88
	this.label = label || this.id;
89
	this.plotType = plotType || "line";
90
	this.color = color;
91
	this.values = [];
92
}, {add:function (v) {
93
	if (v.x == null || v.value == null) {
94
		dojo.raise("dojo.widget.Chart.DataSeries.add: v must have both an 'x' and 'value' property.");
95
	}
96
	this.values.push(v);
97
}, clear:function () {
98
	this.values = [];
99
}, createRange:function (len) {
100
	var idx = this.values.length - 1;
101
	var length = (len || this.values.length);
102
	return {"index":idx, "length":length, "start":Math.max(idx - length, 0)};
103
}, getMean:function (len) {
104
	var range = this.createRange(len);
105
	if (range.index < 0) {
106
		return 0;
107
	}
108
	var t = 0;
109
	var c = 0;
110
	for (var i = range.index; i >= range.start; i--) {
111
		var n = parseFloat(this.values[i].value);
112
		if (!isNaN(n)) {
113
			t += n;
114
			c++;
115
		}
116
	}
117
	t /= Math.max(c, 1);
118
	return t;
119
}, getMovingAverage:function (len) {
120
	var range = this.createRange(len);
121
	if (range.index < 0) {
122
		return 0;
123
	}
124
	var t = 0;
125
	var c = 0;
126
	for (var i = range.index; i >= range.start; i--) {
127
		var n = parseFloat(this.values[i].value);
128
		if (!isNaN(n)) {
129
			t += n;
130
			c++;
131
		}
132
	}
133
	t /= Math.max(c, 1);
134
	return t;
135
}, getVariance:function (len) {
136
	var range = this.createRange(len);
137
	if (range.index < 0) {
138
		return 0;
139
	}
140
	var t = 0;
141
	var s = 0;
142
	var c = 0;
143
	for (var i = range.index; i >= range.start; i--) {
144
		var n = parseFloat(this.values[i].value);
145
		if (!isNaN(n)) {
146
			t += n;
147
			s += Math.pow(n, 2);
148
			c++;
149
		}
150
	}
151
	return (s / c) - Math.pow(t / c, 2);
152
}, getStandardDeviation:function (len) {
153
	return Math.sqrt(this.getVariance(len));
154
}, getMax:function (len) {
155
	var range = this.createRange(len);
156
	if (range.index < 0) {
157
		return 0;
158
	}
159
	var t = 0;
160
	for (var i = range.index; i >= range.start; i--) {
161
		var n = parseFloat(this.values[i].value);
162
		if (!isNaN(n)) {
163
			t = Math.max(n, t);
164
		}
165
	}
166
	return t;
167
}, getMin:function (len) {
168
	var range = this.createRange(len);
169
	if (range.index < 0) {
170
		return 0;
171
	}
172
	var t = 0;
173
	for (var i = range.index; i >= range.start; i--) {
174
		var n = parseFloat(this.values[i].value);
175
		if (!isNaN(n)) {
176
			t = Math.min(n, t);
177
		}
178
	}
179
	return t;
180
}, getMedian:function (len) {
181
	var range = this.createRange(len);
182
	if (range.index < 0) {
183
		return 0;
184
	}
185
	var a = [];
186
	for (var i = range.index; i >= range.start; i--) {
187
		var n = parseFloat(this.values[i].value);
188
		if (!isNaN(n)) {
189
			var b = false;
190
			for (var j = 0; j < a.length && !b; j++) {
191
				if (n == a[j]) {
192
					b = true;
193
				}
194
			}
195
			if (!b) {
196
				a.push(n);
197
			}
198
		}
199
	}
200
	a.sort();
201
	if (a.length > 0) {
202
		return a[Math.ceil(a.length / 2)];
203
	}
204
	return 0;
205
}, getMode:function (len) {
206
	var range = this.createRange(len);
207
	if (range.index < 0) {
208
		return 0;
209
	}
210
	var o = {};
211
	var ret = 0;
212
	var m = 0;
213
	for (var i = range.index; i >= range.start; i--) {
214
		var n = parseFloat(this.values[i].value);
215
		if (!isNaN(n)) {
216
			if (!o[this.values[i].value]) {
217
				o[this.values[i].value] = 1;
218
			} else {
219
				o[this.values[i].value]++;
220
			}
221
		}
222
	}
223
	for (var p in o) {
224
		if (m < o[p]) {
225
			m = o[p];
226
			ret = p;
227
		}
228
	}
229
	return parseFloat(ret);
230
}});
231
dojo.requireIf(dojo.render.svg.capable, "dojo.widget.svg.Chart");
232
dojo.requireIf(!dojo.render.svg.capable && dojo.render.vml.capable, "dojo.widget.vml.Chart");
233