1 | /** |
---|
2 | * jqPlot |
---|
3 | * Pure JavaScript plotting plugin using jQuery |
---|
4 | * |
---|
5 | * Version: @VERSION |
---|
6 | * |
---|
7 | * Copyright (c) 2009-2011 Chris Leonello |
---|
8 | * jqPlot is currently available for use in all personal or commercial projects |
---|
9 | * under both the MIT (http://www.opensource.org/licenses/mit-license.php) and GPL |
---|
10 | * version 2.0 (http://www.gnu.org/licenses/gpl-2.0.html) licenses. This means that you can |
---|
11 | * choose the license that best suits your project and use it accordingly. |
---|
12 | * |
---|
13 | * Although not required, the author would appreciate an email letting him |
---|
14 | * know of any substantial use of jqPlot. You can reach the author at: |
---|
15 | * chris at jqplot dot com or see http://www.jqplot.com/info.php . |
---|
16 | * |
---|
17 | * If you are feeling kind and generous, consider supporting the project by |
---|
18 | * making a donation at: http://www.jqplot.com/donate.php . |
---|
19 | * |
---|
20 | * sprintf functions contained in jqplot.sprintf.js by Ash Searle: |
---|
21 | * |
---|
22 | * version 2007.04.27 |
---|
23 | * author Ash Searle |
---|
24 | * http://hexmen.com/blog/2007/03/printf-sprintf/ |
---|
25 | * http://hexmen.com/js/sprintf.js |
---|
26 | * The author (Ash Searle) has placed this code in the public domain: |
---|
27 | * "This code is unrestricted: you are free to use it however you like." |
---|
28 | * |
---|
29 | */ |
---|
30 | (function($) { |
---|
31 | |
---|
32 | // Class: $.jqplot.BarRenderer |
---|
33 | // A plugin renderer for jqPlot to draw a bar plot. |
---|
34 | // Draws series as a line. |
---|
35 | |
---|
36 | $.jqplot.BarRenderer = function(){ |
---|
37 | $.jqplot.LineRenderer.call(this); |
---|
38 | }; |
---|
39 | |
---|
40 | $.jqplot.BarRenderer.prototype = new $.jqplot.LineRenderer(); |
---|
41 | $.jqplot.BarRenderer.prototype.constructor = $.jqplot.BarRenderer; |
---|
42 | |
---|
43 | // called with scope of series. |
---|
44 | $.jqplot.BarRenderer.prototype.init = function(options, plot) { |
---|
45 | // Group: Properties |
---|
46 | // |
---|
47 | // prop: barPadding |
---|
48 | // Number of pixels between adjacent bars at the same axis value. |
---|
49 | this.barPadding = 8; |
---|
50 | // prop: barMargin |
---|
51 | // Number of pixels between groups of bars at adjacent axis values. |
---|
52 | this.barMargin = 10; |
---|
53 | // prop: barDirection |
---|
54 | // 'vertical' = up and down bars, 'horizontal' = side to side bars |
---|
55 | this.barDirection = 'vertical'; |
---|
56 | // prop: barWidth |
---|
57 | // Width of the bar in pixels (auto by devaul). null = calculated automatically. |
---|
58 | this.barWidth = null; |
---|
59 | // prop: shadowOffset |
---|
60 | // offset of the shadow from the slice and offset of |
---|
61 | // each succesive stroke of the shadow from the last. |
---|
62 | this.shadowOffset = 2; |
---|
63 | // prop: shadowDepth |
---|
64 | // number of strokes to apply to the shadow, |
---|
65 | // each stroke offset shadowOffset from the last. |
---|
66 | this.shadowDepth = 5; |
---|
67 | // prop: shadowAlpha |
---|
68 | // transparency of the shadow (0 = transparent, 1 = opaque) |
---|
69 | this.shadowAlpha = 0.08; |
---|
70 | // prop: waterfall |
---|
71 | // true to enable waterfall plot. |
---|
72 | this.waterfall = false; |
---|
73 | // prop: groups |
---|
74 | // group bars into this many groups |
---|
75 | this.groups = 1; |
---|
76 | // prop: varyBarColor |
---|
77 | // true to color each bar of a series separately rather than |
---|
78 | // have every bar of a given series the same color. |
---|
79 | // If used for non-stacked multiple series bar plots, user should |
---|
80 | // specify a separate 'seriesColors' array for each series. |
---|
81 | // Otherwise, each series will set their bars to the same color array. |
---|
82 | // This option has no Effect for stacked bar charts and is disabled. |
---|
83 | this.varyBarColor = false; |
---|
84 | // prop: highlightMouseOver |
---|
85 | // True to highlight slice when moused over. |
---|
86 | // This must be false to enable highlightMouseDown to highlight when clicking on a slice. |
---|
87 | this.highlightMouseOver = true; |
---|
88 | // prop: highlightMouseDown |
---|
89 | // True to highlight when a mouse button is pressed over a slice. |
---|
90 | // This will be disabled if highlightMouseOver is true. |
---|
91 | this.highlightMouseDown = false; |
---|
92 | // prop: highlightColors |
---|
93 | // an array of colors to use when highlighting a bar. |
---|
94 | this.highlightColors = []; |
---|
95 | this._type = 'bar'; |
---|
96 | |
---|
97 | // if user has passed in highlightMouseDown option and not set highlightMouseOver, disable highlightMouseOver |
---|
98 | if (options.highlightMouseDown && options.highlightMouseOver == null) { |
---|
99 | options.highlightMouseOver = false; |
---|
100 | } |
---|
101 | |
---|
102 | $.extend(true, this, options); |
---|
103 | // fill is still needed to properly draw the legend. |
---|
104 | // bars have to be filled. |
---|
105 | this.fill = true; |
---|
106 | |
---|
107 | if (this.waterfall) { |
---|
108 | this.fillToZero = false; |
---|
109 | this.disableStack = true; |
---|
110 | } |
---|
111 | |
---|
112 | if (this.barDirection == 'vertical' ) { |
---|
113 | this._primaryAxis = '_xaxis'; |
---|
114 | this._stackAxis = 'y'; |
---|
115 | this.fillAxis = 'y'; |
---|
116 | } |
---|
117 | else { |
---|
118 | this._primaryAxis = '_yaxis'; |
---|
119 | this._stackAxis = 'x'; |
---|
120 | this.fillAxis = 'x'; |
---|
121 | } |
---|
122 | // index of the currenty highlighted point, if any |
---|
123 | this._highlightedPoint = null; |
---|
124 | // total number of values for all bar series, total number of bar series, and position of this series |
---|
125 | this._plotSeriesInfo = null; |
---|
126 | // Array of actual data colors used for each data point. |
---|
127 | this._dataColors = []; |
---|
128 | this._barPoints = []; |
---|
129 | |
---|
130 | // set the shape renderer options |
---|
131 | var opts = {lineJoin:'miter', lineCap:'round', fill:true, isarc:false, strokeStyle:this.color, fillStyle:this.color, closePath:this.fill}; |
---|
132 | this.renderer.shapeRenderer.init(opts); |
---|
133 | // set the shadow renderer options |
---|
134 | var sopts = {lineJoin:'miter', lineCap:'round', fill:true, isarc:false, angle:this.shadowAngle, offset:this.shadowOffset, alpha:this.shadowAlpha, depth:this.shadowDepth, closePath:this.fill}; |
---|
135 | this.renderer.shadowRenderer.init(sopts); |
---|
136 | |
---|
137 | plot.postInitHooks.addOnce(postInit); |
---|
138 | plot.postDrawHooks.addOnce(postPlotDraw); |
---|
139 | plot.eventListenerHooks.addOnce('jqplotMouseMove', handleMove); |
---|
140 | plot.eventListenerHooks.addOnce('jqplotMouseDown', handleMouseDown); |
---|
141 | plot.eventListenerHooks.addOnce('jqplotMouseUp', handleMouseUp); |
---|
142 | plot.eventListenerHooks.addOnce('jqplotClick', handleClick); |
---|
143 | plot.eventListenerHooks.addOnce('jqplotRightClick', handleRightClick); |
---|
144 | }; |
---|
145 | |
---|
146 | // called with scope of series |
---|
147 | function barPreInit(target, data, seriesDefaults, options) { |
---|
148 | if (this.rendererOptions.barDirection == 'horizontal') { |
---|
149 | this._stackAxis = 'x'; |
---|
150 | this._primaryAxis = '_yaxis'; |
---|
151 | } |
---|
152 | if (this.rendererOptions.waterfall == true) { |
---|
153 | this._data = $.extend(true, [], this.data); |
---|
154 | var sum = 0; |
---|
155 | var pos = (!this.rendererOptions.barDirection || this.rendererOptions.barDirection == 'vertical') ? 1 : 0; |
---|
156 | for(var i=0; i<this.data.length; i++) { |
---|
157 | sum += this.data[i][pos]; |
---|
158 | if (i>0) { |
---|
159 | this.data[i][pos] += this.data[i-1][pos]; |
---|
160 | } |
---|
161 | } |
---|
162 | this.data[this.data.length] = (pos == 1) ? [this.data.length+1, sum] : [sum, this.data.length+1]; |
---|
163 | this._data[this._data.length] = (pos == 1) ? [this._data.length+1, sum] : [sum, this._data.length+1]; |
---|
164 | } |
---|
165 | if (this.rendererOptions.groups > 1) { |
---|
166 | this.breakOnNull = true; |
---|
167 | var l = this.data.length; |
---|
168 | var skip = parseInt(l/this.rendererOptions.groups, 10); |
---|
169 | var count = 0; |
---|
170 | for (var i=skip; i<l; i+=skip) { |
---|
171 | this.data.splice(i+count, 0, [null, null]); |
---|
172 | count++; |
---|
173 | } |
---|
174 | for (i=0; i<this.data.length; i++) { |
---|
175 | if (this._primaryAxis == '_xaxis') { |
---|
176 | this.data[i][0] = i+1; |
---|
177 | } |
---|
178 | else { |
---|
179 | this.data[i][1] = i+1; |
---|
180 | } |
---|
181 | } |
---|
182 | } |
---|
183 | } |
---|
184 | |
---|
185 | $.jqplot.preSeriesInitHooks.push(barPreInit); |
---|
186 | |
---|
187 | // needs to be called with scope of series, not renderer. |
---|
188 | $.jqplot.BarRenderer.prototype.calcSeriesNumbers = function() { |
---|
189 | var nvals = 0; |
---|
190 | var nseries = 0; |
---|
191 | var paxis = this[this._primaryAxis]; |
---|
192 | var s, series, pos; |
---|
193 | // loop through all series on this axis |
---|
194 | for (var i=0; i < paxis._series.length; i++) { |
---|
195 | series = paxis._series[i]; |
---|
196 | if (series === this) { |
---|
197 | pos = i; |
---|
198 | } |
---|
199 | // is the series rendered as a bar? |
---|
200 | if (series.renderer.constructor == $.jqplot.BarRenderer) { |
---|
201 | // gridData may not be computed yet, use data length insted |
---|
202 | nvals += series.data.length; |
---|
203 | nseries += 1; |
---|
204 | } |
---|
205 | } |
---|
206 | // return total number of values for all bar series, total number of bar series, and position of this series |
---|
207 | return [nvals, nseries, pos]; |
---|
208 | }; |
---|
209 | |
---|
210 | $.jqplot.BarRenderer.prototype.setBarWidth = function() { |
---|
211 | // need to know how many data values we have on the approprate axis and figure it out. |
---|
212 | var i; |
---|
213 | var nvals = 0; |
---|
214 | var nseries = 0; |
---|
215 | var paxis = this[this._primaryAxis]; |
---|
216 | var s, series, pos; |
---|
217 | var temp = this._plotSeriesInfo = this.renderer.calcSeriesNumbers.call(this); |
---|
218 | nvals = temp[0]; |
---|
219 | nseries = temp[1]; |
---|
220 | var nticks = paxis.numberTicks; |
---|
221 | var nbins = (nticks-1)/2; |
---|
222 | // so, now we have total number of axis values. |
---|
223 | if (paxis.name == 'xaxis' || paxis.name == 'x2axis') { |
---|
224 | if (this._stack) { |
---|
225 | this.barWidth = (paxis._offsets.max - paxis._offsets.min) / nvals * nseries - this.barMargin; |
---|
226 | } |
---|
227 | else { |
---|
228 | this.barWidth = ((paxis._offsets.max - paxis._offsets.min)/nbins - this.barPadding * (nseries-1) - this.barMargin*2)/nseries; |
---|
229 | // this.barWidth = (paxis._offsets.max - paxis._offsets.min) / nvals - this.barPadding - this.barMargin/nseries; |
---|
230 | } |
---|
231 | } |
---|
232 | else { |
---|
233 | if (this._stack) { |
---|
234 | this.barWidth = (paxis._offsets.min - paxis._offsets.max) / nvals * nseries - this.barMargin; |
---|
235 | } |
---|
236 | else { |
---|
237 | this.barWidth = ((paxis._offsets.min - paxis._offsets.max)/nbins - this.barPadding * (nseries-1) - this.barMargin*2)/nseries; |
---|
238 | // this.barWidth = (paxis._offsets.min - paxis._offsets.max) / nvals - this.barPadding - this.barMargin/nseries; |
---|
239 | } |
---|
240 | } |
---|
241 | return [nvals, nseries]; |
---|
242 | }; |
---|
243 | |
---|
244 | function computeHighlightColors (colors) { |
---|
245 | var ret = []; |
---|
246 | for (var i=0; i<colors.length; i++){ |
---|
247 | var rgba = $.jqplot.getColorComponents(colors[i]); |
---|
248 | var newrgb = [rgba[0], rgba[1], rgba[2]]; |
---|
249 | var sum = newrgb[0] + newrgb[1] + newrgb[2]; |
---|
250 | for (var j=0; j<3; j++) { |
---|
251 | // when darkening, lowest color component can be is 60. |
---|
252 | newrgb[j] = (sum > 570) ? newrgb[j] * 0.8 : newrgb[j] + 0.3 * (255 - newrgb[j]); |
---|
253 | newrgb[j] = parseInt(newrgb[j], 10); |
---|
254 | } |
---|
255 | ret.push('rgb('+newrgb[0]+','+newrgb[1]+','+newrgb[2]+')'); |
---|
256 | } |
---|
257 | return ret; |
---|
258 | } |
---|
259 | |
---|
260 | $.jqplot.BarRenderer.prototype.draw = function(ctx, gridData, options) { |
---|
261 | var i; |
---|
262 | // Ughhh, have to make a copy of options b/c it may be modified later. |
---|
263 | var opts = $.extend({}, options); |
---|
264 | var shadow = (opts.shadow != undefined) ? opts.shadow : this.shadow; |
---|
265 | var showLine = (opts.showLine != undefined) ? opts.showLine : this.showLine; |
---|
266 | var fill = (opts.fill != undefined) ? opts.fill : this.fill; |
---|
267 | var xaxis = this.xaxis; |
---|
268 | var yaxis = this.yaxis; |
---|
269 | var xp = this._xaxis.series_u2p; |
---|
270 | var yp = this._yaxis.series_u2p; |
---|
271 | var pointx, pointy; |
---|
272 | // clear out data colors. |
---|
273 | this._dataColors = []; |
---|
274 | this._barPoints = []; |
---|
275 | |
---|
276 | if (this.barWidth == null) { |
---|
277 | this.renderer.setBarWidth.call(this); |
---|
278 | } |
---|
279 | |
---|
280 | var temp = this._plotSeriesInfo = this.renderer.calcSeriesNumbers.call(this); |
---|
281 | var nvals = temp[0]; |
---|
282 | var nseries = temp[1]; |
---|
283 | var pos = temp[2]; |
---|
284 | var points = []; |
---|
285 | |
---|
286 | if (this._stack) { |
---|
287 | this._barNudge = 0; |
---|
288 | } |
---|
289 | else { |
---|
290 | this._barNudge = (-Math.abs(nseries/2 - 0.5) + pos) * (this.barWidth + this.barPadding); |
---|
291 | } |
---|
292 | if (showLine) { |
---|
293 | var negativeColors = new $.jqplot.ColorGenerator(this.negativeSeriesColors); |
---|
294 | var positiveColors = new $.jqplot.ColorGenerator(this.seriesColors); |
---|
295 | var negativeColor = negativeColors.get(this.index); |
---|
296 | if (! this.useNegativeColors) { |
---|
297 | negativeColor = opts.fillStyle; |
---|
298 | } |
---|
299 | var positiveColor = opts.fillStyle; |
---|
300 | var base; |
---|
301 | var xstart; |
---|
302 | var ystart; |
---|
303 | |
---|
304 | if (this.barDirection == 'vertical') { |
---|
305 | for (var i=0; i<gridData.length; i++) { |
---|
306 | if (this.data[i][1] == null) { |
---|
307 | continue; |
---|
308 | } |
---|
309 | points = []; |
---|
310 | base = gridData[i][0] + this._barNudge; |
---|
311 | ystart; |
---|
312 | |
---|
313 | // stacked |
---|
314 | if (this._stack && this._prevGridData.length) { |
---|
315 | ystart = this._prevGridData[i][1]; |
---|
316 | } |
---|
317 | // not stacked and first series in stack |
---|
318 | else { |
---|
319 | if (this.fillToZero) { |
---|
320 | ystart = this._yaxis.series_u2p(0); |
---|
321 | } |
---|
322 | else if (this.waterfall && i > 0 && i < this.gridData.length-1) { |
---|
323 | ystart = this.gridData[i-1][1]; |
---|
324 | } |
---|
325 | else if (this.waterfall && i == 0 && i < this.gridData.length-1) { |
---|
326 | if (this._yaxis.min <= 0 && this._yaxis.max >= 0) { |
---|
327 | ystart = this._yaxis.series_u2p(0); |
---|
328 | } |
---|
329 | else if (this._yaxis.min > 0) { |
---|
330 | ystart = ctx.canvas.height; |
---|
331 | } |
---|
332 | else { |
---|
333 | ystart = 0; |
---|
334 | } |
---|
335 | } |
---|
336 | else if (this.waterfall && i == this.gridData.length - 1) { |
---|
337 | if (this._yaxis.min <= 0 && this._yaxis.max >= 0) { |
---|
338 | ystart = this._yaxis.series_u2p(0); |
---|
339 | } |
---|
340 | else if (this._yaxis.min > 0) { |
---|
341 | ystart = ctx.canvas.height; |
---|
342 | } |
---|
343 | else { |
---|
344 | ystart = 0; |
---|
345 | } |
---|
346 | } |
---|
347 | else { |
---|
348 | ystart = ctx.canvas.height; |
---|
349 | } |
---|
350 | } |
---|
351 | if ((this.fillToZero && this._plotData[i][1] < 0) || (this.waterfall && this._data[i][1] < 0)) { |
---|
352 | if (this.varyBarColor && !this._stack) { |
---|
353 | if (this.useNegativeColors) { |
---|
354 | opts.fillStyle = negativeColors.next(); |
---|
355 | } |
---|
356 | else { |
---|
357 | opts.fillStyle = positiveColors.next(); |
---|
358 | } |
---|
359 | } |
---|
360 | else { |
---|
361 | opts.fillStyle = negativeColor; |
---|
362 | } |
---|
363 | } |
---|
364 | else { |
---|
365 | if (this.varyBarColor && !this._stack) { |
---|
366 | opts.fillStyle = positiveColors.next(); |
---|
367 | } |
---|
368 | else { |
---|
369 | opts.fillStyle = positiveColor; |
---|
370 | } |
---|
371 | } |
---|
372 | |
---|
373 | if (!this.fillToZero || this._plotData[i][1] >= 0) { |
---|
374 | points.push([base-this.barWidth/2, ystart]); |
---|
375 | points.push([base-this.barWidth/2, gridData[i][1]]); |
---|
376 | points.push([base+this.barWidth/2, gridData[i][1]]); |
---|
377 | points.push([base+this.barWidth/2, ystart]); |
---|
378 | } |
---|
379 | // for negative bars make sure points are always ordered clockwise |
---|
380 | else { |
---|
381 | points.push([base-this.barWidth/2, gridData[i][1]]); |
---|
382 | points.push([base-this.barWidth/2, ystart]); |
---|
383 | points.push([base+this.barWidth/2, ystart]); |
---|
384 | points.push([base+this.barWidth/2, gridData[i][1]]); |
---|
385 | } |
---|
386 | this._barPoints.push(points); |
---|
387 | // now draw the shadows if not stacked. |
---|
388 | // for stacked plots, they are predrawn by drawShadow |
---|
389 | if (shadow && !this._stack) { |
---|
390 | var sopts = $.extend(true, {}, opts); |
---|
391 | // need to get rid of fillStyle on shadow. |
---|
392 | delete sopts.fillStyle; |
---|
393 | this.renderer.shadowRenderer.draw(ctx, points, sopts); |
---|
394 | } |
---|
395 | var clr = opts.fillStyle || this.color; |
---|
396 | this._dataColors.push(clr); |
---|
397 | this.renderer.shapeRenderer.draw(ctx, points, opts); |
---|
398 | } |
---|
399 | } |
---|
400 | |
---|
401 | else if (this.barDirection == 'horizontal'){ |
---|
402 | for (var i=0; i<gridData.length; i++) { |
---|
403 | if (this.data[i][0] == null) { |
---|
404 | continue; |
---|
405 | } |
---|
406 | points = []; |
---|
407 | base = gridData[i][1] - this._barNudge; |
---|
408 | xstart; |
---|
409 | |
---|
410 | if (this._stack && this._prevGridData.length) { |
---|
411 | xstart = this._prevGridData[i][0]; |
---|
412 | } |
---|
413 | // not stacked and first series in stack |
---|
414 | else { |
---|
415 | if (this.fillToZero) { |
---|
416 | xstart = this._xaxis.series_u2p(0); |
---|
417 | } |
---|
418 | else if (this.waterfall && i > 0 && i < this.gridData.length-1) { |
---|
419 | xstart = this.gridData[i-1][1]; |
---|
420 | } |
---|
421 | else if (this.waterfall && i == 0 && i < this.gridData.length-1) { |
---|
422 | if (this._xaxis.min <= 0 && this._xaxis.max >= 0) { |
---|
423 | xstart = this._xaxis.series_u2p(0); |
---|
424 | } |
---|
425 | else if (this._xaxis.min > 0) { |
---|
426 | xstart = 0; |
---|
427 | } |
---|
428 | else { |
---|
429 | xstart = ctx.canvas.width; |
---|
430 | } |
---|
431 | } |
---|
432 | else if (this.waterfall && i == this.gridData.length - 1) { |
---|
433 | if (this._xaxis.min <= 0 && this._xaxis.max >= 0) { |
---|
434 | xstart = this._xaxis.series_u2p(0); |
---|
435 | } |
---|
436 | else if (this._xaxis.min > 0) { |
---|
437 | xstart = 0; |
---|
438 | } |
---|
439 | else { |
---|
440 | xstart = ctx.canvas.width; |
---|
441 | } |
---|
442 | } |
---|
443 | else { |
---|
444 | xstart = 0; |
---|
445 | } |
---|
446 | } |
---|
447 | if ((this.fillToZero && this._plotData[i][1] < 0) || (this.waterfall && this._data[i][1] < 0)) { |
---|
448 | if (this.varyBarColor && !this._stack) { |
---|
449 | if (this.useNegativeColors) { |
---|
450 | opts.fillStyle = negativeColors.next(); |
---|
451 | } |
---|
452 | else { |
---|
453 | opts.fillStyle = positiveColors.next(); |
---|
454 | } |
---|
455 | } |
---|
456 | } |
---|
457 | else { |
---|
458 | if (this.varyBarColor && !this._stack) { |
---|
459 | opts.fillStyle = positiveColors.next(); |
---|
460 | } |
---|
461 | else { |
---|
462 | opts.fillStyle = positiveColor; |
---|
463 | } |
---|
464 | } |
---|
465 | |
---|
466 | points.push([xstart, base+this.barWidth/2]); |
---|
467 | points.push([xstart, base-this.barWidth/2]); |
---|
468 | points.push([gridData[i][0], base-this.barWidth/2]); |
---|
469 | points.push([gridData[i][0], base+this.barWidth/2]); |
---|
470 | this._barPoints.push(points); |
---|
471 | // now draw the shadows if not stacked. |
---|
472 | // for stacked plots, they are predrawn by drawShadow |
---|
473 | if (shadow && !this._stack) { |
---|
474 | var sopts = $.extend(true, {}, opts); |
---|
475 | delete sopts.fillStyle; |
---|
476 | this.renderer.shadowRenderer.draw(ctx, points, sopts); |
---|
477 | } |
---|
478 | var clr = opts.fillStyle || this.color; |
---|
479 | this._dataColors.push(clr); |
---|
480 | this.renderer.shapeRenderer.draw(ctx, points, opts); |
---|
481 | } |
---|
482 | } |
---|
483 | } |
---|
484 | |
---|
485 | if (this.highlightColors.length == 0) { |
---|
486 | this.highlightColors = computeHighlightColors(this._dataColors); |
---|
487 | } |
---|
488 | |
---|
489 | else if (typeof(this.highlightColors) == 'string') { |
---|
490 | var temp = this.highlightColors; |
---|
491 | this.highlightColors = []; |
---|
492 | for (var i=0; i<this._dataColors.length; i++) { |
---|
493 | this.highlightColors.push(temp); |
---|
494 | } |
---|
495 | } |
---|
496 | |
---|
497 | }; |
---|
498 | |
---|
499 | |
---|
500 | // for stacked plots, shadows will be pre drawn by drawShadow. |
---|
501 | $.jqplot.BarRenderer.prototype.drawShadow = function(ctx, gridData, options) { |
---|
502 | var i; |
---|
503 | var opts = (options != undefined) ? options : {}; |
---|
504 | var shadow = (opts.shadow != undefined) ? opts.shadow : this.shadow; |
---|
505 | var showLine = (opts.showLine != undefined) ? opts.showLine : this.showLine; |
---|
506 | var fill = (opts.fill != undefined) ? opts.fill : this.fill; |
---|
507 | var xaxis = this.xaxis; |
---|
508 | var yaxis = this.yaxis; |
---|
509 | var xp = this._xaxis.series_u2p; |
---|
510 | var yp = this._yaxis.series_u2p; |
---|
511 | var pointx, points, pointy, nvals, nseries, pos; |
---|
512 | |
---|
513 | if (this._stack && this.shadow) { |
---|
514 | if (this.barWidth == null) { |
---|
515 | this.renderer.setBarWidth.call(this); |
---|
516 | } |
---|
517 | |
---|
518 | var temp = this._plotSeriesInfo = this.renderer.calcSeriesNumbers.call(this); |
---|
519 | nvals = temp[0]; |
---|
520 | nseries = temp[1]; |
---|
521 | pos = temp[2]; |
---|
522 | |
---|
523 | if (this._stack) { |
---|
524 | this._barNudge = 0; |
---|
525 | } |
---|
526 | else { |
---|
527 | this._barNudge = (-Math.abs(nseries/2 - 0.5) + pos) * (this.barWidth + this.barPadding); |
---|
528 | } |
---|
529 | if (showLine) { |
---|
530 | |
---|
531 | if (this.barDirection == 'vertical') { |
---|
532 | for (var i=0; i<gridData.length; i++) { |
---|
533 | if (this.data[i][1] == null) { |
---|
534 | continue; |
---|
535 | } |
---|
536 | points = []; |
---|
537 | var base = gridData[i][0] + this._barNudge; |
---|
538 | var ystart; |
---|
539 | |
---|
540 | if (this._stack && this._prevGridData.length) { |
---|
541 | ystart = this._prevGridData[i][1]; |
---|
542 | } |
---|
543 | else { |
---|
544 | if (this.fillToZero) { |
---|
545 | ystart = this._yaxis.series_u2p(0); |
---|
546 | } |
---|
547 | else { |
---|
548 | ystart = ctx.canvas.height; |
---|
549 | } |
---|
550 | } |
---|
551 | |
---|
552 | points.push([base-this.barWidth/2, ystart]); |
---|
553 | points.push([base-this.barWidth/2, gridData[i][1]]); |
---|
554 | points.push([base+this.barWidth/2, gridData[i][1]]); |
---|
555 | points.push([base+this.barWidth/2, ystart]); |
---|
556 | this.renderer.shadowRenderer.draw(ctx, points, opts); |
---|
557 | } |
---|
558 | } |
---|
559 | |
---|
560 | else if (this.barDirection == 'horizontal'){ |
---|
561 | for (var i=0; i<gridData.length; i++) { |
---|
562 | if (this.data[i][0] == null) { |
---|
563 | continue; |
---|
564 | } |
---|
565 | points = []; |
---|
566 | var base = gridData[i][1] - this._barNudge; |
---|
567 | var xstart; |
---|
568 | |
---|
569 | if (this._stack && this._prevGridData.length) { |
---|
570 | xstart = this._prevGridData[i][0]; |
---|
571 | } |
---|
572 | else { |
---|
573 | xstart = 0; |
---|
574 | } |
---|
575 | |
---|
576 | points.push([xstart, base+this.barWidth/2]); |
---|
577 | points.push([gridData[i][0], base+this.barWidth/2]); |
---|
578 | points.push([gridData[i][0], base-this.barWidth/2]); |
---|
579 | points.push([xstart, base-this.barWidth/2]); |
---|
580 | this.renderer.shadowRenderer.draw(ctx, points, opts); |
---|
581 | } |
---|
582 | } |
---|
583 | } |
---|
584 | |
---|
585 | } |
---|
586 | }; |
---|
587 | |
---|
588 | function postInit(target, data, options) { |
---|
589 | for (var i=0; i<this.series.length; i++) { |
---|
590 | if (this.series[i].renderer.constructor == $.jqplot.BarRenderer) { |
---|
591 | // don't allow mouseover and mousedown at same time. |
---|
592 | if (this.series[i].highlightMouseOver) { |
---|
593 | this.series[i].highlightMouseDown = false; |
---|
594 | } |
---|
595 | } |
---|
596 | } |
---|
597 | this.target.bind('mouseout', {plot:this}, function (ev) { unhighlight(ev.data.plot); }); |
---|
598 | } |
---|
599 | |
---|
600 | // called within context of plot |
---|
601 | // create a canvas which we can draw on. |
---|
602 | // insert it before the eventCanvas, so eventCanvas will still capture events. |
---|
603 | function postPlotDraw() { |
---|
604 | // Memory Leaks patch |
---|
605 | if (this.plugins.barRenderer && this.plugins.barRenderer.highlightCanvas) { |
---|
606 | |
---|
607 | this.plugins.barRenderer.highlightCanvas.resetCanvas(); |
---|
608 | this.plugins.barRenderer.highlightCanvas = null; |
---|
609 | } |
---|
610 | |
---|
611 | this.plugins.barRenderer = {highlightedSeriesIndex:null}; |
---|
612 | this.plugins.barRenderer.highlightCanvas = new $.jqplot.GenericCanvas(); |
---|
613 | |
---|
614 | this.eventCanvas._elem.before(this.plugins.barRenderer.highlightCanvas.createElement(this._gridPadding, 'jqplot-barRenderer-highlight-canvas', this._plotDimensions, this)); |
---|
615 | this.plugins.barRenderer.highlightCanvas.setContext(); |
---|
616 | } |
---|
617 | |
---|
618 | function highlight (plot, sidx, pidx, points) { |
---|
619 | var s = plot.series[sidx]; |
---|
620 | var canvas = plot.plugins.barRenderer.highlightCanvas; |
---|
621 | canvas._ctx.clearRect(0,0,canvas._ctx.canvas.width, canvas._ctx.canvas.height); |
---|
622 | s._highlightedPoint = pidx; |
---|
623 | plot.plugins.barRenderer.highlightedSeriesIndex = sidx; |
---|
624 | var opts = {fillStyle: s.highlightColors[pidx]}; |
---|
625 | s.renderer.shapeRenderer.draw(canvas._ctx, points, opts); |
---|
626 | canvas = null; |
---|
627 | } |
---|
628 | |
---|
629 | function unhighlight (plot) { |
---|
630 | var canvas = plot.plugins.barRenderer.highlightCanvas; |
---|
631 | canvas._ctx.clearRect(0,0, canvas._ctx.canvas.width, canvas._ctx.canvas.height); |
---|
632 | for (var i=0; i<plot.series.length; i++) { |
---|
633 | plot.series[i]._highlightedPoint = null; |
---|
634 | } |
---|
635 | plot.plugins.barRenderer.highlightedSeriesIndex = null; |
---|
636 | plot.target.trigger('jqplotDataUnhighlight'); |
---|
637 | canvas = null; |
---|
638 | } |
---|
639 | |
---|
640 | |
---|
641 | function handleMove(ev, gridpos, datapos, neighbor, plot) { |
---|
642 | if (neighbor) { |
---|
643 | var ins = [neighbor.seriesIndex, neighbor.pointIndex, neighbor.data]; |
---|
644 | var evt1 = jQuery.Event('jqplotDataMouseOver'); |
---|
645 | evt1.pageX = ev.pageX; |
---|
646 | evt1.pageY = ev.pageY; |
---|
647 | plot.target.trigger(evt1, ins); |
---|
648 | if (plot.series[ins[0]].highlightMouseOver && !(ins[0] == plot.plugins.barRenderer.highlightedSeriesIndex && ins[1] == plot.series[ins[0]]._highlightedPoint)) { |
---|
649 | var evt = jQuery.Event('jqplotDataHighlight'); |
---|
650 | evt.pageX = ev.pageX; |
---|
651 | evt.pageY = ev.pageY; |
---|
652 | plot.target.trigger(evt, ins); |
---|
653 | highlight (plot, neighbor.seriesIndex, neighbor.pointIndex, neighbor.points); |
---|
654 | } |
---|
655 | } |
---|
656 | else if (neighbor == null) { |
---|
657 | unhighlight (plot); |
---|
658 | } |
---|
659 | } |
---|
660 | |
---|
661 | function handleMouseDown(ev, gridpos, datapos, neighbor, plot) { |
---|
662 | if (neighbor) { |
---|
663 | var ins = [neighbor.seriesIndex, neighbor.pointIndex, neighbor.data]; |
---|
664 | if (plot.series[ins[0]].highlightMouseDown && !(ins[0] == plot.plugins.barRenderer.highlightedSeriesIndex && ins[1] == plot.series[ins[0]]._highlightedPoint)) { |
---|
665 | var evt = jQuery.Event('jqplotDataHighlight'); |
---|
666 | evt.pageX = ev.pageX; |
---|
667 | evt.pageY = ev.pageY; |
---|
668 | plot.target.trigger(evt, ins); |
---|
669 | highlight (plot, neighbor.seriesIndex, neighbor.pointIndex, neighbor.points); |
---|
670 | } |
---|
671 | } |
---|
672 | else if (neighbor == null) { |
---|
673 | unhighlight (plot); |
---|
674 | } |
---|
675 | } |
---|
676 | |
---|
677 | function handleMouseUp(ev, gridpos, datapos, neighbor, plot) { |
---|
678 | var idx = plot.plugins.barRenderer.highlightedSeriesIndex; |
---|
679 | if (idx != null && plot.series[idx].highlightMouseDown) { |
---|
680 | unhighlight(plot); |
---|
681 | } |
---|
682 | } |
---|
683 | |
---|
684 | function handleClick(ev, gridpos, datapos, neighbor, plot) { |
---|
685 | if (neighbor) { |
---|
686 | var ins = [neighbor.seriesIndex, neighbor.pointIndex, neighbor.data]; |
---|
687 | var evt = jQuery.Event('jqplotDataClick'); |
---|
688 | evt.pageX = ev.pageX; |
---|
689 | evt.pageY = ev.pageY; |
---|
690 | plot.target.trigger(evt, ins); |
---|
691 | } |
---|
692 | } |
---|
693 | |
---|
694 | function handleRightClick(ev, gridpos, datapos, neighbor, plot) { |
---|
695 | if (neighbor) { |
---|
696 | var ins = [neighbor.seriesIndex, neighbor.pointIndex, neighbor.data]; |
---|
697 | var idx = plot.plugins.barRenderer.highlightedSeriesIndex; |
---|
698 | if (idx != null && plot.series[idx].highlightMouseDown) { |
---|
699 | unhighlight(plot); |
---|
700 | } |
---|
701 | var evt = jQuery.Event('jqplotDataRightClick'); |
---|
702 | evt.pageX = ev.pageX; |
---|
703 | evt.pageY = ev.pageY; |
---|
704 | plot.target.trigger(evt, ins); |
---|
705 | } |
---|
706 | } |
---|
707 | |
---|
708 | |
---|
709 | })(jQuery); |
---|