jqplot.barRenderer.js 34 KB

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