jqplot.canvasGridRenderer.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. // Class: $.jqplot.CanvasGridRenderer
  33. // The default jqPlot grid renderer, creating a grid on a canvas element.
  34. // The renderer has no additional options beyond the <Grid> class.
  35. $.jqplot.CanvasGridRenderer = function(){
  36. this.shadowRenderer = new $.jqplot.ShadowRenderer();
  37. };
  38. // called with context of Grid object
  39. $.jqplot.CanvasGridRenderer.prototype.init = function(options) {
  40. this._ctx;
  41. $.extend(true, this, options);
  42. // set the shadow renderer options
  43. var sopts = {lineJoin:'miter', lineCap:'round', fill:false, isarc:false, angle:this.shadowAngle, offset:this.shadowOffset, alpha:this.shadowAlpha, depth:this.shadowDepth, lineWidth:this.shadowWidth, closePath:false, strokeStyle:this.shadowColor};
  44. this.renderer.shadowRenderer.init(sopts);
  45. };
  46. // called with context of Grid.
  47. $.jqplot.CanvasGridRenderer.prototype.createElement = function(plot) {
  48. var elem;
  49. // Memory Leaks patch
  50. if (this._elem) {
  51. if ($.jqplot.use_excanvas && window.G_vmlCanvasManager.uninitElement !== undefined) {
  52. elem = this._elem.get(0);
  53. window.G_vmlCanvasManager.uninitElement(elem);
  54. elem = null;
  55. }
  56. this._elem.emptyForce();
  57. this._elem = null;
  58. }
  59. elem = plot.canvasManager.getCanvas();
  60. var w = this._plotDimensions.width;
  61. var h = this._plotDimensions.height;
  62. elem.width = w;
  63. elem.height = h;
  64. this._elem = $(elem);
  65. this._elem.addClass('jqplot-grid-canvas');
  66. this._elem.css({ position: 'absolute', left: 0, top: 0 });
  67. elem = plot.canvasManager.initCanvas(elem);
  68. this._top = this._offsets.top;
  69. this._bottom = h - this._offsets.bottom;
  70. this._left = this._offsets.left;
  71. this._right = w - this._offsets.right;
  72. this._width = this._right - this._left;
  73. this._height = this._bottom - this._top;
  74. // avoid memory leak
  75. elem = null;
  76. return this._elem;
  77. };
  78. $.jqplot.CanvasGridRenderer.prototype.draw = function() {
  79. this._ctx = this._elem.get(0).getContext("2d");
  80. var ctx = this._ctx;
  81. var axes = this._axes;
  82. // Add the grid onto the grid canvas. This is the bottom most layer.
  83. ctx.save();
  84. ctx.clearRect(0, 0, this._plotDimensions.width, this._plotDimensions.height);
  85. ctx.fillStyle = this.backgroundColor || this.background;
  86. ctx.fillRect(this._left, this._top, this._width, this._height);
  87. ctx.save();
  88. ctx.lineJoin = 'miter';
  89. ctx.lineCap = 'butt';
  90. ctx.lineWidth = this.gridLineWidth;
  91. ctx.strokeStyle = this.gridLineColor;
  92. var b, e, s, m;
  93. var ax = ['xaxis', 'yaxis', 'x2axis', 'y2axis'];
  94. for (var i=4; i>0; i--) {
  95. var name = ax[i-1];
  96. var axis = axes[name];
  97. var ticks = axis._ticks;
  98. var numticks = ticks.length;
  99. if (axis.show) {
  100. if (axis.drawBaseline) {
  101. var bopts = {};
  102. if (axis.baselineWidth !== null) {
  103. bopts.lineWidth = axis.baselineWidth;
  104. }
  105. if (axis.baselineColor !== null) {
  106. bopts.strokeStyle = axis.baselineColor;
  107. }
  108. switch (name) {
  109. case 'xaxis':
  110. drawLine (this._left, this._bottom, this._right, this._bottom, bopts);
  111. break;
  112. case 'yaxis':
  113. drawLine (this._left, this._bottom, this._left, this._top, bopts);
  114. break;
  115. case 'x2axis':
  116. drawLine (this._left, this._bottom, this._right, this._bottom, bopts);
  117. break;
  118. case 'y2axis':
  119. drawLine (this._right, this._bottom, this._right, this._top, bopts);
  120. break;
  121. }
  122. }
  123. for (var j=numticks; j>0; j--) {
  124. var t = ticks[j-1];
  125. if (t.show) {
  126. var pos = Math.round(axis.u2p(t.value)) + 0.5;
  127. switch (name) {
  128. case 'xaxis':
  129. // draw the grid line if we should
  130. if (t.showGridline && this.drawGridlines && ((!t.isMinorTick && axis.drawMajorGridlines) || (t.isMinorTick && axis.drawMinorGridlines)) ) {
  131. drawLine(pos, this._top, pos, this._bottom);
  132. }
  133. // draw the mark
  134. if (t.showMark && t.mark && ((!t.isMinorTick && axis.drawMajorTickMarks) || (t.isMinorTick && axis.drawMinorTickMarks)) ) {
  135. s = t.markSize;
  136. m = t.mark;
  137. var pos = Math.round(axis.u2p(t.value)) + 0.5;
  138. switch (m) {
  139. case 'outside':
  140. b = this._bottom;
  141. e = this._bottom+s;
  142. break;
  143. case 'inside':
  144. b = this._bottom-s;
  145. e = this._bottom;
  146. break;
  147. case 'cross':
  148. b = this._bottom-s;
  149. e = this._bottom+s;
  150. break;
  151. default:
  152. b = this._bottom;
  153. e = this._bottom+s;
  154. break;
  155. }
  156. // draw the shadow
  157. if (this.shadow) {
  158. this.renderer.shadowRenderer.draw(ctx, [[pos,b],[pos,e]], {lineCap:'butt', lineWidth:this.gridLineWidth, offset:this.gridLineWidth*0.75, depth:2, fill:false, closePath:false});
  159. }
  160. // draw the line
  161. drawLine(pos, b, pos, e);
  162. }
  163. break;
  164. case 'yaxis':
  165. // draw the grid line
  166. if (t.showGridline && this.drawGridlines && ((!t.isMinorTick && axis.drawMajorGridlines) || (t.isMinorTick && axis.drawMinorGridlines)) ) {
  167. drawLine(this._right, pos, this._left, pos);
  168. }
  169. // draw the mark
  170. if (t.showMark && t.mark && ((!t.isMinorTick && axis.drawMajorTickMarks) || (t.isMinorTick && axis.drawMinorTickMarks)) ) {
  171. s = t.markSize;
  172. m = t.mark;
  173. var pos = Math.round(axis.u2p(t.value)) + 0.5;
  174. switch (m) {
  175. case 'outside':
  176. b = this._left-s;
  177. e = this._left;
  178. break;
  179. case 'inside':
  180. b = this._left;
  181. e = this._left+s;
  182. break;
  183. case 'cross':
  184. b = this._left-s;
  185. e = this._left+s;
  186. break;
  187. default:
  188. b = this._left-s;
  189. e = this._left;
  190. break;
  191. }
  192. // draw the shadow
  193. if (this.shadow) {
  194. this.renderer.shadowRenderer.draw(ctx, [[b, pos], [e, pos]], {lineCap:'butt', lineWidth:this.gridLineWidth*1.5, offset:this.gridLineWidth*0.75, fill:false, closePath:false});
  195. }
  196. drawLine(b, pos, e, pos, {strokeStyle:axis.borderColor});
  197. }
  198. break;
  199. case 'x2axis':
  200. // draw the grid line
  201. if (t.showGridline && this.drawGridlines && ((!t.isMinorTick && axis.drawMajorGridlines) || (t.isMinorTick && axis.drawMinorGridlines)) ) {
  202. drawLine(pos, this._bottom, pos, this._top);
  203. }
  204. // draw the mark
  205. if (t.showMark && t.mark && ((!t.isMinorTick && axis.drawMajorTickMarks) || (t.isMinorTick && axis.drawMinorTickMarks)) ) {
  206. s = t.markSize;
  207. m = t.mark;
  208. var pos = Math.round(axis.u2p(t.value)) + 0.5;
  209. switch (m) {
  210. case 'outside':
  211. b = this._top-s;
  212. e = this._top;
  213. break;
  214. case 'inside':
  215. b = this._top;
  216. e = this._top+s;
  217. break;
  218. case 'cross':
  219. b = this._top-s;
  220. e = this._top+s;
  221. break;
  222. default:
  223. b = this._top-s;
  224. e = this._top;
  225. break;
  226. }
  227. // draw the shadow
  228. if (this.shadow) {
  229. this.renderer.shadowRenderer.draw(ctx, [[pos,b],[pos,e]], {lineCap:'butt', lineWidth:this.gridLineWidth, offset:this.gridLineWidth*0.75, depth:2, fill:false, closePath:false});
  230. }
  231. drawLine(pos, b, pos, e);
  232. }
  233. break;
  234. case 'y2axis':
  235. // draw the grid line
  236. if (t.showGridline && this.drawGridlines && ((!t.isMinorTick && axis.drawMajorGridlines) || (t.isMinorTick && axis.drawMinorGridlines)) ) {
  237. drawLine(this._left, pos, this._right, pos);
  238. }
  239. // draw the mark
  240. if (t.showMark && t.mark && ((!t.isMinorTick && axis.drawMajorTickMarks) || (t.isMinorTick && axis.drawMinorTickMarks)) ) {
  241. s = t.markSize;
  242. m = t.mark;
  243. var pos = Math.round(axis.u2p(t.value)) + 0.5;
  244. switch (m) {
  245. case 'outside':
  246. b = this._right;
  247. e = this._right+s;
  248. break;
  249. case 'inside':
  250. b = this._right-s;
  251. e = this._right;
  252. break;
  253. case 'cross':
  254. b = this._right-s;
  255. e = this._right+s;
  256. break;
  257. default:
  258. b = this._right;
  259. e = this._right+s;
  260. break;
  261. }
  262. // draw the shadow
  263. if (this.shadow) {
  264. this.renderer.shadowRenderer.draw(ctx, [[b, pos], [e, pos]], {lineCap:'butt', lineWidth:this.gridLineWidth*1.5, offset:this.gridLineWidth*0.75, fill:false, closePath:false});
  265. }
  266. drawLine(b, pos, e, pos, {strokeStyle:axis.borderColor});
  267. }
  268. break;
  269. default:
  270. break;
  271. }
  272. }
  273. }
  274. t = null;
  275. }
  276. axis = null;
  277. ticks = null;
  278. }
  279. // Now draw grid lines for additional y axes
  280. //////
  281. // TO DO: handle yMidAxis
  282. //////
  283. ax = ['y3axis', 'y4axis', 'y5axis', 'y6axis', 'y7axis', 'y8axis', 'y9axis', 'yMidAxis'];
  284. for (var i=7; i>0; i--) {
  285. var axis = axes[ax[i-1]];
  286. var ticks = axis._ticks;
  287. if (axis.show) {
  288. var tn = ticks[axis.numberTicks-1];
  289. var t0 = ticks[0];
  290. var left = axis.getLeft();
  291. var points = [[left, tn.getTop() + tn.getHeight()/2], [left, t0.getTop() + t0.getHeight()/2 + 1.0]];
  292. // draw the shadow
  293. if (this.shadow) {
  294. this.renderer.shadowRenderer.draw(ctx, points, {lineCap:'butt', fill:false, closePath:false});
  295. }
  296. // draw the line
  297. drawLine(points[0][0], points[0][1], points[1][0], points[1][1], {lineCap:'butt', strokeStyle:axis.borderColor, lineWidth:axis.borderWidth});
  298. // draw the tick marks
  299. for (var j=ticks.length; j>0; j--) {
  300. var t = ticks[j-1];
  301. s = t.markSize;
  302. m = t.mark;
  303. var pos = Math.round(axis.u2p(t.value)) + 0.5;
  304. if (t.showMark && t.mark) {
  305. switch (m) {
  306. case 'outside':
  307. b = left;
  308. e = left+s;
  309. break;
  310. case 'inside':
  311. b = left-s;
  312. e = left;
  313. break;
  314. case 'cross':
  315. b = left-s;
  316. e = left+s;
  317. break;
  318. default:
  319. b = left;
  320. e = left+s;
  321. break;
  322. }
  323. points = [[b,pos], [e,pos]];
  324. // draw the shadow
  325. if (this.shadow) {
  326. this.renderer.shadowRenderer.draw(ctx, points, {lineCap:'butt', lineWidth:this.gridLineWidth*1.5, offset:this.gridLineWidth*0.75, fill:false, closePath:false});
  327. }
  328. // draw the line
  329. drawLine(b, pos, e, pos, {strokeStyle:axis.borderColor});
  330. }
  331. t = null;
  332. }
  333. t0 = null;
  334. }
  335. axis = null;
  336. ticks = null;
  337. }
  338. ctx.restore();
  339. function drawLine(bx, by, ex, ey, opts) {
  340. ctx.save();
  341. opts = opts || {};
  342. if (opts.lineWidth == null || opts.lineWidth != 0){
  343. $.extend(true, ctx, opts);
  344. ctx.beginPath();
  345. ctx.moveTo(bx, by);
  346. ctx.lineTo(ex, ey);
  347. ctx.stroke();
  348. ctx.restore();
  349. }
  350. }
  351. if (this.shadow) {
  352. var points = [[this._left, this._bottom], [this._right, this._bottom], [this._right, this._top]];
  353. this.renderer.shadowRenderer.draw(ctx, points);
  354. }
  355. // Now draw border around grid. Use axis border definitions. start at
  356. // upper left and go clockwise.
  357. if (this.borderWidth != 0 && this.drawBorder) {
  358. drawLine (this._left, this._top, this._right, this._top, {lineCap:'round', strokeStyle:axes.x2axis.borderColor, lineWidth:axes.x2axis.borderWidth});
  359. drawLine (this._right, this._top, this._right, this._bottom, {lineCap:'round', strokeStyle:axes.y2axis.borderColor, lineWidth:axes.y2axis.borderWidth});
  360. drawLine (this._right, this._bottom, this._left, this._bottom, {lineCap:'round', strokeStyle:axes.xaxis.borderColor, lineWidth:axes.xaxis.borderWidth});
  361. drawLine (this._left, this._bottom, this._left, this._top, {lineCap:'round', strokeStyle:axes.yaxis.borderColor, lineWidth:axes.yaxis.borderWidth});
  362. }
  363. // ctx.lineWidth = this.borderWidth;
  364. // ctx.strokeStyle = this.borderColor;
  365. // ctx.strokeRect(this._left, this._top, this._width, this._height);
  366. ctx.restore();
  367. ctx = null;
  368. axes = null;
  369. };
  370. })(jQuery);