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
|
//>>built
define("dojox/charting/plot2d/common", ["dojo/_base/lang", "dojo/_base/array", "dojo/_base/Color",
"dojox/gfx", "dojox/lang/functional", "../scaler/common"],
function(lang, arr, Color, g, df, sc){
var common = lang.getObject("dojox.charting.plot2d.common", true);
return lang.mixin(common, {
doIfLoaded: sc.doIfLoaded,
makeStroke: function(stroke){
if(!stroke){ return stroke; }
if(typeof stroke == "string" || stroke instanceof Color){
stroke = {color: stroke};
}
return g.makeParameters(g.defaultStroke, stroke);
},
augmentColor: function(target, color){
var t = new Color(target),
c = new Color(color);
c.a = t.a;
return c;
},
augmentStroke: function(stroke, color){
var s = common.makeStroke(stroke);
if(s){
s.color = common.augmentColor(s.color, color);
}
return s;
},
augmentFill: function(fill, color){
var fc, c = new Color(color);
if(typeof fill == "string" || fill instanceof Color){
return common.augmentColor(fill, color);
}
return fill;
},
defaultStats: {
vmin: Number.POSITIVE_INFINITY, vmax: Number.NEGATIVE_INFINITY,
hmin: Number.POSITIVE_INFINITY, hmax: Number.NEGATIVE_INFINITY
},
collectSimpleStats: function(series){
var stats = lang.delegate(common.defaultStats);
for(var i = 0; i < series.length; ++i){
var run = series[i];
for(var j = 0; j < run.data.length; j++){
if(run.data[j] !== null){
if(typeof run.data[j] == "number"){
// 1D case
var old_vmin = stats.vmin, old_vmax = stats.vmax;
if(!("ymin" in run) || !("ymax" in run)){
arr.forEach(run.data, function(val, i){
if(val !== null){
var x = i + 1, y = val;
if(isNaN(y)){ y = 0; }
stats.hmin = Math.min(stats.hmin, x);
stats.hmax = Math.max(stats.hmax, x);
stats.vmin = Math.min(stats.vmin, y);
stats.vmax = Math.max(stats.vmax, y);
}
});
}
if("ymin" in run){ stats.vmin = Math.min(old_vmin, run.ymin); }
if("ymax" in run){ stats.vmax = Math.max(old_vmax, run.ymax); }
}else{
// 2D case
var old_hmin = stats.hmin, old_hmax = stats.hmax,
old_vmin = stats.vmin, old_vmax = stats.vmax;
if(!("xmin" in run) || !("xmax" in run) || !("ymin" in run) || !("ymax" in run)){
arr.forEach(run.data, function(val, i){
if(val !== null){
var x = "x" in val ? val.x : i + 1, y = val.y;
if(isNaN(x)){ x = 0; }
if(isNaN(y)){ y = 0; }
stats.hmin = Math.min(stats.hmin, x);
stats.hmax = Math.max(stats.hmax, x);
stats.vmin = Math.min(stats.vmin, y);
stats.vmax = Math.max(stats.vmax, y);
}
});
}
if("xmin" in run){ stats.hmin = Math.min(old_hmin, run.xmin); }
if("xmax" in run){ stats.hmax = Math.max(old_hmax, run.xmax); }
if("ymin" in run){ stats.vmin = Math.min(old_vmin, run.ymin); }
if("ymax" in run){ stats.vmax = Math.max(old_vmax, run.ymax); }
}
break;
}
}
}
return stats;
},
calculateBarSize: function(/* Number */ availableSize, /* Object */ opt, /* Number? */ clusterSize){
if(!clusterSize){
clusterSize = 1;
}
var gap = opt.gap, size = (availableSize - 2 * gap) / clusterSize;
if("minBarSize" in opt){
size = Math.max(size, opt.minBarSize);
}
if("maxBarSize" in opt){
size = Math.min(size, opt.maxBarSize);
}
size = Math.max(size, 1);
gap = (availableSize - size * clusterSize) / 2;
return {size: size, gap: gap}; // Object
},
collectStackedStats: function(series){
// collect statistics
var stats = lang.clone(common.defaultStats);
if(series.length){
// 1st pass: find the maximal length of runs
stats.hmin = Math.min(stats.hmin, 1);
stats.hmax = df.foldl(series, "seed, run -> Math.max(seed, run.data.length)", stats.hmax);
// 2nd pass: stack values
for(var i = 0; i < stats.hmax; ++i){
var v = series[0].data[i];
v = v && (typeof v == "number" ? v : v.y);
if(isNaN(v)){ v = 0; }
stats.vmin = Math.min(stats.vmin, v);
for(var j = 1; j < series.length; ++j){
var t = series[j].data[i];
t = t && (typeof t == "number" ? t : t.y);
if(isNaN(t)){ t = 0; }
v += t;
}
stats.vmax = Math.max(stats.vmax, v);
}
}
return stats;
},
curve: function(/* Number[] */a, /* Number|String */tension){
// FIX for #7235, submitted by Enzo Michelangeli.
// Emulates the smoothing algorithms used in a famous, unnamed spreadsheet
// program ;)
var array = a.slice(0);
if(tension == "x") {
array[array.length] = arr[0]; // add a last element equal to the first, closing the loop
}
var p=arr.map(array, function(item, i){
if(i==0){ return "M" + item.x + "," + item.y; }
if(!isNaN(tension)) { // use standard Dojo smoothing in tension is numeric
var dx=item.x-array[i-1].x, dy=array[i-1].y;
return "C"+(item.x-(tension-1)*(dx/tension))+","+dy+" "+(item.x-(dx/tension))+","+item.y+" "+item.x+","+item.y;
} else if(tension == "X" || tension == "x" || tension == "S") {
// use Excel "line smoothing" algorithm (http://xlrotor.com/resources/files.shtml)
var p0, p1 = array[i-1], p2 = array[i], p3;
var bz1x, bz1y, bz2x, bz2y;
var f = 1/6;
if(i==1) {
if(tension == "x") {
p0 = array[array.length-2];
} else { // "tension == X || tension == "S"
p0 = p1;
}
f = 1/3;
} else {
p0 = array[i-2];
}
if(i==(array.length-1)) {
if(tension == "x") {
p3 = array[1];
} else { // "tension == X || tension == "S"
p3 = p2;
}
f = 1/3;
} else {
p3 = array[i+1];
}
var p1p2 = Math.sqrt((p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y));
var p0p2 = Math.sqrt((p2.x-p0.x)*(p2.x-p0.x)+(p2.y-p0.y)*(p2.y-p0.y));
var p1p3 = Math.sqrt((p3.x-p1.x)*(p3.x-p1.x)+(p3.y-p1.y)*(p3.y-p1.y));
var p0p2f = p0p2 * f;
var p1p3f = p1p3 * f;
if(p0p2f > p1p2/2 && p1p3f > p1p2/2) {
p0p2f = p1p2/2;
p1p3f = p1p2/2;
} else if(p0p2f > p1p2/2) {
p0p2f = p1p2/2;
p1p3f = p1p2/2 * p1p3/p0p2;
} else if(p1p3f > p1p2/2) {
p1p3f = p1p2/2;
p0p2f = p1p2/2 * p0p2/p1p3;
}
if(tension == "S") {
if(p0 == p1) { p0p2f = 0; }
if(p2 == p3) { p1p3f = 0; }
}
bz1x = p1.x + p0p2f*(p2.x - p0.x)/p0p2;
bz1y = p1.y + p0p2f*(p2.y - p0.y)/p0p2;
bz2x = p2.x - p1p3f*(p3.x - p1.x)/p1p3;
bz2y = p2.y - p1p3f*(p3.y - p1.y)/p1p3;
}
return "C"+(bz1x+","+bz1y+" "+bz2x+","+bz2y+" "+p2.x+","+p2.y);
});
return p.join(" ");
},
getLabel: function(/*Number*/number, /*Boolean*/fixed, /*Number*/precision){
return sc.doIfLoaded("dojo/number", function(numberLib){
return (fixed ? numberLib.format(number, {places : precision}) :
numberLib.format(number)) || "";
}, function(){
return fixed ? number.toFixed(precision) : number.toString();
});
}
});
});
|