common.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.charting.plot2d.common"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.charting.plot2d.common"] = true;
  8. dojo.provide("dojox.charting.plot2d.common");
  9. dojo.require("dojo.colors");
  10. dojo.require("dojox.gfx");
  11. dojo.require("dojox.lang.functional");
  12. (function(){
  13. var df = dojox.lang.functional, dc = dojox.charting.plot2d.common;
  14. dojo.mixin(dojox.charting.plot2d.common, {
  15. makeStroke: function(stroke){
  16. if(!stroke){ return stroke; }
  17. if(typeof stroke == "string" || stroke instanceof dojo.Color){
  18. stroke = {color: stroke};
  19. }
  20. return dojox.gfx.makeParameters(dojox.gfx.defaultStroke, stroke);
  21. },
  22. augmentColor: function(target, color){
  23. var t = new dojo.Color(target),
  24. c = new dojo.Color(color);
  25. c.a = t.a;
  26. return c;
  27. },
  28. augmentStroke: function(stroke, color){
  29. var s = dc.makeStroke(stroke);
  30. if(s){
  31. s.color = dc.augmentColor(s.color, color);
  32. }
  33. return s;
  34. },
  35. augmentFill: function(fill, color){
  36. var fc, c = new dojo.Color(color);
  37. if(typeof fill == "string" || fill instanceof dojo.Color){
  38. return dc.augmentColor(fill, color);
  39. }
  40. return fill;
  41. },
  42. defaultStats: {
  43. vmin: Number.POSITIVE_INFINITY, vmax: Number.NEGATIVE_INFINITY,
  44. hmin: Number.POSITIVE_INFINITY, hmax: Number.NEGATIVE_INFINITY
  45. },
  46. collectSimpleStats: function(series){
  47. var stats = dojo.delegate(dc.defaultStats);
  48. for(var i = 0; i < series.length; ++i){
  49. var run = series[i];
  50. for(var j = 0; j < run.data.length; j++){
  51. if(run.data[j] !== null){
  52. if(typeof run.data[j] == "number"){
  53. // 1D case
  54. var old_vmin = stats.vmin, old_vmax = stats.vmax;
  55. if(!("ymin" in run) || !("ymax" in run)){
  56. dojo.forEach(run.data, function(val, i){
  57. if(val !== null){
  58. var x = i + 1, y = val;
  59. if(isNaN(y)){ y = 0; }
  60. stats.hmin = Math.min(stats.hmin, x);
  61. stats.hmax = Math.max(stats.hmax, x);
  62. stats.vmin = Math.min(stats.vmin, y);
  63. stats.vmax = Math.max(stats.vmax, y);
  64. }
  65. });
  66. }
  67. if("ymin" in run){ stats.vmin = Math.min(old_vmin, run.ymin); }
  68. if("ymax" in run){ stats.vmax = Math.max(old_vmax, run.ymax); }
  69. }else{
  70. // 2D case
  71. var old_hmin = stats.hmin, old_hmax = stats.hmax,
  72. old_vmin = stats.vmin, old_vmax = stats.vmax;
  73. if(!("xmin" in run) || !("xmax" in run) || !("ymin" in run) || !("ymax" in run)){
  74. dojo.forEach(run.data, function(val, i){
  75. if(val !== null){
  76. var x = "x" in val ? val.x : i + 1, y = val.y;
  77. if(isNaN(x)){ x = 0; }
  78. if(isNaN(y)){ y = 0; }
  79. stats.hmin = Math.min(stats.hmin, x);
  80. stats.hmax = Math.max(stats.hmax, x);
  81. stats.vmin = Math.min(stats.vmin, y);
  82. stats.vmax = Math.max(stats.vmax, y);
  83. }
  84. });
  85. }
  86. if("xmin" in run){ stats.hmin = Math.min(old_hmin, run.xmin); }
  87. if("xmax" in run){ stats.hmax = Math.max(old_hmax, run.xmax); }
  88. if("ymin" in run){ stats.vmin = Math.min(old_vmin, run.ymin); }
  89. if("ymax" in run){ stats.vmax = Math.max(old_vmax, run.ymax); }
  90. }
  91. break;
  92. }
  93. }
  94. }
  95. return stats;
  96. },
  97. calculateBarSize: function(/* Number */ availableSize, /* Object */ opt, /* Number? */ clusterSize){
  98. if(!clusterSize){
  99. clusterSize = 1;
  100. }
  101. var gap = opt.gap, size = (availableSize - 2 * gap) / clusterSize;
  102. if("minBarSize" in opt){
  103. size = Math.max(size, opt.minBarSize);
  104. }
  105. if("maxBarSize" in opt){
  106. size = Math.min(size, opt.maxBarSize);
  107. }
  108. size = Math.max(size, 1);
  109. gap = (availableSize - size * clusterSize) / 2;
  110. return {size: size, gap: gap}; // Object
  111. },
  112. collectStackedStats: function(series){
  113. // collect statistics
  114. var stats = dojo.clone(dc.defaultStats);
  115. if(series.length){
  116. // 1st pass: find the maximal length of runs
  117. stats.hmin = Math.min(stats.hmin, 1);
  118. stats.hmax = df.foldl(series, "seed, run -> Math.max(seed, run.data.length)", stats.hmax);
  119. // 2nd pass: stack values
  120. for(var i = 0; i < stats.hmax; ++i){
  121. var v = series[0].data[i];
  122. v = v && (typeof v == "number" ? v : v.y);
  123. if(isNaN(v)){ v = 0; }
  124. stats.vmin = Math.min(stats.vmin, v);
  125. for(var j = 1; j < series.length; ++j){
  126. var t = series[j].data[i];
  127. t = t && (typeof t == "number" ? t : t.y);
  128. if(isNaN(t)){ t = 0; }
  129. v += t;
  130. }
  131. stats.vmax = Math.max(stats.vmax, v);
  132. }
  133. }
  134. return stats;
  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 arr = a.slice(0);
  141. if(tension == "x") {
  142. arr[arr.length] = arr[0]; // add a last element equal to the first, closing the loop
  143. }
  144. var p=dojo.map(arr, 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-arr[i-1].x, dy=arr[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 = arr[i-1], p2 = arr[i], p3;
  152. var bz1x, bz1y, bz2x, bz2y;
  153. var f = 1/6;
  154. if(i==1) {
  155. if(tension == "x") {
  156. p0 = arr[arr.length-2];
  157. } else { // "tension == X || tension == "S"
  158. p0 = p1;
  159. }
  160. f = 1/3;
  161. } else {
  162. p0 = arr[i-2];
  163. }
  164. if(i==(arr.length-1)) {
  165. if(tension == "x") {
  166. p3 = arr[1];
  167. } else { // "tension == X || tension == "S"
  168. p3 = p2;
  169. }
  170. f = 1/3;
  171. } else {
  172. p3 = arr[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. var p0p2f = p0p2 * f;
  178. var p1p3f = p1p3 * f;
  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. if(tension == "S") {
  190. if(p0 == p1) { p0p2f = 0; }
  191. if(p2 == p3) { p1p3f = 0; }
  192. }
  193. bz1x = p1.x + p0p2f*(p2.x - p0.x)/p0p2;
  194. bz1y = p1.y + p0p2f*(p2.y - p0.y)/p0p2;
  195. bz2x = p2.x - p1p3f*(p3.x - p1.x)/p1p3;
  196. bz2y = p2.y - p1p3f*(p3.y - p1.y)/p1p3;
  197. }
  198. return "C"+(bz1x+","+bz1y+" "+bz2x+","+bz2y+" "+p2.x+","+p2.y);
  199. });
  200. return p.join(" ");
  201. },
  202. getLabel: function(/*Number*/number, /*Boolean*/fixed, /*Number*/precision){
  203. if(dojo.number){
  204. return (fixed ? dojo.number.format(number, {places : precision}) :
  205. dojo.number.format(number)) || "";
  206. }
  207. return fixed ? number.toFixed(precision) : number.toString();
  208. }
  209. });
  210. })();
  211. }