3 * Pure JavaScript plotting plugin using jQuery
8 * Copyright (c) 2009-2012 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.
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 .
18 * If you are feeling kind and generous, consider supporting the project by
19 * making a donation at: http://www.jqplot.com/donate.php .
21 * sprintf functions contained in jqplot.sprintf.js by 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."
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);
39 $.jqplot.PyramidGridRenderer.prototype = new $.jqplot.CanvasGridRenderer();
40 $.jqplot.PyramidGridRenderer.prototype.constructor = $.jqplot.PyramidGridRenderer;
42 // called with context of Grid object
43 $.jqplot.CanvasGridRenderer.prototype.init = function(options) {
47 color: 'rgb(230, 219, 179)',
52 $.extend(true, this, options);
53 // set the shadow renderer options
54 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};
55 this.renderer.shadowRenderer.init(sopts);
58 $.jqplot.PyramidGridRenderer.prototype.draw = function() {
59 this._ctx = this._elem.get(0).getContext("2d");
61 var axes = this._axes;
62 var xp = axes.xaxis.u2p;
63 var yp = axes.yMidAxis.u2p;
64 var xnudge = axes.xaxis.max/1000.0;
67 var ax = ['xaxis', 'yaxis', 'x2axis', 'y2axis','yMidAxis'];
68 // Add the grid onto the grid canvas. This is the bottom most layer.
70 ctx.clearRect(0, 0, this._plotDimensions.width, this._plotDimensions.height);
71 ctx.fillStyle = this.backgroundColor || this.background;
73 ctx.fillRect(this._left, this._top, this._width, this._height);
75 if (this.plotBands.show) {
77 var pb = this.plotBands;
78 ctx.fillStyle = pb.color;
81 // find axis to work with
82 if (pb.axis.charAt(0) === 'x') {
83 if (axes.xaxis.show) {
87 else if (pb.axis.charAt(0) === 'y') {
88 if (axes.yaxis.show) {
91 else if (axes.y2axis.show) {
94 else if (axes.yMidAxis.show) {
99 if (axis !== undefined) {
100 // draw some rectangles
101 var start = pb.start;
102 if (start === null) {
105 for (var i = start; i < axis.max; i += 2 * pb.interval) {
106 if (axis.name.charAt(0) === 'y') {
108 if ((i + pb.interval) < axis.max) {
109 y = axis.series_u2p(i + pb.interval) + this._top;
112 y = axis.series_u2p(axis.max) + this._top;
114 w = this._right - this._left;
115 h = axis.series_u2p(start) - axis.series_u2p(start + pb.interval);
116 ctx.fillRect(x, y, w, h);
120 // x = axis.series_u2p(i);
122 // w = axis.series_u2p(start + pb.interval) - axis.series_u2p(start);
131 ctx.lineJoin = 'miter';
132 ctx.lineCap = 'butt';
133 ctx.lineWidth = this.gridLineWidth;
134 ctx.strokeStyle = this.gridLineColor;
136 for (var i=5; i>0; i--) {
138 var axis = axes[name];
139 var ticks = axis._ticks;
140 var numticks = ticks.length;
142 if (axis.drawBaseline) {
144 if (axis.baselineWidth !== null) {
145 bopts.lineWidth = axis.baselineWidth;
147 if (axis.baselineColor !== null) {
148 bopts.strokeStyle = axis.baselineColor;
152 if (axes.yMidAxis.show) {
153 drawLine (this._left, this._bottom, xp0, this._bottom, bopts);
154 drawLine (xpn, this._bottom, this._right, this._bottom, bopts);
157 drawLine (this._left, this._bottom, this._right, this._bottom, bopts);
161 drawLine (this._left, this._bottom, this._left, this._top, bopts);
164 drawLine(xp0, this._bottom, xp0, this._top, bopts);
165 drawLine(xpn, this._bottom, xpn, this._top, bopts);
168 if (axes.yMidAxis.show) {
169 drawLine (this._left, this._top, xp0, this._top, bopts);
170 drawLine (xpn, this._top, this._right, this._top, bopts);
173 drawLine (this._left, this._bottom, this._right, this._bottom, bopts);
177 drawLine (this._right, this._bottom, this._right, this._top, bopts);
182 for (var j=numticks; j>0; j--) {
185 var pos = Math.round(axis.u2p(t.value)) + 0.5;
188 // draw the grid line if we should
189 if (t.showGridline && this.drawGridlines && (!t.isMinorTick || axis.showMinorTicks)) {
190 drawLine(pos, this._top, pos, this._bottom);
194 if (t.showMark && t.mark && (!t.isMinorTick || axis.showMinorTicks)) {
197 var pos = Math.round(axis.u2p(t.value)) + 0.5;
218 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});
221 drawLine(pos, b, pos, e);
225 // draw the grid line
226 if (t.showGridline && this.drawGridlines && (!t.isMinorTick || axis.showMinorTicks)) {
227 drawLine(this._right, pos, this._left, pos);
231 if (t.showMark && t.mark && (!t.isMinorTick || axis.showMinorTicks)) {
234 var pos = Math.round(axis.u2p(t.value)) + 0.5;
255 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});
257 drawLine(b, pos, e, pos, {strokeStyle:axis.borderColor});
261 // draw the grid line
262 if (t.showGridline && this.drawGridlines && (!t.isMinorTick || axis.showMinorTicks)) {
263 drawLine(this._left, pos, xp0, pos);
264 drawLine(xpn, pos, this._right, pos);
267 if (t.showMark && t.mark && (!t.isMinorTick || axis.showMinorTicks)) {
270 var pos = Math.round(axis.u2p(t.value)) + 0.5;
276 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});
278 drawLine(b, pos, e, pos, {strokeStyle:axis.borderColor});
284 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});
286 drawLine(b, pos, e, pos, {strokeStyle:axis.borderColor});
290 // draw the grid line
291 if (t.showGridline && this.drawGridlines && (!t.isMinorTick || axis.showMinorTicks)) {
292 drawLine(pos, this._bottom, pos, this._top);
296 if (t.showMark && t.mark && (!t.isMinorTick || axis.showMinorTicks)) {
299 var pos = Math.round(axis.u2p(t.value)) + 0.5;
320 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});
322 drawLine(pos, b, pos, e);
326 // draw the grid line
327 if (t.showGridline && this.drawGridlines && (!t.isMinorTick || axis.showMinorTicks)) {
328 drawLine(this._left, pos, this._right, pos);
332 if (t.showMark && t.mark && (!t.isMinorTick || axis.showMinorTicks)) {
335 var pos = Math.round(axis.u2p(t.value)) + 0.5;
356 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});
358 drawLine(b, pos, e, pos, {strokeStyle:axis.borderColor});
374 function drawLine(bx, by, ex, ey, opts) {
377 if (opts.lineWidth == null || opts.lineWidth != 0){
378 $.extend(true, ctx, opts);
388 if (axes.yMidAxis.show) {
389 var points = [[this._left, this._bottom], [xp0, this._bottom]];
390 this.renderer.shadowRenderer.draw(ctx, points);
391 var points = [[xpn, this._bottom], [this._right, this._bottom], [this._right, this._top]];
392 this.renderer.shadowRenderer.draw(ctx, points);
393 var points = [[xp0, this._bottom], [xp0, this._top]];
394 this.renderer.shadowRenderer.draw(ctx, points);
397 var points = [[this._left, this._bottom], [this._right, this._bottom], [this._right, this._top]];
398 this.renderer.shadowRenderer.draw(ctx, points);
401 // Now draw border around grid. Use axis border definitions. start at
402 // upper left and go clockwise.
403 if (this.borderWidth != 0 && this.drawBorder) {
404 if (axes.yMidAxis.show) {
405 drawLine (this._left, this._top, xp0, this._top, {lineCap:'round', strokeStyle:axes.x2axis.borderColor, lineWidth:axes.x2axis.borderWidth});
406 drawLine (xpn, this._top, this._right, this._top, {lineCap:'round', strokeStyle:axes.x2axis.borderColor, lineWidth:axes.x2axis.borderWidth});
407 drawLine (this._right, this._top, this._right, this._bottom, {lineCap:'round', strokeStyle:axes.y2axis.borderColor, lineWidth:axes.y2axis.borderWidth});
408 drawLine (this._right, this._bottom, xpn, this._bottom, {lineCap:'round', strokeStyle:axes.xaxis.borderColor, lineWidth:axes.xaxis.borderWidth});
409 drawLine (xp0, this._bottom, this._left, this._bottom, {lineCap:'round', strokeStyle:axes.xaxis.borderColor, lineWidth:axes.xaxis.borderWidth});
410 drawLine (this._left, this._bottom, this._left, this._top, {lineCap:'round', strokeStyle:axes.yaxis.borderColor, lineWidth:axes.yaxis.borderWidth});
411 drawLine (xp0, this._bottom, xp0, this._top, {lineCap:'round', strokeStyle:axes.yaxis.borderColor, lineWidth:axes.yaxis.borderWidth});
412 drawLine (xpn, this._bottom, xpn, this._top, {lineCap:'round', strokeStyle:axes.yaxis.borderColor, lineWidth:axes.yaxis.borderWidth});
415 drawLine (this._left, this._top, this._right, this._top, {lineCap:'round', strokeStyle:axes.x2axis.borderColor, lineWidth:axes.x2axis.borderWidth});
416 drawLine (this._right, this._top, this._right, this._bottom, {lineCap:'round', strokeStyle:axes.y2axis.borderColor, lineWidth:axes.y2axis.borderWidth});
417 drawLine (this._right, this._bottom, this._left, this._bottom, {lineCap:'round', strokeStyle:axes.xaxis.borderColor, lineWidth:axes.xaxis.borderWidth});
418 drawLine (this._left, this._bottom, this._left, this._top, {lineCap:'round', strokeStyle:axes.yaxis.borderColor, lineWidth:axes.yaxis.borderWidth});
421 // ctx.lineWidth = this.borderWidth;
422 // ctx.strokeStyle = this.borderColor;
423 // ctx.strokeRect(this._left, this._top, this._width, this._height);