common.js 7.0 KB

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