Subversion Repositories Applications.papyrus

Rev

Rev 1372 | Go to most recent revision | 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.svg.Chart");
14
dojo.require("dojo.widget.HtmlWidget");
15
dojo.require("dojo.widget.Chart");
16
dojo.require("dojo.html.layout");
17
dojo.require("dojo.math");
18
dojo.require("dojo.svg");
19
dojo.require("dojo.gfx.color");
20
dojo.require("dojo.json");
21
dojo.widget.defineWidget("dojo.widget.svg.Chart", [dojo.widget.HtmlWidget, dojo.widget.Chart], function () {
22
	this.templatePath = null;
23
	this.templateCssPath = null;
24
	this._isInitialize = false;
25
	this.hasData = false;
26
	this.vectorNode = null;
27
	this.plotArea = null;
28
	this.dataGroup = null;
29
	this.axisGroup = null;
30
	this.properties = {height:0, width:0, defaultWidth:600, defaultHeight:400, plotType:null, padding:{top:10, bottom:2, left:60, right:30}, axes:{x:{plotAt:0, label:"", unitLabel:"", unitType:Number, nUnitsToShow:10, range:{min:0, max:200}}, y:{plotAt:0, label:"", unitLabel:"", unitType:Number, nUnitsToShow:10, range:{min:0, max:200}}}};
31
}, {parseProperties:function (node) {
32
	var bRangeX = false;
33
	var bRangeY = false;
34
	if (node.getAttribute("width")) {
35
		this.properties.width = node.getAttribute("width");
36
	}
37
	if (node.getAttribute("height")) {
38
		this.properties.height = node.getAttribute("height");
39
	}
40
	if (node.getAttribute("plotType")) {
41
		this.properties.plotType = node.getAttribute("plotType");
42
	}
43
	if (node.getAttribute("padding")) {
44
		if (node.getAttribute("padding").indexOf(",") > -1) {
45
			var p = node.getAttribute("padding").split(",");
46
		} else {
47
			var p = node.getAttribute("padding").split(" ");
48
		}
49
		if (p.length == 1) {
50
			var pad = parseFloat(p[0]);
51
			this.properties.padding.top = pad;
52
			this.properties.padding.right = pad;
53
			this.properties.padding.bottom = pad;
54
			this.properties.padding.left = pad;
55
		} else {
56
			if (p.length == 2) {
57
				var padV = parseFloat(p[0]);
58
				var padH = parseFloat(p[1]);
59
				this.properties.padding.top = padV;
60
				this.properties.padding.right = padH;
61
				this.properties.padding.bottom = padV;
62
				this.properties.padding.left = padH;
63
			} else {
64
				if (p.length == 4) {
65
					this.properties.padding.top = parseFloat(p[0]);
66
					this.properties.padding.right = parseFloat(p[1]);
67
					this.properties.padding.bottom = parseFloat(p[2]);
68
					this.properties.padding.left = parseFloat(p[3]);
69
				}
70
			}
71
		}
72
	}
73
	if (node.getAttribute("rangeX")) {
74
		var p = node.getAttribute("rangeX");
75
		if (p.indexOf(",") > -1) {
76
			p = p.split(",");
77
		} else {
78
			p = p.split(" ");
79
		}
80
		this.properties.axes.x.range.min = parseFloat(p[0]);
81
		this.properties.axes.x.range.max = parseFloat(p[1]);
82
		bRangeX = true;
83
	}
84
	if (node.getAttribute("rangeY")) {
85
		var p = node.getAttribute("rangeY");
86
		if (p.indexOf(",") > -1) {
87
			p = p.split(",");
88
		} else {
89
			p = p.split(" ");
90
		}
91
		this.properties.axes.y.range.min = parseFloat(p[0]);
92
		this.properties.axes.y.range.max = parseFloat(p[1]);
93
		bRangeY = true;
94
	}
95
	return {rangeX:bRangeX, rangeY:bRangeY};
96
}, setAxesPlot:function (table) {
97
	if (table.getAttribute("axisAt")) {
98
		var p = table.getAttribute("axisAt");
99
		if (p.indexOf(",") > -1) {
100
			p = p.split(",");
101
		} else {
102
			p = p.split(" ");
103
		}
104
		if (!isNaN(parseFloat(p[0]))) {
105
			this.properties.axes.x.plotAt = parseFloat(p[0]);
106
		} else {
107
			if (p[0].toLowerCase() == "ymin") {
108
				this.properties.axes.x.plotAt = this.properties.axes.y.range.min;
109
			} else {
110
				if (p[0].toLowerCase() == "ymax") {
111
					this.properties.axes.x.plotAt = this.properties.axes.y.range.max;
112
				}
113
			}
114
		}
115
		if (!isNaN(parseFloat(p[1]))) {
116
			this.properties.axes.y.plotAt = parseFloat(p[1]);
117
		} else {
118
			if (p[1].toLowerCase() == "xmin") {
119
				this.properties.axes.y.plotAt = this.properties.axes.x.range.min;
120
			} else {
121
				if (p[1].toLowerCase() == "xmax") {
122
					this.properties.axes.y.plotAt = this.properties.axes.x.range.max;
123
				}
124
			}
125
		}
126
	} else {
127
		this.properties.axes.x.plotAt = this.properties.axes.y.range.min;
128
		this.properties.axes.y.plotAt = this.properties.axes.x.range.min;
129
	}
130
}, drawVectorNode:function () {
131
	dojo.svg.g.suspend();
132
	if (this.vectorNode) {
133
		this.destroy();
134
	}
135
	this.vectorNode = document.createElementNS(dojo.svg.xmlns.svg, "svg");
136
	this.vectorNode.setAttribute("width", this.properties.width);
137
	this.vectorNode.setAttribute("height", this.properties.height);
138
	dojo.svg.g.resume();
139
}, drawPlotArea:function () {
140
	dojo.svg.g.suspend();
141
	if (this.plotArea) {
142
		this.plotArea.parentNode.removeChild(this.plotArea);
143
		this.plotArea = null;
144
	}
145
	var defs = document.createElementNS(dojo.svg.xmlns.svg, "defs");
146
	var clip = document.createElementNS(dojo.svg.xmlns.svg, "clipPath");
147
	clip.setAttribute("id", "plotClip" + this.widgetId);
148
	var rect = document.createElementNS(dojo.svg.xmlns.svg, "rect");
149
	rect.setAttribute("x", this.properties.padding.left);
150
	rect.setAttribute("y", this.properties.padding.top);
151
	rect.setAttribute("width", this.properties.width - this.properties.padding.left - this.properties.padding.right);
152
	rect.setAttribute("height", this.properties.height - this.properties.padding.top - this.properties.padding.bottom);
153
	clip.appendChild(rect);
154
	defs.appendChild(clip);
155
	this.vectorNode.appendChild(defs);
156
	this.plotArea = document.createElementNS(dojo.svg.xmlns.svg, "g");
157
	this.vectorNode.appendChild(this.plotArea);
158
	var rect = document.createElementNS(dojo.svg.xmlns.svg, "rect");
159
	rect.setAttribute("x", this.properties.padding.left);
160
	rect.setAttribute("y", this.properties.padding.top);
161
	rect.setAttribute("width", this.properties.width - this.properties.padding.left - this.properties.padding.right);
162
	rect.setAttribute("height", this.properties.height - this.properties.padding.top - this.properties.padding.bottom);
163
	rect.setAttribute("fill", "#fff");
164
	this.plotArea.appendChild(rect);
165
	dojo.svg.g.resume();
166
}, drawDataGroup:function () {
167
	dojo.svg.g.suspend();
168
	if (this.dataGroup) {
169
		this.dataGroup.parentNode.removeChild(this.dataGroup);
170
		this.dataGroup = null;
171
	}
172
	this.dataGroup = document.createElementNS(dojo.svg.xmlns.svg, "g");
173
	this.dataGroup.setAttribute("style", "clip-path:url(#plotClip" + this.widgetId + ");");
174
	this.plotArea.appendChild(this.dataGroup);
175
	dojo.svg.g.resume();
176
}, drawAxes:function () {
177
	dojo.svg.g.suspend();
178
	if (this.axisGroup) {
179
		this.axisGroup.parentNode.removeChild(this.axisGroup);
180
		this.axisGroup = null;
181
	}
182
	this.axisGroup = document.createElementNS(dojo.svg.xmlns.svg, "g");
183
	this.plotArea.appendChild(this.axisGroup);
184
	var stroke = 1;
185
	var line = document.createElementNS(dojo.svg.xmlns.svg, "line");
186
	var y = dojo.widget.svg.Chart.Plotter.getY(this.properties.axes.x.plotAt, this);
187
	line.setAttribute("y1", y);
188
	line.setAttribute("y2", y);
189
	line.setAttribute("x1", this.properties.padding.left - stroke);
190
	line.setAttribute("x2", this.properties.width - this.properties.padding.right);
191
	line.setAttribute("style", "stroke:#000;stroke-width:" + stroke + ";");
192
	this.axisGroup.appendChild(line);
193
	var textSize = 10;
194
	var text = document.createElementNS(dojo.svg.xmlns.svg, "text");
195
	text.setAttribute("x", this.properties.padding.left);
196
	text.setAttribute("y", this.properties.height - this.properties.padding.bottom + textSize + 2);
197
	text.setAttribute("style", "text-anchor:middle;font-size:" + textSize + "px;fill:#000;");
198
	text.appendChild(document.createTextNode(dojo.math.round(parseFloat(this.properties.axes.x.range.min), 2)));
199
	this.axisGroup.appendChild(text);
200
	var text = document.createElementNS(dojo.svg.xmlns.svg, "text");
201
	text.setAttribute("x", this.properties.width - this.properties.padding.right - (textSize / 2));
202
	text.setAttribute("y", this.properties.height - this.properties.padding.bottom + textSize + 2);
203
	text.setAttribute("style", "text-anchor:middle;font-size:" + textSize + "px;fill:#000;");
204
	text.appendChild(document.createTextNode(dojo.math.round(parseFloat(this.properties.axes.x.range.max), 2)));
205
	this.axisGroup.appendChild(text);
206
	var line = document.createElementNS(dojo.svg.xmlns.svg, "line");
207
	var x = dojo.widget.svg.Chart.Plotter.getX(this.properties.axes.y.plotAt, this);
208
	line.setAttribute("x1", x);
209
	line.setAttribute("x2", x);
210
	line.setAttribute("y1", this.properties.padding.top);
211
	line.setAttribute("y2", this.properties.height - this.properties.padding.bottom);
212
	line.setAttribute("style", "stroke:#000;stroke-width:" + stroke + ";");
213
	this.axisGroup.appendChild(line);
214
	var text = document.createElementNS(dojo.svg.xmlns.svg, "text");
215
	text.setAttribute("x", this.properties.padding.left - 4);
216
	text.setAttribute("y", this.properties.height - this.properties.padding.bottom);
217
	text.setAttribute("style", "text-anchor:end;font-size:" + textSize + "px;fill:#000;");
218
	text.appendChild(document.createTextNode(dojo.math.round(parseFloat(this.properties.axes.y.range.min), 2)));
219
	this.axisGroup.appendChild(text);
220
	var text = document.createElementNS(dojo.svg.xmlns.svg, "text");
221
	text.setAttribute("x", this.properties.padding.left - 4);
222
	text.setAttribute("y", this.properties.padding.top + (textSize / 2));
223
	text.setAttribute("style", "text-anchor:end;font-size:" + textSize + "px;fill:#000;");
224
	text.appendChild(document.createTextNode(dojo.math.round(parseFloat(this.properties.axes.y.range.max), 2)));
225
	this.axisGroup.appendChild(text);
226
	dojo.svg.g.resume();
227
}, init:function () {
228
	if (!this.properties.width || !this.properties.height) {
229
		var box = dojo.html.getContentBox(this.domNode);
230
		if (!this.properties.width) {
231
			this.properties.width = (box.width < 32) ? this.properties.defaultWidth : box.width;
232
		}
233
		if (!this.properties.height) {
234
			this.properties.height = (box.height < 32) ? this.properties.defaultHeight : box.height;
235
		}
236
	}
237
	this.drawVectorNode();
238
	this.drawPlotArea();
239
	this.drawDataGroup();
240
	this.drawAxes();
241
	this.domNode.appendChild(this.vectorNode);
242
	this.assignColors();
243
	this._isInitialized = true;
244
}, destroy:function () {
245
	while (this.domNode.childNodes.length > 0) {
246
		this.domNode.removeChild(this.domNode.childNodes.item(0));
247
	}
248
	this.vectorNode = this.plotArea = this.dataGroup = this.axisGroup = null;
249
}, render:function () {
250
	dojo.svg.g.suspend();
251
	if (this.dataGroup) {
252
		while (this.dataGroup.childNodes.length > 0) {
253
			this.dataGroup.removeChild(this.dataGroup.childNodes.item(0));
254
		}
255
	} else {
256
		this.init();
257
	}
258
	for (var i = 0; i < this.series.length; i++) {
259
		dojo.widget.svg.Chart.Plotter.plot(this.series[i], this);
260
	}
261
	dojo.svg.g.resume();
262
}, postCreate:function () {
263
	var table = this.domNode.getElementsByTagName("table")[0];
264
	if (table) {
265
		var ranges = this.parseProperties(table);
266
		var bRangeX = false;
267
		var bRangeY = false;
268
		var axisValues = this.parseData(table);
269
		if (!bRangeX) {
270
			this.properties.axes.x.range = {min:axisValues.x.min, max:axisValues.x.max};
271
		}
272
		if (!bRangeY) {
273
			this.properties.axes.y.range = {min:axisValues.y.min, max:axisValues.y.max};
274
		}
275
		this.setAxesPlot(table);
276
		this.domNode.removeChild(table);
277
	}
278
	if (this.series.length > 0) {
279
		this.render();
280
	}
281
}});
282
dojo.widget.svg.Chart.Plotter = new function () {
283
	var self = this;
284
	var plotters = {};
285
	var types = dojo.widget.Chart.PlotTypes;
286
	this.getX = function (value, chart) {
287
		var v = parseFloat(value);
288
		var min = chart.properties.axes.x.range.min;
289
		var max = chart.properties.axes.x.range.max;
290
		var ofst = 0 - min;
291
		min += ofst;
292
		max += ofst;
293
		v += ofst;
294
		var xmin = chart.properties.padding.left;
295
		var xmax = chart.properties.width - chart.properties.padding.right;
296
		var x = (v * ((xmax - xmin) / max)) + xmin;
297
		return x;
298
	};
299
	this.getY = function (value, chart) {
300
		var v = parseFloat(value);
301
		var max = chart.properties.axes.y.range.max;
302
		var min = chart.properties.axes.y.range.min;
303
		var ofst = 0;
304
		if (min < 0) {
305
			ofst += Math.abs(min);
306
		}
307
		min += ofst;
308
		max += ofst;
309
		v += ofst;
310
		var ymin = chart.properties.height - chart.properties.padding.bottom;
311
		var ymax = chart.properties.padding.top;
312
		var y = (((ymin - ymax) / (max - min)) * (max - v)) + ymax;
313
		return y;
314
	};
315
	this.addPlotter = function (name, func) {
316
		plotters[name] = func;
317
	};
318
	this.plot = function (series, chart) {
319
		if (series.values.length == 0) {
320
			return;
321
		}
322
		if (series.plotType && plotters[series.plotType]) {
323
			return plotters[series.plotType](series, chart);
324
		} else {
325
			if (chart.plotType && plotters[chart.plotType]) {
326
				return plotters[chart.plotType](series, chart);
327
			}
328
		}
329
	};
330
	plotters["bar"] = function (series, chart) {
331
		var space = 1;
332
		var lastW = 0;
333
		for (var i = 0; i < series.values.length; i++) {
334
			var x = self.getX(series.values[i].x, chart);
335
			var w;
336
			if (i == series.values.length - 1) {
337
				w = lastW;
338
			} else {
339
				w = self.getX(series.values[i + 1].x, chart) - x - space;
340
				lastW = w;
341
			}
342
			x -= (w / 2);
343
			var yA = self.getY(chart.properties.axes.x.plotAt, chart);
344
			var y = self.getY(series.values[i].value, chart);
345
			var h = Math.abs(yA - y);
346
			if (parseFloat(series.values[i].value) < chart.properties.axes.x.plotAt) {
347
				var oy = yA;
348
				yA = y;
349
				y = oy;
350
			}
351
			var bar = document.createElementNS(dojo.svg.xmlns.svg, "rect");
352
			bar.setAttribute("fill", series.color);
353
			bar.setAttribute("title", series.label + ": " + series.values[i].value);
354
			bar.setAttribute("stroke-width", "0");
355
			bar.setAttribute("x", x);
356
			bar.setAttribute("y", y);
357
			bar.setAttribute("width", w);
358
			bar.setAttribute("height", h);
359
			bar.setAttribute("fill-opacity", "0.9");
360
			chart.dataGroup.appendChild(bar);
361
		}
362
	};
363
	plotters["line"] = function (series, chart) {
364
		var tension = 1.5;
365
		var line = document.createElementNS(dojo.svg.xmlns.svg, "path");
366
		line.setAttribute("fill", "none");
367
		line.setAttribute("stroke", series.color);
368
		line.setAttribute("stroke-width", "2");
369
		line.setAttribute("stroke-opacity", "0.85");
370
		line.setAttribute("title", series.label);
371
		chart.dataGroup.appendChild(line);
372
		var path = [];
373
		for (var i = 0; i < series.values.length; i++) {
374
			var x = self.getX(series.values[i].x, chart);
375
			var y = self.getY(series.values[i].value, chart);
376
			var dx = chart.properties.padding.left + 1;
377
			var dy = chart.properties.height - chart.properties.padding.bottom;
378
			if (i > 0) {
379
				dx = x - self.getX(series.values[i - 1].x, chart);
380
				dy = self.getY(series.values[i - 1].value, chart);
381
			}
382
			if (i == 0) {
383
				path.push("M");
384
			} else {
385
				path.push("C");
386
				var cx = x - (tension - 1) * (dx / tension);
387
				path.push(cx + "," + dy);
388
				cx = x - (dx / tension);
389
				path.push(cx + "," + y);
390
			}
391
			path.push(x + "," + y);
392
		}
393
		line.setAttribute("d", path.join(" "));
394
	};
395
	plotters["area"] = function (series, chart) {
396
		var tension = 1.5;
397
		var line = document.createElementNS(dojo.svg.xmlns.svg, "path");
398
		line.setAttribute("fill", series.color);
399
		line.setAttribute("fill-opacity", "0.4");
400
		line.setAttribute("stroke", series.color);
401
		line.setAttribute("stroke-width", "1");
402
		line.setAttribute("stroke-opacity", "0.8");
403
		line.setAttribute("title", series.label);
404
		chart.dataGroup.appendChild(line);
405
		var path = [];
406
		for (var i = 0; i < series.values.length; i++) {
407
			var x = self.getX(series.values[i].x, chart);
408
			var y = self.getY(series.values[i].value, chart);
409
			var dx = chart.properties.padding.left + 1;
410
			var dy = chart.properties.height - chart.properties.padding.bottom;
411
			if (i > 0) {
412
				dx = x - self.getX(series.values[i - 1].x, chart);
413
				dy = self.getY(series.values[i - 1].value, chart);
414
			}
415
			if (i == 0) {
416
				path.push("M");
417
			} else {
418
				path.push("C");
419
				var cx = x - (tension - 1) * (dx / tension);
420
				path.push(cx + "," + dy);
421
				cx = x - (dx / tension);
422
				path.push(cx + "," + y);
423
			}
424
			path.push(x + "," + y);
425
		}
426
		path.push("L");
427
		path.push(x + "," + self.getY(0, chart));
428
		path.push("L");
429
		path.push(self.getX(0, chart) + "," + self.getY(0, chart));
430
		path.push("Z");
431
		line.setAttribute("d", path.join(" "));
432
	}, plotters["scatter"] = function (series, chart) {
433
		var r = 7;
434
		for (var i = 0; i < series.values.length; i++) {
435
			var x = self.getX(series.values[i].x, chart);
436
			var y = self.getY(series.values[i].value, chart);
437
			var point = document.createElementNS(dojo.svg.xmlns.svg, "path");
438
			point.setAttribute("fill", series.color);
439
			point.setAttribute("stroke-width", "0");
440
			point.setAttribute("title", series.label + ": " + series.values[i].value);
441
			point.setAttribute("d", "M " + x + "," + (y - r) + " " + "Q " + x + "," + y + " " + (x + r) + "," + y + " " + "Q " + x + "," + y + " " + x + "," + (y + r) + " " + "Q " + x + "," + y + " " + (x - r) + "," + y + " " + "Q " + x + "," + y + " " + x + "," + (y - r) + " " + "Z");
442
			chart.dataGroup.appendChild(point);
443
		}
444
	};
445
	plotters["bubble"] = function (series, chart) {
446
		var minR = 1;
447
		var min = chart.properties.axes.x.range.min;
448
		var max = chart.properties.axes.x.range.max;
449
		var ofst = 0 - min;
450
		min += ofst;
451
		max += ofst;
452
		var xmin = chart.properties.padding.left;
453
		var xmax = chart.properties.width - chart.properties.padding.right;
454
		var factor = (max - min) / (xmax - xmin) * 25;
455
		for (var i = 0; i < series.values.length; i++) {
456
			var size = series.values[i].size;
457
			if (isNaN(parseFloat(size))) {
458
				size = minR;
459
			}
460
			var point = document.createElementNS(dojo.svg.xmlns.svg, "circle");
461
			point.setAttribute("stroke-width", 0);
462
			point.setAttribute("fill", series.color);
463
			point.setAttribute("fill-opacity", "0.8");
464
			point.setAttribute("r", (parseFloat(size) * factor) / 2);
465
			point.setAttribute("cx", self.getX(series.values[i].x, chart));
466
			point.setAttribute("cy", self.getY(series.values[i].value, chart));
467
			point.setAttribute("title", series.label + ": " + series.values[i].value + " (" + size + ")");
468
			chart.dataGroup.appendChild(point);
469
		}
470
	};
471
}();
472