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
|
//>>built
define("dojox/charting/plot2d/Stacked", ["dojo/_base/lang", "dojo/_base/declare", "dojo/_base/array", "./Default", "./common",
"dojox/lang/functional", "dojox/lang/functional/reversed", "dojox/lang/functional/sequence"],
function(lang, declare, arr, Default, dc, df, dfr, dfs){
/*=====
var Default = dojox.charting.plot2d.Default;
=====*/
var purgeGroup = dfr.lambda("item.purgeGroup()");
return declare("dojox.charting.plot2d.Stacked", Default, {
// summary:
// Like the default plot, Stacked sets up lines, areas and markers
// in a stacked fashion (values on the y axis added to each other)
// as opposed to a direct one.
getSeriesStats: function(){
// summary:
// Calculate the min/max on all attached series in both directions.
// returns: Object
// {hmin, hmax, vmin, vmax} min/max in both directions.
var stats = dc.collectStackedStats(this.series);
this._maxRunLength = stats.hmax;
return stats;
},
render: function(dim, offsets){
// summary:
// Run the calculations for any axes for this plot.
// dim: Object
// An object in the form of { width, height }
// offsets: Object
// An object of the form { l, r, t, b}.
// returns: dojox.charting.plot2d.Stacked
// A reference to this plot for functional chaining.
if(this._maxRunLength <= 0){
return this;
}
// stack all values
var acc = df.repeat(this._maxRunLength, "-> 0", 0);
for(var i = 0; i < this.series.length; ++i){
var run = this.series[i];
for(var j = 0; j < run.data.length; ++j){
var v = run.data[j];
if(v !== null){
if(isNaN(v)){ v = 0; }
acc[j] += v;
}
}
}
// draw runs in backwards
if(this.zoom && !this.isDataDirty()){
return this.performZoom(dim, offsets);
}
this.resetEvents();
this.dirty = this.isDirty();
if(this.dirty){
arr.forEach(this.series, purgeGroup);
this._eventSeries = {};
this.cleanGroup();
var s = this.group;
df.forEachRev(this.series, function(item){ item.cleanGroup(s); });
}
var t = this.chart.theme, events = this.events(),
ht = this._hScaler.scaler.getTransformerFromModel(this._hScaler),
vt = this._vScaler.scaler.getTransformerFromModel(this._vScaler);
for(var i = this.series.length - 1; i >= 0; --i){
var run = this.series[i];
if(!this.dirty && !run.dirty){
t.skip();
this._reconnectEvents(run.name);
continue;
}
run.cleanGroup();
var theme = t.next(this.opt.areas ? "area" : "line", [this.opt, run], true),
s = run.group, outline,
lpoly = arr.map(acc, function(v, i){
return {
x: ht(i + 1) + offsets.l,
y: dim.height - offsets.b - vt(v)
};
}, this);
var lpath = this.opt.tension ? dc.curve(lpoly, this.opt.tension) : "";
if(this.opt.areas){
var apoly = lang.clone(lpoly);
if(this.opt.tension){
var p=dc.curve(apoly, this.opt.tension);
p += " L" + lpoly[lpoly.length - 1].x + "," + (dim.height - offsets.b) +
" L" + lpoly[0].x + "," + (dim.height - offsets.b) +
" L" + lpoly[0].x + "," + lpoly[0].y;
run.dyn.fill = s.createPath(p).setFill(theme.series.fill).getFill();
} else {
apoly.push({x: lpoly[lpoly.length - 1].x, y: dim.height - offsets.b});
apoly.push({x: lpoly[0].x, y: dim.height - offsets.b});
apoly.push(lpoly[0]);
run.dyn.fill = s.createPolyline(apoly).setFill(theme.series.fill).getFill();
}
}
if(this.opt.lines || this.opt.markers){
if(theme.series.outline){
outline = dc.makeStroke(theme.series.outline);
outline.width = 2 * outline.width + theme.series.stroke.width;
}
}
if(this.opt.markers){
run.dyn.marker = theme.symbol;
}
var frontMarkers, outlineMarkers, shadowMarkers;
if(theme.series.shadow && theme.series.stroke){
var shadow = theme.series.shadow,
spoly = arr.map(lpoly, function(c){
return {x: c.x + shadow.dx, y: c.y + shadow.dy};
});
if(this.opt.lines){
if(this.opt.tension){
run.dyn.shadow = s.createPath(dc.curve(spoly, this.opt.tension)).setStroke(shadow).getStroke();
} else {
run.dyn.shadow = s.createPolyline(spoly).setStroke(shadow).getStroke();
}
}
if(this.opt.markers){
shadow = theme.marker.shadow;
shadowMarkers = arr.map(spoly, function(c){
return s.createPath("M" + c.x + " " + c.y + " " + theme.symbol).
setStroke(shadow).setFill(shadow.color);
}, this);
}
}
if(this.opt.lines){
if(outline){
if(this.opt.tension){
run.dyn.outline = s.createPath(lpath).setStroke(outline).getStroke();
} else {
run.dyn.outline = s.createPolyline(lpoly).setStroke(outline).getStroke();
}
}
if(this.opt.tension){
run.dyn.stroke = s.createPath(lpath).setStroke(theme.series.stroke).getStroke();
} else {
run.dyn.stroke = s.createPolyline(lpoly).setStroke(theme.series.stroke).getStroke();
}
}
if(this.opt.markers){
frontMarkers = new Array(lpoly.length);
outlineMarkers = new Array(lpoly.length);
outline = null;
if(theme.marker.outline){
outline = dc.makeStroke(theme.marker.outline);
outline.width = 2 * outline.width + (theme.marker.stroke ? theme.marker.stroke.width : 0);
}
arr.forEach(lpoly, function(c, i){
var path = "M" + c.x + " " + c.y + " " + theme.symbol;
if(outline){
outlineMarkers[i] = s.createPath(path).setStroke(outline);
}
frontMarkers[i] = s.createPath(path).setStroke(theme.marker.stroke).setFill(theme.marker.fill);
}, this);
if(events){
var eventSeries = new Array(frontMarkers.length);
arr.forEach(frontMarkers, function(s, i){
var o = {
element: "marker",
index: i,
run: run,
shape: s,
outline: outlineMarkers[i] || null,
shadow: shadowMarkers && shadowMarkers[i] || null,
cx: lpoly[i].x,
cy: lpoly[i].y,
x: i + 1,
y: run.data[i]
};
this._connectEvents(o);
eventSeries[i] = o;
}, this);
this._eventSeries[run.name] = eventSeries;
}else{
delete this._eventSeries[run.name];
}
}
run.dirty = false;
// update the accumulator
for(var j = 0; j < run.data.length; ++j){
var v = run.data[j];
if(v !== null){
if(isNaN(v)){ v = 0; }
acc[j] -= v;
}
}
}
this.dirty = false;
return this; // dojox.charting.plot2d.Stacked
}
});
});
|