2 * o------------------------------------------------------------------------------o
3 * | This file is part of the RGraph package - you can learn more at: |
5 * | http://www.rgraph.net |
7 * | This package is licensed under the RGraph license. For all kinds of business |
8 * | purposes there is a small one-time licensing fee to pay and for non |
9 * | commercial purposes it is free to use. You can read the full license here: |
11 * | http://www.rgraph.net/LICENSE.txt |
12 * o------------------------------------------------------------------------------o
15 if (typeof(RGraph) == 'undefined') RGraph = {isRGraph:true,type:'common'};
17 RGraph.AllowAdjusting = function (obj)
19 var canvas = obj.canvas;
20 var context = obj.context;
24 if (obj.type == 'line') {
25 canvas.onmousedown = function (e)
27 e = RGraph.FixEventObject(e);
29 var obj = e.target.__object__;
31 var canvas = obj.canvas;
32 var context = obj.context;
33 var coords = obj.coords;
34 var mouseCoords = RGraph.getMouseXY(e);
38 for (var i=0; i<coords.length; ++i) {
40 if ( mouseCoords[0] > coords[i][0] - 5
41 && mouseCoords[1] > coords[i][1] - 5
42 && mouseCoords[0] < coords[i][0] + 5
43 && mouseCoords[1] < coords[i][1] + 5
46 var numDataSeries = obj.original_data.length;
47 var numDataPoints = obj.original_data[0].length;
48 var data_series = i / numDataPoints;
49 data_series = Math.floor(data_series);
53 canvas.style.cursor = 'ns-resize';
54 RGraph.Registry.Set('chart.adjusting.line.' + id, [obj, i, [coords[i][0], coords[i][1]], data_series]);
62 canvas.onmousemove = function (e)
64 e = RGraph.FixEventObject(e);
65 var id = e.target.__object__.id;
67 var state = RGraph.Registry.Get('chart.adjusting.line.' + id);
72 var canvas = obj.canvas;
73 var context = obj.context;
74 var data_series = state[3];
75 var points = obj.original_data[data_series];
76 var mouseCoords = RGraph.getMouseXY(e);
77 var x = mouseCoords[0];
78 var y = mouseCoords[1];
80 if (y >= (obj.canvas.height - obj.Get('chart.gutter'))) {
81 y = obj.canvas.height - obj.Get('chart.gutter');
82 } else if (y <= obj.Get('chart.gutter')) {
83 y = obj.Get('chart.gutter');
86 var pos = obj.canvas.height - (2 * obj.Get('chart.gutter'));
87 pos = pos - (y - obj.Get('chart.gutter'));
88 var value = (obj.max / (obj.canvas.height - (2 * obj.Get('chart.gutter')))) * pos;
90 // Adjust the index so that it's applicable to the correct data series
91 for (var i=0; i<data_series; ++i) {
92 idx -= obj.original_data[0].length;
95 obj.original_data[data_series][idx] = value;
97 obj.Set('chart.ymax', obj.max);
98 canvas.style.cursor = 'ns-resize';
102 * Fire the onadjust event
104 RGraph.FireCustomEvent(obj, 'onadjust');
110 var canvas = e.target;
111 var context = canvas.__object__.context;
112 var obj = canvas.__object__;
113 var mouseCoords = RGraph.getMouseXY(e);
114 var x = mouseCoords[0];
115 var y = mouseCoords[1];
117 for (var i=0; i<obj.coords.length; ++i) {
119 if ( x > obj.coords[i][0] - 5
120 && y > obj.coords[i][1] - 5
121 && x < obj.coords[i][0] + 5
122 && y < obj.coords[i][1] + 5
125 canvas.style.cursor = 'ns-resize';
131 e.target.style.cursor = null;
135 canvas.onmouseup = function (e)
137 var id = e.target.__object__.id;
139 RGraph.Registry.Set('chart.adjusting.line.' + id, null);
140 e.target.style.cursor = null;
144 canvas.onmouseout = function (e)
152 } else if (obj.type == 'hprogress') {
155 canvas.onmousedown = function (e)
157 var id = e.target.__object__.id;
159 RGraph.Registry.Set('chart.adjusting.progress.' + id, [true]);
161 canvas.onmousemove(e);
165 canvas.onmousemove = function (e)
167 var id = e.target.__object__.id;
168 var state = RGraph.Registry.Get('chart.adjusting.progress.' + id);
170 if (state && state.length) {
171 var obj = e.target.__object__;
172 var canvas = obj.canvas;
173 var context = obj.context;
175 if (obj.type == 'hprogress') {
177 var coords = RGraph.getMouseXY(e);
178 coords[0] = Math.max(0, coords[0] - obj.Get('chart.gutter'));
179 var barWidth = canvas.width - (2 * obj.Get('chart.gutter'));
181 // Work out the new value
182 var value = (coords[0] / barWidth) * (obj.max - obj.Get('chart.min'));
183 value += obj.Get('chart.min');
185 obj.value = Math.max(0, value.toFixed());
186 RGraph.Clear(obj.canvas);
189 } else if (obj.type == 'vprogress') {
191 var coords = RGraph.getMouseXY(e);
192 coords[1] = Math.max(0, coords[1] - obj.Get('chart.gutter'));
193 var barHeight = canvas.height - (2 * obj.Get('chart.gutter'));
195 // Work out the new value
196 var value = ( (barHeight - coords[1]) / barHeight) * obj.max;
198 obj.value = Math.max(0, value.toFixed());
199 RGraph.Clear(obj.canvas);
204 * Fire the onadjust event
206 RGraph.FireCustomEvent(obj, 'onadjust');
211 canvas.onmouseup = function (e)
213 var id = e.target.__object__.id;
214 RGraph.Registry.Set('chart.adjusting.progress.' + id, null);
218 canvas.onmouseout = function (e)
226 } else if (obj.type == 'rose') {
229 obj.Set('chart.ymax', obj.max);
232 canvas.onmousemove = function (e)
234 var obj = e.target.__object__;
236 var canvas = obj.canvas;
237 var context = obj.context;
238 var coords = RGraph.getMouseXY(e);
239 var segment = RGraph.Registry.Get('chart.adjusting.rose.' + id);
240 var x = Math.abs(coords[0] - obj.centerx);
241 var y = Math.abs(coords[1] - obj.centery);
242 var theta = Math.atan(y / x) * (180 / Math.PI); // theta is now in DEGREES
245 // Account for the correct quadrant
246 if (coords[0] >= obj.centerx && coords[1] < obj.centery) {
248 } else if (coords[0] >= obj.centerx && coords[1] >= obj.centery) {
250 } else if (coords[0] < obj.centerx && coords[1] >= obj.centery) {
254 } else if (coords[0] < obj.centerx && coords[1] < obj.centery) {
260 var Hyp = Math.abs(Adj / Math.sin(theta / (180 / Math.PI)));
262 for (var i=0; i<obj.angles.length; ++i) {
264 theta > obj.angles[i][0]
265 && theta < obj.angles[i][1] ) {
267 if (RGraph.Registry.Get('chart.adjusting.rose.' + id) && i == segment[5]) {
268 var newvalue = (Hyp / (obj.radius - 25) ) * obj.max;
269 obj.data[i] = Math.min(newvalue, obj.max);
271 RGraph.Clear(obj.canvas);
275 * Fire the onadjust event
277 RGraph.FireCustomEvent(obj, 'onadjust');
280 if (Hyp <= (obj.angles[i][2] + 5) && Hyp >= (obj.angles[i][2] - 5) ) {
281 canvas.style.cursor = 'move';
284 } else if (obj.Get('chart.tooltips') && Hyp <= (obj.angles[i][2] - 5) ) {
285 canvas.style.cursor = 'pointer';
292 canvas.style.cursor = 'default';
296 canvas.onmousedown = function (e)
298 var obj = e.target.__object__;
300 var canvas = obj.canvas;
301 var context = obj.context;
302 var coords = RGraph.getMouseXY(e);
303 var segment = RGraph.getSegment(e, 5);
305 if (segment && segment.length && !RGraph.Registry.Get('chart.adjusting.rose.' + id)) {
306 var x = Math.abs(coords[0] - obj.centerx);
307 var y = Math.abs(coords[1] - obj.centery);
309 var a = Math.atan(y / x) * (180 / Math.PI); // a is now in DEGREES
311 // Account for the correct quadrant
312 if (coords[0] >= obj.centerx && coords[1] < obj.centery) {
315 } else if (coords[0] >= obj.centerx && coords[1] >= obj.centery) {
317 } else if (coords[0] < obj.centerx && coords[1] >= obj.centery) {
320 } else if (coords[0] < obj.centerx && coords[1] < obj.centery) {
324 var hyp = Math.abs(y / Math.sin(a / 57.3));
326 if (hyp >= (segment[2] - 10) ) {
329 * Hide any currently shown tooltip
331 if (RGraph.Registry.Get('chart.tooltip')) {
332 RGraph.Registry.Get('chart.tooltip').style.display = 'none';
333 RGraph.Registry.Set('chart.tooltip', null);
336 RGraph.Registry.Set('chart.adjusting.rose.' + id, segment);
344 canvas.onmouseup = function (e)
346 var obj = e.target.__object__;
349 if (RGraph.Registry.Get('chart.adjusting.rose.' + id)) {
351 RGraph.Registry.Set('chart.adjusting.rose.' + id, null);
359 canvas.onmouseout = function (e)
367 } else if (obj.type == 'bar') {
369 // Stacked bar charts not supported
370 if (obj.Get('chart.grouping') == 'stacked') {
371 alert('[BAR] Adjusting stacked bar charts is not supported');
376 var canvas = obj.canvas;
377 var context = obj.context;
380 canvas.onmousemove = function (e)
382 var obj = e.target.__object__;
384 var canvas = obj.canvas;
385 var context = obj.context;
386 var mouse = RGraph.getMouseXY(e);
387 var mousex = mouse[0];
388 var mousey = mouse[1]; // mousey, mousey...
390 // Loop through the coords to see if the mouse position is at the top of a bar
391 for (var i=0; i<obj.coords.length; ++i) {
392 if (mousex > obj.coords[i][0] && mousex < (obj.coords[i][0] + obj.coords[i][2])) {
394 // Change the mouse pointer
395 if (mousey > (obj.coords[i][1] - 5) && mousey < (obj.coords[i][1] + 5)) {
396 canvas.style.cursor = 'ns-resize';
398 canvas.style.cursor = 'default';
401 var idx = RGraph.Registry.Get('chart.adjusting.bar.' + id)
403 if (typeof(idx) == 'number') {
404 var newheight = obj.grapharea - (mousey - obj.Get('chart.gutter'));
405 var newvalue = (newheight / obj.grapharea) * obj.max;
407 // Top and bottom boundaries
408 if (newvalue > obj.max) newvalue = obj.max;
409 if (newvalue < 0) newvalue = 0;
411 ///////////////// This was fun to work out... /////////////////
412 for (var j=0, index=0; j<obj.data.length; ++j,++index) {
413 if (typeof(obj.data[j]) == 'object') {
414 for (var k=0; k<obj.data[j].length && index <= idx; ++k, ++index) {
416 obj.data[j][k] = newvalue;
423 } else if (typeof(obj.data[j]) == 'number') {
426 obj.data[j] = newvalue;
436 ///////////////////////////////////////////////////////////////
438 RGraph.Clear(canvas);
442 * Fire the onadjust event
444 RGraph.FireCustomEvent(obj, 'onadjust');
451 canvas.style.cursor = 'default';
456 canvas.onmousedown = function (e)
458 var obj = e.target.__object__;
460 var canvas = obj.canvas;
461 var context = obj.context;
462 var mouse = RGraph.getMouseXY(e);
463 var mousex = mouse[0];
464 var mousey = mouse[1]; // mousey, mousey...
466 // Loop through the coords to see if the mouse position is at the top of a bar
467 for (var i=0; i<obj.coords.length; ++i) {
469 mousex > obj.coords[i][0] && mousex < (obj.coords[i][0] + obj.coords[i][2])
473 obj.Set('chart.ymax', obj.max);
474 RGraph.Registry.Set('chart.adjusting.bar.' + id, i);
475 canvas.onmousemove(e);
482 canvas.onmouseup = function (e)
484 var id = e.target.__object__.id;
486 RGraph.Registry.Set('chart.adjusting.bar.' + id, null);
490 canvas.onmouseout = function (e)
499 } else if (obj.type == 'tradar') {
502 var canvas = obj.canvas;
503 var context = obj.context;
506 canvas.onmousemove = function (e)
508 var obj = e.target.__object__;
510 var canvas = obj.canvas;
511 var context = obj.context;
512 var mouseDown = RGraph.Registry.Get('chart.adjusting.tradar.' + id);
513 var mouseCoords = RGraph.getMouseXY(e);
518 canvas.style.cursor = 'move';
520 var dx = mouseCoords[0] - obj.centerx;
521 var dy = mouseCoords[1] - obj.centery;
522 var hyp = Math.sqrt((dx * dx) + (dy * dy));
524 var newvalue = (hyp / (obj.size / 2)) * obj.max;
526 newvalue = Math.min(obj.max, newvalue);
527 newvalue = Math.max(0, newvalue);
530 * Only redraw the graph if the mouse is in the same quadrant as the point
532 if ( (dx >= 0 ? true : false) == mouseDown[1] && (dy >= 0 ? true : false) == mouseDown[2]) {
533 obj.data[mouseDown[0]] = newvalue;
534 RGraph.Clear(canvas);
538 * Fire the onadjust event
540 RGraph.FireCustomEvent(obj, 'onadjust');
546 // Determine if the mouse is near a point, and if so, change the pointer
547 for (var i=0; i<obj.coords.length; ++i) {
549 var dx = Math.abs(mouseCoords[0] - obj.coords[i][0]);
550 var dy = Math.abs(mouseCoords[1] - obj.coords[i][1]);
551 var a = Math.atan(dy / dx);
554 var hyp = Math.sqrt((dx * dx) + (dy * dy));
557 canvas.style.cursor = 'move';
562 canvas.style.cursor = 'default';
567 canvas.onmousedown = function (e)
569 e = RGraph.FixEventObject(e);
571 var obj = e.target.__object__;
573 var canvas = obj.canvas;
574 var context = obj.context;
575 var mouseCoords = RGraph.getMouseXY(e);
578 // Determine if the mouse is near a point
579 for (var i=0; i<obj.coords.length; ++i) {
581 var dx = Math.abs(mouseCoords[0] - obj.coords[i][0]);
582 var dy = Math.abs(mouseCoords[1] - obj.coords[i][1]);
583 var a = Math.atan(dy / dx);
586 var hyp = Math.sqrt((dx * dx) + (dy * dy));
589 canvas.style.cursor = 'pointer';
590 RGraph.Registry.Set('chart.adjusting.tradar.' + id, [i, obj.coords[i][0] > obj.centerx, obj.coords[i][1] > obj.centery]);
595 canvas.style.cursor = 'default';
599 canvas.onmouseup = function (e)
601 RGraph.Registry.Set('chart.adjusting.tradar.' + e.target.id, null);
602 canvas.style.cursor = 'default';
606 canvas.onmouseout = function (e)
615 * Returns 1 or -1 depening on whether the given number is positive or negative.
616 * Zero is considered positive.
618 * @param int num The number
619 * @return int 1 if the number is positive or zero, -1 if it's negative
621 //RGraph.getSign = function (num)
623 // return num >= 0 ? 1 : -1;