Debian Codegraph @

dojox/charting/plot2d/Spider.js

raw symbols SCIP
1 define(["dojo/_base/lang", "dojo/_base/declare", "dojo/_base/connect", "dojo/_base/array",
2 "dojo/dom-geometry", "dojo/_base/fx", "dojo/fx", "dojo/sniff",
3 "./Base", "./_PlotEvents", "./common", "../axis2d/common",
4 "dojox/gfx", "dojox/gfx/matrix", "dojox/gfx/fx", "dojox/lang/functional",
5 "dojox/lang/utils", "dojo/fx/easing"],
6 function(lang, declare, hub, arr, domGeom, baseFx, coreFx, has,
7 Base, PlotEvents, dc, da, g, m, gfxfx, df, du, easing){
8
9 var FUDGE_FACTOR = 0.2; // use to overlap fans
10
11 var Spider = declare("dojox.charting.plot2d.Spider", [Base, PlotEvents], {
12 // summary:
13 // The plot that represents a typical Spider chart.
14 defaultParams: {
15 labels: true,
16 ticks: false,
17 fixed: true,
18 precision: 1,
19 labelOffset: -10,
20 labelStyle: "default", // default/rows/auto
21 htmlLabels: true, // use HTML to draw labels
22 startAngle: -90, // start angle for slices in degrees
23 divisions: 3, // radius tick count
24 axisColor: "", // spider axis color
25 axisWidth: 0, // spider axis stroke width
26 spiderColor: "", // spider web color
27 spiderWidth: 0, // spider web stroke width
28 seriesWidth: 0, // plot border with
29 seriesFillAlpha: 0.2, // plot fill alpha
30 spiderOrigin: 0.16,
31 markerSize: 3, // radius of plot vertex (px)
32 spiderType: "polygon", //"circle"
33 animationType: easing.backOut,
34 animate: null,
35 axisTickFont: "",
36 axisTickFontColor: "",
37 axisFont: "",
38 axisFontColor: ""
39 },
40 optionalParams: {
41 radius: 0,
42 font: "",
43 fontColor: ""
44 },
45
46 constructor: function(chart, kwArgs){
47 // summary:
48 // Create a Spider plot.
49 // chart: dojox/charting/Chart
50 // The chart this plot belongs to.
51 // kwArgs: dojox.charting.plot2d.__DefaultCtorArgs?
52 // An optional keyword arguments object to help define this plot's parameters.
53 this.opt = lang.clone(this.defaultParams);
54 du.updateWithObject(this.opt, kwArgs);
55 du.updateWithPattern(this.opt, kwArgs, this.optionalParams);
56 this.dyn = [];
57 this.datas = {};
58 this.labelKey = [];
59 this.oldSeriePoints = {};
60 this.animate = this.opt.animate === null ? {} : this.opt.animate;
61 this.animations = {};
62 },
63 clear: function(){
64 // summary:
65 // Clear out all of the information tied to this plot.
66 // returns: dojox/charting/plot2d/Spider
67 // A reference to this plot for functional chaining.
68 this.inherited(arguments);
69 this.dyn = [];
70 this.axes = [];
71 this.datas = {};
72 this.labelKey = [];
73 this.oldSeriePoints = {};
74 this.animations = {};
75 return this; // dojox/charting/plot2d/Spider
76 },
77 setAxis: function(axis){
78 // summary:
79 // Optionally set axis min and max property.
80 // returns: dojox/charting/plot2d/Spider
81 // The reference to this plot for functional chaining.
82
83 // override the computed min/max with provided values if any
84 if(axis){
85 if(axis.opt.min != undefined){
86 this.datas[axis.name].min = axis.opt.min;
87 }
88 if(axis.opt.max != undefined){
89 this.datas[axis.name].max = axis.opt.max;
90 }
91 }
92 return this; // dojox/charting/plot2d/Spider
93 },
94 addSeries: function(run){
95 // summary:
96 // Add a data series to this plot.
97 // run: dojox.charting.Series
98 // The series to be added.
99 // returns: dojox/charting/plot2d/Base
100 // A reference to this plot for functional chaining.
101 this.series.push(run);
102 var key;
103 for(key in run.data){
104 var val = run.data[key],
105 data = this.datas[key];
106 if(data){
107 data.vlist.push(val);
108 data.min = Math.min(data.min, val);
109 data.max = Math.max(data.max, val);
110 }else{
111 var axisKey = "__"+key;
112 this.axes.push(axisKey);
113 this[axisKey] = key;
114 this.datas[key] = {min: val, max: val, vlist: [val]};
115 }
116 }
117 if(this.labelKey.length <= 0){
118 for(key in run.data){
119 this.labelKey.push(key);
120 }
121 }
122 return this; // dojox.charting.plot2d.Base
123 },
124 getSeriesStats: function(){
125 // summary:
126 // Calculate the min/max on all attached series in both directions.
127 // returns: Object
128 // {hmin, hmax, vmin, vmax} min/max in both directions.
129 return dc.collectSimpleStats(this.series, function(v){ return v === null; }); // Object
130 },
131 render: function(dim, offsets){
132 // summary:
133 // Render the plot on the chart.
134 // dim: Object
135 // An object of the form { width, height }.
136 // offsets: Object
137 // An object of the form { l, r, t, b }.
138 // returns: dojox/charting/plot2d/Spider
139 // A reference to this plot for functional chaining.
140 if(!this.dirty){ return this; }
141 this.dirty = false;
142 this.cleanGroup();
143 var s = this.group, t = this.chart.theme;
144 this.resetEvents();
145
146 if(!this.series || !this.series.length){
147 return this;
148 }
149
150 // calculate the geometry
151 var o = this.opt, ta = t.axis,
152 rx = (dim.width - offsets.l - offsets.r) / 2,
153 ry = (dim.height - offsets.t - offsets.b) / 2,
154 r = Math.min(rx, ry),
155 axisTickFont = o.font || (ta.majorTick && ta.majorTick.font) || (ta.tick && ta.tick.font) || "normal normal normal 7pt Tahoma",
156 axisFont = o.axisFont || (ta.tick && ta.tick.titleFont) || "normal normal normal 11pt Tahoma",
157 axisTickFontColor = o.axisTickFontColor || (ta.majorTick && ta.majorTick.fontColor) || (ta.tick && ta.tick.fontColor) || "silver",
158 axisFontColor = o.axisFontColor || (ta.tick && ta.tick.titleFontColor) || "black",
159 axisColor = o.axisColor || (ta.tick && ta.tick.axisColor) || "silver",
160 spiderColor = o.spiderColor || (ta.tick && ta.tick.spiderColor) || "silver",
161 axisWidth = o.axisWidth || (ta.stroke && ta.stroke.width) || 2,
162 spiderWidth = o.spiderWidth || (ta.stroke && ta.stroke.width) || 2,
163 seriesWidth = o.seriesWidth || (ta.stroke && ta.stroke.width) || 2,
164 asize = g.normalizedLength(g.splitFontString(axisFont).size),
165 startAngle = m._degToRad(o.startAngle),
166 start = startAngle, labels, shift, labelR,
167 outerPoints, innerPoints, divisionPoints, divisionRadius, labelPoints,
168 ro = o.spiderOrigin, dv = o.divisions >= 3 ? o.divisions : 3, ms = o.markerSize,
169 spt = o.spiderType, at = o.animationType, lboffset = o.labelOffset < -10 ? o.labelOffset : -10,
170 axisExtra = 0.2,
171 i, j, point, len, fontWidth, render, serieEntry, run, data, min, max, distance;
172
173 if(o.labels){
174 labels = arr.map(this.series, function(s){
175 return s.name;
176 }, this);
177 shift = df.foldl1(df.map(labels, function(label){
178 var font = t.series.font;
179 return g._base._getTextBox(label, {
180 font: font
181 }).w;
182 }, this), "Math.max(a, b)") / 2;
183 r = Math.min(rx - 2 * shift, ry - asize) + lboffset;
184 labelR = r - lboffset;
185 }
186 if("radius" in o){
187 r = o.radius;
188 labelR = r - lboffset;
189 }
190 r /= (1+axisExtra);
191 var circle = {
192 cx: offsets.l + rx,
193 cy: offsets.t + ry,
194 r: r
195 };
196
197 for(var i = 0; i < this.series.length; i++){
198 serieEntry = this.series[i];
199 if(!this.dirty && !serieEntry.dirty){
200 t.skip();
201 continue;
202 }
203 serieEntry.cleanGroup();
204 run = serieEntry.data;
205 if(run !== null){
206 len = this._getObjectLength(run);
207 //construct connect points
208 if(!outerPoints || outerPoints.length <= 0){
209 outerPoints = [], innerPoints = [], labelPoints = [];
210 this._buildPoints(outerPoints, len, circle, r, start, true, dim);
211 this._buildPoints(innerPoints, len, circle, r*ro, start, true, dim);
212 this._buildPoints(labelPoints, len, circle, labelR, start, false, dim);
213 if(dv > 2){
214 divisionPoints = [], divisionRadius = [];
215 for (j = 0; j < dv - 2; j++){
216 divisionPoints[j] = [];
217 this._buildPoints(divisionPoints[j], len, circle, r*(ro + (1-ro)*(j+1)/(dv-1)), start, true, dim);
218 divisionRadius[j] = r*(ro + (1-ro)*(j+1)/(dv-1));
219 }
220 }
221 }
222 }
223 }
224
225 //draw Spider
226 //axis
227 var axisGroup = s.createGroup(), axisStroke = {color: axisColor, width: axisWidth},
228 spiderStroke = {color: spiderColor, width: spiderWidth};
229 for (j = outerPoints.length - 1; j >= 0; --j){
230 point = outerPoints[j];
231 var st = {
232 x: point.x + (point.x - circle.cx) * axisExtra,
233 y: point.y + (point.y - circle.cy) * axisExtra
234 },
235 nd = {
236 x: point.x + (point.x - circle.cx) * axisExtra / 2,
237 y: point.y + (point.y - circle.cy) * axisExtra / 2
238 };
239 axisGroup.createLine({
240 x1: circle.cx,
241 y1: circle.cy,
242 x2: st.x,
243 y2: st.y
244 }).setStroke(axisStroke);
245 //arrow
246 this._drawArrow(axisGroup, st, nd, axisStroke);
247 }
248
249 // draw the label
250 var labelGroup = s.createGroup();
251 for (j = labelPoints.length - 1; j >= 0; --j){
252 point = labelPoints[j];
253 fontWidth = g._base._getTextBox(this.labelKey[j], {font: axisFont}).w || 0;
254 render = this.opt.htmlLabels && g.renderer != "vml" ? "html" : "gfx";
255 var elem = da.createText[render](this.chart, labelGroup, (!domGeom.isBodyLtr() && render == "html") ? (point.x + fontWidth - dim.width) : point.x, point.y,
256 "middle", this.labelKey[j], axisFont, axisFontColor);
257 if(this.opt.htmlLabels){
258 this.htmlElements.push(elem);
259 }
260 }
261
262 //spider web: polygon or circle
263 var spiderGroup = s.createGroup();
264 if(spt == "polygon"){
265 spiderGroup.createPolyline(outerPoints).setStroke(spiderStroke);
266 spiderGroup.createPolyline(innerPoints).setStroke(spiderStroke);
267 if(divisionPoints.length > 0){
268 for (j = divisionPoints.length - 1; j >= 0; --j){
269 spiderGroup.createPolyline(divisionPoints[j]).setStroke(spiderStroke);
270 }
271 }
272 }else{//circle
273 spiderGroup.createCircle({cx: circle.cx, cy: circle.cy, r: r}).setStroke(spiderStroke);
274 spiderGroup.createCircle({cx: circle.cx, cy: circle.cy, r: r*ro}).setStroke(spiderStroke);
275 if(divisionRadius.length > 0){
276 for (j = divisionRadius.length - 1; j >= 0; --j){
277 spiderGroup.createCircle({cx: circle.cx, cy: circle.cy, r: divisionRadius[j]}).setStroke(spiderStroke);
278 }
279 }
280 }
281 //text
282 len = this._getObjectLength(this.datas);
283 var textGroup = s.createGroup(), k = 0;
284 for(var key in this.datas){
285 data = this.datas[key];
286 min = data.min;
287 max = data.max;
288 distance = max - min;
289 end = start + 2 * Math.PI * k / len;
290 for (i = 0; i < dv; i++){
291 var text = min + distance*i/(dv-1);
292 point = this._getCoordinate(circle, r*(ro + (1-ro)*i/(dv-1)), end, dim);
293 text = this._getLabel(text);
294 fontWidth = g._base._getTextBox(text, {font: axisTickFont}).w || 0;
295 render = this.opt.htmlLabels && g.renderer != "vml" ? "html" : "gfx";
296 if(this.opt.htmlLabels){
297 this.htmlElements.push(da.createText[render]
298 (this.chart, textGroup, (!domGeom.isBodyLtr() && render == "html") ? (point.x + fontWidth - dim.width) : point.x, point.y,
299 "start", text, axisTickFont, axisTickFontColor));
300 }
301 }
302 k++;
303 }
304
305 //draw series (animation)
306 this.chart.seriesShapes = {};
307 for (i = this.series.length - 1; i >= 0; i--){
308 serieEntry = this.series[i];
309 run = serieEntry.data;
310 if(run !== null){
311 var theme = t.next("spider", [o, serieEntry]),
312 f = g.normalizeColor(theme.series.fill),
313 sk = {color: theme.series.fill, width: seriesWidth};
314 f.a = o.seriesFillAlpha;
315 serieEntry.dyn = {fill: f, stroke: sk};
316 if(serieEntry.hidden){
317 continue;
318 }
319 //series polygon
320 var seriePoints = [], tipData = [];
321 k = 0;
322 for(key in run){
323 data = this.datas[key];
324 min = data.min;
325 max = data.max;
326 distance = max - min;
327 var entry = run[key], end = start + 2 * Math.PI * k / len;
328 point = this._getCoordinate(circle, r*(ro + (1-ro)*(entry-min)/distance), end, dim);
329 seriePoints.push(point);
330 tipData.push({sname: serieEntry.name, key: key, data: entry});
331 k++;
332 }
333 seriePoints[seriePoints.length] = seriePoints[0];
334 tipData[tipData.length] = tipData[0];
335 var polygonBoundRect = this._getBoundary(seriePoints),
336 ts = serieEntry.group;
337
338
339 var osps = this.oldSeriePoints[serieEntry.name];
340 var cs = this._createSeriesEntry(ts, (osps || innerPoints), seriePoints, f, sk, r, ro, ms, at);
341 this.chart.seriesShapes[serieEntry.name] = cs;
342 this.oldSeriePoints[serieEntry.name] = seriePoints;
343
344 var po = {
345 element: "spider_poly",
346 index: i,
347 id: "spider_poly_"+serieEntry.name,
348 run: serieEntry,
349 plot: this,
350 shape: cs.poly,
351 parent: ts,
352 brect: polygonBoundRect,
353 cx: circle.cx,
354 cy: circle.cy,
355 cr: r,
356 f: f,
357 s: s
358 };
359 this._connectEvents(po);
360
361 var so = {
362 element: "spider_plot",
363 index: i,
364 id: "spider_plot_"+serieEntry.name,
365 run: serieEntry,
366 plot: this,
367 shape: serieEntry.group
368 };
369 this._connectEvents(so);
370
371 arr.forEach(cs.circles, function(c, i){
372 var co = {
373 element: "spider_circle",
374 index: i,
375 id: "spider_circle_"+serieEntry.name+i,
376 run: serieEntry,
377 plot: this,
378 shape: c,
379 parent: ts,
380 tdata: tipData[i],
381 cx: seriePoints[i].x,
382 cy: seriePoints[i].y,
383 f: f,
384 s: s
385 };
386 this._connectEvents(co);
387 }, this);
388 }
389 }
390 return this; // dojox/charting/plot2d/Spider
391 },
392 _createSeriesEntry: function(ts, osps, sps, f, sk, r, ro, ms, at){
393 //polygon
394 var initpoints = this.animate?osps:sps;
395 var spoly = ts.createPolyline(initpoints).setFill(f).setStroke(sk), scircle = [];
396 for (var j = 0; j < initpoints.length; j++){
397 var point = initpoints[j], cr = ms;
398 var circle = ts.createCircle({cx: point.x, cy: point.y, r: cr}).setFill(f).setStroke(sk);
399 scircle.push(circle);
400 }
401 if(this.animate) {
402 var anims = arr.map(sps, function (np, j) {
403 // create animation
404 var sp = osps[j],
405 anim = new baseFx.Animation(lang.delegate({
406 duration: 1000,
407 easing: at,
408 curve: [sp.y, np.y]
409 }, this.animate));
410 var spl = spoly, sc = scircle[j];
411 hub.connect(anim, "onAnimate", function (y) {
412 //apply poly
413 var pshape = spl.getShape();
414 pshape.points[j].y = y;
415 spl.setShape(pshape);
416 //apply circle
417 var cshape = sc.getShape();
418 cshape.cy = y;
419 sc.setShape(cshape);
420 });
421 return anim;
422 }, this);
423
424 var anims1 = arr.map(sps, function (np, j) {
425 // create animation
426 var sp = osps[j],
427 anim = new baseFx.Animation(lang.delegate({
428 duration: 1000,
429 easing: at,
430 curve: [sp.x, np.x]
431 }, this.animate));
432 var spl = spoly, sc = scircle[j];
433 hub.connect(anim, "onAnimate", function (x) {
434 //apply poly
435 var pshape = spl.getShape();
436 pshape.points[j].x = x;
437 spl.setShape(pshape);
438 //apply circle
439 var cshape = sc.getShape();
440 cshape.cx = x;
441 sc.setShape(cshape);
442 });
443 return anim;
444 }, this);
445 var masterAnimation = coreFx.combine(anims.concat(anims1)); //dojo.fx.chain(anims);
446 masterAnimation.play();
447 }
448 return {group :ts, poly: spoly, circles: scircle};
449 },
450 plotEvent: function(o){
451 // summary:
452 // Stub function for use by specific plots.
453 // o: Object
454 // An object intended to represent event parameters.
455 if(o.element == "spider_plot"){
456 //dojo gfx function "moveToFront" not work in IE
457 if(o.type == "onmouseover" && !has("ie")){
458 o.shape.moveToFront();
459 }
460 }
461 },
462
463 tooltipFunc: function(o){
464 if(o.element == "spider_circle"){
465 return o.tdata.sname + "<br/>" + o.tdata.key + "<br/>" + o.tdata.data;
466 }else{
467 return null;
468 }
469 },
470
471 _getBoundary: function(points){
472 var xmax = points[0].x,
473 xmin = points[0].x,
474 ymax = points[0].y,
475 ymin = points[0].y;
476 for(var i = 0; i < points.length; i++){
477 var point = points[i];
478 xmax = Math.max(point.x, xmax);
479 ymax = Math.max(point.y, ymax);
480 xmin = Math.min(point.x, xmin);
481 ymin = Math.min(point.y, ymin);
482 }
483 return {
484 x: xmin,
485 y: ymin,
486 width: xmax - xmin,
487 height: ymax - ymin
488 };
489 },
490
491 _drawArrow: function(s, start, end, stroke){
492 var len = Math.sqrt(Math.pow(end.x - start.x, 2) + Math.pow(end.y - start.y, 2)),
493 sin = (end.y - start.y)/len, cos = (end.x - start.x)/len,
494 point2 = {x: end.x + (len/3)*(-sin), y: end.y + (len/3)*cos},
495 point3 = {x: end.x + (len/3)*sin, y: end.y + (len/3)*(-cos)};
496 s.createPolyline([start, point2, point3]).setFill(stroke.color).setStroke(stroke);
497 },
498
499 _buildPoints: function(points, count, circle, radius, angle, recursive, dim){
500 for(var i = 0; i < count; i++){
501 var end = angle + 2 * Math.PI * i / count;
502 points.push(this._getCoordinate(circle, radius, end, dim));
503 }
504 if(recursive){
505 points.push(this._getCoordinate(circle, radius, angle + 2 * Math.PI, dim));
506 }
507 },
508
509 _getCoordinate: function(circle, radius, angle, dim){
510 var x = circle.cx + radius * Math.cos(angle);
511 if(has("dojo-bidi") && this.chart.isRightToLeft() && dim){
512 x = dim.width - x;
513 }
514 return {
515 x: x,
516 y: circle.cy + radius * Math.sin(angle)
517 }
518 },
519
520 _getObjectLength: function(obj){
521 var count = 0;
522 if(lang.isObject(obj)){
523 for(var key in obj){
524 count++;
525 }
526 }
527 return count;
528 },
529
530 // utilities
531 _getLabel: function(number){
532 return dc.getLabel(number, this.opt.fixed, this.opt.precision);
533 }
534 });
535
536 return Spider; // dojox/plot2d/Spider
537 });