jqplot.ohlcRenderer.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /**
  2. * jqPlot
  3. * Pure JavaScript plotting plugin using jQuery
  4. *
  5. * Version: @VERSION
  6. * Revision: @REVISION
  7. *
  8. * Copyright (c) 2009-2013 Chris Leonello
  9. * jqPlot is currently available for use in all personal or commercial projects
  10. * under both the MIT (http://www.opensource.org/licenses/mit-license.php) and GPL
  11. * version 2.0 (http://www.gnu.org/licenses/gpl-2.0.html) licenses. This means that you can
  12. * choose the license that best suits your project and use it accordingly.
  13. *
  14. * Although not required, the author would appreciate an email letting him
  15. * know of any substantial use of jqPlot. You can reach the author at:
  16. * chris at jqplot dot com or see http://www.jqplot.com/info.php .
  17. *
  18. * If you are feeling kind and generous, consider supporting the project by
  19. * making a donation at: http://www.jqplot.com/donate.php .
  20. *
  21. * sprintf functions contained in jqplot.sprintf.js by Ash Searle:
  22. *
  23. * version 2007.04.27
  24. * author Ash Searle
  25. * http://hexmen.com/blog/2007/03/printf-sprintf/
  26. * http://hexmen.com/js/sprintf.js
  27. * The author (Ash Searle) has placed this code in the public domain:
  28. * "This code is unrestricted: you are free to use it however you like."
  29. *
  30. */
  31. (function($) {
  32. /**
  33. * Class: $.jqplot.OHLCRenderer
  34. * jqPlot Plugin to draw Open Hi Low Close, Candlestick and Hi Low Close charts.
  35. *
  36. * To use this plugin, include the renderer js file in
  37. * your source:
  38. *
  39. * > <script type="text/javascript" src="plugins/jqplot.ohlcRenderer.js"></script>
  40. *
  41. * You will most likely want to use a date axis renderer
  42. * for the x axis also, so include the date axis render js file also:
  43. *
  44. * > <script type="text/javascript" src="plugins/jqplot.dateAxisRenderer.js"></script>
  45. *
  46. * Then you set the renderer in the series options on your plot:
  47. *
  48. * > series: [{renderer:$.jqplot.OHLCRenderer}]
  49. *
  50. * For OHLC and candlestick charts, data should be specified
  51. * like so:
  52. *
  53. * > dat = [['07/06/2009',138.7,139.68,135.18,135.4], ['06/29/2009',143.46,144.66,139.79,140.02], ...]
  54. *
  55. * If the data array has only 4 values per point instead of 5,
  56. * the renderer will create a Hi Low Close chart instead. In that case,
  57. * data should be supplied like:
  58. *
  59. * > dat = [['07/06/2009',139.68,135.18,135.4], ['06/29/2009',144.66,139.79,140.02], ...]
  60. *
  61. * To generate a candlestick chart instead of an OHLC chart,
  62. * set the "candlestick" option to true:
  63. *
  64. * > series: [{renderer:$.jqplot.OHLCRenderer, rendererOptions:{candleStick:true}}],
  65. *
  66. */
  67. $.jqplot.OHLCRenderer = function(){
  68. // subclass line renderer to make use of some of its methods.
  69. $.jqplot.LineRenderer.call(this);
  70. // prop: candleStick
  71. // true to render chart as candleStick.
  72. // Must have an open price, cannot be a hlc chart.
  73. this.candleStick = false;
  74. // prop: tickLength
  75. // length of the line in pixels indicating open and close price.
  76. // Default will auto calculate based on plot width and
  77. // number of points displayed.
  78. this.tickLength = 'auto';
  79. // prop: bodyWidth
  80. // width of the candlestick body in pixels. Default will auto calculate
  81. // based on plot width and number of candlesticks displayed.
  82. this.bodyWidth = 'auto';
  83. // prop: openColor
  84. // color of the open price tick mark. Default is series color.
  85. this.openColor = null;
  86. // prop: closeColor
  87. // color of the close price tick mark. Default is series color.
  88. this.closeColor = null;
  89. // prop: wickColor
  90. // color of the hi-lo line thorugh the candlestick body.
  91. // Default is the series color.
  92. this.wickColor = null;
  93. // prop: fillUpBody
  94. // true to render an "up" day (close price greater than open price)
  95. // with a filled candlestick body.
  96. this.fillUpBody = false;
  97. // prop: fillDownBody
  98. // true to render a "down" day (close price lower than open price)
  99. // with a filled candlestick body.
  100. this.fillDownBody = true;
  101. // prop: upBodyColor
  102. // Color of candlestick body of an "up" day. Default is series color.
  103. this.upBodyColor = null;
  104. // prop: downBodyColor
  105. // Color of candlestick body on a "down" day. Default is series color.
  106. this.downBodyColor = null;
  107. // prop: hlc
  108. // true if is a hi-low-close chart (no open price).
  109. // This is determined automatically from the series data.
  110. this.hlc = false;
  111. // prop: lineWidth
  112. // Width of the hi-low line and open/close ticks.
  113. // Must be set in the rendererOptions for the series.
  114. this.lineWidth = 1.5;
  115. this._tickLength;
  116. this._bodyWidth;
  117. };
  118. $.jqplot.OHLCRenderer.prototype = new $.jqplot.LineRenderer();
  119. $.jqplot.OHLCRenderer.prototype.constructor = $.jqplot.OHLCRenderer;
  120. // called with scope of series.
  121. $.jqplot.OHLCRenderer.prototype.init = function(options) {
  122. options = options || {};
  123. // lineWidth has to be set on the series, changes in renderer
  124. // constructor have no effect. set the default here
  125. // if no renderer option for lineWidth is specified.
  126. this.lineWidth = options.lineWidth || 1.5;
  127. $.jqplot.LineRenderer.prototype.init.call(this, options);
  128. this._type = 'ohlc';
  129. // set the yaxis data bounds here to account for hi and low values
  130. var db = this._yaxis._dataBounds;
  131. var d = this._plotData;
  132. // if data points have less than 5 values, force a hlc chart.
  133. if (d[0].length < 5) {
  134. this.renderer.hlc = true;
  135. for (var j=0; j<d.length; j++) {
  136. if (d[j][2] < db.min || db.min == null) {
  137. db.min = d[j][2];
  138. }
  139. if (d[j][1] > db.max || db.max == null) {
  140. db.max = d[j][1];
  141. }
  142. }
  143. }
  144. else {
  145. for (var j=0; j<d.length; j++) {
  146. if (d[j][3] < db.min || db.min == null) {
  147. db.min = d[j][3];
  148. }
  149. if (d[j][2] > db.max || db.max == null) {
  150. db.max = d[j][2];
  151. }
  152. }
  153. }
  154. };
  155. // called within scope of series.
  156. $.jqplot.OHLCRenderer.prototype.draw = function(ctx, gd, options) {
  157. var d = this.data;
  158. var xmin = this._xaxis.min;
  159. var xmax = this._xaxis.max;
  160. // index of last value below range of plot.
  161. var xminidx = 0;
  162. // index of first value above range of plot.
  163. var xmaxidx = d.length;
  164. var xp = this._xaxis.series_u2p;
  165. var yp = this._yaxis.series_u2p;
  166. var i, prevColor, ops, b, h, w, a, points;
  167. var o;
  168. var r = this.renderer;
  169. var opts = (options != undefined) ? options : {};
  170. var shadow = (opts.shadow != undefined) ? opts.shadow : this.shadow;
  171. var fill = (opts.fill != undefined) ? opts.fill : this.fill;
  172. var fillAndStroke = (opts.fillAndStroke != undefined) ? opts.fillAndStroke : this.fillAndStroke;
  173. r.bodyWidth = (opts.bodyWidth != undefined) ? opts.bodyWidth : r.bodyWidth;
  174. r.tickLength = (opts.tickLength != undefined) ? opts.tickLength : r.tickLength;
  175. ctx.save();
  176. if (this.show) {
  177. var x, open, hi, low, close;
  178. // need to get widths based on number of points shown,
  179. // not on total number of points. Use the results
  180. // to speed up drawing in next step.
  181. for (var i=0; i<d.length; i++) {
  182. if (d[i][0] < xmin) {
  183. xminidx = i;
  184. }
  185. else if (d[i][0] < xmax) {
  186. xmaxidx = i+1;
  187. }
  188. }
  189. var dwidth = this.gridData[xmaxidx-1][0] - this.gridData[xminidx][0];
  190. var nvisiblePoints = xmaxidx - xminidx;
  191. try {
  192. var dinterval = Math.abs(this._xaxis.series_u2p(parseInt(this._xaxis._intervalStats[0].sortedIntervals[0].interval, 10)) - this._xaxis.series_u2p(0));
  193. }
  194. catch (e) {
  195. var dinterval = dwidth / nvisiblePoints;
  196. }
  197. if (r.candleStick) {
  198. if (typeof(r.bodyWidth) == 'number') {
  199. r._bodyWidth = r.bodyWidth;
  200. }
  201. else {
  202. r._bodyWidth = Math.min(20, dinterval/1.65);
  203. }
  204. }
  205. else {
  206. if (typeof(r.tickLength) == 'number') {
  207. r._tickLength = r.tickLength;
  208. }
  209. else {
  210. r._tickLength = Math.min(10, dinterval/3.5);
  211. }
  212. }
  213. for (var i=xminidx; i<xmaxidx; i++) {
  214. x = xp(d[i][0]);
  215. if (r.hlc) {
  216. open = null;
  217. hi = yp(d[i][1]);
  218. low = yp(d[i][2]);
  219. close = yp(d[i][3]);
  220. }
  221. else {
  222. open = yp(d[i][1]);
  223. hi = yp(d[i][2]);
  224. low = yp(d[i][3]);
  225. close = yp(d[i][4]);
  226. }
  227. o = {};
  228. if (r.candleStick && !r.hlc) {
  229. w = r._bodyWidth;
  230. a = x - w/2;
  231. // draw candle
  232. // determine if candle up or down
  233. // up, remember grid coordinates increase downward
  234. if (close < open) {
  235. // draw wick
  236. if (r.wickColor) {
  237. o.color = r.wickColor;
  238. }
  239. else if (r.downBodyColor) {
  240. o.color = r.upBodyColor;
  241. }
  242. ops = $.extend(true, {}, opts, o);
  243. r.shapeRenderer.draw(ctx, [[x, hi], [x, close]], ops);
  244. r.shapeRenderer.draw(ctx, [[x, open], [x, low]], ops);
  245. o = {};
  246. b = close;
  247. h = open - close;
  248. // if color specified, use it
  249. if (r.fillUpBody) {
  250. o.fillRect = true;
  251. }
  252. else {
  253. o.strokeRect = true;
  254. w = w - this.lineWidth;
  255. a = x - w/2;
  256. }
  257. if (r.upBodyColor) {
  258. o.color = r.upBodyColor;
  259. o.fillStyle = r.upBodyColor;
  260. }
  261. points = [a, b, w, h];
  262. }
  263. // down
  264. else if (close > open) {
  265. // draw wick
  266. if (r.wickColor) {
  267. o.color = r.wickColor;
  268. }
  269. else if (r.downBodyColor) {
  270. o.color = r.downBodyColor;
  271. }
  272. ops = $.extend(true, {}, opts, o);
  273. r.shapeRenderer.draw(ctx, [[x, hi], [x, open]], ops);
  274. r.shapeRenderer.draw(ctx, [[x, close], [x, low]], ops);
  275. o = {};
  276. b = open;
  277. h = close - open;
  278. // if color specified, use it
  279. if (r.fillDownBody) {
  280. o.fillRect = true;
  281. }
  282. else {
  283. o.strokeRect = true;
  284. w = w - this.lineWidth;
  285. a = x - w/2;
  286. }
  287. if (r.downBodyColor) {
  288. o.color = r.downBodyColor;
  289. o.fillStyle = r.downBodyColor;
  290. }
  291. points = [a, b, w, h];
  292. }
  293. // even, open = close
  294. else {
  295. // draw wick
  296. if (r.wickColor) {
  297. o.color = r.wickColor;
  298. }
  299. ops = $.extend(true, {}, opts, o);
  300. r.shapeRenderer.draw(ctx, [[x, hi], [x, low]], ops);
  301. o = {};
  302. o.fillRect = false;
  303. o.strokeRect = false;
  304. a = [x - w/2, open];
  305. b = [x + w/2, close];
  306. w = null;
  307. h = null;
  308. points = [a, b];
  309. }
  310. ops = $.extend(true, {}, opts, o);
  311. r.shapeRenderer.draw(ctx, points, ops);
  312. }
  313. else {
  314. prevColor = opts.color;
  315. if (r.openColor) {
  316. opts.color = r.openColor;
  317. }
  318. // draw open tick
  319. if (!r.hlc) {
  320. r.shapeRenderer.draw(ctx, [[x-r._tickLength, open], [x, open]], opts);
  321. }
  322. opts.color = prevColor;
  323. // draw wick
  324. if (r.wickColor) {
  325. opts.color = r.wickColor;
  326. }
  327. r.shapeRenderer.draw(ctx, [[x, hi], [x, low]], opts);
  328. opts.color = prevColor;
  329. // draw close tick
  330. if (r.closeColor) {
  331. opts.color = r.closeColor;
  332. }
  333. r.shapeRenderer.draw(ctx, [[x, close], [x+r._tickLength, close]], opts);
  334. opts.color = prevColor;
  335. }
  336. }
  337. }
  338. ctx.restore();
  339. };
  340. $.jqplot.OHLCRenderer.prototype.drawShadow = function(ctx, gd, options) {
  341. // This is a no-op, shadows drawn with lines.
  342. };
  343. // called with scope of plot.
  344. $.jqplot.OHLCRenderer.checkOptions = function(target, data, options) {
  345. // provide some sensible highlighter options by default
  346. // These aren't good for hlc, only for ohlc or candlestick
  347. if (!options.highlighter) {
  348. options.highlighter = {
  349. showMarker:false,
  350. tooltipAxes: 'y',
  351. yvalues: 4,
  352. formatString:'<table class="jqplot-highlighter"><tr><td>date:</td><td>%s</td></tr><tr><td>open:</td><td>%s</td></tr><tr><td>hi:</td><td>%s</td></tr><tr><td>low:</td><td>%s</td></tr><tr><td>close:</td><td>%s</td></tr></table>'
  353. };
  354. }
  355. };
  356. //$.jqplot.preInitHooks.push($.jqplot.OHLCRenderer.checkOptions);
  357. })(jQuery);