]> git.pond.sub.org Git - eow/blob - static/dojo-release-1.1.1/dojox/charting/plot2d/Stacked.js
Comment class stub
[eow] / static / dojo-release-1.1.1 / dojox / charting / plot2d / Stacked.js
1 if(!dojo._hasResource["dojox.charting.plot2d.Stacked"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2 dojo._hasResource["dojox.charting.plot2d.Stacked"] = true;
3 dojo.provide("dojox.charting.plot2d.Stacked");
4
5 dojo.require("dojox.charting.plot2d.common");
6 dojo.require("dojox.charting.plot2d.Default");
7
8 dojo.require("dojox.lang.functional");
9 dojo.require("dojox.lang.functional.sequence");
10 dojo.require("dojox.lang.functional.reversed");
11
12 (function(){
13         var df = dojox.lang.functional, dc = dojox.charting.plot2d.common,
14                 purgeGroup = df.lambda("item.purgeGroup()");
15
16         dojo.declare("dojox.charting.plot2d.Stacked", dojox.charting.plot2d.Default, {
17                 calculateAxes: function(dim){
18                         var stats = dc.collectStackedStats(this.series);
19                         this._maxRunLength = stats.hmax;
20                         this._calc(dim, stats);
21                         return this;
22                 },
23                 render: function(dim, offsets){
24                         // stack all values
25                         var acc = df.repeat(this._maxRunLength, "-> 0", 0);
26                         for(var i = 0; i < this.series.length; ++i){
27                                 var run = this.series[i];
28                                 for(var j = 0; j < run.data.length; ++j){
29                                         var v = run.data[j];
30                                         if(isNaN(v)){ v = 0; }
31                                         acc[j] += v;
32                                 }
33                         }
34                         // draw runs in backwards
35                         if(this.dirty){
36                                 dojo.forEach(this.series, purgeGroup);
37                                 this.cleanGroup();
38                                 var s = this.group;
39                                 df.forEachRev(this.series, function(item){ item.cleanGroup(s); });
40                         }
41
42                         //      inner function for translating polylines to curves with tension
43                         function curve(arr, tension){
44                                 var p=dojo.map(arr, function(item, i){
45                                         if(i==0){ return "M" + item.x + "," + item.y; }
46                                         var dx=item.x-arr[i-1].x, dy=arr[i-1].y;
47                                         return "C"+(item.x-(tension-1)*(dx/tension))+","+dy+" "+(item.x-(dx/tension))+","+item.y+" "+item.x+","+item.y;
48                                 });
49                                 return p.join(" ");
50                         }
51                                 
52                         var t = this.chart.theme, stroke, outline, color, marker;
53                         for(var i = this.series.length - 1; i >= 0; --i){
54                                 var run = this.series[i];
55                                 if(!this.dirty && !run.dirty){ continue; }
56                                 run.cleanGroup();
57                                 var s = run.group,
58                                         lpoly = dojo.map(acc, function(v, i){
59                                                 return {
60                                                         x: this._hScaler.scale * (i + 1 - this._hScaler.bounds.lower) + offsets.l,
61                                                         y: dim.height - offsets.b - this._vScaler.scale * (v - this._vScaler.bounds.lower)
62                                                 };
63                                         }, this);
64                                 if(!run.fill || !run.stroke){
65                                         // need autogenerated color
66                                         color = new dojo.Color(t.next("color"));
67                                 }
68
69                                 var lpath="";
70                                 if(this.opt.tension){
71                                         lpath=curve(lpoly, this.opt.tension);
72                                 }
73                                 
74                                 if(this.opt.areas){
75                                         var apoly = dojo.clone(lpoly);
76                                         var fill = run.fill ? run.fill : dc.augmentFill(t.series.fill, color);
77                                         if(this.opt.tension){
78                                                 var p=curve(apoly, this.opt.tension);
79                                                 p += " L" + lpoly[lpoly.length-1].x + "," + (dim.height - offsets.b) + " "
80                                                         + "L" + lpoly[0].x + "," + (dim.height - offsets.b) + " "
81                                                         + "L" + lpoly[0].x + "," + lpoly[0].y;
82                                                 s.createPath(p).setFill(fill);
83                                         } else {
84                                                 apoly.push({x: lpoly[lpoly.length - 1].x, y: dim.height - offsets.b});
85                                                 apoly.push({x: lpoly[0].x, y: dim.height - offsets.b});
86                                                 apoly.push(lpoly[0]);
87                                                 s.createPolyline(apoly).setFill(fill);
88                                         }
89                                 }
90                                 if(this.opt.lines || this.opt.markers){
91                                         // need a stroke
92                                         stroke = run.stroke ? dc.makeStroke(run.stroke) : dc.augmentStroke(t.series.stroke, color);
93                                         if(run.outline || t.series.outline){
94                                                 outline = dc.makeStroke(run.outline ? run.outline : t.series.outline);
95                                                 outline.width = 2 * outline.width + stroke.width;
96                                         }
97                                 }
98                                 if(this.opt.markers){
99                                         // need a marker
100                                         marker = run.marker ? run.marker : t.next("marker");
101                                 }
102                                 if(this.opt.shadows && stroke){
103                                         var sh = this.opt.shadows, shadowColor = new dojo.Color([0, 0, 0, 0.3]),
104                                                 spoly = dojo.map(lpoly, function(c){
105                                                         return {x: c.x + sh.dx, y: c.y + sh.dy};
106                                                 }),
107                                                 shadowStroke = dojo.clone(outline ? outline : stroke);
108                                         shadowStroke.color = shadowColor;
109                                         shadowStroke.width += sh.dw ? sh.dw : 0;
110                                         if(this.opt.lines){
111                                                 if(this.opt.tension){
112                                                         s.createPath(curve(spoly, this.opt.tension)).setStroke(shadowStroke);
113                                                 } else {
114                                                         s.createPolyline(spoly).setStroke(shadowStroke);
115                                                 }
116                                         }
117                                         if(this.opt.markers){
118                                                 dojo.forEach(spoly, function(c){
119                                                         s.createPath("M" + c.x + " " + c.y + " " + marker).setStroke(shadowStroke).setFill(shadowColor);
120                                                 }, this);
121                                         }
122                                 }
123                                 if(this.opt.lines){
124                                         if(outline){
125                                                 if(this.opt.tension){
126                                                         s.createPath(lpath).setStroke(outline);
127                                                 } else {
128                                                         s.createPolyline(lpoly).setStroke(outline);
129                                                 }
130                                         }
131                                         if(this.opt.tension){
132                                                 s.createPath(lpath).setStroke(stroke);
133                                         } else {
134                                                 s.createPolyline(lpoly).setStroke(stroke);
135                                         }
136                                 }
137                                 if(this.opt.markers){
138                                         dojo.forEach(lpoly, function(c){
139                                                 var path = "M" + c.x + " " + c.y + " " + marker;
140                                                 if(outline){
141                                                         s.createPath(path).setStroke(outline);
142                                                 }
143                                                 s.createPath(path).setStroke(stroke).setFill(stroke.color);
144                                         }, this);
145                                 }
146                                 run.dirty = false;
147                                 // update the accumulator
148                                 for(var j = 0; j < run.data.length; ++j){
149                                         var v = run.data[j];
150                                         if(isNaN(v)){ v = 0; }
151                                         acc[j] -= v;
152                                 }
153                         }
154                         this.dirty = false;
155                         return this;
156                 }
157         });
158 })();
159
160 }