jqplot.pyramidRenderer.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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. // Need to ensure pyramid axis and grid renderers are loaded.
  33. // You should load these with script tags in the html head, that is more efficient
  34. // as the browser will cache the request.
  35. // Note, have to block with synchronous request in order to execute bar renderer code.
  36. if ($.jqplot.PyramidAxisRenderer === undefined) {
  37. $.ajax({
  38. url: $.jqplot.pluginLocation + 'jqplot.pyramidAxisRenderer.js',
  39. dataType: "script",
  40. async: false
  41. });
  42. }
  43. if ($.jqplot.PyramidGridRenderer === undefined) {
  44. $.ajax({
  45. url: $.jqplot.pluginLocation + 'jqplot.pyramidGridRenderer.js',
  46. dataType: "script",
  47. async: false
  48. });
  49. }
  50. $.jqplot.PyramidRenderer = function(){
  51. $.jqplot.LineRenderer.call(this);
  52. };
  53. $.jqplot.PyramidRenderer.prototype = new $.jqplot.LineRenderer();
  54. $.jqplot.PyramidRenderer.prototype.constructor = $.jqplot.PyramidRenderer;
  55. // called with scope of a series
  56. $.jqplot.PyramidRenderer.prototype.init = function(options, plot) {
  57. options = options || {};
  58. this._type = 'pyramid';
  59. // Group: Properties
  60. //
  61. // prop: barPadding
  62. this.barPadding = 10;
  63. this.barWidth = null;
  64. // prop: fill
  65. // True to fill the bars.
  66. this.fill = true;
  67. // prop: highlightMouseOver
  68. // True to highlight slice when moused over.
  69. // This must be false to enable highlightMouseDown to highlight when clicking on a slice.
  70. this.highlightMouseOver = true;
  71. // prop: highlightMouseDown
  72. // True to highlight when a mouse button is pressed over a slice.
  73. // This will be disabled if highlightMouseOver is true.
  74. this.highlightMouseDown = false;
  75. // prop: highlightColors
  76. // an array of colors to use when highlighting a slice.
  77. this.highlightColors = [];
  78. // prop highlightThreshold
  79. // Expand the highlightable region in the x direction.
  80. // E.g. a value of 3 will highlight a bar when the mouse is
  81. // within 3 pixels of the bar in the x direction.
  82. this.highlightThreshold = 2;
  83. // prop: synchronizeHighlight
  84. // Index of another series to highlight when this series is highlighted.
  85. // null or false to not synchronize.
  86. this.synchronizeHighlight = false;
  87. // prop: offsetBars
  88. // False will center bars on their y value.
  89. // True will push bars up by 1/2 bar width to fill between their y values.
  90. // If true, there needs to be 1 more tick than there are bars.
  91. this.offsetBars = false;
  92. // if user has passed in highlightMouseDown option and not set highlightMouseOver, disable highlightMouseOver
  93. if (options.highlightMouseDown && options.highlightMouseOver == null) {
  94. options.highlightMouseOver = false;
  95. }
  96. this.side = 'right';
  97. $.extend(true, this, options);
  98. // if (this.fill === false) {
  99. // this.shadow = false;
  100. // }
  101. if (this.side === 'left') {
  102. this._highlightThreshold = [[-this.highlightThreshold, 0], [-this.highlightThreshold, 0], [0,0], [0,0]];
  103. }
  104. else {
  105. this._highlightThreshold = [[0,0], [0,0], [this.highlightThreshold, 0], [this.highlightThreshold, 0]];
  106. }
  107. this.renderer.options = options;
  108. // index of the currenty highlighted point, if any
  109. this._highlightedPoint = null;
  110. // Array of actual data colors used for each data point.
  111. this._dataColors = [];
  112. this._barPoints = [];
  113. this.fillAxis = 'y';
  114. this._primaryAxis = '_yaxis';
  115. this._xnudge = 0;
  116. // set the shape renderer options
  117. 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};
  118. this.renderer.shapeRenderer.init(opts);
  119. // set the shadow renderer options
  120. var shadow_offset = options.shadowOffset;
  121. // set the shadow renderer options
  122. if (shadow_offset == null) {
  123. // scale the shadowOffset to the width of the line.
  124. if (this.lineWidth > 2.5) {
  125. shadow_offset = 1.25 * (1 + (Math.atan((this.lineWidth/2.5))/0.785398163 - 1)*0.6);
  126. // var shadow_offset = this.shadowOffset;
  127. }
  128. // for skinny lines, don't make such a big shadow.
  129. else {
  130. shadow_offset = 1.25 * Math.atan((this.lineWidth/2.5))/0.785398163;
  131. }
  132. }
  133. 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};
  134. this.renderer.shadowRenderer.init(sopts);
  135. plot.postDrawHooks.addOnce(postPlotDraw);
  136. plot.eventListenerHooks.addOnce('jqplotMouseMove', handleMove);
  137. // if this is the left side of pyramid, set y values to negative.
  138. if (this.side === 'left') {
  139. for (var i=0, l=this.data.length; i<l; i++) {
  140. this.data[i][1] = -Math.abs(this.data[i][1]);
  141. }
  142. }
  143. };
  144. // setGridData
  145. // converts the user data values to grid coordinates and stores them
  146. // in the gridData array.
  147. // Called with scope of a series.
  148. $.jqplot.PyramidRenderer.prototype.setGridData = function(plot) {
  149. // recalculate the grid data
  150. var xp = this._xaxis.series_u2p;
  151. var yp = this._yaxis.series_u2p;
  152. var data = this._plotData;
  153. var pdata = this._prevPlotData;
  154. this.gridData = [];
  155. this._prevGridData = [];
  156. var l = data.length;
  157. var adjust = false;
  158. var i;
  159. // if any data values are < 0, consider this a negative series
  160. for (i = 0; i < l; i++) {
  161. if (data[i][1] < 0) {
  162. this.side = 'left';
  163. }
  164. }
  165. if (this._yaxis.name === 'yMidAxis' && this.side === 'right') {
  166. this._xnudge = this._xaxis.max/2000.0;
  167. adjust = true;
  168. }
  169. for (i = 0; i < l; i++) {
  170. // if not a line series or if no nulls in data, push the converted point onto the array.
  171. if (data[i][0] != null && data[i][1] != null) {
  172. this.gridData.push([xp(data[i][1]), yp(data[i][0])]);
  173. }
  174. // else if there is a null, preserve it.
  175. else if (data[i][0] == null) {
  176. this.gridData.push([xp(data[i][1]), null]);
  177. }
  178. else if (data[i][1] == null) {
  179. this.gridData.push(null, [yp(data[i][0])]);
  180. }
  181. // finally, adjust x grid data if have to
  182. if (data[i][1] === 0 && adjust) {
  183. this.gridData[i][0] = xp(this._xnudge);
  184. }
  185. }
  186. };
  187. // makeGridData
  188. // converts any arbitrary data values to grid coordinates and
  189. // returns them. This method exists so that plugins can use a series'
  190. // linerenderer to generate grid data points without overwriting the
  191. // grid data associated with that series.
  192. // Called with scope of a series.
  193. $.jqplot.PyramidRenderer.prototype.makeGridData = function(data, plot) {
  194. // recalculate the grid data
  195. var xp = this._xaxis.series_u2p;
  196. var yp = this._yaxis.series_u2p;
  197. var gd = [];
  198. var l = data.length;
  199. var adjust = false;
  200. var i;
  201. // if any data values are < 0, consider this a negative series
  202. for (i = 0; i < l; i++) {
  203. if (data[i][1] < 0) {
  204. this.side = 'left';
  205. }
  206. }
  207. if (this._yaxis.name === 'yMidAxis' && this.side === 'right') {
  208. this._xnudge = this._xaxis.max/2000.0;
  209. adjust = true;
  210. }
  211. for (i = 0; i < l; i++) {
  212. // if not a line series or if no nulls in data, push the converted point onto the array.
  213. if (data[i][0] != null && data[i][1] != null) {
  214. gd.push([xp(data[i][1]), yp(data[i][0])]);
  215. }
  216. // else if there is a null, preserve it.
  217. else if (data[i][0] == null) {
  218. gd.push([xp(data[i][1]), null]);
  219. }
  220. else if (data[i][1] == null) {
  221. gd.push([null, yp(data[i][0])]);
  222. }
  223. // finally, adjust x grid data if have to
  224. if (data[i][1] === 0 && adjust) {
  225. gd[i][0] = xp(this._xnudge);
  226. }
  227. }
  228. return gd;
  229. };
  230. $.jqplot.PyramidRenderer.prototype.setBarWidth = function() {
  231. // need to know how many data values we have on the approprate axis and figure it out.
  232. var i;
  233. var nvals = 0;
  234. var nseries = 0;
  235. var paxis = this[this._primaryAxis];
  236. var s, series, pos;
  237. nvals = paxis.max - paxis.min;
  238. var nticks = paxis.numberTicks;
  239. var nbins = (nticks-1)/2;
  240. // so, now we have total number of axis values.
  241. var temp = (this.barPadding === 0) ? 1.0 : 0;
  242. if (paxis.name == 'xaxis' || paxis.name == 'x2axis') {
  243. this.barWidth = (paxis._offsets.max - paxis._offsets.min) / nvals - this.barPadding + temp;
  244. }
  245. else {
  246. if (this.fill) {
  247. this.barWidth = (paxis._offsets.min - paxis._offsets.max) / nvals - this.barPadding + temp;
  248. }
  249. else {
  250. this.barWidth = (paxis._offsets.min - paxis._offsets.max) / nvals;
  251. }
  252. }
  253. };
  254. $.jqplot.PyramidRenderer.prototype.draw = function(ctx, gridData, options) {
  255. var i;
  256. // Ughhh, have to make a copy of options b/c it may be modified later.
  257. var opts = $.extend({}, options);
  258. var shadow = (opts.shadow != undefined) ? opts.shadow : this.shadow;
  259. var showLine = (opts.showLine != undefined) ? opts.showLine : this.showLine;
  260. var fill = (opts.fill != undefined) ? opts.fill : this.fill;
  261. var xp = this._xaxis.series_u2p;
  262. var yp = this._yaxis.series_u2p;
  263. var pointx, pointy;
  264. // clear out data colors.
  265. this._dataColors = [];
  266. this._barPoints = [];
  267. if (this.renderer.options.barWidth == null) {
  268. this.renderer.setBarWidth.call(this);
  269. }
  270. // var temp = this._plotSeriesInfo = this.renderer.calcSeriesNumbers.call(this);
  271. // var nvals = temp[0];
  272. // var nseries = temp[1];
  273. // var pos = temp[2];
  274. var points = [],
  275. w,
  276. h;
  277. // this._barNudge = 0;
  278. if (showLine) {
  279. var negativeColors = new $.jqplot.ColorGenerator(this.negativeSeriesColors);
  280. var positiveColors = new $.jqplot.ColorGenerator(this.seriesColors);
  281. var negativeColor = negativeColors.get(this.index);
  282. if (! this.useNegativeColors) {
  283. negativeColor = opts.fillStyle;
  284. }
  285. var positiveColor = opts.fillStyle;
  286. var base;
  287. var xstart = this._xaxis.series_u2p(this._xnudge);
  288. var ystart = this._yaxis.series_u2p(this._yaxis.min);
  289. var yend = this._yaxis.series_u2p(this._yaxis.max);
  290. var bw = this.barWidth;
  291. var bw2 = bw/2.0;
  292. var points = [];
  293. var yadj = this.offsetBars ? bw2 : 0;
  294. for (var i=0, l=gridData.length; i<l; i++) {
  295. if (this.data[i][0] == null) {
  296. continue;
  297. }
  298. base = gridData[i][1];
  299. // not stacked and first series in stack
  300. if (this._plotData[i][1] < 0) {
  301. if (this.varyBarColor && !this._stack) {
  302. if (this.useNegativeColors) {
  303. opts.fillStyle = negativeColors.next();
  304. }
  305. else {
  306. opts.fillStyle = positiveColors.next();
  307. }
  308. }
  309. }
  310. else {
  311. if (this.varyBarColor && !this._stack) {
  312. opts.fillStyle = positiveColors.next();
  313. }
  314. else {
  315. opts.fillStyle = positiveColor;
  316. }
  317. }
  318. if (this.fill) {
  319. if (this._plotData[i][1] >= 0) {
  320. // xstart = this._xaxis.series_u2p(this._xnudge);
  321. w = gridData[i][0] - xstart;
  322. h = this.barWidth;
  323. points = [xstart, base - bw2 - yadj, w, h];
  324. }
  325. else {
  326. // xstart = this._xaxis.series_u2p(0);
  327. w = xstart - gridData[i][0];
  328. h = this.barWidth;
  329. points = [gridData[i][0], base - bw2 - yadj, w, h];
  330. }
  331. this._barPoints.push([[points[0], points[1] + h], [points[0], points[1]], [points[0] + w, points[1]], [points[0] + w, points[1] + h]]);
  332. if (shadow) {
  333. this.renderer.shadowRenderer.draw(ctx, points);
  334. }
  335. var clr = opts.fillStyle || this.color;
  336. this._dataColors.push(clr);
  337. this.renderer.shapeRenderer.draw(ctx, points, opts);
  338. }
  339. else {
  340. if (i === 0) {
  341. points =[[xstart, ystart], [gridData[i][0], ystart], [gridData[i][0], gridData[i][1] - bw2 - yadj]];
  342. }
  343. else if (i < l-1) {
  344. 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]]);
  345. }
  346. // finally, draw the line
  347. else {
  348. 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]]);
  349. if (shadow) {
  350. this.renderer.shadowRenderer.draw(ctx, points);
  351. }
  352. var clr = opts.fillStyle || this.color;
  353. this._dataColors.push(clr);
  354. this.renderer.shapeRenderer.draw(ctx, points, opts);
  355. }
  356. }
  357. }
  358. }
  359. if (this.highlightColors.length == 0) {
  360. this.highlightColors = $.jqplot.computeHighlightColors(this._dataColors);
  361. }
  362. else if (typeof(this.highlightColors) == 'string') {
  363. this.highlightColors = [];
  364. for (var i=0; i<this._dataColors.length; i++) {
  365. this.highlightColors.push(this.highlightColors);
  366. }
  367. }
  368. };
  369. // setup default renderers for axes and legend so user doesn't have to
  370. // called with scope of plot
  371. function preInit(target, data, options) {
  372. options = options || {};
  373. options.axesDefaults = options.axesDefaults || {};
  374. options.grid = options.grid || {};
  375. options.legend = options.legend || {};
  376. options.seriesDefaults = options.seriesDefaults || {};
  377. // only set these if there is a pie series
  378. var setopts = false;
  379. if (options.seriesDefaults.renderer === $.jqplot.PyramidRenderer) {
  380. setopts = true;
  381. }
  382. else if (options.series) {
  383. for (var i=0; i < options.series.length; i++) {
  384. if (options.series[i].renderer === $.jqplot.PyramidRenderer) {
  385. setopts = true;
  386. }
  387. }
  388. }
  389. if (setopts) {
  390. options.axesDefaults.renderer = $.jqplot.PyramidAxisRenderer;
  391. options.grid.renderer = $.jqplot.PyramidGridRenderer;
  392. options.seriesDefaults.pointLabels = {show: false};
  393. }
  394. }
  395. // called within context of plot
  396. // create a canvas which we can draw on.
  397. // insert it before the eventCanvas, so eventCanvas will still capture events.
  398. function postPlotDraw() {
  399. // Memory Leaks patch
  400. if (this.plugins.pyramidRenderer && this.plugins.pyramidRenderer.highlightCanvas) {
  401. this.plugins.pyramidRenderer.highlightCanvas.resetCanvas();
  402. this.plugins.pyramidRenderer.highlightCanvas = null;
  403. }
  404. this.plugins.pyramidRenderer = {highlightedSeriesIndex:null};
  405. this.plugins.pyramidRenderer.highlightCanvas = new $.jqplot.GenericCanvas();
  406. this.eventCanvas._elem.before(this.plugins.pyramidRenderer.highlightCanvas.createElement(this._gridPadding, 'jqplot-pyramidRenderer-highlight-canvas', this._plotDimensions, this));
  407. this.plugins.pyramidRenderer.highlightCanvas.setContext();
  408. this.eventCanvas._elem.bind('mouseleave', {plot:this}, function (ev) { unhighlight(ev.data.plot); });
  409. }
  410. function highlight (plot, sidx, pidx, points) {
  411. var s = plot.series[sidx];
  412. var canvas = plot.plugins.pyramidRenderer.highlightCanvas;
  413. canvas._ctx.clearRect(0,0,canvas._ctx.canvas.width, canvas._ctx.canvas.height);
  414. s._highlightedPoint = pidx;
  415. plot.plugins.pyramidRenderer.highlightedSeriesIndex = sidx;
  416. var opts = {fillStyle: s.highlightColors[pidx], fillRect: false};
  417. s.renderer.shapeRenderer.draw(canvas._ctx, points, opts);
  418. if (s.synchronizeHighlight !== false && plot.series.length >= s.synchronizeHighlight && s.synchronizeHighlight !== sidx) {
  419. s = plot.series[s.synchronizeHighlight];
  420. opts = {fillStyle: s.highlightColors[pidx], fillRect: false};
  421. s.renderer.shapeRenderer.draw(canvas._ctx, s._barPoints[pidx], opts);
  422. }
  423. canvas = null;
  424. }
  425. function unhighlight (plot) {
  426. var canvas = plot.plugins.pyramidRenderer.highlightCanvas;
  427. canvas._ctx.clearRect(0,0, canvas._ctx.canvas.width, canvas._ctx.canvas.height);
  428. for (var i=0; i<plot.series.length; i++) {
  429. plot.series[i]._highlightedPoint = null;
  430. }
  431. plot.plugins.pyramidRenderer.highlightedSeriesIndex = null;
  432. plot.target.trigger('jqplotDataUnhighlight');
  433. canvas = null;
  434. }
  435. function handleMove(ev, gridpos, datapos, neighbor, plot) {
  436. if (neighbor) {
  437. var ins = [neighbor.seriesIndex, neighbor.pointIndex, neighbor.data];
  438. var evt1 = jQuery.Event('jqplotDataMouseOver');
  439. evt1.pageX = ev.pageX;
  440. evt1.pageY = ev.pageY;
  441. plot.target.trigger(evt1, ins);
  442. if (plot.series[ins[0]].highlightMouseOver && !(ins[0] == plot.plugins.pyramidRenderer.highlightedSeriesIndex && ins[1] == plot.series[ins[0]]._highlightedPoint)) {
  443. var evt = jQuery.Event('jqplotDataHighlight');
  444. evt.which = ev.which;
  445. evt.pageX = ev.pageX;
  446. evt.pageY = ev.pageY;
  447. plot.target.trigger(evt, ins);
  448. highlight (plot, neighbor.seriesIndex, neighbor.pointIndex, neighbor.points);
  449. }
  450. }
  451. else if (neighbor == null) {
  452. unhighlight (plot);
  453. }
  454. }
  455. // Have to add hook here, becuase it needs called before series is inited.
  456. $.jqplot.preInitHooks.push(preInit);
  457. })(jQuery);