Debian Codegraph @

dojox/charting/plot2d/common.js

raw symbols SCIP
1 define(["dojo/_base/lang", "dojo/_base/array", "dojo/_base/Color",
2 "dojox/gfx", "dojox/lang/functional", "../scaler/common"],
3 function(lang, arr, Color, g, df, sc){
4
5 var common = lang.getObject("dojox.charting.plot2d.common", true);
6
7 return lang.mixin(common, {
8 doIfLoaded: sc.doIfLoaded,
9 makeStroke: function(stroke){
10 if(!stroke){ return stroke; }
11 if(typeof stroke == "string" || stroke instanceof Color){
12 stroke = {color: stroke};
13 }
14 return g.makeParameters(g.defaultStroke, stroke);
15 },
16 augmentColor: function(target, color){
17 var t = new Color(target),
18 c = new Color(color);
19 c.a = t.a;
20 return c;
21 },
22 augmentStroke: function(stroke, color){
23 var s = common.makeStroke(stroke);
24 if(s){
25 s.color = common.augmentColor(s.color, color);
26 }
27 return s;
28 },
29 augmentFill: function(fill, color){
30 var fc, c = new Color(color);
31 if(typeof fill == "string" || fill instanceof Color){
32 return common.augmentColor(fill, color);
33 }
34 return fill;
35 },
36
37 defaultStats: {
38 vmin: Number.POSITIVE_INFINITY, vmax: Number.NEGATIVE_INFINITY,
39 hmin: Number.POSITIVE_INFINITY, hmax: Number.NEGATIVE_INFINITY
40 },
41
42 collectSimpleStats: function(series, isNullValue){
43 var stats = lang.delegate(common.defaultStats);
44 for(var i = 0; i < series.length; ++i){
45 var run = series[i];
46 for(var j = 0; j < run.data.length; j++){
47 if(!isNullValue(run.data[j])){
48 if(typeof run.data[j] == "number"){
49 // 1D case
50 var old_vmin = stats.vmin, old_vmax = stats.vmax;
51 arr.forEach(run.data, function(val, i){
52 if(!isNullValue(val)){
53 var x = i + 1, y = val;
54 if(isNaN(y)){ y = 0; }
55 stats.hmin = Math.min(stats.hmin, x);
56 stats.hmax = Math.max(stats.hmax, x);
57 stats.vmin = Math.min(stats.vmin, y);
58 stats.vmax = Math.max(stats.vmax, y);
59 }
60 });
61 if("ymin" in run){ stats.vmin = Math.min(old_vmin, run.ymin); }
62 if("ymax" in run){ stats.vmax = Math.max(old_vmax, run.ymax); }
63 }else{
64 // 2D case
65 var old_hmin = stats.hmin, old_hmax = stats.hmax,
66 old_vmin = stats.vmin, old_vmax = stats.vmax;
67 if(!("xmin" in run) || !("xmax" in run) || !("ymin" in run) || !("ymax" in run)){
68 arr.forEach(run.data, function(val, i){
69 if(!isNullValue(val)){
70 var x = "x" in val ? val.x : i + 1, y = val.y;
71 if(isNaN(x)){ x = 0; }
72 if(isNaN(y)){ y = 0; }
73 stats.hmin = Math.min(stats.hmin, x);
74 stats.hmax = Math.max(stats.hmax, x);
75 stats.vmin = Math.min(stats.vmin, y);
76 stats.vmax = Math.max(stats.vmax, y);
77 }
78 });
79 }
80 if("xmin" in run){ stats.hmin = Math.min(old_hmin, run.xmin); }
81 if("xmax" in run){ stats.hmax = Math.max(old_hmax, run.xmax); }
82 if("ymin" in run){ stats.vmin = Math.min(old_vmin, run.ymin); }
83 if("ymax" in run){ stats.vmax = Math.max(old_vmax, run.ymax); }
84 }
85
86 break;
87 }
88 }
89 }
90 return stats;
91 },
92
93 calculateBarSize: function(/* Number */ availableSize, /* Object */ opt, /* Number? */ clusterSize){
94 if(!clusterSize){
95 clusterSize = 1;
96 }
97 var gap = opt.gap, size = (availableSize - 2 * gap) / clusterSize;
98 if("minBarSize" in opt){
99 size = Math.max(size, opt.minBarSize);
100 }
101 if("maxBarSize" in opt){
102 size = Math.min(size, opt.maxBarSize);
103 }
104 size = Math.max(size, 1);
105 gap = (availableSize - size * clusterSize) / 2;
106 return {size: size, gap: gap}; // Object
107 },
108
109 collectStackedStats: function(series){
110 // collect statistics
111 var stats = lang.clone(common.defaultStats);
112 if(series.length){
113 // 1st pass: find the maximal length of runs
114 stats.hmin = Math.min(stats.hmin, 1);
115 stats.hmax = df.foldl(series, "seed, run -> Math.max(seed, run.data.length)", stats.hmax);
116 // 2nd pass: stack values
117 for(var i = 0; i < stats.hmax; ++i){
118 var v = series[0].data[i];
119 v = v && (typeof v == "number" ? v : v.y);
120 if(isNaN(v)){ v = 0; }
121 stats.vmin = Math.min(stats.vmin, v);
122 for(var j = 1; j < series.length; ++j){
123 var t = series[j].data[i];
124 t = t && (typeof t == "number" ? t : t.y);
125 if(isNaN(t)){ t = 0; }
126 v += t;
127 }
128 stats.vmax = Math.max(stats.vmax, v);
129 }
130 }
131 return stats;
132 },
133
134 curve: function(/* Number[] */a, /* Number|String */tension){
135 // FIX for #7235, submitted by Enzo Michelangeli.
136 // Emulates the smoothing algorithms used in a famous, unnamed spreadsheet
137 // program ;)
138 var array = a.slice(0);
139 if(tension == "x") {
140 array[array.length] = array[0]; // add a last element equal to the first, closing the loop
141 }
142 var p=arr.map(array, function(item, i){
143 if(i==0){ return "M" + item.x + "," + item.y; }
144 if(!isNaN(tension)) { // use standard Dojo smoothing in tension is numeric
145 var dx=item.x-array[i-1].x, dy=array[i-1].y;
146 return "C"+(item.x-(tension-1)*(dx/tension))+","+dy+" "+(item.x-(dx/tension))+","+item.y+" "+item.x+","+item.y;
147 } else if(tension == "X" || tension == "x" || tension == "S") {
148 // use Excel "line smoothing" algorithm (http://xlrotor.com/resources/files.shtml)
149 var p0, p1 = array[i-1], p2 = array[i], p3;
150 var bz1x, bz1y, bz2x, bz2y;
151 var f = 1/6;
152 if(i==1) {
153 if(tension == "x") {
154 p0 = array[array.length-2];
155 } else { // "tension == X || tension == "S"
156 p0 = p1;
157 }
158 f = 1/3;
159 } else {
160 p0 = array[i-2];
161 }
162 if(i==(array.length-1)) {
163 if(tension == "x") {
164 p3 = array[1];
165 } else { // "tension == X || tension == "S"
166 p3 = p2;
167 }
168 f = 1/3;
169 } else {
170 p3 = array[i+1];
171 }
172 var p1p2 = Math.sqrt((p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y));
173 var p0p2 = Math.sqrt((p2.x-p0.x)*(p2.x-p0.x)+(p2.y-p0.y)*(p2.y-p0.y));
174 var p1p3 = Math.sqrt((p3.x-p1.x)*(p3.x-p1.x)+(p3.y-p1.y)*(p3.y-p1.y));
175
176 var p0p2f = p0p2 * f;
177 var p1p3f = p1p3 * f;
178
179 if(p0p2f > p1p2/2 && p1p3f > p1p2/2) {
180 p0p2f = p1p2/2;
181 p1p3f = p1p2/2;
182 } else if(p0p2f > p1p2/2) {
183 p0p2f = p1p2/2;
184 p1p3f = p1p2/2 * p1p3/p0p2;
185 } else if(p1p3f > p1p2/2) {
186 p1p3f = p1p2/2;
187 p0p2f = p1p2/2 * p0p2/p1p3;
188 }
189
190 if(tension == "S") {
191 if(p0 == p1) { p0p2f = 0; }
192 if(p2 == p3) { p1p3f = 0; }
193 }
194
195 bz1x = p1.x + p0p2f*(p2.x - p0.x)/p0p2;
196 bz1y = p1.y + p0p2f*(p2.y - p0.y)/p0p2;
197 bz2x = p2.x - p1p3f*(p3.x - p1.x)/p1p3;
198 bz2y = p2.y - p1p3f*(p3.y - p1.y)/p1p3;
199 }
200 return "C"+(bz1x+","+bz1y+" "+bz2x+","+bz2y+" "+p2.x+","+p2.y);
201 });
202 return p.join(" ");
203 },
204
205 getLabel: function(/*Number*/number, /*Boolean*/fixed, /*Number*/precision){
206 return sc.doIfLoaded("dojo/number", function(numberLib){
207 return (fixed ? numberLib.format(number, {places : precision}) :
208 numberLib.format(number)) || "";
209 }, function(){
210 return fixed ? number.toFixed(precision) : number.toString();
211 });
212 },
213
214 purgeGroup: function (item) {
215 return item.purgeGroup();
216 }
217 });
218 });