Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

224 righe
9.3 KiB

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