Chart_wev8.js
17.4 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
/*
Copyright (c) 2004-2005, The Dojo Foundation
All Rights Reserved.
Licensed under the Academic Free License version 2.1 or above OR the
modified BSD license. For more information on Dojo licensing, see:
http://dojotoolkit.org/community/licensing.shtml
*/
dojo.provide("dojo.widget.svg.Chart");
dojo.require("dojo.widget.HtmlWidget");
dojo.require("dojo.widget.Chart");
dojo.require("dojo.math");
dojo.require("dojo.html");
dojo.require("dojo.svg");
dojo.require("dojo.graphics.color");
dojo.widget.svg.Chart=function(){
dojo.widget.Chart.call(this);
dojo.widget.HtmlWidget.call(this);
};
dojo.inherits(dojo.widget.svg.Chart, dojo.widget.HtmlWidget);
dojo.lang.extend(dojo.widget.svg.Chart, {
// widget props
templatePath:null,
templateCssPath:null,
// state
_isInitialized:false,
hasData:false,
// chart props
vectorNode:null,
plotArea:null,
dataGroup:null,
axisGroup:null,
properties:{
height:400, // defaults, will resize to the domNode.
width:600,
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
}
}
}
},
fillInTemplate:function(args,frag){
this.parseData();
this.initialize();
this.render();
},
parseData:function(){
},
initialize:function(){
// begin by grabbing the table, and reading it in.
var table=this.domNode.getElementsByTagName("table")[0];
if (!table) return;
var bRangeX=false;
var bRangeY=false;
// properties off the table
if (table.getAttribute("width")) this.properties.width=table.getAttribute("width");
if (table.getAttribute("height")) this.properties.height=table.getAttribute("height");
if (table.getAttribute("plotType")) this.properties.plotType=table.getAttribute("plotType");
if (table.getAttribute("padding")){
if (table.getAttribute("padding").indexOf(",") > -1)
var p=table.getAttribute("padding").split(",");
else var p=table.getAttribute("padding").split(" ");
if (p.length==1){
var pad=parseFloat(p[0]);
this.properties.padding.top=pad;
this.properties.padding.right=pad;
this.properties.padding.bottom=pad;
this.properties.padding.left=pad;
} else if(p.length==2){
var padV=parseFloat(p[0]);
var padH=parseFloat(p[1]);
this.properties.padding.top=padV;
this.properties.padding.right=padH;
this.properties.padding.bottom=padV;
this.properties.padding.left=padH;
} else if(p.length==4){
this.properties.padding.top=parseFloat(p[0]);
this.properties.padding.right=parseFloat(p[1]);
this.properties.padding.bottom=parseFloat(p[2]);
this.properties.padding.left=parseFloat(p[3]);
}
}
if (table.getAttribute("rangeX")){
var p=table.getAttribute("rangeX");
if (p.indexOf(",")>-1) p=p.split(",");
else p=p.split(" ");
this.properties.axes.x.range.min=parseFloat(p[0]);
this.properties.axes.x.range.max=parseFloat(p[1]);
bRangeX=true;
}
if (table.getAttribute("rangeY")){
var p=table.getAttribute("rangeY");
if (p.indexOf(",")>-1) p=p.split(",");
else p=p.split(" ");
this.properties.axes.y.range.min=parseFloat(p[0]);
this.properties.axes.y.range.max=parseFloat(p[1]);
bRangeY=true;
}
var thead=table.getElementsByTagName("thead")[0];
var tbody=table.getElementsByTagName("tbody")[0];
if(!(thead&&tbody)) dojo.raise("dojo.widget.Chart: supplied table must define a head and a body.");
// set up the series.
var columns=thead.getElementsByTagName("tr")[0].getElementsByTagName("th"); // should be <tr><..>
// assume column 0 == X
for (var i=1; i<columns.length; i++){
var key="column"+i;
var label=columns[i].innerHTML;
var plotType=columns[i].getAttribute("plotType")||"line";
var color=columns[i].getAttribute("color");
var ds=new dojo.widget.Chart.DataSeries(key,label,plotType,color);
this.series.push(ds);
}
// ok, get the values.
var rows=tbody.getElementsByTagName("tr");
var xMin=Number.MAX_VALUE,xMax=Number.MIN_VALUE;
var yMin=Number.MAX_VALUE,yMax=Number.MIN_VALUE;
var ignore = ["accesskey","align","bgcolor","class","colspan","height","id","nowrap","rowspan","style","tabindex","title","valign","width"];
for(var i=0; i<rows.length; i++){
var row=rows[i];
var cells=row.getElementsByTagName("td");
var x=Number.MIN_VALUE;
for (var j=0; j<cells.length; j++){
if (j==0){
x=parseFloat(cells[j].innerHTML);
xMin=Math.min(xMin, x);
xMax=Math.max(xMax, x);
} else {
var ds=this.series[j-1];
var y=parseFloat(cells[j].innerHTML);
yMin=Math.min(yMin,y);
yMax=Math.max(yMax,y);
var o={x:x, value:y};
var attrs=cells[j].attributes;
for(var k=0; k<attrs.length; k++){
var attr=attrs.item(k);
var bIgnore=false;
for (var l=0; l<ignore.length; l++){
if (attr.nodeName.toLowerCase()==ignore[l]){
bIgnore=true;
break;
}
}
if(!bIgnore) o[attr.nodeName]=attr.nodeValue;
}
ds.add(o);
}
}
}
// fix the axes
if(!bRangeX){
this.properties.axes.x.range={min:xMin, max:xMax};
}
if(!bRangeY){
this.properties.axes.y.range={min:yMin, max:yMax};
}
// where to plot the axes
if (table.getAttribute("axisAt")){
var p=table.getAttribute("axisAt");
if (p.indexOf(",")>-1) p=p.split(",");
else p=p.split(" ");
// x axis
if (!isNaN(parseFloat(p[0]))){
this.properties.axes.x.plotAt=parseFloat(p[0]);
} else if (p[0].toLowerCase()=="ymin"){
this.properties.axes.x.plotAt=this.properties.axes.y.range.min;
} else if (p[0].toLowerCase()=="ymax"){
this.properties.axes.x.plotAt=this.properties.axes.y.range.max;
}
// y axis
if (!isNaN(parseFloat(p[1]))){
this.properties.axes.y.plotAt=parseFloat(p[1]);
} else if (p[1].toLowerCase()=="xmin"){
this.properties.axes.y.plotAt=this.properties.axes.x.range.min;
} else if (p[1].toLowerCase()=="xmax"){
this.properties.axes.y.plotAt=this.properties.axes.x.range.max;
}
} else {
this.properties.axes.x.plotAt=this.properties.axes.y.range.min;
this.properties.axes.y.plotAt=this.properties.axes.x.range.min;
}
// table values should be populated, now pop it off.
this.domNode.removeChild(table);
// get the width and the height.
// this.properties.width=dojo.html.getInnerWidth(this.domNode);
// this.properties.height=dojo.html.getInnerHeight(this.domNode);
// ok, lets create the chart itself.
dojo.svg.g.suspend();
if(this.vectorNode) this.destroy();
this.vectorNode=document.createElementNS(dojo.svg.xmlns.svg, "svg");
this.vectorNode.setAttribute("width", this.properties.width);
this.vectorNode.setAttribute("height", this.properties.height);
// set up the clip path for the plot area.
var defs = document.createElementNS(dojo.svg.xmlns.svg, "defs");
var clip = document.createElementNS(dojo.svg.xmlns.svg, "clipPath");
clip.setAttribute("id","plotClip"+this.widgetId);
var rect = document.createElementNS(dojo.svg.xmlns.svg, "rect");
rect.setAttribute("x", this.properties.padding.left);
rect.setAttribute("y", this.properties.padding.top);
rect.setAttribute("width", this.properties.width-this.properties.padding.left-this.properties.padding.right);
rect.setAttribute("height", this.properties.height-this.properties.padding.bottom-this.properties.padding.bottom);
clip.appendChild(rect);
defs.appendChild(clip);
this.vectorNode.appendChild(defs);
// the plot background.
this.plotArea = document.createElementNS(dojo.svg.xmlns.svg, "g");
this.vectorNode.appendChild(this.plotArea);
var rect = document.createElementNS(dojo.svg.xmlns.svg, "rect");
rect.setAttribute("x", this.properties.padding.left);
rect.setAttribute("y", this.properties.padding.top);
rect.setAttribute("width", this.properties.width-this.properties.padding.left-this.properties.padding.right);
rect.setAttribute("height", this.properties.height-this.properties.padding.bottom-this.properties.padding.bottom);
rect.setAttribute("fill", "#fff");
this.plotArea.appendChild(rect);
// data group
this.dataGroup = document.createElementNS(dojo.svg.xmlns.svg, "g");
this.dataGroup.setAttribute("style","clip-path:url(#plotClip"+this.widgetId+");");
this.plotArea.appendChild(this.dataGroup);
// axis group
this.axisGroup = document.createElementNS(dojo.svg.xmlns.svg, "g");
this.plotArea.appendChild(this.axisGroup);
// x axis
var stroke=1;
var line = document.createElementNS(dojo.svg.xmlns.svg, "line");
var y=dojo.widget.svg.Chart.Plotter.getY(this.properties.axes.x.plotAt, this);
line.setAttribute("y1", y);
line.setAttribute("y2", y);
line.setAttribute("x1",this.properties.padding.left-stroke);
line.setAttribute("x2",this.properties.width-this.properties.padding.right);
line.setAttribute("style","stroke:#000;stroke-width:"+stroke+";");
this.axisGroup.appendChild(line);
// x axis units.
// (min and max)
var textSize=10;
var text = document.createElementNS(dojo.svg.xmlns.svg, "text");
text.setAttribute("x", this.properties.padding.left);
text.setAttribute("y", this.properties.height-this.properties.padding.bottom+textSize+2);
text.setAttribute("style", "text-anchor:middle;font-size:"+textSize+"px;fill:#000;");
text.appendChild(document.createTextNode(dojo.math.round(parseFloat(this.properties.axes.x.range.min)),2));
this.axisGroup.appendChild(text);
var text = document.createElementNS(dojo.svg.xmlns.svg, "text");
text.setAttribute("x", this.properties.width-this.properties.padding.right-(textSize/2));
text.setAttribute("y", this.properties.height-this.properties.padding.bottom+textSize+2);
text.setAttribute("style", "text-anchor:middle;font-size:"+textSize+"px;fill:#000;");
text.appendChild(document.createTextNode(dojo.math.round(parseFloat(this.properties.axes.x.range.max)),2));
this.axisGroup.appendChild(text);
// y axis
var line=document.createElementNS(dojo.svg.xmlns.svg, "line");
var x=dojo.widget.svg.Chart.Plotter.getX(this.properties.axes.y.plotAt, this);
line.setAttribute("x1", x);
line.setAttribute("x2", x);
line.setAttribute("y1", this.properties.padding.top);
line.setAttribute("y2", this.properties.height-this.properties.padding.bottom);
line.setAttribute("style", "stroke:#000;stroke-width:"+stroke+";");
this.axisGroup.appendChild(line);
// y axis units
var text = document.createElementNS(dojo.svg.xmlns.svg, "text");
text.setAttribute("x", this.properties.padding.left-4);
text.setAttribute("y", this.properties.height-this.properties.padding.bottom);
text.setAttribute("style", "text-anchor:end;font-size:"+textSize+"px;fill:#000;");
text.appendChild(document.createTextNode(dojo.math.round(parseFloat(this.properties.axes.y.range.min)),2));
this.axisGroup.appendChild(text);
var text = document.createElementNS(dojo.svg.xmlns.svg, "text");
text.setAttribute("x", this.properties.padding.left-4);
text.setAttribute("y", this.properties.padding.top+(textSize/2));
text.setAttribute("style", "text-anchor:end;font-size:"+textSize+"px;fill:#000;");
text.appendChild(document.createTextNode(dojo.math.round(parseFloat(this.properties.axes.y.range.max)),2));
this.axisGroup.appendChild(text);
this.domNode.appendChild(this.vectorNode);
dojo.svg.g.resume();
// this is last.
this.assignColors();
this._isInitialized=true;
},
destroy:function(){
while(this.domNode.childNodes.length>0){
this.domNode.removeChild(this.domNode.childNodes.item(0));
}
this.vectorNode=this.plotArea=this.dataGroup=this.axisGroup=null;
},
render:function(){
dojo.svg.g.suspend();
if (this.dataGroup){
while(this.dataGroup.childNodes.length>0){
this.dataGroup.removeChild(this.dataGroup.childNodes.item(0));
}
} else {
this.initialize();
}
// the remove/append is an attempt to streamline the rendering, it's totally optional
// var p=this.dataGroup.parentNode;
// p.removeChild(this.dataGroup);
for(var i=0; i<this.series.length; i++){
dojo.widget.svg.Chart.Plotter.plot(this.series[i], this);
}
// p.appendChild(this.dataGroup);
dojo.svg.g.resume();
}
});
dojo.widget.svg.Chart.PlotTypes = {
Bar:"bar",
Line:"line",
Scatter:"scatter",
Bubble:"bubble"
};
dojo.widget.svg.Chart.Plotter=new function(){
var _this=this;
var plotters = {};
var types=dojo.widget.svg.Chart.PlotTypes;
this.getX=function(value, chart){
var v=parseFloat(value);
var min=chart.properties.axes.x.range.min;
var max=chart.properties.axes.x.range.max;
var ofst=0-min;
min+=ofst; max+=ofst; v+=ofst;
var xmin=chart.properties.padding.left;
var xmax=chart.properties.width-chart.properties.padding.right;
var x=(v*((xmax-xmin)/max))+xmin;
return x;
};
this.getY=function(value, chart){
var v=parseFloat(value);
var max=chart.properties.axes.y.range.max;
var min=chart.properties.axes.y.range.min;
var ofst=0;
if(min<0)ofst+=Math.abs(min);
min+=ofst; max+=ofst; v+=ofst;
var ymin=chart.properties.height-chart.properties.padding.bottom;
var ymax=chart.properties.padding.top;
var y=(((ymin-ymax)/(max-min))*(max-v))+ymax;
return y;
};
this.addPlotter=function(name, func){
plotters[name]=func;
};
this.plot=function(series, chart){
if (series.values.length==0) return;
if (series.plotType && plotters[series.plotType]){
return plotters[series.plotType](series, chart);
}
else if (chart.plotType && plotters[chart.plotType]){
return plotters[chart.plotType](series, chart);
}
// else {
// return plotters[types.Bar](series, chart);
// }
};
// plotting
plotters[types.Bar]=function(series, chart){
var space=1;
var lastW = 0;
for (var i=0; i<series.values.length; i++){
var x=_this.getX(series.values[i].x, chart);
var w;
if (i==series.values.length-1){
w=lastW;
} else{
w=_this.getX(series.values[i+1].x, chart)-x-space;
lastW=w;
}
x-=(w/2);
var yA=_this.getY(chart.properties.axes.x.plotAt, chart);
var y=_this.getY(series.values[i].value, chart);
var h=Math.abs(yA-y);
if (parseFloat(series.values[i].value)<chart.properties.axes.x.plotAt){
var oy=yA;
yA=y;
y=oy;
}
var bar=document.createElementNS(dojo.svg.xmlns.svg, "rect");
bar.setAttribute("fill", series.color);
bar.setAttribute("title", series.label + ": " + series.values[i].value);
bar.setAttribute("stroke-width", "0");
bar.setAttribute("x", x);
bar.setAttribute("y", y);
bar.setAttribute("width", w);
bar.setAttribute("height", h);
bar.setAttribute("fill-opacity", "0.9");
chart.dataGroup.appendChild(bar);
}
};
plotters[types.Line]=function(series, chart){
var tension=3;
var line = document.createElementNS(dojo.svg.xmlns.svg, "path");
line.setAttribute("fill", "none");
line.setAttribute("stroke", series.color);
line.setAttribute("stroke-width", "1.5");
line.setAttribute("stroke-opacity", "0.85");
line.setAttribute("title", series.label);
chart.dataGroup.appendChild(line);
var path = [];
for (var i=0; i<series.values.length; i++){
var x = _this.getX(series.values[i].x, chart)
var y = _this.getY(series.values[i].value, chart);
var dx = chart.properties.padding.left+1;
var dy = chart.properties.height-chart.properties.padding.bottom;
if (i>0){
dx=x-_this.getX(series.values[i-1].x, chart);
dy=_this.getY(series.values[i-1].value, chart);
}
if (i==0) path.push("M");
else {
path.push("C");
var cx=x-(tension-1)*(dx/tension);
path.push(cx+","+dy);
cx=x-(dx/tension);
path.push(cx+","+y);
}
path.push(x+","+y);
}
line.setAttribute("d", path.join(" "));
};
plotters[types.Scatter]=function(series, chart){
var r=7;
for (var i=0; i<series.values.length; i++){
var x=_this.getX(series.values[i].x, chart);
var y=_this.getY(series.values[i].value, chart);
var point = document.createElementNS(dojo.svg.xmlns.svg, "path");
point.setAttribute("fill", series.color);
point.setAttribute("stroke-width", "0");
point.setAttribute("title", series.label + ": " + series.values[i].value);
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"
);
chart.dataGroup.appendChild(point);
}
};
plotters[types.Bubble]=function(series, chart){
// added param for series[n].value: size
var minR=1;
// do this off the x axis?
var min=chart.properties.axes.x.range.min;
var max=chart.properties.axes.x.range.max;
var ofst=0-min;
min+=ofst; max+=ofst; v+=ofst;
var xmin=chart.properties.padding.left;
var xmax=chart.properties.width-chart.properties.padding.right;
var factor=(max-min)/(xmax-xmin)*25;
for (var i=0; i<series.values.length; i++){
var size = series.values[i].size;
if (isNaN(parseFloat(size))) size=minR;
var point=document.createElementNS(dojo.svg.xmlns.svg, "circle");
point.setAttribute("stroke-width", 0);
point.setAttribute("fill", series.color);
point.setAttribute("fill-opacity", "0.8");
point.setAttribute("r", (parseFloat(size)*factor)/2);
point.setAttribute("cx", _this.getX(series.values[i].x, chart));
point.setAttribute("cy", _this.getY(series.values[i].value, chart));
point.setAttribute("title", series.label + ": " + series.values[i].value + " (" + size + ")");
chart.dataGroup.appendChild(point);
}
};
}();