Subversion Repositories Applications.papyrus

Rev

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