Candlesticks.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. define("dojox/charting/plot2d/Candlesticks", ["dojo/_base/lang", "dojo/_base/declare", "dojo/_base/array", "./Base", "./common",
  2. "dojox/lang/functional", "dojox/lang/functional/reversed", "dojox/lang/utils", "dojox/gfx/fx"],
  3. function(lang, declare, arr, Base, dc, df, dfr, du, fx){
  4. /*=====
  5. var Base = dojox.charting.plot2d.Base;
  6. =====*/
  7. var purgeGroup = dfr.lambda("item.purgeGroup()");
  8. // Candlesticks are based on the Bars plot type; we expect the following passed
  9. // as values in a series:
  10. // { x?, open, close, high, low, mid? }
  11. // if x is not provided, the array index is used.
  12. // failing to provide the OHLC values will throw an error.
  13. return declare("dojox.charting.plot2d.Candlesticks", Base, {
  14. // summary:
  15. // A plot that represents typical candlesticks (financial reporting, primarily).
  16. // Unlike most charts, the Candlestick expects data points to be represented by
  17. // an object of the form { x?, open, close, high, low, mid? }, where both
  18. // x and mid are optional parameters. If x is not provided, the index of the
  19. // data array is used.
  20. defaultParams: {
  21. hAxis: "x", // use a horizontal axis named "x"
  22. vAxis: "y", // use a vertical axis named "y"
  23. gap: 2, // gap between columns in pixels
  24. animate: null // animate bars into place
  25. },
  26. optionalParams: {
  27. minBarSize: 1, // minimal candle width in pixels
  28. maxBarSize: 1, // maximal candle width in pixels
  29. // theme component
  30. stroke: {},
  31. outline: {},
  32. shadow: {},
  33. fill: {},
  34. font: "",
  35. fontColor: ""
  36. },
  37. constructor: function(chart, kwArgs){
  38. // summary:
  39. // The constructor for a candlestick chart.
  40. // chart: dojox.charting.Chart
  41. // The chart this plot belongs to.
  42. // kwArgs: dojox.charting.plot2d.__BarCtorArgs?
  43. // An optional keyword arguments object to help define the plot.
  44. this.opt = lang.clone(this.defaultParams);
  45. du.updateWithObject(this.opt, kwArgs);
  46. du.updateWithPattern(this.opt, kwArgs, this.optionalParams);
  47. this.series = [];
  48. this.hAxis = this.opt.hAxis;
  49. this.vAxis = this.opt.vAxis;
  50. this.animate = this.opt.animate;
  51. },
  52. collectStats: function(series){
  53. // summary:
  54. // Collect all statistics for drawing this chart. Since the common
  55. // functionality only assumes x and y, Candlesticks must create it's own
  56. // stats (since data has no y value, but open/close/high/low instead).
  57. // series: dojox.charting.Series[]
  58. // The data series array to be drawn on this plot.
  59. // returns: Object
  60. // Returns an object in the form of { hmin, hmax, vmin, vmax }.
  61. // we have to roll our own, since we need to use all four passed
  62. // values to figure out our stats, and common only assumes x and y.
  63. var stats = lang.delegate(dc.defaultStats);
  64. for(var i=0; i<series.length; i++){
  65. var run = series[i];
  66. if(!run.data.length){ continue; }
  67. var old_vmin = stats.vmin, old_vmax = stats.vmax;
  68. if(!("ymin" in run) || !("ymax" in run)){
  69. arr.forEach(run.data, function(val, idx){
  70. if(val !== null){
  71. var x = val.x || idx + 1;
  72. stats.hmin = Math.min(stats.hmin, x);
  73. stats.hmax = Math.max(stats.hmax, x);
  74. stats.vmin = Math.min(stats.vmin, val.open, val.close, val.high, val.low);
  75. stats.vmax = Math.max(stats.vmax, val.open, val.close, val.high, val.low);
  76. }
  77. });
  78. }
  79. if("ymin" in run){ stats.vmin = Math.min(old_vmin, run.ymin); }
  80. if("ymax" in run){ stats.vmax = Math.max(old_vmax, run.ymax); }
  81. }
  82. return stats; // Object
  83. },
  84. getSeriesStats: function(){
  85. // summary:
  86. // Calculate the min/max on all attached series in both directions.
  87. // returns: Object
  88. // {hmin, hmax, vmin, vmax} min/max in both directions.
  89. var stats = this.collectStats(this.series);
  90. stats.hmin -= 0.5;
  91. stats.hmax += 0.5;
  92. return stats;
  93. },
  94. render: function(dim, offsets){
  95. // summary:
  96. // Run the calculations for any axes for this plot.
  97. // dim: Object
  98. // An object in the form of { width, height }
  99. // offsets: Object
  100. // An object of the form { l, r, t, b}.
  101. // returns: dojox.charting.plot2d.Candlesticks
  102. // A reference to this plot for functional chaining.
  103. if(this.zoom && !this.isDataDirty()){
  104. return this.performZoom(dim, offsets);
  105. }
  106. this.resetEvents();
  107. this.dirty = this.isDirty();
  108. if(this.dirty){
  109. arr.forEach(this.series, purgeGroup);
  110. this._eventSeries = {};
  111. this.cleanGroup();
  112. var s = this.group;
  113. df.forEachRev(this.series, function(item){ item.cleanGroup(s); });
  114. }
  115. var t = this.chart.theme, f, gap, width,
  116. ht = this._hScaler.scaler.getTransformerFromModel(this._hScaler),
  117. vt = this._vScaler.scaler.getTransformerFromModel(this._vScaler),
  118. baseline = Math.max(0, this._vScaler.bounds.lower),
  119. baselineHeight = vt(baseline),
  120. events = this.events();
  121. f = dc.calculateBarSize(this._hScaler.bounds.scale, this.opt);
  122. gap = f.gap;
  123. width = f.size;
  124. for(var i = this.series.length - 1; i >= 0; --i){
  125. var run = this.series[i];
  126. if(!this.dirty && !run.dirty){
  127. t.skip();
  128. this._reconnectEvents(run.name);
  129. continue;
  130. }
  131. run.cleanGroup();
  132. var theme = t.next("candlestick", [this.opt, run]), s = run.group,
  133. eventSeries = new Array(run.data.length);
  134. for(var j = 0; j < run.data.length; ++j){
  135. var v = run.data[j];
  136. if(v !== null){
  137. var finalTheme = t.addMixin(theme, "candlestick", v, true);
  138. // calculate the points we need for OHLC
  139. var x = ht(v.x || (j+0.5)) + offsets.l + gap,
  140. y = dim.height - offsets.b,
  141. open = vt(v.open),
  142. close = vt(v.close),
  143. high = vt(v.high),
  144. low = vt(v.low);
  145. if("mid" in v){
  146. var mid = vt(v.mid);
  147. }
  148. if(low > high){
  149. var tmp = high;
  150. high = low;
  151. low = tmp;
  152. }
  153. if(width >= 1){
  154. // draw the line and rect, set up as a group and pass that to the events.
  155. var doFill = open > close;
  156. var line = { x1: width/2, x2: width/2, y1: y - high, y2: y - low },
  157. rect = {
  158. x: 0, y: y-Math.max(open, close),
  159. width: width, height: Math.max(doFill ? open-close : close-open, 1)
  160. };
  161. var shape = s.createGroup();
  162. shape.setTransform({dx: x, dy: 0 });
  163. var inner = shape.createGroup();
  164. inner.createLine(line).setStroke(finalTheme.series.stroke);
  165. inner.createRect(rect).setStroke(finalTheme.series.stroke).
  166. setFill(doFill ? finalTheme.series.fill : "white");
  167. if("mid" in v){
  168. // add the mid line.
  169. inner.createLine({
  170. x1: (finalTheme.series.stroke.width||1), x2: width - (finalTheme.series.stroke.width || 1),
  171. y1: y - mid, y2: y - mid
  172. }).setStroke(doFill ? "white" : finalTheme.series.stroke);
  173. }
  174. // TODO: double check this.
  175. run.dyn.fill = finalTheme.series.fill;
  176. run.dyn.stroke = finalTheme.series.stroke;
  177. if(events){
  178. var o = {
  179. element: "candlestick",
  180. index: j,
  181. run: run,
  182. shape: inner,
  183. x: x,
  184. y: y-Math.max(open, close),
  185. cx: width/2,
  186. cy: (y-Math.max(open, close)) + (Math.max(doFill ? open-close : close-open, 1)/2),
  187. width: width,
  188. height: Math.max(doFill ? open-close : close-open, 1),
  189. data: v
  190. };
  191. this._connectEvents(o);
  192. eventSeries[j] = o;
  193. }
  194. }
  195. if(this.animate){
  196. this._animateCandlesticks(shape, y - low, high - low);
  197. }
  198. }
  199. }
  200. this._eventSeries[run.name] = eventSeries;
  201. run.dirty = false;
  202. }
  203. this.dirty = false;
  204. return this; // dojox.charting.plot2d.Candlesticks
  205. },
  206. _animateCandlesticks: function(shape, voffset, vsize){
  207. fx.animateTransform(lang.delegate({
  208. shape: shape,
  209. duration: 1200,
  210. transform: [
  211. {name: "translate", start: [0, voffset - (voffset/vsize)], end: [0, 0]},
  212. {name: "scale", start: [1, 1/vsize], end: [1, 1]},
  213. {name: "original"}
  214. ]
  215. }, this.animate)).play();
  216. }
  217. });
  218. });