plantwatchdog 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/History.txt +4 -0
- data/License.txt +674 -0
- data/Manifest.txt +42 -0
- data/README.txt +91 -0
- data/Rakefile +22 -0
- data/bin/plantwatchdog +8 -0
- data/bin/upload_measurements +2 -0
- data/config.ru +35 -0
- data/config/app_config.yaml +10 -0
- data/lib/plantwatchdog/aggregation.rb +220 -0
- data/lib/plantwatchdog/aggregation_methods.rb +90 -0
- data/lib/plantwatchdog/data.rb +126 -0
- data/lib/plantwatchdog/db.rb +37 -0
- data/lib/plantwatchdog/gems.rb +5 -0
- data/lib/plantwatchdog/main.rb +76 -0
- data/lib/plantwatchdog/model.rb +442 -0
- data/lib/plantwatchdog/sinatra.rb +206 -0
- data/public/images/arrow-down.gif +0 -0
- data/public/images/arrow-left.gif +0 -0
- data/public/images/arrow-right.gif +0 -0
- data/public/images/arrow-up.gif +0 -0
- data/public/images/spinner.gif +0 -0
- data/public/images/tabs.png +0 -0
- data/public/js/customflot.js +120 -0
- data/public/js/jquery-1.3.2.min.js +19 -0
- data/public/js/jquery.flot.crosshair.js +157 -0
- data/public/js/jquery.flot.js +2119 -0
- data/public/js/jquery.flot.navigate.js +272 -0
- data/public/js/jquery.flot.selection.js +299 -0
- data/public/js/select-chain.js +71 -0
- data/public/js/tools.tabs-1.0.4.js +285 -0
- data/public/tabs.css +87 -0
- data/sample/solar/create_solar.rb +31 -0
- data/sample/solar/measurements/client.sqlite3 +0 -0
- data/sample/solar/static/devices.yml +17 -0
- data/sample/solar/static/metadata.yml +30 -0
- data/sample/solar/static/plants.yml +3 -0
- data/sample/solar/static/users.yml +4 -0
- data/sample/solar/upload_measurements +26 -0
- data/templates/graph.erb +134 -0
- data/templates/index.erb +24 -0
- data/templates/monthly_graph.erb +41 -0
- data/test/test_aggregation.rb +161 -0
- data/test/test_aggregation_methods.rb +50 -0
- data/test/test_base.rb +83 -0
- data/test/test_data.rb +118 -0
- data/test/test_model.rb +142 -0
- data/test/test_sync.rb +71 -0
- data/test/test_web.rb +87 -0
- metadata +167 -0
@@ -0,0 +1,272 @@
|
|
1
|
+
/*
|
2
|
+
Flot plugin for adding panning and zooming capabilities to a plot.
|
3
|
+
|
4
|
+
The default behaviour is double click and scrollwheel up/down to zoom
|
5
|
+
in, drag to pan. The plugin defines plot.zoom({ center }),
|
6
|
+
plot.zoomOut() and plot.pan(offset) so you easily can add custom
|
7
|
+
controls. It also fires a "plotpan" and "plotzoom" event when
|
8
|
+
something happens, useful for synchronizing plots.
|
9
|
+
|
10
|
+
Example usage:
|
11
|
+
|
12
|
+
plot = $.plot(...);
|
13
|
+
|
14
|
+
// zoom default amount in on the pixel (100, 200)
|
15
|
+
plot.zoom({ center: { left: 10, top: 20 } });
|
16
|
+
|
17
|
+
// zoom out again
|
18
|
+
plot.zoomOut({ center: { left: 10, top: 20 } });
|
19
|
+
|
20
|
+
// pan 100 pixels to the left and 20 down
|
21
|
+
plot.pan({ left: -100, top: 20 })
|
22
|
+
|
23
|
+
|
24
|
+
Options:
|
25
|
+
|
26
|
+
zoom: {
|
27
|
+
interactive: false
|
28
|
+
trigger: "dblclick" // or "click" for single click
|
29
|
+
amount: 1.5 // 2 = 200% (zoom in), 0.5 = 50% (zoom out)
|
30
|
+
}
|
31
|
+
|
32
|
+
pan: {
|
33
|
+
interactive: false
|
34
|
+
}
|
35
|
+
|
36
|
+
xaxis, yaxis, x2axis, y2axis: {
|
37
|
+
zoomRange: null // or [number, number] (min range, max range)
|
38
|
+
panRange: null // or [number, number] (min, max)
|
39
|
+
}
|
40
|
+
|
41
|
+
"interactive" enables the built-in drag/click behaviour. "amount" is
|
42
|
+
the amount to zoom the viewport relative to the current range, so 1 is
|
43
|
+
100% (i.e. no change), 1.5 is 150% (zoom in), 0.7 is 70% (zoom out).
|
44
|
+
|
45
|
+
"zoomRange" is the interval in which zooming can happen, e.g. with
|
46
|
+
zoomRange: [1, 100] the zoom will never scale the axis so that the
|
47
|
+
difference between min and max is smaller than 1 or larger than 100.
|
48
|
+
You can set either of them to null to ignore.
|
49
|
+
|
50
|
+
"panRange" confines the panning to stay within a range, e.g. with
|
51
|
+
panRange: [-10, 20] panning stops at -10 in one end and at 20 in the
|
52
|
+
other. Either can be null.
|
53
|
+
*/
|
54
|
+
|
55
|
+
|
56
|
+
// First two dependencies, jquery.event.drag.js and
|
57
|
+
// jquery.mousewheel.js, we put them inline here to save people the
|
58
|
+
// effort of downloading them.
|
59
|
+
|
60
|
+
/*
|
61
|
+
jquery.event.drag.js ~ v1.5 ~ Copyright (c) 2008, Three Dub Media (http://threedubmedia.com)
|
62
|
+
Licensed under the MIT License ~ http://threedubmedia.googlecode.com/files/MIT-LICENSE.txt
|
63
|
+
*/
|
64
|
+
(function(E){E.fn.drag=function(L,K,J){if(K){this.bind("dragstart",L)}if(J){this.bind("dragend",J)}return !L?this.trigger("drag"):this.bind("drag",K?K:L)};var A=E.event,B=A.special,F=B.drag={not:":input",distance:0,which:1,dragging:false,setup:function(J){J=E.extend({distance:F.distance,which:F.which,not:F.not},J||{});J.distance=I(J.distance);A.add(this,"mousedown",H,J);if(this.attachEvent){this.attachEvent("ondragstart",D)}},teardown:function(){A.remove(this,"mousedown",H);if(this===F.dragging){F.dragging=F.proxy=false}G(this,true);if(this.detachEvent){this.detachEvent("ondragstart",D)}}};B.dragstart=B.dragend={setup:function(){},teardown:function(){}};function H(L){var K=this,J,M=L.data||{};if(M.elem){K=L.dragTarget=M.elem;L.dragProxy=F.proxy||K;L.cursorOffsetX=M.pageX-M.left;L.cursorOffsetY=M.pageY-M.top;L.offsetX=L.pageX-L.cursorOffsetX;L.offsetY=L.pageY-L.cursorOffsetY}else{if(F.dragging||(M.which>0&&L.which!=M.which)||E(L.target).is(M.not)){return }}switch(L.type){case"mousedown":E.extend(M,E(K).offset(),{elem:K,target:L.target,pageX:L.pageX,pageY:L.pageY});A.add(document,"mousemove mouseup",H,M);G(K,false);F.dragging=null;return false;case !F.dragging&&"mousemove":if(I(L.pageX-M.pageX)+I(L.pageY-M.pageY)<M.distance){break}L.target=M.target;J=C(L,"dragstart",K);if(J!==false){F.dragging=K;F.proxy=L.dragProxy=E(J||K)[0]}case"mousemove":if(F.dragging){J=C(L,"drag",K);if(B.drop){B.drop.allowed=(J!==false);B.drop.handler(L)}if(J!==false){break}L.type="mouseup"}case"mouseup":A.remove(document,"mousemove mouseup",H);if(F.dragging){if(B.drop){B.drop.handler(L)}C(L,"dragend",K)}G(K,true);F.dragging=F.proxy=M.elem=false;break}return true}function C(M,K,L){M.type=K;var J=E.event.handle.call(L,M);return J===false?false:J||M.result}function I(J){return Math.pow(J,2)}function D(){return(F.dragging===false)}function G(K,J){if(!K){return }K.unselectable=J?"off":"on";K.onselectstart=function(){return J};if(K.style){K.style.MozUserSelect=J?"":"none"}}})(jQuery);
|
65
|
+
|
66
|
+
|
67
|
+
/* jquery.mousewheel.min.js
|
68
|
+
* Copyright (c) 2009 Brandon Aaron (http://brandonaaron.net)
|
69
|
+
* Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
|
70
|
+
* and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
|
71
|
+
* Thanks to: http://adomas.org/javascript-mouse-wheel/ for some pointers.
|
72
|
+
* Thanks to: Mathias Bank(http://www.mathias-bank.de) for a scope bug fix.
|
73
|
+
*
|
74
|
+
* Version: 3.0.2
|
75
|
+
*
|
76
|
+
* Requires: 1.2.2+
|
77
|
+
*/
|
78
|
+
(function(c){var a=["DOMMouseScroll","mousewheel"];c.event.special.mousewheel={setup:function(){if(this.addEventListener){for(var d=a.length;d;){this.addEventListener(a[--d],b,false)}}else{this.onmousewheel=b}},teardown:function(){if(this.removeEventListener){for(var d=a.length;d;){this.removeEventListener(a[--d],b,false)}}else{this.onmousewheel=null}}};c.fn.extend({mousewheel:function(d){return d?this.bind("mousewheel",d):this.trigger("mousewheel")},unmousewheel:function(d){return this.unbind("mousewheel",d)}});function b(f){var d=[].slice.call(arguments,1),g=0,e=true;f=c.event.fix(f||window.event);f.type="mousewheel";if(f.wheelDelta){g=f.wheelDelta/120}if(f.detail){g=-f.detail/3}d.unshift(f,g);return c.event.handle.apply(this,d)}})(jQuery);
|
79
|
+
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
(function ($) {
|
84
|
+
var options = {
|
85
|
+
xaxis: {
|
86
|
+
zoomRange: null, // or [number, number] (min range, max range)
|
87
|
+
panRange: null // or [number, number] (min, max)
|
88
|
+
},
|
89
|
+
zoom: {
|
90
|
+
interactive: false,
|
91
|
+
trigger: "dblclick", // or "click" for single click
|
92
|
+
amount: 1.5 // how much to zoom relative to current position, 2 = 200% (zoom in), 0.5 = 50% (zoom out)
|
93
|
+
},
|
94
|
+
pan: {
|
95
|
+
interactive: false
|
96
|
+
}
|
97
|
+
};
|
98
|
+
|
99
|
+
function init(plot) {
|
100
|
+
function bindEvents(plot, eventHolder) {
|
101
|
+
var o = plot.getOptions();
|
102
|
+
if (o.zoom.interactive) {
|
103
|
+
function clickHandler(e, zoomOut) {
|
104
|
+
var c = plot.offset();
|
105
|
+
c.left = e.pageX - c.left;
|
106
|
+
c.top = e.pageY - c.top;
|
107
|
+
if (zoomOut)
|
108
|
+
plot.zoomOut({ center: c });
|
109
|
+
else
|
110
|
+
plot.zoom({ center: c });
|
111
|
+
}
|
112
|
+
|
113
|
+
eventHolder[o.zoom.trigger](clickHandler);
|
114
|
+
|
115
|
+
eventHolder.mousewheel(function (e, delta) {
|
116
|
+
clickHandler(e, delta < 0);
|
117
|
+
return false;
|
118
|
+
});
|
119
|
+
}
|
120
|
+
if (o.pan.interactive) {
|
121
|
+
var prevCursor = 'default', pageX = 0, pageY = 0;
|
122
|
+
|
123
|
+
eventHolder.bind("dragstart", { distance: 10 }, function (e) {
|
124
|
+
if (e.which != 1) // only accept left-click
|
125
|
+
return false;
|
126
|
+
eventHolderCursor = eventHolder.css('cursor');
|
127
|
+
eventHolder.css('cursor', 'move');
|
128
|
+
pageX = e.pageX;
|
129
|
+
pageY = e.pageY;
|
130
|
+
});
|
131
|
+
eventHolder.bind("drag", function (e) {
|
132
|
+
// unused at the moment, but we need it here to
|
133
|
+
// trigger the dragstart/dragend events
|
134
|
+
});
|
135
|
+
eventHolder.bind("dragend", function (e) {
|
136
|
+
eventHolder.css('cursor', prevCursor);
|
137
|
+
plot.pan({ left: pageX - e.pageX,
|
138
|
+
top: pageY - e.pageY });
|
139
|
+
});
|
140
|
+
}
|
141
|
+
}
|
142
|
+
|
143
|
+
plot.zoomOut = function (args) {
|
144
|
+
if (!args)
|
145
|
+
args = {};
|
146
|
+
|
147
|
+
if (!args.amount)
|
148
|
+
args.amount = plot.getOptions().zoom.amount
|
149
|
+
|
150
|
+
args.amount = 1 / args.amount;
|
151
|
+
plot.zoom(args);
|
152
|
+
}
|
153
|
+
|
154
|
+
plot.zoom = function (args) {
|
155
|
+
if (!args)
|
156
|
+
args = {};
|
157
|
+
|
158
|
+
var axes = plot.getAxes(),
|
159
|
+
options = plot.getOptions(),
|
160
|
+
c = args.center,
|
161
|
+
amount = args.amount ? args.amount : options.zoom.amount,
|
162
|
+
w = plot.width(), h = plot.height();
|
163
|
+
|
164
|
+
if (!c)
|
165
|
+
c = { left: w / 2, top: h / 2 };
|
166
|
+
|
167
|
+
var xf = c.left / w,
|
168
|
+
x1 = c.left - xf * w / amount,
|
169
|
+
x2 = c.left + (1 - xf) * w / amount,
|
170
|
+
yf = c.top / h,
|
171
|
+
y1 = c.top - yf * h / amount,
|
172
|
+
y2 = c.top + (1 - yf) * h / amount;
|
173
|
+
|
174
|
+
function scaleAxis(min, max, name) {
|
175
|
+
var axis = axes[name],
|
176
|
+
axisOptions = options[name];
|
177
|
+
|
178
|
+
if (!axis.used)
|
179
|
+
return;
|
180
|
+
|
181
|
+
min = axis.c2p(min);
|
182
|
+
max = axis.c2p(max);
|
183
|
+
if (max < min) { // make sure min < max
|
184
|
+
var tmp = min
|
185
|
+
min = max;
|
186
|
+
max = tmp;
|
187
|
+
}
|
188
|
+
|
189
|
+
var range = max - min, zr = axisOptions.zoomRange;
|
190
|
+
if (zr &&
|
191
|
+
((zr[0] != null && range < zr[0]) ||
|
192
|
+
(zr[1] != null && range > zr[1])))
|
193
|
+
return;
|
194
|
+
|
195
|
+
axisOptions.min = min;
|
196
|
+
axisOptions.max = max;
|
197
|
+
}
|
198
|
+
|
199
|
+
scaleAxis(x1, x2, 'xaxis');
|
200
|
+
scaleAxis(x1, x2, 'x2axis');
|
201
|
+
scaleAxis(y1, y2, 'yaxis');
|
202
|
+
scaleAxis(y1, y2, 'y2axis');
|
203
|
+
|
204
|
+
plot.setupGrid();
|
205
|
+
plot.draw();
|
206
|
+
|
207
|
+
if (!args.preventEvent)
|
208
|
+
plot.getPlaceholder().trigger("plotzoom", [ plot ]);
|
209
|
+
}
|
210
|
+
|
211
|
+
plot.pan = function (args) {
|
212
|
+
var l = +args.left, t = +args.top,
|
213
|
+
axes = plot.getAxes(), options = plot.getOptions();
|
214
|
+
|
215
|
+
if (isNaN(l))
|
216
|
+
l = 0;
|
217
|
+
if (isNaN(t))
|
218
|
+
t = 0;
|
219
|
+
|
220
|
+
function panAxis(delta, name) {
|
221
|
+
var axis = axes[name],
|
222
|
+
axisOptions = options[name],
|
223
|
+
min, max;
|
224
|
+
|
225
|
+
if (!axis.used)
|
226
|
+
return;
|
227
|
+
|
228
|
+
min = axis.c2p(axis.p2c(axis.min) + delta),
|
229
|
+
max = axis.c2p(axis.p2c(axis.max) + delta);
|
230
|
+
|
231
|
+
var pr = axisOptions.panRange;
|
232
|
+
if (pr) {
|
233
|
+
// check whether we hit the wall
|
234
|
+
if (pr[0] != null && pr[0] > min) {
|
235
|
+
delta = pr[0] - min;
|
236
|
+
min += delta;
|
237
|
+
max += delta;
|
238
|
+
}
|
239
|
+
|
240
|
+
if (pr[1] != null && pr[1] < max) {
|
241
|
+
delta = pr[1] - max;
|
242
|
+
min += delta;
|
243
|
+
max += delta;
|
244
|
+
}
|
245
|
+
}
|
246
|
+
|
247
|
+
axisOptions.min = min;
|
248
|
+
axisOptions.max = max;
|
249
|
+
}
|
250
|
+
|
251
|
+
panAxis(l, 'xaxis');
|
252
|
+
panAxis(l, 'x2axis');
|
253
|
+
panAxis(t, 'yaxis');
|
254
|
+
panAxis(t, 'y2axis');
|
255
|
+
|
256
|
+
plot.setupGrid();
|
257
|
+
plot.draw();
|
258
|
+
|
259
|
+
if (!args.preventEvent)
|
260
|
+
plot.getPlaceholder().trigger("plotpan", [ plot ]);
|
261
|
+
}
|
262
|
+
|
263
|
+
plot.hooks.bindEvents.push(bindEvents);
|
264
|
+
}
|
265
|
+
|
266
|
+
$.plot.plugins.push({
|
267
|
+
init: init,
|
268
|
+
options: options,
|
269
|
+
name: 'navigate',
|
270
|
+
version: '1.1'
|
271
|
+
});
|
272
|
+
})(jQuery);
|
@@ -0,0 +1,299 @@
|
|
1
|
+
/*
|
2
|
+
Flot plugin for selecting regions.
|
3
|
+
|
4
|
+
The plugin defines the following options:
|
5
|
+
|
6
|
+
selection: {
|
7
|
+
mode: null or "x" or "y" or "xy",
|
8
|
+
color: color
|
9
|
+
}
|
10
|
+
|
11
|
+
You enable selection support by setting the mode to one of "x", "y" or
|
12
|
+
"xy". In "x" mode, the user will only be able to specify the x range,
|
13
|
+
similarly for "y" mode. For "xy", the selection becomes a rectangle
|
14
|
+
where both ranges can be specified. "color" is color of the selection.
|
15
|
+
|
16
|
+
When selection support is enabled, a "plotselected" event will be emitted
|
17
|
+
on the DOM element you passed into the plot function. The event
|
18
|
+
handler gets one extra parameter with the ranges selected on the axes,
|
19
|
+
like this:
|
20
|
+
|
21
|
+
placeholder.bind("plotselected", function(event, ranges) {
|
22
|
+
alert("You selected " + ranges.xaxis.from + " to " + ranges.xaxis.to)
|
23
|
+
// similar for yaxis, secondary axes are in x2axis
|
24
|
+
// and y2axis if present
|
25
|
+
});
|
26
|
+
|
27
|
+
The "plotselected" event is only fired when the user has finished
|
28
|
+
making the selection. A "plotselecting" event is fired during the
|
29
|
+
process with the same parameters as the "plotselected" event, in case
|
30
|
+
you want to know what's happening while it's happening,
|
31
|
+
|
32
|
+
A "plotunselected" event with no arguments is emitted when the user
|
33
|
+
clicks the mouse to remove the selection.
|
34
|
+
|
35
|
+
The plugin allso adds the following methods to the plot object:
|
36
|
+
|
37
|
+
- setSelection(ranges, preventEvent)
|
38
|
+
|
39
|
+
Set the selection rectangle. The passed in ranges is on the same
|
40
|
+
form as returned in the "plotselected" event. If the selection
|
41
|
+
mode is "x", you should put in either an xaxis (or x2axis) object,
|
42
|
+
if the mode is "y" you need to put in an yaxis (or y2axis) object
|
43
|
+
and both xaxis/x2axis and yaxis/y2axis if the selection mode is
|
44
|
+
"xy", like this:
|
45
|
+
|
46
|
+
setSelection({ xaxis: { from: 0, to: 10 }, yaxis: { from: 40, to: 60 } });
|
47
|
+
|
48
|
+
setSelection will trigger the "plotselected" event when called. If
|
49
|
+
you don't want that to happen, e.g. if you're inside a
|
50
|
+
"plotselected" handler, pass true as the second parameter.
|
51
|
+
|
52
|
+
- clearSelection(preventEvent)
|
53
|
+
|
54
|
+
Clear the selection rectangle. Pass in true to avoid getting a
|
55
|
+
"plotunselected" event.
|
56
|
+
|
57
|
+
- getSelection()
|
58
|
+
|
59
|
+
Returns the current selection in the same format as the
|
60
|
+
"plotselected" event. If there's currently no selection, the
|
61
|
+
function returns null.
|
62
|
+
|
63
|
+
*/
|
64
|
+
|
65
|
+
(function ($) {
|
66
|
+
function init(plot) {
|
67
|
+
var selection = {
|
68
|
+
first: { x: -1, y: -1}, second: { x: -1, y: -1},
|
69
|
+
show: false,
|
70
|
+
active: false
|
71
|
+
};
|
72
|
+
|
73
|
+
// FIXME: The drag handling implemented here should be
|
74
|
+
// abstracted out, there's some similar code from a library in
|
75
|
+
// the navigation plugin, this should be massaged a bit to fit
|
76
|
+
// the Flot cases here better and reused. Doing this would
|
77
|
+
// make this plugin much slimmer.
|
78
|
+
var savedhandlers = {};
|
79
|
+
|
80
|
+
function onMouseMove(e) {
|
81
|
+
if (selection.active) {
|
82
|
+
plot.getPlaceholder().trigger("plotselecting", [ getSelection() ]);
|
83
|
+
|
84
|
+
updateSelection(e);
|
85
|
+
}
|
86
|
+
}
|
87
|
+
|
88
|
+
function onMouseDown(e) {
|
89
|
+
if (e.which != 1) // only accept left-click
|
90
|
+
return;
|
91
|
+
|
92
|
+
// cancel out any text selections
|
93
|
+
document.body.focus();
|
94
|
+
|
95
|
+
// prevent text selection and drag in old-school browsers
|
96
|
+
if (document.onselectstart !== undefined && savedhandlers.onselectstart == null) {
|
97
|
+
savedhandlers.onselectstart = document.onselectstart;
|
98
|
+
document.onselectstart = function () { return false; };
|
99
|
+
}
|
100
|
+
if (document.ondrag !== undefined && savedhandlers.ondrag == null) {
|
101
|
+
savedhandlers.ondrag = document.ondrag;
|
102
|
+
document.ondrag = function () { return false; };
|
103
|
+
}
|
104
|
+
|
105
|
+
setSelectionPos(selection.first, e);
|
106
|
+
|
107
|
+
selection.active = true;
|
108
|
+
|
109
|
+
$(document).one("mouseup", onMouseUp);
|
110
|
+
}
|
111
|
+
|
112
|
+
function onMouseUp(e) {
|
113
|
+
// revert drag stuff for old-school browsers
|
114
|
+
if (document.onselectstart !== undefined)
|
115
|
+
document.onselectstart = savedhandlers.onselectstart;
|
116
|
+
if (document.ondrag !== undefined)
|
117
|
+
document.ondrag = savedhandlers.ondrag;
|
118
|
+
|
119
|
+
// no more draggy-dee-drag
|
120
|
+
selection.active = false;
|
121
|
+
updateSelection(e);
|
122
|
+
|
123
|
+
if (selectionIsSane())
|
124
|
+
triggerSelectedEvent();
|
125
|
+
else {
|
126
|
+
// this counts as a clear
|
127
|
+
plot.getPlaceholder().trigger("plotunselected", [ ]);
|
128
|
+
plot.getPlaceholder().trigger("plotselecting", [ null ]);
|
129
|
+
}
|
130
|
+
|
131
|
+
return false;
|
132
|
+
}
|
133
|
+
|
134
|
+
function getSelection() {
|
135
|
+
if (!selectionIsSane())
|
136
|
+
return null;
|
137
|
+
|
138
|
+
var x1 = Math.min(selection.first.x, selection.second.x),
|
139
|
+
x2 = Math.max(selection.first.x, selection.second.x),
|
140
|
+
y1 = Math.max(selection.first.y, selection.second.y),
|
141
|
+
y2 = Math.min(selection.first.y, selection.second.y);
|
142
|
+
|
143
|
+
var r = {};
|
144
|
+
var axes = plot.getAxes();
|
145
|
+
if (axes.xaxis.used)
|
146
|
+
r.xaxis = { from: axes.xaxis.c2p(x1), to: axes.xaxis.c2p(x2) };
|
147
|
+
if (axes.x2axis.used)
|
148
|
+
r.x2axis = { from: axes.x2axis.c2p(x1), to: axes.x2axis.c2p(x2) };
|
149
|
+
if (axes.yaxis.used)
|
150
|
+
r.yaxis = { from: axes.yaxis.c2p(y1), to: axes.yaxis.c2p(y2) };
|
151
|
+
if (axes.y2axis.used)
|
152
|
+
r.y2axis = { from: axes.y2axis.c2p(y1), to: axes.y2axis.c2p(y2) };
|
153
|
+
return r;
|
154
|
+
}
|
155
|
+
|
156
|
+
function triggerSelectedEvent() {
|
157
|
+
var r = getSelection();
|
158
|
+
|
159
|
+
plot.getPlaceholder().trigger("plotselected", [ r ]);
|
160
|
+
|
161
|
+
// backwards-compat stuff, to be removed in future
|
162
|
+
var axes = plot.getAxes();
|
163
|
+
if (axes.xaxis.used && axes.yaxis.used)
|
164
|
+
plot.getPlaceholder().trigger("selected", [ { x1: r.xaxis.from, y1: r.yaxis.from, x2: r.xaxis.to, y2: r.yaxis.to } ]);
|
165
|
+
}
|
166
|
+
|
167
|
+
function clamp(min, value, max) {
|
168
|
+
return value < min? min: (value > max? max: value);
|
169
|
+
}
|
170
|
+
|
171
|
+
function setSelectionPos(pos, e) {
|
172
|
+
var o = plot.getOptions();
|
173
|
+
var offset = plot.getPlaceholder().offset();
|
174
|
+
var plotOffset = plot.getPlotOffset();
|
175
|
+
pos.x = clamp(0, e.pageX - offset.left - plotOffset.left, plot.width());
|
176
|
+
pos.y = clamp(0, e.pageY - offset.top - plotOffset.top, plot.height());
|
177
|
+
|
178
|
+
if (o.selection.mode == "y")
|
179
|
+
pos.x = pos == selection.first? 0: plot.width();
|
180
|
+
|
181
|
+
if (o.selection.mode == "x")
|
182
|
+
pos.y = pos == selection.first? 0: plot.height();
|
183
|
+
}
|
184
|
+
|
185
|
+
function updateSelection(pos) {
|
186
|
+
if (pos.pageX == null)
|
187
|
+
return;
|
188
|
+
|
189
|
+
setSelectionPos(selection.second, pos);
|
190
|
+
if (selectionIsSane()) {
|
191
|
+
selection.show = true;
|
192
|
+
plot.triggerRedrawOverlay();
|
193
|
+
}
|
194
|
+
else
|
195
|
+
clearSelection(true);
|
196
|
+
}
|
197
|
+
|
198
|
+
function clearSelection(preventEvent) {
|
199
|
+
if (selection.show) {
|
200
|
+
selection.show = false;
|
201
|
+
plot.triggerRedrawOverlay();
|
202
|
+
if (!preventEvent)
|
203
|
+
plot.getPlaceholder().trigger("plotunselected", [ ]);
|
204
|
+
}
|
205
|
+
}
|
206
|
+
|
207
|
+
function setSelection(ranges, preventEvent) {
|
208
|
+
var axis, range, axes = plot.getAxes();
|
209
|
+
var o = plot.getOptions();
|
210
|
+
|
211
|
+
if (o.selection.mode == "y") {
|
212
|
+
selection.first.x = 0;
|
213
|
+
selection.second.x = plot.width();
|
214
|
+
}
|
215
|
+
else {
|
216
|
+
axis = ranges["xaxis"]? axes["xaxis"]: (ranges["x2axis"]? axes["x2axis"]: axes["xaxis"]);
|
217
|
+
range = ranges["xaxis"] || ranges["x2axis"] || { from:ranges["x1"], to:ranges["x2"] }
|
218
|
+
selection.first.x = axis.p2c(Math.min(range.from, range.to));
|
219
|
+
selection.second.x = axis.p2c(Math.max(range.from, range.to));
|
220
|
+
}
|
221
|
+
|
222
|
+
if (o.selection.mode == "x") {
|
223
|
+
selection.first.y = 0;
|
224
|
+
selection.second.y = plot.height();
|
225
|
+
}
|
226
|
+
else {
|
227
|
+
axis = ranges["yaxis"]? axes["yaxis"]: (ranges["y2axis"]? axes["y2axis"]: axes["yaxis"]);
|
228
|
+
range = ranges["yaxis"] || ranges["y2axis"] || { from:ranges["y1"], to:ranges["y2"] }
|
229
|
+
selection.first.y = axis.p2c(Math.min(range.from, range.to));
|
230
|
+
selection.second.y = axis.p2c(Math.max(range.from, range.to));
|
231
|
+
}
|
232
|
+
|
233
|
+
selection.show = true;
|
234
|
+
plot.triggerRedrawOverlay();
|
235
|
+
if (!preventEvent)
|
236
|
+
triggerSelectedEvent();
|
237
|
+
}
|
238
|
+
|
239
|
+
function selectionIsSane() {
|
240
|
+
var minSize = 5;
|
241
|
+
return Math.abs(selection.second.x - selection.first.x) >= minSize &&
|
242
|
+
Math.abs(selection.second.y - selection.first.y) >= minSize;
|
243
|
+
}
|
244
|
+
|
245
|
+
plot.clearSelection = clearSelection;
|
246
|
+
plot.setSelection = setSelection;
|
247
|
+
plot.getSelection = getSelection;
|
248
|
+
|
249
|
+
plot.hooks.bindEvents.push(function(plot, eventHolder) {
|
250
|
+
var o = plot.getOptions();
|
251
|
+
if (o.selection.mode != null)
|
252
|
+
eventHolder.mousemove(onMouseMove);
|
253
|
+
|
254
|
+
if (o.selection.mode != null)
|
255
|
+
eventHolder.mousedown(onMouseDown);
|
256
|
+
});
|
257
|
+
|
258
|
+
|
259
|
+
plot.hooks.drawOverlay.push(function (plot, ctx) {
|
260
|
+
// draw selection
|
261
|
+
if (selection.show && selectionIsSane()) {
|
262
|
+
var plotOffset = plot.getPlotOffset();
|
263
|
+
var o = plot.getOptions();
|
264
|
+
|
265
|
+
ctx.save();
|
266
|
+
ctx.translate(plotOffset.left, plotOffset.top);
|
267
|
+
|
268
|
+
var c = $.color.parse(o.selection.color);
|
269
|
+
|
270
|
+
ctx.strokeStyle = c.scale('a', 0.8).toString();
|
271
|
+
ctx.lineWidth = 1;
|
272
|
+
ctx.lineJoin = "round";
|
273
|
+
ctx.fillStyle = c.scale('a', 0.4).toString();
|
274
|
+
|
275
|
+
var x = Math.min(selection.first.x, selection.second.x),
|
276
|
+
y = Math.min(selection.first.y, selection.second.y),
|
277
|
+
w = Math.abs(selection.second.x - selection.first.x),
|
278
|
+
h = Math.abs(selection.second.y - selection.first.y);
|
279
|
+
|
280
|
+
ctx.fillRect(x, y, w, h);
|
281
|
+
ctx.strokeRect(x, y, w, h);
|
282
|
+
|
283
|
+
ctx.restore();
|
284
|
+
}
|
285
|
+
});
|
286
|
+
}
|
287
|
+
|
288
|
+
$.plot.plugins.push({
|
289
|
+
init: init,
|
290
|
+
options: {
|
291
|
+
selection: {
|
292
|
+
mode: null, // one of null, "x", "y" or "xy"
|
293
|
+
color: "#e8cfac"
|
294
|
+
}
|
295
|
+
},
|
296
|
+
name: 'selection',
|
297
|
+
version: '1.0'
|
298
|
+
});
|
299
|
+
})(jQuery);
|