OSDN Git Service

初回コミット(v2.6.17.1)
[magic3/magic3.git] / scripts / jquery / jqplot1.0 / plugins / jqplot.dragable.js
1 /**
2  * jqPlot
3  * Pure JavaScript plotting plugin using jQuery
4  *
5  * Version: 1.0.4
6  * Revision: 1120
7  *
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. 
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     /**
34      * Class: $.jqplot.Dragable
35      * Plugin to make plotted points dragable by the user.
36      */
37     $.jqplot.Dragable = function(options) {
38         // Group: Properties
39         this.markerRenderer = new $.jqplot.MarkerRenderer({shadow:false});
40         this.shapeRenderer = new $.jqplot.ShapeRenderer();
41         this.isDragging = false;
42         this.isOver = false;
43         this._ctx;
44         this._elem;
45         this._point;
46         this._gridData;
47         // prop: color
48         // CSS color spec for the dragged point (and adjacent line segment or bar).
49         this.color;
50         // prop: constrainTo
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);
55     };
56     
57     function DragCanvas() {
58         $.jqplot.GenericCanvas.call(this);
59         this.isDragging = false;
60         this.isOver = false;
61         this._neighbor;
62         this._cursors = [];
63     }
64     
65     DragCanvas.prototype = new $.jqplot.GenericCanvas();
66     DragCanvas.prototype.constructor = DragCanvas;
67     
68     
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;
76     };
77     
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() {
83         // Memory Leaks patch    
84         if (this.plugins.dragable && this.plugins.dragable.highlightCanvas) {
85             this.plugins.dragable.highlightCanvas.resetCanvas();
86             this.plugins.dragable.highlightCanvas = null;
87         }
88
89         this.plugins.dragable = {previousCursor:'auto', isOver:false};
90         this.plugins.dragable.dragCanvas = new DragCanvas();
91         
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();
94     };
95     
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]);
102
103     
104     function initDragPoint(plot, neighbor) {
105         var s = plot.series[neighbor.seriesIndex];
106         var drag = s.plugins.dragable;
107         
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;
114         if (!drag.color) {
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+')';
119         }
120         mr.color = drag.color;
121         mr.init();
122
123         var start = (neighbor.pointIndex > 0) ? neighbor.pointIndex - 1 : 0;
124         var end = neighbor.pointIndex+2;
125         drag._gridData = s.gridData.slice(start, end);
126     }
127     
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;
134             var gd = s.gridData;
135             
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;
139             
140             // compute data values for any listeners.
141             var xu = s._xaxis.series_p2u(x);
142             var yu = s._yaxis.series_p2u(y);
143             
144             // clear the canvas then redraw effect at new position.
145             var ctx = dc._ctx;
146             ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
147             
148             // adjust our gridData for the new mouse position
149             if (dp.pointIndex > 0) {
150                 drag._gridData[1] = [x, y];
151             }
152             else {
153                 drag._gridData[0] = [x, y];
154             }
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]]);
157         }
158         else if (neighbor != null) {
159             var series = plot.series[neighbor.seriesIndex];
160             if (series.isDragable) {
161                 var dc = plot.plugins.dragable.dragCanvas;
162                 if (!dc.isOver) {
163                     dc._cursors.push(ev.target.style.cursor);
164                     ev.target.style.cursor = "pointer";
165                 }
166                 dc.isOver = true;
167             }
168         }
169         else if (neighbor == null) {
170             var dc = plot.plugins.dragable.dragCanvas;
171             if (dc.isOver) {
172                 ev.target.style.cursor = dc._cursors.pop();
173                 dc.isOver = false;
174             }
175         }
176     }
177     
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]);
191             }
192         }
193         // Just in case of a hickup, we'll clear the drag canvas and reset.
194         else {
195            var ctx = dc._ctx;
196            ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
197            dc.isDragging = false;
198         }
199     }
200     
201     function handleUp(ev, gridpos, datapos, neighbor, plot) {
202         if (plot.plugins.dragable.dragCanvas.isDragging) {
203             var dc = plot.plugins.dragable.dragCanvas;
204             // clear the canvas
205             var ctx = dc._ctx;
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);
220             dc._neighbor = null;
221             ev.target.style.cursor = dc._cursors.pop();
222             plot.target.trigger('jqplotDragStop', [gridpos, datapos]);
223         }
224     }
225 })(jQuery);