Version 3.18.1
Show:

File: charts/js/CandlestickSeries.js

  1. /**
  2. * Provides functionality for creating a candlestick series.
  3. *
  4. * @module charts
  5. * @submodule series-candlestick
  6. */
  7. /**
  8. * The CandlestickSeries class renders columns (candles) and lines (wicks) representing the open, high, low and close
  9. * values for a chart.
  10. *
  11. * @class CandlestickSeries
  12. * @extends RangeSeries
  13. * @constructor
  14. * @param {Object} config (optional) Configuration parameters.
  15. * @submodule series-candlestick
  16. */
  17. function CandlestickSeries()
  18. {
  19. CandlestickSeries.superclass.constructor.apply(this, arguments);
  20. }
  21. CandlestickSeries.NAME = "candlestickSeries";
  22. CandlestickSeries.ATTRS = {
  23. /**
  24. * Read-only attribute indicating the type of series.
  25. *
  26. * @attribute type
  27. * @type String
  28. * @readOnly
  29. * @default candlestick
  30. */
  31. type: {
  32. value: "candlestick"
  33. },
  34. /**
  35. * The graphic in which drawings will be rendered.
  36. *
  37. * @attribute graphic
  38. * @type Graphic
  39. */
  40. graphic: {
  41. lazyAdd: false,
  42. setter: function(val) {
  43. //woraround for Attribute order of operations bug
  44. if(!this.get("rendered")) {
  45. this.set("rendered", true);
  46. }
  47. this.set("upcandle", val.addShape({
  48. type: "path"
  49. }));
  50. this.set("downcandle", val.addShape({
  51. type: "path"
  52. }));
  53. this.set("wick", val.addShape({
  54. type: "path"
  55. }));
  56. return val;
  57. }
  58. },
  59. /**
  60. * Reference to the candlestick used when the close value is higher than the open value.
  61. *
  62. * @attribute upcandle
  63. * @type Path
  64. */
  65. upcandle: {},
  66. /**
  67. * Reference to the candlestick used when the open value is higher than the close value.
  68. *
  69. * @attribute downcandle
  70. * @type Path
  71. */
  72. downcandle: {},
  73. /**
  74. * Reference to the line drawn between the high and low values.
  75. *
  76. * @attribute wick
  77. * @type Path
  78. */
  79. wick: {}
  80. /**
  81. * Style properties used for drawing candles and wicks. This attribute is inherited from `RangeSeries`. Below are the default values:
  82. * <dl>
  83. * <dt>upcandle</dt><dd>Properties for a candle representing a period that closes higher than it opens.
  84. * <dl>
  85. * <dt>fill</dt><dd>A hash containing the following values:
  86. * <dl>
  87. * <dt>color</dt><dd>Color of the fill. The default value is "#00aa00".</dd>
  88. * </dd>
  89. * <dt>alpha</dt><dd>Number from 0 to 1 indicating the opacity of the marker fill. The default value is 1.</dd>
  90. * </dl>
  91. * </dd>
  92. * <dt>border</dt><dd>A hash containing the following values:
  93. * <dl>
  94. * <dt>color</dt><dd>Color of the border. The default value is "#000000".</dd>
  95. * <dt>alpha</dt><dd>Number from 0 to 1 indicating the opacity of the marker border. The default value is 1.</dd>
  96. * <dt>weight</dt><dd>Number indicating the width of the border. The default value is 0.</dd>
  97. * </dl>
  98. * </dd>
  99. * </dl>
  100. * </dd>
  101. * <dt>downcandle</dt><dd>Properties for a candle representing a period that opens higher than it closes.
  102. * <dl>
  103. * <dt>fill</dt><dd>A hash containing the following values:
  104. * <dl>
  105. * <dt>color</dt><dd>Color of the fill. The default value is "#aa0000".</dd>
  106. * </dd>
  107. * <dt>alpha</dt><dd>Number from 0 to 1 indicating the opacity of the marker fill. The default value is 1.</dd>
  108. * </dl>
  109. * </dd>
  110. * <dt>border</dt><dd>A hash containing the following values:
  111. * <dl>
  112. * <dt>color</dt><dd>Color of the border. The default value is "#000000".</dd>
  113. * <dt>alpha</dt><dd>Number from 0 to 1 indicating the opacity of the marker border. The default value is 1.</dd>
  114. * <dt>weight</dt><dd>Number indicating the width of the border. The default value is 0.</dd>
  115. * </dl>
  116. * </dd>
  117. * </dl>
  118. * </dd>
  119. * <dt>wick</dt><dd>Properties for the wick, which is a line drawn from the high point of the period to the low point of the period.
  120. * <dl>
  121. * <dt>color</dt><dd>The color of the wick. The default value is "#000000".</dd>
  122. * <dt>weight</dt><dd>The weight of the wick. The default value is 1.</dd>
  123. * <dt>alpha</dt><dd>Number from 0 to 1 indicating the opacity of the wick. The default value is 1.</dd>
  124. * </dl>
  125. * </dd>
  126. * </dl>
  127. *
  128. * @attribute styles
  129. * @type Object
  130. */
  131. };
  132. Y.extend(CandlestickSeries, Y.RangeSeries, {
  133. /**
  134. * Draws markers for an Candlestick series.
  135. *
  136. * @method
  137. * @param {Array} xcoords The xcoordinates to be plotted.
  138. * @param {Array} opencoords The coordinates representing the open values.
  139. * @param {Array} highcoords The coordinates representing the high values.
  140. * @param {Array} lowcoords The coordinates representing the low values.
  141. * @param {Array} closecoords The coordinates representing the close values.
  142. * @param {Number} len The number of x coordinates to plot.
  143. * @param {Number} width The width of each candlestick marker.
  144. * @param {Number} halfwidth Half the width of each candlestick marker.
  145. * @param {Object} styles The styles for the series.
  146. * @private
  147. */
  148. _drawMarkers: function(xcoords, opencoords, highcoords, lowcoords, closecoords, len, width, halfwidth, styles)
  149. {
  150. var upcandle = this.get("upcandle"),
  151. downcandle = this.get("downcandle"),
  152. candle,
  153. wick = this.get("wick"),
  154. wickStyles = styles.wick,
  155. wickWidth = wickStyles.width,
  156. cx,
  157. opencoord,
  158. highcoord,
  159. lowcoord,
  160. closecoord,
  161. left,
  162. right,
  163. top,
  164. bottom,
  165. height,
  166. leftPadding = styles.padding.left,
  167. up,
  168. i,
  169. isNumber = Y.Lang.isNumber;
  170. upcandle.set(styles.upcandle);
  171. downcandle.set(styles.downcandle);
  172. wick.set({
  173. fill: wickStyles.fill,
  174. stroke: wickStyles.stroke,
  175. shapeRendering: wickStyles.shapeRendering
  176. });
  177. upcandle.clear();
  178. downcandle.clear();
  179. wick.clear();
  180. for(i = 0; i < len; i = i + 1)
  181. {
  182. cx = Math.round(xcoords[i] + leftPadding);
  183. left = cx - halfwidth;
  184. right = cx + halfwidth;
  185. opencoord = Math.round(opencoords[i]);
  186. highcoord = Math.round(highcoords[i]);
  187. lowcoord = Math.round(lowcoords[i]);
  188. closecoord = Math.round(closecoords[i]);
  189. up = opencoord > closecoord;
  190. top = up ? closecoord : opencoord;
  191. bottom = up ? opencoord : closecoord;
  192. height = bottom - top;
  193. candle = up ? upcandle : downcandle;
  194. if(candle && isNumber(left) && isNumber(top) && isNumber(width) && isNumber(height))
  195. {
  196. candle.drawRect(left, top, width, height);
  197. }
  198. if(isNumber(cx) && isNumber(highcoord) && isNumber(lowcoord))
  199. {
  200. wick.drawRect(cx - wickWidth/2, highcoord, wickWidth, lowcoord - highcoord);
  201. }
  202. }
  203. upcandle.end();
  204. downcandle.end();
  205. wick.end();
  206. wick.toBack();
  207. },
  208. /**
  209. * Toggles visibility
  210. *
  211. * @method _toggleVisible
  212. * @param {Boolean} visible indicates visibilitye
  213. * @private
  214. */
  215. _toggleVisible: function(visible)
  216. {
  217. this.get("upcandle").set("visible", visible);
  218. this.get("downcandle").set("visible", visible);
  219. this.get("wick").set("visible", visible);
  220. },
  221. /**
  222. * Destructor implementation for the CartesianSeries class. Calls destroy on all Graphic instances.
  223. *
  224. * @method destructor
  225. * @protected
  226. */
  227. destructor: function()
  228. {
  229. var upcandle = this.get("upcandle"),
  230. downcandle = this.get("downcandle"),
  231. wick = this.get("wick");
  232. if(upcandle)
  233. {
  234. upcandle.destroy();
  235. }
  236. if(downcandle)
  237. {
  238. downcandle.destroy();
  239. }
  240. if(wick)
  241. {
  242. wick.destroy();
  243. }
  244. },
  245. /**
  246. * Gets the default value for the `styles` attribute. Overrides
  247. * base implementation.
  248. *
  249. * @method _getDefaultStyles
  250. * @return Object
  251. * @private
  252. */
  253. _getDefaultStyles: function()
  254. {
  255. var styles = {
  256. upcandle: {
  257. shapeRendering: "crispEdges",
  258. fill: {
  259. color: "#00aa00",
  260. alpha: 1
  261. },
  262. stroke: {
  263. color: "#000000",
  264. alpha: 1,
  265. weight: 0
  266. }
  267. },
  268. downcandle: {
  269. shapeRendering: "crispEdges",
  270. fill: {
  271. color: "#aa0000",
  272. alpha: 1
  273. },
  274. stroke: {
  275. color: "#000000",
  276. alpha: 1,
  277. weight: 0
  278. }
  279. },
  280. wick: {
  281. shapeRendering: "crispEdges",
  282. width: 1,
  283. fill: {
  284. color: "#000000",
  285. alpha: 1
  286. },
  287. stroke: {
  288. color: "#000000",
  289. alpha: 1,
  290. weight: 0
  291. }
  292. }
  293. };
  294. return this._mergeStyles(styles, CandlestickSeries.superclass._getDefaultStyles());
  295. }
  296. });
  297. Y.CandlestickSeries = CandlestickSeries;