source: Dev/trunk/src/client/dojox/charting/plot2d/common.js @ 525

Last change on this file since 525 was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

File size: 6.9 KB
Line 
1define(["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){
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(run.data[j] !== null){
48                                                if(typeof run.data[j] == "number"){
49                                                        // 1D case
50                                                        var old_vmin = stats.vmin, old_vmax = stats.vmax;
51                                                        if(!("ymin" in run) || !("ymax" in run)){
52                                                                arr.forEach(run.data, function(val, i){
53                                                                        if(val !== null){
54                                                                                var x = i + 1, y = val;
55                                                                                if(isNaN(y)){ y = 0; }
56                                                                                stats.hmin = Math.min(stats.hmin, x);
57                                                                                stats.hmax = Math.max(stats.hmax, x);
58                                                                                stats.vmin = Math.min(stats.vmin, y);
59                                                                                stats.vmax = Math.max(stats.vmax, y);
60                                                                        }
61                                                                });
62                                                        }
63                                                        if("ymin" in run){ stats.vmin = Math.min(old_vmin, run.ymin); }
64                                                        if("ymax" in run){ stats.vmax = Math.max(old_vmax, run.ymax); }
65                                                }else{
66                                                        // 2D case
67                                                        var old_hmin = stats.hmin, old_hmax = stats.hmax,
68                                                                old_vmin = stats.vmin, old_vmax = stats.vmax;
69                                                        if(!("xmin" in run) || !("xmax" in run) || !("ymin" in run) || !("ymax" in run)){
70                                                                arr.forEach(run.data, function(val, i){
71                                                                        if(val !== null){
72                                                                                var x = "x" in val ? val.x : i + 1, y = val.y;
73                                                                                if(isNaN(x)){ x = 0; }
74                                                                                if(isNaN(y)){ y = 0; }
75                                                                                stats.hmin = Math.min(stats.hmin, x);
76                                                                                stats.hmax = Math.max(stats.hmax, x);
77                                                                                stats.vmin = Math.min(stats.vmin, y);
78                                                                                stats.vmax = Math.max(stats.vmax, y);
79                                                                        }
80                                                                });
81                                                        }
82                                                        if("xmin" in run){ stats.hmin = Math.min(old_hmin, run.xmin); }
83                                                        if("xmax" in run){ stats.hmax = Math.max(old_hmax, run.xmax); }
84                                                        if("ymin" in run){ stats.vmin = Math.min(old_vmin, run.ymin); }
85                                                        if("ymax" in run){ stats.vmax = Math.max(old_vmax, run.ymax); }
86                                                }
87
88                                                break;
89                                        }
90                                }
91                        }
92                        return stats;
93                },
94
95                calculateBarSize: function(/* Number */ availableSize, /* Object */ opt, /* Number? */ clusterSize){
96                        if(!clusterSize){
97                                clusterSize = 1;
98                        }
99                        var gap = opt.gap, size = (availableSize - 2 * gap) / clusterSize;
100                        if("minBarSize" in opt){
101                                size = Math.max(size, opt.minBarSize);
102                        }
103                        if("maxBarSize" in opt){
104                                size = Math.min(size, opt.maxBarSize);
105                        }
106                        size = Math.max(size, 1);
107                        gap = (availableSize - size * clusterSize) / 2;
108                        return {size: size, gap: gap};  // Object
109                },
110
111                collectStackedStats: function(series){
112                        // collect statistics
113                        var stats = lang.clone(common.defaultStats);
114                        if(series.length){
115                                // 1st pass: find the maximal length of runs
116                                stats.hmin = Math.min(stats.hmin, 1);
117                                stats.hmax = df.foldl(series, "seed, run -> Math.max(seed, run.data.length)", stats.hmax);
118                                // 2nd pass: stack values
119                                for(var i = 0; i < stats.hmax; ++i){
120                                        var v = series[0].data[i];
121                                        v = v && (typeof v == "number" ? v : v.y);
122                                        if(isNaN(v)){ v = 0; }
123                                        stats.vmin = Math.min(stats.vmin, v);
124                                        for(var j = 1; j < series.length; ++j){
125                                                var t = series[j].data[i];
126                                                t = t && (typeof t == "number" ? t : t.y);
127                                                if(isNaN(t)){ t = 0; }
128                                                v += t;
129                                        }
130                                        stats.vmax = Math.max(stats.vmax, v);
131                                }
132                        }
133                        return stats;
134                },
135
136                curve: function(/* Number[] */a, /* Number|String */tension){
137                        //      FIX for #7235, submitted by Enzo Michelangeli.
138                        //      Emulates the smoothing algorithms used in a famous, unnamed spreadsheet
139                        //              program ;)
140                        var array = a.slice(0);
141                        if(tension == "x") {
142                                array[array.length] = array[0];   // add a last element equal to the first, closing the loop
143                        }
144                        var p=arr.map(array, function(item, i){
145                                if(i==0){ return "M" + item.x + "," + item.y; }
146                                if(!isNaN(tension)) { // use standard Dojo smoothing in tension is numeric
147                                        var dx=item.x-array[i-1].x, dy=array[i-1].y;
148                                        return "C"+(item.x-(tension-1)*(dx/tension))+","+dy+" "+(item.x-(dx/tension))+","+item.y+" "+item.x+","+item.y;
149                                } else if(tension == "X" || tension == "x" || tension == "S") {
150                                        // use Excel "line smoothing" algorithm (http://xlrotor.com/resources/files.shtml)
151                                        var p0, p1 = array[i-1], p2 = array[i], p3;
152                                        var bz1x, bz1y, bz2x, bz2y;
153                                        var f = 1/6;
154                                        if(i==1) {
155                                                if(tension == "x") {
156                                                        p0 = array[array.length-2];
157                                                } else { // "tension == X || tension == "S"
158                                                        p0 = p1;
159                                                }
160                                                f = 1/3;
161                                        } else {
162                                                p0 = array[i-2];
163                                        }
164                                        if(i==(array.length-1)) {
165                                                if(tension == "x") {
166                                                        p3 = array[1];
167                                                } else { // "tension == X || tension == "S"
168                                                        p3 = p2;
169                                                }
170                                                f = 1/3;
171                                        } else {
172                                                p3 = array[i+1];
173                                        }
174                                        var p1p2 = Math.sqrt((p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y));
175                                        var p0p2 = Math.sqrt((p2.x-p0.x)*(p2.x-p0.x)+(p2.y-p0.y)*(p2.y-p0.y));
176                                        var p1p3 = Math.sqrt((p3.x-p1.x)*(p3.x-p1.x)+(p3.y-p1.y)*(p3.y-p1.y));
177
178                                        var p0p2f = p0p2 * f;
179                                        var p1p3f = p1p3 * f;
180
181                                        if(p0p2f > p1p2/2 && p1p3f > p1p2/2) {
182                                                p0p2f = p1p2/2;
183                                                p1p3f = p1p2/2;
184                                        } else if(p0p2f > p1p2/2) {
185                                                p0p2f = p1p2/2;
186                                                p1p3f = p1p2/2 * p1p3/p0p2;
187                                        } else if(p1p3f > p1p2/2) {
188                                                p1p3f = p1p2/2;
189                                                p0p2f = p1p2/2 * p0p2/p1p3;
190                                        }
191
192                                        if(tension == "S") {
193                                                if(p0 == p1) { p0p2f = 0; }
194                                                if(p2 == p3) { p1p3f = 0; }
195                                        }
196
197                                        bz1x = p1.x + p0p2f*(p2.x - p0.x)/p0p2;
198                                        bz1y = p1.y + p0p2f*(p2.y - p0.y)/p0p2;
199                                        bz2x = p2.x - p1p3f*(p3.x - p1.x)/p1p3;
200                                        bz2y = p2.y - p1p3f*(p3.y - p1.y)/p1p3;
201                                }
202                                return "C"+(bz1x+","+bz1y+" "+bz2x+","+bz2y+" "+p2.x+","+p2.y);
203                        });
204                        return p.join(" ");
205                },
206               
207                getLabel: function(/*Number*/number, /*Boolean*/fixed, /*Number*/precision){
208                        return sc.doIfLoaded("dojo/number", function(numberLib){
209                                return (fixed ? numberLib.format(number, {places : precision}) :
210                                        numberLib.format(number)) || "";
211                        }, function(){
212                                return fixed ? number.toFixed(precision) : number.toString();
213                        });
214                }
215        });
216});
Note: See TracBrowser for help on using the repository browser.