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."
33 // Need to ensure pyramid axis and grid renderers are loaded.
34 // You should load these with script tags in the html head, that is more efficient
35 // as the browser will cache the request.
36 // Note, have to block with synchronous request in order to execute bar renderer code.
37 if ($.jqplot.PyramidAxisRenderer === undefined) {
39 url: $.jqplot.pluginLocation + 'jqplot.pyramidAxisRenderer.js',
45 if ($.jqplot.PyramidGridRenderer === undefined) {
47 url: $.jqplot.pluginLocation + 'jqplot.pyramidGridRenderer.js',
53 $.jqplot.PyramidRenderer = function(){
54 $.jqplot.LineRenderer.call(this);
57 $.jqplot.PyramidRenderer.prototype = new $.jqplot.LineRenderer();
58 $.jqplot.PyramidRenderer.prototype.constructor = $.jqplot.PyramidRenderer;
60 // called with scope of a series
61 $.jqplot.PyramidRenderer.prototype.init = function(options, plot) {
62 options = options || {};
63 this._type = 'pyramid';
70 // True to fill the bars.
72 // prop: highlightMouseOver
73 // True to highlight slice when moused over.
74 // This must be false to enable highlightMouseDown to highlight when clicking on a slice.
75 this.highlightMouseOver = true;
76 // prop: highlightMouseDown
77 // True to highlight when a mouse button is pressed over a slice.
78 // This will be disabled if highlightMouseOver is true.
79 this.highlightMouseDown = false;
80 // prop: highlightColors
81 // an array of colors to use when highlighting a slice.
82 this.highlightColors = [];
83 // prop highlightThreshold
84 // Expand the highlightable region in the x direction.
85 // E.g. a value of 3 will highlight a bar when the mouse is
86 // within 3 pixels of the bar in the x direction.
87 this.highlightThreshold = 2;
88 // prop: synchronizeHighlight
89 // Index of another series to highlight when this series is highlighted.
90 // null or false to not synchronize.
91 this.synchronizeHighlight = false;
93 // False will center bars on their y value.
94 // True will push bars up by 1/2 bar width to fill between their y values.
95 // If true, there needs to be 1 more tick than there are bars.
96 this.offsetBars = false;
98 // if user has passed in highlightMouseDown option and not set highlightMouseOver, disable highlightMouseOver
99 if (options.highlightMouseDown && options.highlightMouseOver == null) {
100 options.highlightMouseOver = false;
105 $.extend(true, this, options);
107 // if (this.fill === false) {
108 // this.shadow = false;
111 if (this.side === 'left') {
112 this._highlightThreshold = [[-this.highlightThreshold, 0], [-this.highlightThreshold, 0], [0,0], [0,0]];
116 this._highlightThreshold = [[0,0], [0,0], [this.highlightThreshold, 0], [this.highlightThreshold, 0]];
119 this.renderer.options = options;
120 // index of the currenty highlighted point, if any
121 this._highlightedPoint = null;
122 // Array of actual data colors used for each data point.
123 this._dataColors = [];
124 this._barPoints = [];
126 this._primaryAxis = '_yaxis';
129 // set the shape renderer options
130 var opts = {lineJoin:'miter', lineCap:'butt', fill:this.fill, fillRect:this.fill, isarc:false, strokeStyle:this.color, fillStyle:this.color, closePath:this.fill, lineWidth: this.lineWidth};
131 this.renderer.shapeRenderer.init(opts);
132 // set the shadow renderer options
133 var shadow_offset = options.shadowOffset;
134 // set the shadow renderer options
135 if (shadow_offset == null) {
136 // scale the shadowOffset to the width of the line.
137 if (this.lineWidth > 2.5) {
138 shadow_offset = 1.25 * (1 + (Math.atan((this.lineWidth/2.5))/0.785398163 - 1)*0.6);
139 // var shadow_offset = this.shadowOffset;
141 // for skinny lines, don't make such a big shadow.
143 shadow_offset = 1.25 * Math.atan((this.lineWidth/2.5))/0.785398163;
146 var sopts = {lineJoin:'miter', lineCap:'butt', fill:this.fill, fillRect:this.fill, isarc:false, angle:this.shadowAngle, offset:shadow_offset, alpha:this.shadowAlpha, depth:this.shadowDepth, closePath:this.fill, lineWidth: this.lineWidth};
147 this.renderer.shadowRenderer.init(sopts);
149 plot.postDrawHooks.addOnce(postPlotDraw);
150 plot.eventListenerHooks.addOnce('jqplotMouseMove', handleMove);
152 // if this is the left side of pyramid, set y values to negative.
153 if (this.side === 'left') {
154 for (var i=0, l=this.data.length; i<l; i++) {
155 this.data[i][1] = -Math.abs(this.data[i][1]);
161 // converts the user data values to grid coordinates and stores them
162 // in the gridData array.
163 // Called with scope of a series.
164 $.jqplot.PyramidRenderer.prototype.setGridData = function(plot) {
165 // recalculate the grid data
166 var xp = this._xaxis.series_u2p;
167 var yp = this._yaxis.series_u2p;
168 var data = this._plotData;
169 var pdata = this._prevPlotData;
171 this._prevGridData = [];
176 // if any data values are < 0, consider this a negative series
177 for (i = 0; i < l; i++) {
178 if (data[i][1] < 0) {
183 if (this._yaxis.name === 'yMidAxis' && this.side === 'right') {
184 this._xnudge = this._xaxis.max/2000.0;
188 for (i = 0; i < l; i++) {
189 // if not a line series or if no nulls in data, push the converted point onto the array.
190 if (data[i][0] != null && data[i][1] != null) {
191 this.gridData.push([xp(data[i][1]), yp(data[i][0])]);
193 // else if there is a null, preserve it.
194 else if (data[i][0] == null) {
195 this.gridData.push([xp(data[i][1]), null]);
197 else if (data[i][1] == null) {
198 this.gridData.push(null, [yp(data[i][0])]);
200 // finally, adjust x grid data if have to
201 if (data[i][1] === 0 && adjust) {
202 this.gridData[i][0] = xp(this._xnudge);
208 // converts any arbitrary data values to grid coordinates and
209 // returns them. This method exists so that plugins can use a series'
210 // linerenderer to generate grid data points without overwriting the
211 // grid data associated with that series.
212 // Called with scope of a series.
213 $.jqplot.PyramidRenderer.prototype.makeGridData = function(data, plot) {
214 // recalculate the grid data
215 var xp = this._xaxis.series_u2p;
216 var yp = this._yaxis.series_u2p;
222 // if any data values are < 0, consider this a negative series
223 for (i = 0; i < l; i++) {
224 if (data[i][1] < 0) {
229 if (this._yaxis.name === 'yMidAxis' && this.side === 'right') {
230 this._xnudge = this._xaxis.max/2000.0;
234 for (i = 0; i < l; i++) {
235 // if not a line series or if no nulls in data, push the converted point onto the array.
236 if (data[i][0] != null && data[i][1] != null) {
237 gd.push([xp(data[i][1]), yp(data[i][0])]);
239 // else if there is a null, preserve it.
240 else if (data[i][0] == null) {
241 gd.push([xp(data[i][1]), null]);
243 else if (data[i][1] == null) {
244 gd.push([null, yp(data[i][0])]);
246 // finally, adjust x grid data if have to
247 if (data[i][1] === 0 && adjust) {
248 gd[i][0] = xp(this._xnudge);
255 $.jqplot.PyramidRenderer.prototype.setBarWidth = function() {
256 // need to know how many data values we have on the approprate axis and figure it out.
260 var paxis = this[this._primaryAxis];
262 nvals = paxis.max - paxis.min;
263 var nticks = paxis.numberTicks;
264 var nbins = (nticks-1)/2;
265 // so, now we have total number of axis values.
266 var temp = (this.barPadding === 0) ? 1.0 : 0;
267 if (paxis.name == 'xaxis' || paxis.name == 'x2axis') {
268 this.barWidth = (paxis._offsets.max - paxis._offsets.min) / nvals - this.barPadding + temp;
272 this.barWidth = (paxis._offsets.min - paxis._offsets.max) / nvals - this.barPadding + temp;
275 this.barWidth = (paxis._offsets.min - paxis._offsets.max) / nvals;
280 $.jqplot.PyramidRenderer.prototype.draw = function(ctx, gridData, options) {
282 // Ughhh, have to make a copy of options b/c it may be modified later.
283 var opts = $.extend({}, options);
284 var shadow = (opts.shadow != undefined) ? opts.shadow : this.shadow;
285 var showLine = (opts.showLine != undefined) ? opts.showLine : this.showLine;
286 var fill = (opts.fill != undefined) ? opts.fill : this.fill;
287 var xp = this._xaxis.series_u2p;
288 var yp = this._yaxis.series_u2p;
290 // clear out data colors.
291 this._dataColors = [];
292 this._barPoints = [];
294 if (this.renderer.options.barWidth == null) {
295 this.renderer.setBarWidth.call(this);
298 // var temp = this._plotSeriesInfo = this.renderer.calcSeriesNumbers.call(this);
299 // var nvals = temp[0];
300 // var nseries = temp[1];
301 // var pos = temp[2];
306 // this._barNudge = 0;
309 var negativeColors = new $.jqplot.ColorGenerator(this.negativeSeriesColors);
310 var positiveColors = new $.jqplot.ColorGenerator(this.seriesColors);
311 var negativeColor = negativeColors.get(this.index);
312 if (! this.useNegativeColors) {
313 negativeColor = opts.fillStyle;
315 var positiveColor = opts.fillStyle;
317 var xstart = this._xaxis.series_u2p(this._xnudge);
318 var ystart = this._yaxis.series_u2p(this._yaxis.min);
319 var yend = this._yaxis.series_u2p(this._yaxis.max);
320 var bw = this.barWidth;
323 var yadj = this.offsetBars ? bw2 : 0;
325 for (var i=0, l=gridData.length; i<l; i++) {
326 if (this.data[i][0] == null) {
329 base = gridData[i][1];
330 // not stacked and first series in stack
332 if (this._plotData[i][1] < 0) {
333 if (this.varyBarColor && !this._stack) {
334 if (this.useNegativeColors) {
335 opts.fillStyle = negativeColors.next();
338 opts.fillStyle = positiveColors.next();
343 if (this.varyBarColor && !this._stack) {
344 opts.fillStyle = positiveColors.next();
347 opts.fillStyle = positiveColor;
353 if (this._plotData[i][1] >= 0) {
354 // xstart = this._xaxis.series_u2p(this._xnudge);
355 w = gridData[i][0] - xstart;
357 points = [xstart, base - bw2 - yadj, w, h];
360 // xstart = this._xaxis.series_u2p(0);
361 w = xstart - gridData[i][0];
363 points = [gridData[i][0], base - bw2 - yadj, w, h];
366 this._barPoints.push([[points[0], points[1] + h], [points[0], points[1]], [points[0] + w, points[1]], [points[0] + w, points[1] + h]]);
369 this.renderer.shadowRenderer.draw(ctx, points);
371 var clr = opts.fillStyle || this.color;
372 this._dataColors.push(clr);
373 this.renderer.shapeRenderer.draw(ctx, points, opts);
378 points =[[xstart, ystart], [gridData[i][0], ystart], [gridData[i][0], gridData[i][1] - bw2 - yadj]];
382 points = points.concat([[gridData[i-1][0], gridData[i-1][1] - bw2 - yadj], [gridData[i][0], gridData[i][1] + bw2 - yadj], [gridData[i][0], gridData[i][1] - bw2 - yadj]]);
385 // finally, draw the line
387 points = points.concat([[gridData[i-1][0], gridData[i-1][1] - bw2 - yadj], [gridData[i][0], gridData[i][1] + bw2 - yadj], [gridData[i][0], yend], [xstart, yend]]);
390 this.renderer.shadowRenderer.draw(ctx, points);
392 var clr = opts.fillStyle || this.color;
393 this._dataColors.push(clr);
394 this.renderer.shapeRenderer.draw(ctx, points, opts);
400 if (this.highlightColors.length == 0) {
401 this.highlightColors = $.jqplot.computeHighlightColors(this._dataColors);
404 else if (typeof(this.highlightColors) == 'string') {
405 this.highlightColors = [];
406 for (var i=0; i<this._dataColors.length; i++) {
407 this.highlightColors.push(this.highlightColors);
414 // setup default renderers for axes and legend so user doesn't have to
415 // called with scope of plot
416 function preInit(target, data, options) {
417 options = options || {};
418 options.axesDefaults = options.axesDefaults || {};
419 options.grid = options.grid || {};
420 options.legend = options.legend || {};
421 options.seriesDefaults = options.seriesDefaults || {};
422 // only set these if there is a pie series
424 if (options.seriesDefaults.renderer === $.jqplot.PyramidRenderer) {
427 else if (options.series) {
428 for (var i=0; i < options.series.length; i++) {
429 if (options.series[i].renderer === $.jqplot.PyramidRenderer) {
436 options.axesDefaults.renderer = $.jqplot.PyramidAxisRenderer;
437 options.grid.renderer = $.jqplot.PyramidGridRenderer;
438 options.seriesDefaults.pointLabels = {show: false};
442 // called within context of plot
443 // create a canvas which we can draw on.
444 // insert it before the eventCanvas, so eventCanvas will still capture events.
445 function postPlotDraw() {
446 // Memory Leaks patch
447 if (this.plugins.pyramidRenderer && this.plugins.pyramidRenderer.highlightCanvas) {
449 this.plugins.pyramidRenderer.highlightCanvas.resetCanvas();
450 this.plugins.pyramidRenderer.highlightCanvas = null;
453 this.plugins.pyramidRenderer = {highlightedSeriesIndex:null};
454 this.plugins.pyramidRenderer.highlightCanvas = new $.jqplot.GenericCanvas();
456 this.eventCanvas._elem.before(this.plugins.pyramidRenderer.highlightCanvas.createElement(this._gridPadding, 'jqplot-pyramidRenderer-highlight-canvas', this._plotDimensions, this));
457 this.plugins.pyramidRenderer.highlightCanvas.setContext();
458 this.eventCanvas._elem.bind('mouseleave', {plot:this}, function (ev) { unhighlight(ev.data.plot); });
461 function highlight (plot, sidx, pidx, points) {
462 var s = plot.series[sidx];
463 var canvas = plot.plugins.pyramidRenderer.highlightCanvas;
464 canvas._ctx.clearRect(0,0,canvas._ctx.canvas.width, canvas._ctx.canvas.height);
465 s._highlightedPoint = pidx;
466 plot.plugins.pyramidRenderer.highlightedSeriesIndex = sidx;
467 var opts = {fillStyle: s.highlightColors[pidx], fillRect: false};
468 s.renderer.shapeRenderer.draw(canvas._ctx, points, opts);
469 if (s.synchronizeHighlight !== false && plot.series.length >= s.synchronizeHighlight && s.synchronizeHighlight !== sidx) {
470 s = plot.series[s.synchronizeHighlight];
471 opts = {fillStyle: s.highlightColors[pidx], fillRect: false};
472 s.renderer.shapeRenderer.draw(canvas._ctx, s._barPoints[pidx], opts);
477 function unhighlight (plot) {
478 var canvas = plot.plugins.pyramidRenderer.highlightCanvas;
479 canvas._ctx.clearRect(0,0, canvas._ctx.canvas.width, canvas._ctx.canvas.height);
480 for (var i=0; i<plot.series.length; i++) {
481 plot.series[i]._highlightedPoint = null;
483 plot.plugins.pyramidRenderer.highlightedSeriesIndex = null;
484 plot.target.trigger('jqplotDataUnhighlight');
489 function handleMove(ev, gridpos, datapos, neighbor, plot) {
491 var ins = [neighbor.seriesIndex, neighbor.pointIndex, neighbor.data];
492 var evt1 = jQuery.Event('jqplotDataMouseOver');
493 evt1.pageX = ev.pageX;
494 evt1.pageY = ev.pageY;
495 plot.target.trigger(evt1, ins);
496 if (plot.series[ins[0]].highlightMouseOver && !(ins[0] == plot.plugins.pyramidRenderer.highlightedSeriesIndex && ins[1] == plot.series[ins[0]]._highlightedPoint)) {
497 var evt = jQuery.Event('jqplotDataHighlight');
498 evt.which = ev.which;
499 evt.pageX = ev.pageX;
500 evt.pageY = ev.pageY;
501 plot.target.trigger(evt, ins);
502 highlight (plot, neighbor.seriesIndex, neighbor.pointIndex, neighbor.points);
505 else if (neighbor == null) {
510 // Have to add hook here, becuase it needs called before series is inited.
511 $.jqplot.preInitHooks.push(preInit);