3 * Pure JavaScript plotting plugin using jQuery
8 * Copyright (c) 2009-2012 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.
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 .
18 * If you are feeling kind and generous, consider supporting the project by
19 * making a donation at: http://www.jqplot.com/donate.php .
21 * sprintf functions contained in jqplot.sprintf.js by 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."
34 * Class: $.jqplot.Dragable
35 * Plugin to make plotted points dragable by the user.
37 $.jqplot.Dragable = function(options) {
39 this.markerRenderer = new $.jqplot.MarkerRenderer({shadow:false});
40 this.shapeRenderer = new $.jqplot.ShapeRenderer();
41 this.isDragging = false;
48 // CSS color spec for the dragged point (and adjacent line segment or bar).
51 // Constrain dragging motion to an axis or to none.
52 // Allowable values are 'none', 'x', 'y'
53 this.constrainTo = 'none'; // 'x', 'y', or 'none';
54 $.extend(true, this, options);
57 function DragCanvas() {
58 $.jqplot.GenericCanvas.call(this);
59 this.isDragging = false;
65 DragCanvas.prototype = new $.jqplot.GenericCanvas();
66 DragCanvas.prototype.constructor = DragCanvas;
69 // called within scope of series
70 $.jqplot.Dragable.parseOptions = function (defaults, opts) {
71 var options = opts || {};
72 this.plugins.dragable = new $.jqplot.Dragable(options.dragable);
73 // since this function is called before series options are parsed,
74 // we can set this here and it will be overridden if needed.
75 this.isDragable = $.jqplot.config.enablePlugins;
78 // called within context of plot
79 // create a canvas which we can draw on.
80 // insert it before the eventCanvas, so eventCanvas will still capture events.
81 // add a new DragCanvas object to the plot plugins to handle drawing on this new canvas.
82 $.jqplot.Dragable.postPlotDraw = function() {
84 if (this.plugins.dragable && this.plugins.dragable.highlightCanvas) {
85 this.plugins.dragable.highlightCanvas.resetCanvas();
86 this.plugins.dragable.highlightCanvas = null;
89 this.plugins.dragable = {previousCursor:'auto', isOver:false};
90 this.plugins.dragable.dragCanvas = new DragCanvas();
92 this.eventCanvas._elem.before(this.plugins.dragable.dragCanvas.createElement(this._gridPadding, 'jqplot-dragable-canvas', this._plotDimensions, this));
93 var dctx = this.plugins.dragable.dragCanvas.setContext();
96 //$.jqplot.preInitHooks.push($.jqplot.Dragable.init);
97 $.jqplot.preParseSeriesOptionsHooks.push($.jqplot.Dragable.parseOptions);
98 $.jqplot.postDrawHooks.push($.jqplot.Dragable.postPlotDraw);
99 $.jqplot.eventListenerHooks.push(['jqplotMouseMove', handleMove]);
100 $.jqplot.eventListenerHooks.push(['jqplotMouseDown', handleDown]);
101 $.jqplot.eventListenerHooks.push(['jqplotMouseUp', handleUp]);
104 function initDragPoint(plot, neighbor) {
105 var s = plot.series[neighbor.seriesIndex];
106 var drag = s.plugins.dragable;
108 // first, init the mark renderer for the dragged point
109 var smr = s.markerRenderer;
110 var mr = drag.markerRenderer;
111 mr.style = smr.style;
112 mr.lineWidth = smr.lineWidth + 2.5;
113 mr.size = smr.size + 5;
115 var rgba = $.jqplot.getColorComponents(smr.color);
116 var newrgb = [rgba[0], rgba[1], rgba[2]];
117 var alpha = (rgba[3] >= 0.6) ? rgba[3]*0.6 : rgba[3]*(2-rgba[3]);
118 drag.color = 'rgba('+newrgb[0]+','+newrgb[1]+','+newrgb[2]+','+alpha+')';
120 mr.color = drag.color;
123 var start = (neighbor.pointIndex > 0) ? neighbor.pointIndex - 1 : 0;
124 var end = neighbor.pointIndex+2;
125 drag._gridData = s.gridData.slice(start, end);
128 function handleMove(ev, gridpos, datapos, neighbor, plot) {
129 if (plot.plugins.dragable.dragCanvas.isDragging) {
130 var dc = plot.plugins.dragable.dragCanvas;
131 var dp = dc._neighbor;
132 var s = plot.series[dp.seriesIndex];
133 var drag = s.plugins.dragable;
136 // compute the new grid position with any constraints.
137 var x = (drag.constrainTo == 'y') ? dp.gridData[0] : gridpos.x;
138 var y = (drag.constrainTo == 'x') ? dp.gridData[1] : gridpos.y;
140 // compute data values for any listeners.
141 var xu = s._xaxis.series_p2u(x);
142 var yu = s._yaxis.series_p2u(y);
144 // clear the canvas then redraw effect at new position.
146 ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
148 // adjust our gridData for the new mouse position
149 if (dp.pointIndex > 0) {
150 drag._gridData[1] = [x, y];
153 drag._gridData[0] = [x, y];
155 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}});
156 plot.target.trigger('jqplotSeriesPointChange', [dp.seriesIndex, dp.pointIndex, [xu,yu], [x,y]]);
158 else if (neighbor != null) {
159 var series = plot.series[neighbor.seriesIndex];
160 if (series.isDragable) {
161 var dc = plot.plugins.dragable.dragCanvas;
163 dc._cursors.push(ev.target.style.cursor);
164 ev.target.style.cursor = "pointer";
169 else if (neighbor == null) {
170 var dc = plot.plugins.dragable.dragCanvas;
172 ev.target.style.cursor = dc._cursors.pop();
178 function handleDown(ev, gridpos, datapos, neighbor, plot) {
179 var dc = plot.plugins.dragable.dragCanvas;
180 dc._cursors.push(ev.target.style.cursor);
181 if (neighbor != null) {
182 var s = plot.series[neighbor.seriesIndex];
183 var drag = s.plugins.dragable;
184 if (s.isDragable && !dc.isDragging) {
185 dc._neighbor = neighbor;
186 dc.isDragging = true;
187 initDragPoint(plot, neighbor);
188 drag.markerRenderer.draw(s.gridData[neighbor.pointIndex][0], s.gridData[neighbor.pointIndex][1], dc._ctx);
189 ev.target.style.cursor = "move";
190 plot.target.trigger('jqplotDragStart', [neighbor.seriesIndex, neighbor.pointIndex, gridpos, datapos]);
193 // Just in case of a hickup, we'll clear the drag canvas and reset.
196 ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
197 dc.isDragging = false;
201 function handleUp(ev, gridpos, datapos, neighbor, plot) {
202 if (plot.plugins.dragable.dragCanvas.isDragging) {
203 var dc = plot.plugins.dragable.dragCanvas;
206 ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
207 dc.isDragging = false;
208 // redraw the series canvas at the new point.
209 var dp = dc._neighbor;
210 var s = plot.series[dp.seriesIndex];
211 var drag = s.plugins.dragable;
212 // compute the new grid position with any constraints.
213 var x = (drag.constrainTo == 'y') ? dp.data[0] : datapos[s.xaxis];
214 var y = (drag.constrainTo == 'x') ? dp.data[1] : datapos[s.yaxis];
215 // var x = datapos[s.xaxis];
216 // var y = datapos[s.yaxis];
217 s.data[dp.pointIndex][0] = x;
218 s.data[dp.pointIndex][1] = y;
219 plot.drawSeries({preventJqPlotSeriesDrawTrigger:true}, dp.seriesIndex);
221 ev.target.style.cursor = dc._cursors.pop();
222 plot.target.trigger('jqplotDragStop', [gridpos, datapos]);