jqplot.dragable.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. /**
  33. * Class: $.jqplot.Dragable
  34. * Plugin to make plotted points dragable by the user.
  35. */
  36. $.jqplot.Dragable = function(options) {
  37. // Group: Properties
  38. this.markerRenderer = new $.jqplot.MarkerRenderer({shadow:false});
  39. this.shapeRenderer = new $.jqplot.ShapeRenderer();
  40. this.isDragging = false;
  41. this.isOver = false;
  42. this._ctx;
  43. this._elem;
  44. this._point;
  45. this._gridData;
  46. // prop: color
  47. // CSS color spec for the dragged point (and adjacent line segment or bar).
  48. this.color;
  49. // prop: constrainTo
  50. // Constrain dragging motion to an axis or to none.
  51. // Allowable values are 'none', 'x', 'y'
  52. this.constrainTo = 'none'; // 'x', 'y', or 'none';
  53. $.extend(true, this, options);
  54. };
  55. function DragCanvas() {
  56. $.jqplot.GenericCanvas.call(this);
  57. this.isDragging = false;
  58. this.isOver = false;
  59. this._neighbor;
  60. this._cursors = [];
  61. }
  62. DragCanvas.prototype = new $.jqplot.GenericCanvas();
  63. DragCanvas.prototype.constructor = DragCanvas;
  64. // called within scope of series
  65. $.jqplot.Dragable.parseOptions = function (defaults, opts) {
  66. var options = opts || {};
  67. this.plugins.dragable = new $.jqplot.Dragable(options.dragable);
  68. // since this function is called before series options are parsed,
  69. // we can set this here and it will be overridden if needed.
  70. this.isDragable = $.jqplot.config.enablePlugins;
  71. };
  72. // called within context of plot
  73. // create a canvas which we can draw on.
  74. // insert it before the eventCanvas, so eventCanvas will still capture events.
  75. // add a new DragCanvas object to the plot plugins to handle drawing on this new canvas.
  76. $.jqplot.Dragable.postPlotDraw = function() {
  77. // Memory Leaks patch
  78. if (this.plugins.dragable && this.plugins.dragable.highlightCanvas) {
  79. this.plugins.dragable.highlightCanvas.resetCanvas();
  80. this.plugins.dragable.highlightCanvas = null;
  81. }
  82. this.plugins.dragable = {previousCursor:'auto', isOver:false};
  83. this.plugins.dragable.dragCanvas = new DragCanvas();
  84. this.eventCanvas._elem.before(this.plugins.dragable.dragCanvas.createElement(this._gridPadding, 'jqplot-dragable-canvas', this._plotDimensions, this));
  85. var dctx = this.plugins.dragable.dragCanvas.setContext();
  86. };
  87. //$.jqplot.preInitHooks.push($.jqplot.Dragable.init);
  88. $.jqplot.preParseSeriesOptionsHooks.push($.jqplot.Dragable.parseOptions);
  89. $.jqplot.postDrawHooks.push($.jqplot.Dragable.postPlotDraw);
  90. $.jqplot.eventListenerHooks.push(['jqplotMouseMove', handleMove]);
  91. $.jqplot.eventListenerHooks.push(['jqplotMouseDown', handleDown]);
  92. $.jqplot.eventListenerHooks.push(['jqplotMouseUp', handleUp]);
  93. function initDragPoint(plot, neighbor) {
  94. var s = plot.series[neighbor.seriesIndex];
  95. var drag = s.plugins.dragable;
  96. // first, init the mark renderer for the dragged point
  97. var smr = s.markerRenderer;
  98. var mr = drag.markerRenderer;
  99. mr.style = smr.style;
  100. mr.lineWidth = smr.lineWidth + 2.5;
  101. mr.size = smr.size + 5;
  102. if (!drag.color) {
  103. var rgba = $.jqplot.getColorComponents(smr.color);
  104. var newrgb = [rgba[0], rgba[1], rgba[2]];
  105. var alpha = (rgba[3] >= 0.6) ? rgba[3]*0.6 : rgba[3]*(2-rgba[3]);
  106. drag.color = 'rgba('+newrgb[0]+','+newrgb[1]+','+newrgb[2]+','+alpha+')';
  107. }
  108. mr.color = drag.color;
  109. mr.init();
  110. var start = (neighbor.pointIndex > 0) ? neighbor.pointIndex - 1 : 0;
  111. var end = neighbor.pointIndex+2;
  112. drag._gridData = s.gridData.slice(start, end);
  113. }
  114. function handleMove(ev, gridpos, datapos, neighbor, plot) {
  115. if (plot.plugins.dragable.dragCanvas.isDragging) {
  116. var dc = plot.plugins.dragable.dragCanvas;
  117. var dp = dc._neighbor;
  118. var s = plot.series[dp.seriesIndex];
  119. var drag = s.plugins.dragable;
  120. var gd = s.gridData;
  121. // compute the new grid position with any constraints.
  122. var x = (drag.constrainTo == 'y') ? dp.gridData[0] : gridpos.x;
  123. var y = (drag.constrainTo == 'x') ? dp.gridData[1] : gridpos.y;
  124. // compute data values for any listeners.
  125. var xu = s._xaxis.series_p2u(x);
  126. var yu = s._yaxis.series_p2u(y);
  127. // clear the canvas then redraw effect at new position.
  128. var ctx = dc._ctx;
  129. ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
  130. // adjust our gridData for the new mouse position
  131. if (dp.pointIndex > 0) {
  132. drag._gridData[1] = [x, y];
  133. }
  134. else {
  135. drag._gridData[0] = [x, y];
  136. }
  137. plot.series[dp.seriesIndex].draw(dc._ctx, {gridData:drag._gridData, shadow:false, preventJqPlotSeriesDrawTrigger:true, color:drag.color, markerOptions:{color:drag.color, shadow:false}, trendline:{show:false}});
  138. plot.target.trigger('jqplotSeriesPointChange', [dp.seriesIndex, dp.pointIndex, [xu,yu], [x,y]]);
  139. }
  140. else if (neighbor != null) {
  141. var series = plot.series[neighbor.seriesIndex];
  142. if (series.isDragable) {
  143. var dc = plot.plugins.dragable.dragCanvas;
  144. if (!dc.isOver) {
  145. dc._cursors.push(ev.target.style.cursor);
  146. ev.target.style.cursor = "pointer";
  147. }
  148. dc.isOver = true;
  149. }
  150. }
  151. else if (neighbor == null) {
  152. var dc = plot.plugins.dragable.dragCanvas;
  153. if (dc.isOver) {
  154. ev.target.style.cursor = dc._cursors.pop();
  155. dc.isOver = false;
  156. }
  157. }
  158. }
  159. function handleDown(ev, gridpos, datapos, neighbor, plot) {
  160. var dc = plot.plugins.dragable.dragCanvas;
  161. dc._cursors.push(ev.target.style.cursor);
  162. if (neighbor != null) {
  163. var s = plot.series[neighbor.seriesIndex];
  164. var drag = s.plugins.dragable;
  165. if (s.isDragable && !dc.isDragging) {
  166. dc._neighbor = neighbor;
  167. dc.isDragging = true;
  168. initDragPoint(plot, neighbor);
  169. drag.markerRenderer.draw(s.gridData[neighbor.pointIndex][0], s.gridData[neighbor.pointIndex][1], dc._ctx);
  170. ev.target.style.cursor = "move";
  171. plot.target.trigger('jqplotDragStart', [neighbor.seriesIndex, neighbor.pointIndex, gridpos, datapos]);
  172. }
  173. }
  174. // Just in case of a hickup, we'll clear the drag canvas and reset.
  175. else {
  176. var ctx = dc._ctx;
  177. ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
  178. dc.isDragging = false;
  179. }
  180. }
  181. function handleUp(ev, gridpos, datapos, neighbor, plot) {
  182. if (plot.plugins.dragable.dragCanvas.isDragging) {
  183. var dc = plot.plugins.dragable.dragCanvas;
  184. // clear the canvas
  185. var ctx = dc._ctx;
  186. ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
  187. dc.isDragging = false;
  188. // redraw the series canvas at the new point.
  189. var dp = dc._neighbor;
  190. var s = plot.series[dp.seriesIndex];
  191. var drag = s.plugins.dragable;
  192. // compute the new grid position with any constraints.
  193. var x = (drag.constrainTo == 'y') ? dp.data[0] : datapos[s.xaxis];
  194. var y = (drag.constrainTo == 'x') ? dp.data[1] : datapos[s.yaxis];
  195. // var x = datapos[s.xaxis];
  196. // var y = datapos[s.yaxis];
  197. s.data[dp.pointIndex][0] = x;
  198. s.data[dp.pointIndex][1] = y;
  199. plot.drawSeries({preventJqPlotSeriesDrawTrigger:true}, dp.seriesIndex);
  200. dc._neighbor = null;
  201. ev.target.style.cursor = dc._cursors.pop();
  202. plot.target.trigger('jqplotDragStop', [gridpos, datapos]);
  203. }
  204. }
  205. })(jQuery);