]> git.pond.sub.org Git - eow/blob - static/dojo-release-1.1.1/dojox/charting/plot2d/common.js
add Dojo 1.1.1
[eow] / static / dojo-release-1.1.1 / dojox / charting / plot2d / common.js
1 if(!dojo._hasResource["dojox.charting.plot2d.common"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2 dojo._hasResource["dojox.charting.plot2d.common"] = true;
3 dojo.provide("dojox.charting.plot2d.common");
4
5 dojo.require("dojo.colors");
6 dojo.require("dojox.gfx");
7 dojo.require("dojox.lang.functional");
8
9 (function(){
10         var df = dojox.lang.functional, dc = dojox.charting.plot2d.common;
11         
12         dojo.mixin(dojox.charting.plot2d.common, {
13                 makeStroke: function(stroke){
14                         if(!stroke){ return stroke; }
15                         if(typeof stroke == "string" || stroke instanceof dojo.Color){
16                                 stroke = {color: stroke};
17                         }
18                         return dojox.gfx.makeParameters(dojox.gfx.defaultStroke, stroke);
19                 },
20                 augmentColor: function(target, color){
21                         var t = new dojo.Color(target),
22                                 c = new dojo.Color(color);
23                         c.a = t.a;
24                         return c;
25                 },
26                 augmentStroke: function(stroke, color){
27                         var s = dc.makeStroke(stroke);
28                         if(s){
29                                 s.color = dc.augmentColor(s.color, color);
30                         }
31                         return s;
32                 },
33                 augmentFill: function(fill, color){
34                         var fc, c = new dojo.Color(color);
35                         if(typeof fill == "string" || fill instanceof dojo.Color){
36                                 return dc.augmentColor(fill, color);
37                         }
38                         return fill;
39                 },
40                 
41                 defaultStats: {hmin: Number.POSITIVE_INFINITY, hmax: Number.NEGATIVE_INFINITY, 
42                         vmin: Number.POSITIVE_INFINITY, vmax: Number.NEGATIVE_INFINITY},
43                 
44                 collectSimpleStats: function(series){
45                         var stats = dojo.clone(dc.defaultStats);
46                         for(var i = 0; i < series.length; ++i){
47                                 var run = series[i];
48                                 if(!run.data.length){ continue; }
49                                 if(typeof run.data[0] == "number"){
50                                         // 1D case
51                                         var old_vmin = stats.vmin, old_vmax = stats.vmax;
52                                         if(!("ymin" in run) || !("ymax" in run)){
53                                                 dojo.forEach(run.data, function(val, i){
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                                         if("ymin" in run){ stats.vmin = Math.min(old_vmin, run.ymin); }
63                                         if("ymax" in run){ stats.vmax = Math.max(old_vmax, run.ymax); }
64                                 }else{
65                                         // 2D case
66                                         var old_hmin = stats.hmin, old_hmax = stats.hmax,
67                                                 old_vmin = stats.vmin, old_vmax = stats.vmax;
68                                         if(!("xmin" in run) || !("xmax" in run) || !("ymin" in run) || !("ymax" in run)){
69                                                 dojo.forEach(run.data, function(val, i){
70                                                         var x = val.x, 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                                         if("xmin" in run){ stats.hmin = Math.min(old_hmin, run.xmin); }
80                                         if("xmax" in run){ stats.hmax = Math.max(old_hmax, run.xmax); }
81                                         if("ymin" in run){ stats.vmin = Math.min(old_vmin, run.ymin); }
82                                         if("ymax" in run){ stats.vmax = Math.max(old_vmax, run.ymax); }
83                                 }
84                         }
85                         return stats;
86                 },
87                 
88                 collectStackedStats: function(series){
89                         // collect statistics
90                         var stats = dojo.clone(dc.defaultStats);
91                         if(series.length){
92                                 // 1st pass: find the maximal length of runs
93                                 stats.hmin = Math.min(stats.hmin, 1);
94                                 stats.hmax = df.foldl(series, "seed, run -> Math.max(seed, run.data.length)", stats.hmax);
95                                 // 2nd pass: stack values
96                                 for(var i = 0; i < stats.hmax; ++i){
97                                         var v = series[0].data[i];
98                                         if(isNaN(v)){ v = 0; }
99                                         stats.vmin = Math.min(stats.vmin, v);
100                                         for(var j = 1; j < series.length; ++j){
101                                                 var t = series[j].data[i];
102                                                 if(isNaN(t)){ t = 0; }
103                                                 v += t;
104                                         }
105                                         stats.vmax = Math.max(stats.vmax, v);
106                                 }
107                         }
108                         return stats;
109                 }
110         });
111 })();
112
113 }