Default.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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.Default"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.charting.plot2d.Default"] = true;
  8. dojo.provide("dojox.charting.plot2d.Default");
  9. dojo.require("dojox.charting.plot2d.common");
  10. dojo.require("dojox.charting.plot2d.Base");
  11. dojo.require("dojox.lang.utils");
  12. dojo.require("dojox.lang.functional");
  13. dojo.require("dojox.lang.functional.reversed");
  14. dojo.require("dojox.gfx.fx");
  15. /*=====
  16. dojo.declare("dojox.charting.plot2d.__DefaultCtorArgs", dojox.charting.plot2d.__PlotCtorArgs, {
  17. // summary:
  18. // The arguments used for any/most plots.
  19. // hAxis: String?
  20. // The horizontal axis name.
  21. hAxis: "x",
  22. // vAxis: String?
  23. // The vertical axis name
  24. vAxis: "y",
  25. // lines: Boolean?
  26. // Whether or not to draw lines on this plot. Defaults to true.
  27. lines: true,
  28. // areas: Boolean?
  29. // Whether or not to draw areas on this plot. Defaults to false.
  30. areas: false,
  31. // markers: Boolean?
  32. // Whether or not to draw markers at data points on this plot. Default is false.
  33. markers: false,
  34. // tension: Number|String?
  35. // Whether or not to apply 'tensioning' to the lines on this chart.
  36. // Options include a number, "X", "x", or "S"; if a number is used, the
  37. // simpler bezier curve calculations are used to draw the lines. If X, x or S
  38. // is used, the more accurate smoothing algorithm is used.
  39. tension: "",
  40. // animate: Boolean?
  41. // Whether or not to animate the chart to place.
  42. animate: false,
  43. // stroke: dojox.gfx.Stroke?
  44. // An optional stroke to use for any series on the plot.
  45. stroke: {},
  46. // outline: dojox.gfx.Stroke?
  47. // An optional stroke used to outline any series on the plot.
  48. outline: {},
  49. // shadow: dojox.gfx.Stroke?
  50. // An optional stroke to use to draw any shadows for a series on a plot.
  51. shadow: {},
  52. // fill: dojox.gfx.Fill?
  53. // Any fill to be used for elements on the plot (such as areas).
  54. fill: {},
  55. // font: String?
  56. // A font definition to be used for labels and other text-based elements on the plot.
  57. font: "",
  58. // fontColor: String|dojo.Color?
  59. // The color to be used for any text-based elements on the plot.
  60. fontColor: "",
  61. // markerStroke: dojo.gfx.Stroke?
  62. // An optional stroke to use for any markers on the plot.
  63. markerStroke: {},
  64. // markerOutline: dojo.gfx.Stroke?
  65. // An optional outline to use for any markers on the plot.
  66. markerOutline: {},
  67. // markerShadow: dojo.gfx.Stroke?
  68. // An optional shadow to use for any markers on the plot.
  69. markerShadow: {},
  70. // markerFill: dojo.gfx.Fill?
  71. // An optional fill to use for any markers on the plot.
  72. markerFill: {},
  73. // markerFont: String?
  74. // An optional font definition to use for any markers on the plot.
  75. markerFont: "",
  76. // markerFontColor: String|dojo.Color?
  77. // An optional color to use for any marker text on the plot.
  78. markerFontColor: ""
  79. });
  80. =====*/
  81. (function(){
  82. var df = dojox.lang.functional, du = dojox.lang.utils,
  83. dc = dojox.charting.plot2d.common,
  84. purgeGroup = df.lambda("item.purgeGroup()");
  85. var DEFAULT_ANIMATION_LENGTH = 1200; // in ms
  86. dojo.declare("dojox.charting.plot2d.Default", dojox.charting.plot2d.Base, {
  87. defaultParams: {
  88. hAxis: "x", // use a horizontal axis named "x"
  89. vAxis: "y", // use a vertical axis named "y"
  90. lines: true, // draw lines
  91. areas: false, // draw areas
  92. markers: false, // draw markers
  93. tension: "", // draw curved lines (tension is "X", "x", or "S")
  94. animate: false // animate chart to place
  95. },
  96. optionalParams: {
  97. // theme component
  98. stroke: {},
  99. outline: {},
  100. shadow: {},
  101. fill: {},
  102. font: "",
  103. fontColor: "",
  104. markerStroke: {},
  105. markerOutline: {},
  106. markerShadow: {},
  107. markerFill: {},
  108. markerFont: "",
  109. markerFontColor: ""
  110. },
  111. constructor: function(chart, kwArgs){
  112. // summary:
  113. // Return a new plot.
  114. // chart: dojox.charting.Chart2D
  115. // The chart this plot belongs to.
  116. // kwArgs: dojox.charting.plot2d.__DefaultCtorArgs?
  117. // An optional arguments object to help define this plot.
  118. this.opt = dojo.clone(this.defaultParams);
  119. du.updateWithObject(this.opt, kwArgs);
  120. du.updateWithPattern(this.opt, kwArgs, this.optionalParams);
  121. this.series = [];
  122. this.hAxis = this.opt.hAxis;
  123. this.vAxis = this.opt.vAxis;
  124. // animation properties
  125. this.animate = this.opt.animate;
  126. },
  127. render: function(dim, offsets){
  128. // summary:
  129. // Render/draw everything on this plot.
  130. // dim: Object
  131. // An object of the form { width, height }
  132. // offsets: Object
  133. // An object of the form { l, r, t, b }
  134. // returns: dojox.charting.plot2d.Default
  135. // A reference to this plot for functional chaining.
  136. // make sure all the series is not modified
  137. if(this.zoom && !this.isDataDirty()){
  138. return this.performZoom(dim, offsets);
  139. }
  140. this.resetEvents();
  141. this.dirty = this.isDirty();
  142. if(this.dirty){
  143. dojo.forEach(this.series, purgeGroup);
  144. this._eventSeries = {};
  145. this.cleanGroup();
  146. this.group.setTransform(null);
  147. var s = this.group;
  148. df.forEachRev(this.series, function(item){ item.cleanGroup(s); });
  149. }
  150. var t = this.chart.theme, stroke, outline, marker, events = this.events();
  151. for(var i = this.series.length - 1; i >= 0; --i){
  152. var run = this.series[i];
  153. if(!this.dirty && !run.dirty){
  154. t.skip();
  155. this._reconnectEvents(run.name);
  156. continue;
  157. }
  158. run.cleanGroup();
  159. if(!run.data.length){
  160. run.dirty = false;
  161. t.skip();
  162. continue;
  163. }
  164. var theme = t.next(this.opt.areas ? "area" : "line", [this.opt, run], true),
  165. s = run.group, rsegments = [], startindexes = [], rseg = null, lpoly,
  166. ht = this._hScaler.scaler.getTransformerFromModel(this._hScaler),
  167. vt = this._vScaler.scaler.getTransformerFromModel(this._vScaler),
  168. eventSeries = this._eventSeries[run.name] = new Array(run.data.length);
  169. // split the run data into dense segments (each containing no nulls)
  170. for(var j = 0; j < run.data.length; j++){
  171. if(run.data[j] != null){
  172. if(!rseg){
  173. rseg = [];
  174. startindexes.push(j);
  175. rsegments.push(rseg)
  176. }
  177. rseg.push(run.data[j]);
  178. }else{
  179. rseg = null;
  180. }
  181. }
  182. for(var seg = 0; seg < rsegments.length; seg++){
  183. if(typeof rsegments[seg][0] == "number"){
  184. lpoly = dojo.map(rsegments[seg], function(v, i){
  185. return {
  186. x: ht(i + startindexes[seg] + 1) + offsets.l,
  187. y: dim.height - offsets.b - vt(v)
  188. };
  189. }, this);
  190. }else{
  191. lpoly = dojo.map(rsegments[seg], function(v, i){
  192. return {
  193. x: ht(v.x) + offsets.l,
  194. y: dim.height - offsets.b - vt(v.y)
  195. };
  196. }, this);
  197. }
  198. var lpath = this.opt.tension ? dc.curve(lpoly, this.opt.tension) : "";
  199. if(this.opt.areas && lpoly.length > 1){
  200. var fill = theme.series.fill;
  201. var apoly = dojo.clone(lpoly);
  202. if(this.opt.tension){
  203. var apath = "L" + apoly[apoly.length-1].x + "," + (dim.height - offsets.b) +
  204. " L" + apoly[0].x + "," + (dim.height - offsets.b) +
  205. " L" + apoly[0].x + "," + apoly[0].y;
  206. run.dyn.fill = s.createPath(lpath + " " + apath).setFill(fill).getFill();
  207. } else {
  208. apoly.push({x: lpoly[lpoly.length - 1].x, y: dim.height - offsets.b});
  209. apoly.push({x: lpoly[0].x, y: dim.height - offsets.b});
  210. apoly.push(lpoly[0]);
  211. run.dyn.fill = s.createPolyline(apoly).setFill(fill).getFill();
  212. }
  213. }
  214. if(this.opt.lines || this.opt.markers){
  215. // need a stroke
  216. stroke = theme.series.stroke;
  217. if(theme.series.outline){
  218. outline = run.dyn.outline = dc.makeStroke(theme.series.outline);
  219. outline.width = 2 * outline.width + stroke.width;
  220. }
  221. }
  222. if(this.opt.markers){
  223. run.dyn.marker = theme.symbol;
  224. }
  225. var frontMarkers = null, outlineMarkers = null, shadowMarkers = null;
  226. if(stroke && theme.series.shadow && lpoly.length > 1){
  227. var shadow = theme.series.shadow,
  228. spoly = dojo.map(lpoly, function(c){
  229. return {x: c.x + shadow.dx, y: c.y + shadow.dy};
  230. });
  231. if(this.opt.lines){
  232. if(this.opt.tension){
  233. run.dyn.shadow = s.createPath(dc.curve(spoly, this.opt.tension)).setStroke(shadow).getStroke();
  234. } else {
  235. run.dyn.shadow = s.createPolyline(spoly).setStroke(shadow).getStroke();
  236. }
  237. }
  238. if(this.opt.markers && theme.marker.shadow){
  239. shadow = theme.marker.shadow;
  240. shadowMarkers = dojo.map(spoly, function(c){
  241. return s.createPath("M" + c.x + " " + c.y + " " + theme.symbol).
  242. setStroke(shadow).setFill(shadow.color);
  243. }, this);
  244. }
  245. }
  246. if(this.opt.lines && lpoly.length > 1){
  247. if(outline){
  248. if(this.opt.tension){
  249. run.dyn.outline = s.createPath(lpath).setStroke(outline).getStroke();
  250. } else {
  251. run.dyn.outline = s.createPolyline(lpoly).setStroke(outline).getStroke();
  252. }
  253. }
  254. if(this.opt.tension){
  255. run.dyn.stroke = s.createPath(lpath).setStroke(stroke).getStroke();
  256. } else {
  257. run.dyn.stroke = s.createPolyline(lpoly).setStroke(stroke).getStroke();
  258. }
  259. }
  260. if(this.opt.markers){
  261. frontMarkers = new Array(lpoly.length);
  262. outlineMarkers = new Array(lpoly.length);
  263. outline = null;
  264. if(theme.marker.outline){
  265. outline = dc.makeStroke(theme.marker.outline);
  266. outline.width = 2 * outline.width + (theme.marker.stroke ? theme.marker.stroke.width : 0);
  267. }
  268. dojo.forEach(lpoly, function(c, i){
  269. var path = "M" + c.x + " " + c.y + " " + theme.symbol;
  270. if(outline){
  271. outlineMarkers[i] = s.createPath(path).setStroke(outline);
  272. }
  273. frontMarkers[i] = s.createPath(path).setStroke(theme.marker.stroke).setFill(theme.marker.fill);
  274. }, this);
  275. run.dyn.markerFill = theme.marker.fill;
  276. run.dyn.markerStroke = theme.marker.stroke;
  277. if(events){
  278. dojo.forEach(frontMarkers, function(s, i){
  279. var o = {
  280. element: "marker",
  281. index: i + startindexes[seg],
  282. run: run,
  283. shape: s,
  284. outline: outlineMarkers[i] || null,
  285. shadow: shadowMarkers && shadowMarkers[i] || null,
  286. cx: lpoly[i].x,
  287. cy: lpoly[i].y
  288. };
  289. if(typeof rsegments[seg][0] == "number"){
  290. o.x = i + startindexes[seg] + 1;
  291. o.y = rsegments[seg][i];
  292. }else{
  293. o.x = rsegments[seg][i].x;
  294. o.y = rsegments[seg][i].y;
  295. }
  296. this._connectEvents(o);
  297. eventSeries[i + startindexes[seg]] = o;
  298. }, this);
  299. }else{
  300. delete this._eventSeries[run.name];
  301. }
  302. }
  303. }
  304. run.dirty = false;
  305. }
  306. if(this.animate){
  307. // grow from the bottom
  308. var plotGroup = this.group;
  309. dojox.gfx.fx.animateTransform(dojo.delegate({
  310. shape: plotGroup,
  311. duration: DEFAULT_ANIMATION_LENGTH,
  312. transform:[
  313. {name:"translate", start: [0, dim.height - offsets.b], end: [0, 0]},
  314. {name:"scale", start: [1, 0], end:[1, 1]},
  315. {name:"original"}
  316. ]
  317. }, this.animate)).play();
  318. }
  319. this.dirty = false;
  320. return this; // dojox.charting.plot2d.Default
  321. }
  322. });
  323. })();
  324. }