jqplot.pyramidGridRenderer.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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.PyramidGridRenderer = function(){
  36. $.jqplot.CanvasGridRenderer.call(this);
  37. };
  38. $.jqplot.PyramidGridRenderer.prototype = new $.jqplot.CanvasGridRenderer();
  39. $.jqplot.PyramidGridRenderer.prototype.constructor = $.jqplot.PyramidGridRenderer;
  40. // called with context of Grid object
  41. $.jqplot.CanvasGridRenderer.prototype.init = function(options) {
  42. this._ctx;
  43. this.plotBands = {
  44. show: false,
  45. color: 'rgb(230, 219, 179)',
  46. axis: 'y',
  47. start: null,
  48. interval: 10
  49. };
  50. $.extend(true, this, options);
  51. // set the shadow renderer options
  52. 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};
  53. this.renderer.shadowRenderer.init(sopts);
  54. };
  55. $.jqplot.PyramidGridRenderer.prototype.draw = function() {
  56. this._ctx = this._elem.get(0).getContext("2d");
  57. var ctx = this._ctx;
  58. var axes = this._axes;
  59. var xp = axes.xaxis.u2p;
  60. var yp = axes.yMidAxis.u2p;
  61. var xnudge = axes.xaxis.max/1000.0;
  62. var xp0 = xp(0);
  63. var xpn = xp(xnudge);
  64. var ax = ['xaxis', 'yaxis', 'x2axis', 'y2axis','yMidAxis'];
  65. // Add the grid onto the grid canvas. This is the bottom most layer.
  66. ctx.save();
  67. ctx.clearRect(0, 0, this._plotDimensions.width, this._plotDimensions.height);
  68. ctx.fillStyle = this.backgroundColor || this.background;
  69. ctx.fillRect(this._left, this._top, this._width, this._height);
  70. if (this.plotBands.show) {
  71. ctx.save();
  72. var pb = this.plotBands;
  73. ctx.fillStyle = pb.color;
  74. var axis;
  75. var x, y, w, h;
  76. // find axis to work with
  77. if (pb.axis.charAt(0) === 'x') {
  78. if (axes.xaxis.show) {
  79. axis = axes.xaxis;
  80. }
  81. }
  82. else if (pb.axis.charAt(0) === 'y') {
  83. if (axes.yaxis.show) {
  84. axis = axes.yaxis;
  85. }
  86. else if (axes.y2axis.show) {
  87. axis = axes.y2axis;
  88. }
  89. else if (axes.yMidAxis.show) {
  90. axis = axes.yMidAxis;
  91. }
  92. }
  93. if (axis !== undefined) {
  94. // draw some rectangles
  95. var start = pb.start;
  96. if (start === null) {
  97. start = axis.min;
  98. }
  99. for (var i = start; i < axis.max; i += 2 * pb.interval) {
  100. if (axis.name.charAt(0) === 'y') {
  101. x = this._left;
  102. if ((i + pb.interval) < axis.max) {
  103. y = axis.series_u2p(i + pb.interval) + this._top;
  104. }
  105. else {
  106. y = axis.series_u2p(axis.max) + this._top;
  107. }
  108. w = this._right - this._left;
  109. h = axis.series_u2p(start) - axis.series_u2p(start + pb.interval);
  110. ctx.fillRect(x, y, w, h);
  111. }
  112. // else {
  113. // y = 0;
  114. // x = axis.series_u2p(i);
  115. // h = this._height;
  116. // w = axis.series_u2p(start + pb.interval) - axis.series_u2p(start);
  117. // }
  118. }
  119. }
  120. ctx.restore();
  121. }
  122. ctx.save();
  123. ctx.lineJoin = 'miter';
  124. ctx.lineCap = 'butt';
  125. ctx.lineWidth = this.gridLineWidth;
  126. ctx.strokeStyle = this.gridLineColor;
  127. var b, e, s, m;
  128. for (var i=5; i>0; i--) {
  129. var name = ax[i-1];
  130. var axis = axes[name];
  131. var ticks = axis._ticks;
  132. var numticks = ticks.length;
  133. if (axis.show) {
  134. if (axis.drawBaseline) {
  135. var bopts = {};
  136. if (axis.baselineWidth !== null) {
  137. bopts.lineWidth = axis.baselineWidth;
  138. }
  139. if (axis.baselineColor !== null) {
  140. bopts.strokeStyle = axis.baselineColor;
  141. }
  142. switch (name) {
  143. case 'xaxis':
  144. if (axes.yMidAxis.show) {
  145. drawLine (this._left, this._bottom, xp0, this._bottom, bopts);
  146. drawLine (xpn, this._bottom, this._right, this._bottom, bopts);
  147. }
  148. else {
  149. drawLine (this._left, this._bottom, this._right, this._bottom, bopts);
  150. }
  151. break;
  152. case 'yaxis':
  153. drawLine (this._left, this._bottom, this._left, this._top, bopts);
  154. break;
  155. case 'yMidAxis':
  156. drawLine(xp0, this._bottom, xp0, this._top, bopts);
  157. drawLine(xpn, this._bottom, xpn, this._top, bopts);
  158. break;
  159. case 'x2axis':
  160. if (axes.yMidAxis.show) {
  161. drawLine (this._left, this._top, xp0, this._top, bopts);
  162. drawLine (xpn, this._top, this._right, this._top, bopts);
  163. }
  164. else {
  165. drawLine (this._left, this._bottom, this._right, this._bottom, bopts);
  166. }
  167. break;
  168. case 'y2axis':
  169. drawLine (this._right, this._bottom, this._right, this._top, bopts);
  170. break;
  171. }
  172. }
  173. for (var j=numticks; j>0; j--) {
  174. var t = ticks[j-1];
  175. if (t.show) {
  176. var pos = Math.round(axis.u2p(t.value)) + 0.5;
  177. switch (name) {
  178. case 'xaxis':
  179. // draw the grid line if we should
  180. if (t.showGridline && this.drawGridlines && (!t.isMinorTick || axis.showMinorTicks)) {
  181. drawLine(pos, this._top, pos, this._bottom);
  182. }
  183. // draw the mark
  184. if (t.showMark && t.mark && (!t.isMinorTick || axis.showMinorTicks)) {
  185. s = t.markSize;
  186. m = t.mark;
  187. var pos = Math.round(axis.u2p(t.value)) + 0.5;
  188. switch (m) {
  189. case 'outside':
  190. b = this._bottom;
  191. e = this._bottom+s;
  192. break;
  193. case 'inside':
  194. b = this._bottom-s;
  195. e = this._bottom;
  196. break;
  197. case 'cross':
  198. b = this._bottom-s;
  199. e = this._bottom+s;
  200. break;
  201. default:
  202. b = this._bottom;
  203. e = this._bottom+s;
  204. break;
  205. }
  206. // draw the shadow
  207. if (this.shadow) {
  208. 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});
  209. }
  210. // draw the line
  211. drawLine(pos, b, pos, e);
  212. }
  213. break;
  214. case 'yaxis':
  215. // draw the grid line
  216. if (t.showGridline && this.drawGridlines && (!t.isMinorTick || axis.showMinorTicks)) {
  217. drawLine(this._right, pos, this._left, pos);
  218. }
  219. // draw the mark
  220. if (t.showMark && t.mark && (!t.isMinorTick || axis.showMinorTicks)) {
  221. s = t.markSize;
  222. m = t.mark;
  223. var pos = Math.round(axis.u2p(t.value)) + 0.5;
  224. switch (m) {
  225. case 'outside':
  226. b = this._left-s;
  227. e = this._left;
  228. break;
  229. case 'inside':
  230. b = this._left;
  231. e = this._left+s;
  232. break;
  233. case 'cross':
  234. b = this._left-s;
  235. e = this._left+s;
  236. break;
  237. default:
  238. b = this._left-s;
  239. e = this._left;
  240. break;
  241. }
  242. // draw the shadow
  243. if (this.shadow) {
  244. 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});
  245. }
  246. drawLine(b, pos, e, pos, {strokeStyle:axis.borderColor});
  247. }
  248. break;
  249. case 'yMidAxis':
  250. // draw the grid line
  251. if (t.showGridline && this.drawGridlines && (!t.isMinorTick || axis.showMinorTicks)) {
  252. drawLine(this._left, pos, xp0, pos);
  253. drawLine(xpn, pos, this._right, pos);
  254. }
  255. // draw the mark
  256. if (t.showMark && t.mark && (!t.isMinorTick || axis.showMinorTicks)) {
  257. s = t.markSize;
  258. m = t.mark;
  259. var pos = Math.round(axis.u2p(t.value)) + 0.5;
  260. b = xp0;
  261. e = xp0 + s;
  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. b = xpn - s;
  268. e = xpn;
  269. // draw the shadow
  270. if (this.shadow) {
  271. 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});
  272. }
  273. drawLine(b, pos, e, pos, {strokeStyle:axis.borderColor});
  274. }
  275. break;
  276. case 'x2axis':
  277. // draw the grid line
  278. if (t.showGridline && this.drawGridlines && (!t.isMinorTick || axis.showMinorTicks)) {
  279. drawLine(pos, this._bottom, pos, this._top);
  280. }
  281. // draw the mark
  282. if (t.showMark && t.mark && (!t.isMinorTick || axis.showMinorTicks)) {
  283. s = t.markSize;
  284. m = t.mark;
  285. var pos = Math.round(axis.u2p(t.value)) + 0.5;
  286. switch (m) {
  287. case 'outside':
  288. b = this._top-s;
  289. e = this._top;
  290. break;
  291. case 'inside':
  292. b = this._top;
  293. e = this._top+s;
  294. break;
  295. case 'cross':
  296. b = this._top-s;
  297. e = this._top+s;
  298. break;
  299. default:
  300. b = this._top-s;
  301. e = this._top;
  302. break;
  303. }
  304. // draw the shadow
  305. if (this.shadow) {
  306. 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});
  307. }
  308. drawLine(pos, b, pos, e);
  309. }
  310. break;
  311. case 'y2axis':
  312. // draw the grid line
  313. if (t.showGridline && this.drawGridlines && (!t.isMinorTick || axis.showMinorTicks)) {
  314. drawLine(this._left, pos, this._right, pos);
  315. }
  316. // draw the mark
  317. if (t.showMark && t.mark && (!t.isMinorTick || axis.showMinorTicks)) {
  318. s = t.markSize;
  319. m = t.mark;
  320. var pos = Math.round(axis.u2p(t.value)) + 0.5;
  321. switch (m) {
  322. case 'outside':
  323. b = this._right;
  324. e = this._right+s;
  325. break;
  326. case 'inside':
  327. b = this._right-s;
  328. e = this._right;
  329. break;
  330. case 'cross':
  331. b = this._right-s;
  332. e = this._right+s;
  333. break;
  334. default:
  335. b = this._right;
  336. e = this._right+s;
  337. break;
  338. }
  339. // draw the shadow
  340. if (this.shadow) {
  341. 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});
  342. }
  343. drawLine(b, pos, e, pos, {strokeStyle:axis.borderColor});
  344. }
  345. break;
  346. default:
  347. break;
  348. }
  349. }
  350. }
  351. t = null;
  352. }
  353. axis = null;
  354. ticks = null;
  355. }
  356. ctx.restore();
  357. function drawLine(bx, by, ex, ey, opts) {
  358. ctx.save();
  359. opts = opts || {};
  360. if (opts.lineWidth == null || opts.lineWidth != 0){
  361. $.extend(true, ctx, opts);
  362. ctx.beginPath();
  363. ctx.moveTo(bx, by);
  364. ctx.lineTo(ex, ey);
  365. ctx.stroke();
  366. }
  367. ctx.restore();
  368. }
  369. if (this.shadow) {
  370. if (axes.yMidAxis.show) {
  371. var points = [[this._left, this._bottom], [xp0, this._bottom]];
  372. this.renderer.shadowRenderer.draw(ctx, points);
  373. var points = [[xpn, this._bottom], [this._right, this._bottom], [this._right, this._top]];
  374. this.renderer.shadowRenderer.draw(ctx, points);
  375. var points = [[xp0, this._bottom], [xp0, this._top]];
  376. this.renderer.shadowRenderer.draw(ctx, points);
  377. }
  378. else {
  379. var points = [[this._left, this._bottom], [this._right, this._bottom], [this._right, this._top]];
  380. this.renderer.shadowRenderer.draw(ctx, points);
  381. }
  382. }
  383. // Now draw border around grid. Use axis border definitions. start at
  384. // upper left and go clockwise.
  385. if (this.borderWidth != 0 && this.drawBorder) {
  386. if (axes.yMidAxis.show) {
  387. drawLine (this._left, this._top, xp0, this._top, {lineCap:'round', strokeStyle:axes.x2axis.borderColor, lineWidth:axes.x2axis.borderWidth});
  388. drawLine (xpn, this._top, this._right, this._top, {lineCap:'round', strokeStyle:axes.x2axis.borderColor, lineWidth:axes.x2axis.borderWidth});
  389. drawLine (this._right, this._top, this._right, this._bottom, {lineCap:'round', strokeStyle:axes.y2axis.borderColor, lineWidth:axes.y2axis.borderWidth});
  390. drawLine (this._right, this._bottom, xpn, this._bottom, {lineCap:'round', strokeStyle:axes.xaxis.borderColor, lineWidth:axes.xaxis.borderWidth});
  391. drawLine (xp0, this._bottom, this._left, this._bottom, {lineCap:'round', strokeStyle:axes.xaxis.borderColor, lineWidth:axes.xaxis.borderWidth});
  392. drawLine (this._left, this._bottom, this._left, this._top, {lineCap:'round', strokeStyle:axes.yaxis.borderColor, lineWidth:axes.yaxis.borderWidth});
  393. drawLine (xp0, this._bottom, xp0, this._top, {lineCap:'round', strokeStyle:axes.yaxis.borderColor, lineWidth:axes.yaxis.borderWidth});
  394. drawLine (xpn, this._bottom, xpn, this._top, {lineCap:'round', strokeStyle:axes.yaxis.borderColor, lineWidth:axes.yaxis.borderWidth});
  395. }
  396. else {
  397. drawLine (this._left, this._top, this._right, this._top, {lineCap:'round', strokeStyle:axes.x2axis.borderColor, lineWidth:axes.x2axis.borderWidth});
  398. drawLine (this._right, this._top, this._right, this._bottom, {lineCap:'round', strokeStyle:axes.y2axis.borderColor, lineWidth:axes.y2axis.borderWidth});
  399. drawLine (this._right, this._bottom, this._left, this._bottom, {lineCap:'round', strokeStyle:axes.xaxis.borderColor, lineWidth:axes.xaxis.borderWidth});
  400. drawLine (this._left, this._bottom, this._left, this._top, {lineCap:'round', strokeStyle:axes.yaxis.borderColor, lineWidth:axes.yaxis.borderWidth});
  401. }
  402. }
  403. // ctx.lineWidth = this.borderWidth;
  404. // ctx.strokeStyle = this.borderColor;
  405. // ctx.strokeRect(this._left, this._top, this._width, this._height);
  406. ctx.restore();
  407. ctx = null;
  408. axes = null;
  409. };
  410. })(jQuery);