source: Dev/trunk/RGraph/libraries/RGraph.common.adjusting.js @ 201

Last change on this file since 201 was 77, checked in by fpvanagthoven, 14 years ago

RGraph

File size: 43.5 KB
Line 
1    /**
2    * o------------------------------------------------------------------------------o
3    * | This file is part of the RGraph package - you can learn more at:             |
4    * |                                                                              |
5    * |                          http://www.rgraph.net                               |
6    * |                                                                              |
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:  |
10    * |                                                                              |
11    * |                      http://www.rgraph.net/LICENSE.txt                       |
12    * o------------------------------------------------------------------------------o
13    */
14
15    if (typeof(RGraph) == 'undefined') RGraph = {isRGraph:true,type:'common'};
16
17    RGraph.AllowAdjusting = function (obj)
18    {
19        var canvas  = obj.canvas;
20        var context = obj.context;
21       
22        RGraph.Register(obj);
23       
24        if (obj.type == 'thermometer') {
25            var canvas_onmousedown = function (e)
26            {
27                e = RGraph.FixEventObject(e);
28
29                var obj         = e.target.__object__;
30                var id          = obj.id;
31                var canvas      = obj.canvas;
32                var context     = obj.context;
33                var coords      = obj.coords;
34
35                var mouseCoords = RGraph.getMouseXY(e);
36                var mouseX      = mouseCoords[0];
37                var mouseY      = mouseCoords[1];
38               
39                if (   mouseX > obj.coords[0]
40                    && mouseX < (obj.coords[0] + obj.coords[2])
41                    ) {
42
43                    RGraph.Registry.Set('chart.thermometer.' + id + '.resizing', true);
44                    obj.canvas.style.cursor = 'ns-resize';
45
46                    /**
47                    * Fire the RGraph event
48                    */
49                    RGraph.FireCustomEvent(e.target.__object__, 'onadjustbegin');
50                   
51                    // Fire the onmousemove handler
52                    canvas_onmousemove(e);
53                }
54            }
55            canvas.addEventListener('mousedown', canvas_onmousedown, false);
56            RGraph.AddEventListener(canvas.id, 'mousedown', canvas_onmousedown);
57
58
59
60            var canvas_onmousemove = function (e)
61            {
62                e = RGraph.FixEventObject(e);
63               
64                var obj         = e.target.__object__;
65                var id          = obj.id;
66                var canvas      = obj.canvas;
67                var context     = obj.context;
68                var coords      = obj.coords;
69
70                var mouseCoords = RGraph.getMouseXY(e);
71                var mouseX      = mouseCoords[0];
72                var mouseY      = mouseCoords[1];
73               
74                if (RGraph.Registry.Get('chart.thermometer.' + id + '.resizing')) {
75                   
76                    var capRadius = (RGraph.GetWidth(obj) - obj.gutterLeft - obj.gutterRight) / 2;
77                    var top       = obj.gutterTop + capRadius;
78                    var bottom    = coords[1] + coords[3];
79                    var newvalue = obj.max - ((mouseY - top) / (bottom - obj.gutterTop - capRadius)) * (obj.max - obj.min);
80
81                    obj.value = Math.round(newvalue);
82
83                    /**
84                    * Bounds checking
85                    */
86                    if (obj.value > obj.max) {
87                        obj.value = obj.max;
88                    } else if (obj.value < obj.min) {
89                        obj.value = obj.min;
90                    }
91
92                    RGraph.Clear(canvas);
93                    obj.Draw();
94
95                    /**
96                    * Fire the RGraph event
97                    */
98                    RGraph.FireCustomEvent(e.target.__object__, 'onadjust');
99                }
100            }
101            canvas.addEventListener('mousemove', canvas_onmousemove, false);
102            RGraph.AddEventListener(canvas.id, 'mousemove', canvas_onmousemove);
103
104
105
106            var canvas_onmouseup = function (e)
107            {
108                e = RGraph.FixEventObject(e);
109               
110                var obj         = e.target.__object__;
111                var id          = obj.id;
112                var canvas      = obj.canvas;
113                var context     = obj.context;
114                var coords      = obj.coords;
115
116                var mouseCoords = RGraph.getMouseXY(e);
117                var mouseX      = mouseCoords[0];
118                var mouseY      = mouseCoords[1];
119
120                RGraph.Registry.Set('chart.thermometer.' + id + '.resizing', false);
121                obj.canvas.style.cursor = 'default';
122
123                /**
124                * Fire the RGraph event
125                */
126                RGraph.FireCustomEvent(e.target.__object__, 'onadjustend');
127            }
128            canvas.addEventListener('mouseup', canvas_onmouseup, false);
129            RGraph.AddEventListener(canvas.id, 'mouseup', canvas_onmouseup);
130           
131           
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147        } else if (obj.type == 'line') {
148            var canvas_onmousedown = function (e)
149            {
150                e = RGraph.FixEventObject(e);
151
152                var obj         = e.target.__object__;
153                var id          = obj.id;
154                var canvas      = obj.canvas;
155                var context     = obj.context;
156                var coords      = obj.coords;
157                var mouseCoords = RGraph.getMouseXY(e);
158   
159                RGraph.Redraw();
160   
161                for (var i=0; i<coords.length; ++i) {
162   
163                    if (   mouseCoords[0] > coords[i][0] - 5
164                        && mouseCoords[1] > coords[i][1] - 5
165                        && mouseCoords[0] < coords[i][0] + 5
166                        && mouseCoords[1] < coords[i][1] + 5
167                       ) {
168
169                        var numDataSeries = obj.original_data.length;
170                        var numDataPoints = obj.original_data[0].length;
171                        var data_series   = i / numDataPoints;
172                            data_series = Math.floor(data_series);
173   
174   
175   
176                      canvas.style.cursor = 'ns-resize';
177                      RGraph.FireCustomEvent(obj, 'onadjustbegin');
178                      RGraph.Registry.Set('chart.adjusting.line.' + id, [obj, i, [coords[i][0], coords[i][1]], data_series]);
179   
180                      return;
181                    }
182                }
183            }
184            canvas.addEventListener('mousedown', canvas_onmousedown, false);
185            RGraph.AddEventListener(canvas.id, 'mousedown', canvas_onmousedown);
186   
187   
188            var canvas_onmousemove = function (e)
189            {
190                e = RGraph.FixEventObject(e);
191                var id = e.target.__object__.id;
192   
193                var state = RGraph.Registry.Get('chart.adjusting.line.' + id);
194   
195                if (state) {
196                    var obj         = state[0];
197                    var idx         = state[1];
198                    var canvas      = obj.canvas;
199                    var context     = obj.context;
200                    var data_series = state[3];
201                    var points      = obj.original_data[data_series];
202                    var mouseCoords = RGraph.getMouseXY(e);
203                    var x           = mouseCoords[0];
204                    var y           = mouseCoords[1];
205                    var h           = RGraph.GetHeight(obj);
206                   
207   
208                    // Don't allow adjusting to the gutter if out-of-bounds is NOT specified
209                    if (!obj.Get('chart.outofbounds')) {
210                        if (y >= (h - obj.gutterBottom)) {
211                            y = h - obj.gutterBottom;
212                        } else if (y <= obj.gutterTop) {
213                            y = obj.gutterTop;
214                        }
215                    }
216
217                    // This accounts for a center X axis
218                    if (obj.Get('chart.xaxispos') == 'center') {
219                        y *= 2;
220                        y -= (obj.gutterTop / 2);
221                    } else if (obj.Get('chart.xaxispos') == 'top') {
222                        y = RGraph.GetHeight(obj) - y;
223                    }
224
225                    var pos   = h - obj.gutterTop - obj.gutterBottom;
226                        pos   = pos - (y - obj.gutterTop);
227                    var value = (obj.max / (h - obj.gutterTop - obj.gutterBottom)) * pos;
228                   
229                    if (obj.Get('chart.ylabels.invert')) {
230                        value = obj.max - value;
231                    }
232
233
234
235                    // Adjust the index so that it's applicable to the correct data series
236                    for (var i=0; i<data_series; ++i) {
237                        idx -= obj.original_data[0].length;
238                    }
239
240                    obj.original_data[data_series][idx] = value;
241   
242                    obj.Set('chart.ymax', obj.max);
243                    canvas.style.cursor = 'ns-resize';
244                    RGraph.Redraw();
245
246                    /**
247                    * Fire the onadjust event
248                    */
249                    RGraph.FireCustomEvent(obj, 'onadjust');
250   
251                    return;
252   
253                } else {
254                   
255                    var canvas  = e.target;
256                    var context = canvas.__object__.context;
257                    var obj     = canvas.__object__;
258                    var mouseCoords = RGraph.getMouseXY(e);
259                    var x       = mouseCoords[0];
260                    var y       = mouseCoords[1];
261   
262                    for (var i=0; i<obj.coords.length; ++i) {
263   
264                        if (   x > obj.coords[i][0] - 5
265                            && y > obj.coords[i][1] - 5
266                            && x < obj.coords[i][0] + 5
267                            && y < obj.coords[i][1] + 5
268                           ) {
269   
270                           canvas.style.cursor = 'ns-resize';
271                           return;
272                        }
273                    }
274                }
275               
276                e.target.style.cursor = null;
277            }
278            canvas.addEventListener('mousemove', canvas_onmousemove, false);
279            RGraph.AddEventListener(canvas.id, 'mousemove', canvas_onmousemove);
280   
281   
282            var canvas_onmouseup = function (e)
283            {
284                var id = e.target.__object__.id;
285               
286                if (RGraph.Registry.Get('chart.adjusting.line.' + id)) {
287                    RGraph.FireCustomEvent(e.target.__object__, 'onadjustend');
288                }
289
290                RGraph.Registry.Set('chart.adjusting.line.' + id, null);
291                e.target.style.cursor = null;
292            }
293            canvas.addEventListener('mouseup', canvas_onmouseup, false);
294            RGraph.AddEventListener(canvas.id, 'mouseup', canvas_onmouseup);
295   
296   
297            var canvas_onmouseout = function (e)
298            {
299                canvas_onmouseup(e);
300            }
301            canvas.addEventListener('mouseout', canvas_onmouseout, false);
302            RGraph.AddEventListener(canvas.id, 'mouseout', canvas_onmouseout);
303       
304        /**
305        * HProgress bar
306        */
307        } else if (obj.type == 'hprogress') {
308
309           
310            var canvas_onmousedown = function (e)
311            {
312                var id = e.target.__object__.id;
313
314                RGraph.Registry.Set('chart.adjusting.hprogress.' + id, [true]);
315               
316                RGraph.FireCustomEvent(e.target.__object__, 'onadjustbegin');
317               
318                canvas_onmousemove(e);
319            }
320            canvas.addEventListener('mousedown', canvas_onmousedown, false);
321            RGraph.AddEventListener(canvas.id, 'mousedown', canvas_onmousedown);
322
323
324            var canvas_onmousemove = function (e)
325            {
326                var id    = e.target.__object__.id;
327                var state = RGraph.Registry.Get('chart.adjusting.hprogress.' + id);
328
329                if (state && state.length) {
330                    var obj     = e.target.__object__;
331                    var canvas  = obj.canvas;
332                    var context = obj.context;
333                   
334                    if (obj.type == 'hprogress') {
335                   
336                        var coords = RGraph.getMouseXY(e);
337                            coords[0] = Math.max(0, coords[0] - obj.gutterLeft);
338                        var barWidth  = canvas.width - obj.gutterLeft - obj.gutterRight;
339                       
340                        // Work out the new value
341                        var value  = (coords[0] / barWidth) * (obj.max - obj.Get('chart.min'));
342                            value += obj.Get('chart.min');
343                       
344                        obj.value = Math.max(0, value.toFixed());
345                        RGraph.Clear(obj.canvas);
346                        obj.Draw();
347
348/*
349                    } else if (obj.type == 'vprogress') {
350
351                        var coords = RGraph.getMouseXY(e);
352                            coords[1] = Math.max(0, coords[1] - obj.Get('chart.gutter'));
353                        var barHeight = canvas.height - (2 * obj.Get('chart.gutter'));
354                       
355                        // Work out the new value
356                        var value = ( (barHeight - coords[1]) / barHeight) * obj.max;
357                       
358                        obj.value = Math.max(0, value.toFixed());
359                        RGraph.Clear(obj.canvas);
360                        obj.Draw();
361*/
362                    }
363
364                    /**
365                    * Fire the onadjust event
366                    */
367                    RGraph.FireCustomEvent(obj, 'onadjust');
368                }
369            }
370            canvas.addEventListener('mousemove', canvas_onmousemove, false);
371            RGraph.AddEventListener(canvas.id, 'mousemove', canvas_onmousemove);
372           
373           
374            var canvas_onmouseup = function (e)
375            {
376                var id = e.target.__object__.id;
377
378                if (RGraph.Registry.Get('chart.adjusting.hprogress.' + id)) {
379                    RGraph.FireCustomEvent(e.target.__object__, 'onadjustend');
380                }
381
382                RGraph.Registry.Set('chart.adjusting.hprogress.' + id, null);
383            }
384            canvas.addEventListener('mouseup', canvas_onmouseup, false);
385            RGraph.AddEventListener(canvas.id, 'mouseup', canvas_onmouseup);
386   
387   
388            var canvas_onmouseout = function (e)
389            {
390                canvas_onmouseup(e);
391            }
392            canvas.addEventListener('mouseout', canvas_onmouseout, false);
393            RGraph.AddEventListener(canvas.id, 'mouseout', canvas_onmouseout);
394       
395        /**
396        * VProgress bar
397        */
398        } else if (obj.type == 'vprogress') {
399
400           
401            var canvas_onmousedown = function (e)
402            {
403                var id = e.target.__object__.id;
404
405                RGraph.Registry.Set('chart.adjusting.vprogress.' + id, [true]);
406               
407                RGraph.FireCustomEvent(e.target.__object__, 'onadjustbegin');
408               
409                canvas_onmousemove(e);
410            }
411            canvas.addEventListener('mousedown', canvas_onmousedown, false);
412            RGraph.AddEventListener(canvas.id, 'mousedown', canvas_onmousedown);
413
414
415            var canvas_onmousemove = function (e)
416            {
417                var id    = e.target.__object__.id;
418                var state = RGraph.Registry.Get('chart.adjusting.vprogress.' + id);
419
420                if (state && state.length) {
421                    var obj     = e.target.__object__;
422                    var canvas  = obj.canvas;
423                    var context = obj.context;
424                   
425                    if (obj.type == 'hprogress') {
426                   
427                        var coords = RGraph.getMouseXY(e);
428                            coords[0] = Math.max(0, coords[0] - obj.gutterLeft);
429                        var barWidth  = canvas.width - obj.gutterLeft - obj.gutterRight;
430                       
431                        // Work out the new value
432                        var value  = (coords[0] / barWidth) * (obj.max - obj.Get('chart.min'));
433                            value += obj.Get('chart.min');
434                       
435                        obj.value = Math.max(0, value.toFixed());
436                        RGraph.Clear(obj.canvas);
437                        obj.Draw();
438
439                    } else if (obj.type == 'vprogress') {
440
441                        var coords = RGraph.getMouseXY(e);
442                            coords[1] = Math.max(0, coords[1] - obj.gutterTop);
443                        var barHeight = canvas.height - obj.gutterTop -  obj.gutterBottom;
444                       
445                        // Work out the new value
446                        var value = ( (barHeight - coords[1]) / barHeight) * obj.max;
447                       
448                        obj.value = Math.max(0, value.toFixed());
449                        RGraph.Clear(obj.canvas);
450                        obj.Draw();
451                    }
452
453                    /**
454                    * Fire the onadjust event
455                    */
456                    RGraph.FireCustomEvent(obj, 'onadjust');
457                }
458            }
459            canvas.addEventListener('mousemove', canvas_onmousemove, false);
460            RGraph.AddEventListener(canvas.id, 'mousemove', canvas_onmousemove);
461           
462           
463            var canvas_onmouseup = function (e)
464            {
465                var id = e.target.__object__.id;
466
467                if (RGraph.Registry.Get('chart.adjusting.vprogress.' + id)) {
468                    RGraph.FireCustomEvent(e.target.__object__, 'onadjustend');
469                }
470
471                RGraph.Registry.Set('chart.adjusting.vprogress.' + id, null);
472            }
473            canvas.addEventListener('mouseup', canvas_onmouseup, false);
474            RGraph.AddEventListener(canvas.id, 'mouseup', canvas_onmouseup);
475   
476   
477            var canvas_onmouseout = function (e)
478            {
479                canvas_onmouseup(e);
480            }
481            canvas.addEventListener('mouseout', canvas_onmouseout, false);
482            RGraph.AddEventListener(canvas.id, 'mouseout', canvas_onmouseout);
483       
484        /**
485        * Rose chart
486        */
487        } else if (obj.type == 'rose') {
488
489
490            obj.Set('chart.ymax', obj.max);
491
492
493            var canvas_onmousemove = function (e)
494            {
495                var obj     = e.target.__object__;
496                var id      = obj.id;
497                var canvas  = obj.canvas;
498                var context = obj.context;
499                var coords  = RGraph.getMouseXY(e);
500                var segment = RGraph.Registry.Get('chart.adjusting.rose.' + id);
501                var x       = Math.abs(coords[0] - obj.centerx);
502                var y       = Math.abs(coords[1] - obj.centery);
503                var theta   = Math.atan(y / x) * (180 / Math.PI); // theta is now in DEGREES
504
505
506                // Account for the correct quadrant
507                if (coords[0] >= obj.centerx && coords[1] < obj.centery) {
508                    theta = 90 - theta;
509                } else if (coords[0] >= obj.centerx && coords[1] >= obj.centery) {
510                    theta += 90;
511                } else if (coords[0] < obj.centerx && coords[1] >= obj.centery) {
512                    theta = 90 - theta;
513                    theta = 180 + theta;
514                   
515                } else if (coords[0] < obj.centerx && coords[1] < obj.centery) {
516                    theta = theta + 270;
517                }
518
519                var Opp = y;
520                var Adj = x;
521                var Hyp = Math.abs(Adj / Math.sin(theta / (180 / Math.PI)));
522
523                for (var i=0; i<obj.angles.length; ++i) {
524                    if (
525                           theta > obj.angles[i][0]
526                        && theta < obj.angles[i][1]) {
527
528                        if (RGraph.Registry.Get('chart.adjusting.rose.' + id) && i == segment[6]) {
529                            var newvalue  = (Hyp / (obj.radius - 25) ) * obj.max;
530                            obj.data[i]   = Math.min(newvalue, obj.max);
531
532                            RGraph.Clear(obj.canvas);
533                            obj.Draw();
534
535                            /**
536                            * Fire the onadjust event
537                            */
538                            RGraph.FireCustomEvent(obj, 'onadjust');
539                        }
540                       
541                        if (Hyp <= (obj.angles[i][2] + 5) && Hyp >= (obj.angles[i][2] - 5) ) {
542                            canvas.style.cursor = 'move';
543                            return false;
544                       
545                        } else if (obj.Get('chart.tooltips') && Hyp <= (obj.angles[i][2] - 5) ) {
546                            canvas.style.cursor = 'pointer';
547                            return false;
548                        }
549
550                    }
551                }
552
553                canvas.style.cursor = 'default';
554               
555                return;
556            }
557            canvas.addEventListener('mousemove', canvas_onmousemove, false);
558            RGraph.AddEventListener(canvas.id, 'mousemove', canvas_onmousemove);
559
560
561            var canvas_onmousedown = function (e)
562            {
563                var obj     = e.target.__object__;
564                var id      = obj.id;
565                var canvas  = obj.canvas;
566                var context = obj.context;
567                var coords  = RGraph.getMouseXY(e);
568                var segment = obj.getSegment(e, 5);
569
570                if (segment && segment.length && !RGraph.Registry.Get('chart.adjusting.rose.' + id)) {
571                    var x = Math.abs(coords[0] - obj.centerx);
572                    var y = Math.abs(coords[1] - obj.centery);
573                    var a = Math.atan(y / x) * (180 / Math.PI); // a is now in DEGREES
574
575                    // Account for the correct quadrant
576                    if (coords[0] >= obj.centerx && coords[1] < obj.centery) {
577                        a  = 90 - a;
578                        a += 270;
579                    } else if (coords[0] >= obj.centerx && coords[1] >= obj.centery) {
580                        // Nada
581                    } else if (coords[0] < obj.centerx && coords[1] >= obj.centery) {
582                         a  = 90 - a;
583                         a += 90;
584                    } else if (coords[0] < obj.centerx && coords[1] < obj.centery) {
585                        a += 180;
586                    }
587
588                    var hyp = Math.abs(y / Math.sin(a / 57.3));
589
590                    if (hyp >= (segment[2] - 10) ) {
591
592                        /**
593                        * Hide any currently shown tooltip
594                        */
595                        if (RGraph.Registry.Get('chart.tooltip')) {
596                            RGraph.Registry.Get('chart.tooltip').style.display = 'none';
597                            RGraph.Registry.Set('chart.tooltip', null);
598                        }
599                       
600                        RGraph.Registry.Set('chart.adjusting.rose.' + id, segment);
601                       
602                        RGraph.FireCustomEvent(e.target.__object__, 'onadjustbegin');
603                       
604                        e.stopPropagation();
605                    }
606                }
607            }
608            canvas.addEventListener('mousedown', canvas_onmousedown, false);
609            RGraph.AddEventListener(canvas.id, 'mousedown', canvas_onmousedown);
610
611
612            var canvas_onmouseup = function (e)
613            {
614                var obj = e.target.__object__;
615                var id  = obj.id;
616
617                if (RGraph.Registry.Get('chart.adjusting.rose.' + id)) {
618
619
620                    RGraph.FireCustomEvent(e.target.__object__, 'onadjustend');
621                    RGraph.Registry.Set('chart.adjusting.rose.' + id, null);
622                    e.stopPropagation();
623                   
624                    return false;
625                }
626            }
627            canvas.addEventListener('mouseup', canvas_onmouseup, false);
628            RGraph.AddEventListener(canvas.id, 'mouseup', canvas_onmouseup);
629   
630   
631            var canvas_onmouseout = function (e)
632            {
633                canvas_onmouseup(e);
634            }
635            canvas.addEventListener('mouseout', canvas_onmouseout, false);
636            RGraph.AddEventListener(canvas.id, 'mouseout', canvas_onmouseout);
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656        /**
657        * Bar chart
658        */
659        } else if (obj.type == 'bar') {
660       
661            // Stacked bar charts not supported
662            if (obj.Get('chart.grouping') == 'stacked') {
663                alert('[BAR] Adjusting stacked bar charts is not supported');
664                return;
665            }
666
667
668            var canvas  = obj.canvas;
669            var context = obj.context;
670
671
672            var canvas_onmousemove = function (e)
673            {
674                var obj     = e.target.__object__;
675                var id      = obj.id;
676                var canvas  = obj.canvas;
677                var context = obj.context;
678                var mouse   = RGraph.getMouseXY(e);
679                var mousex  = mouse[0];
680                var mousey  = mouse[1]; // mousey, mousey...
681               
682
683                // Loop through the coords to see if the mouse position is at the top of a bar
684                for (var i=0; i<obj.coords.length; ++i) {
685
686                    var barX = obj.coords[i][0];
687                    var barY = obj.coords[i][1];
688                    var barW = obj.coords[i][2];
689                    var barH = obj.coords[i][3];
690                   
691                    if (mousex > barX && mousex < (barX + barW)) {
692
693                        /**
694                        * Change the mouse pointer
695                        */
696                        //if (   (obj.Get('chart.xaxispos') == 'bottom' && (mousey > (barY - 5) && mousey < (barY + 5)))
697                        //    || (((obj.Get('chart.xaxispos') == 'center' || obj.Get('chart.xaxispos') == 'top') && mousey > (barY + barH - 5) && mousey < (barY + barH + 5)))
698                        //    || ((obj.Get('chart.xaxispos') == 'center' && barX < (obj.canvas.height / 2) && mousey < (barY + 5) && mousey > barY))
699                        //   ) {
700                        //    canvas.style.cursor = 'ns-resize';
701
702                        //} else {
703                        //    canvas.style.cursor = 'default';
704                        //}
705
706
707
708                        var idx = RGraph.Registry.Get('chart.adjusting.bar.' + id)
709                       
710                        if (typeof(idx) == 'number') {
711
712                            // This accounts for a center X axis
713                            if (obj.Get('chart.xaxispos') == 'center') {
714                                obj.grapharea /= 2;
715                            }
716
717                            var newheight = obj.grapharea - (mousey - obj.gutterTop);
718                            var newvalue  = (newheight / obj.grapharea) * obj.max;
719                           
720                            // Account for X axis at the top
721                            if (obj.Get('chart.xaxispos') == 'top') {
722                                newvalue = obj.max - newvalue;
723                            }
724
725                            // Top and bottom boundaries
726                            if (newvalue > obj.max) newvalue = obj.max;
727
728                            if (obj.Get('chart.xaxispos') == 'center') {
729                                if (newvalue < (-1 * obj.max)) newvalue = (-1 * obj.max);
730                           
731                            } else {
732                                if (newvalue < 0) newvalue = 0;
733                            }
734
735                            ///////////////// This was fun to work out... /////////////////
736
737                            var j, index;
738
739                            for (var j=0, index=0; j<obj.data.length; ++j,++index) {
740
741                                if (typeof(obj.data[j]) == 'object') {
742
743                                    for (var k=0; k<obj.data[j].length && index <= idx; ++k, ++index) {
744                                        if (index == idx) {
745
746                                       
747                                            if (obj.Get('chart.xaxispos') == 'top') {
748                                                newvalue *= -1;
749                                            }
750
751                                            obj.data[j][k] = newvalue;
752                                            var b = true;
753                                            break;
754                                        }
755                                    }
756                                   
757                                    --index;
758                                } else if (typeof(obj.data[j]) == 'number') {
759
760                                    if (index == idx) {
761                                       
762                                        if (obj.Get('chart.xaxispos') == 'top') {
763                                            newvalue *= -1;
764                                        }
765
766                                        obj.data[j] = newvalue;
767
768                                        // No need to set b
769                                        break;
770                                    }
771                                }
772                               
773                                if (b) {
774                                    break;
775                                }
776                            }
777                            ///////////////////////////////////////////////////////////////
778
779                            RGraph.Clear(canvas);
780                            obj.Draw();
781
782                            /**
783                            * Fire the onadjust event
784                            */
785                            RGraph.FireCustomEvent(obj, 'onadjust');
786                        }
787
788                        return;
789                    }
790                }
791               
792                canvas.style.cursor = 'default';
793            }
794            canvas.addEventListener('mousemove', canvas_onmousemove, false);
795            RGraph.AddEventListener(canvas.id, 'mousemove', canvas_onmousemove);
796
797
798
799            var canvas_onmousedown = function (e)
800            {
801                var obj     = e.target.__object__;
802                var id      = obj.id;
803                var canvas  = obj.canvas;
804                var context = obj.context;
805                var mouse   = RGraph.getMouseXY(e);
806                var mousex  = mouse[0];
807                var mousey  = mouse[1];
808
809                // Loop through the coords to see if the mouse position is at the top of a bar
810                for (var i=0; i<obj.coords.length; ++i) {
811                    if (mousex > obj.coords[i][0] && mousex < (obj.coords[i][0] + obj.coords[i][2])) {
812
813                        RGraph.FireCustomEvent(obj, 'onadjustbegin');
814
815                        obj.Set('chart.ymax', obj.max);
816                        RGraph.Registry.Set('chart.adjusting.bar.' + id, i);
817                       
818                        canvas_onmousemove(e);
819                    }
820                }
821            }
822            canvas.addEventListener('mousedown', canvas_onmousedown, false);
823            RGraph.AddEventListener(canvas.id, 'mousedown', canvas_onmousedown);
824
825
826
827            var canvas_onmouseup = function (e)
828            {
829                var id = e.target.__object__.id;
830               
831                if (typeof(RGraph.Registry.Get('chart.adjusting.bar.' + id)) == 'number') {
832                    RGraph.FireCustomEvent(e.target.__object__, 'onadjustend');
833                }
834               
835                RGraph.Registry.Set('chart.adjusting.bar.' + id, null);
836            }
837            canvas.addEventListener('mouseup', canvas_onmouseup, false);
838            RGraph.AddEventListener(canvas.id, 'mouseup', canvas_onmouseup);
839
840
841            var canvas_onmouseout = function (e)
842            {
843                canvas_onmouseup(e);
844            }
845            canvas.addEventListener('mouseout', canvas_onmouseout, false);
846            RGraph.AddEventListener(canvas.id, 'mouseout', canvas_onmouseout);
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861        /**
862        * The Radar chart
863        */
864        } else if (obj.type == 'radar') {
865
866
867            var canvas = obj.canvas;
868            var context = obj.context;
869           
870           
871            var canvas_onmousemove = function (e)
872            {
873                var id          = e.target.id;
874                var obj         = e.target.__object__;
875                var canvas      = obj.canvas;
876                var context     = obj.context;
877                var mouseDown   = RGraph.Registry.Get('chart.adjusting.radar.' + id);
878                var mouseCoords = RGraph.getMouseXY(e);
879
880                if (mouseDown) {
881
882                    canvas.style.cursor = 'move';
883                   
884                    var dataset = mouseDown[0];
885                    var index   = mouseDown[1];
886                    var dx      = mouseCoords[0] - obj.centerx;
887                    var dy      = mouseCoords[1] - obj.centery;
888                    var hyp     = Math.sqrt((dx * dx) + (dy * dy));
889                    var newvalue = (hyp / (obj.size / 2)) * obj.max;
890                   
891                    newvalue = Math.min(obj.max, newvalue);
892                    newvalue = Math.max(0, newvalue);
893
894                    obj.data[mouseDown[0]][mouseDown[1]] = newvalue;
895                    RGraph.Clear(canvas);
896                    obj.Draw();
897
898                    /**
899                    * Fire the onadjust event
900                    */
901                    RGraph.FireCustomEvent(obj, 'onadjust');
902
903                } else {
904
905                    // Determine if the mouse is near a point, and if so, change the pointer
906                    for (var ds = 0; ds<obj.coords.length; ds++) {
907                        for (var i=0; i<obj.coords[ds].length; ++i) {
908                           
909                            var dx = Math.abs(mouseCoords[0] - obj.coords[ds][i][0]);
910                            var dy = Math.abs(mouseCoords[1] - obj.coords[ds][i][1]);
911                            var a  = Math.atan(dy / dx);
912       
913                           
914                            var hyp = Math.sqrt((dx * dx) + (dy * dy));
915       
916                            if (hyp <= 5) {
917                                canvas.style.cursor = 'move';
918                                return;
919                            }
920                        }
921                    }
922
923                    canvas.style.cursor = 'default';
924                }
925            }
926            canvas.addEventListener('mousemove', canvas_onmousemove, false);
927            RGraph.AddEventListener(canvas.id, 'mousemove', canvas_onmousemove);
928           
929           
930            var canvas_onmousedown = function (e)
931            {
932                e = RGraph.FixEventObject(e);
933               
934                var obj         = e.target.__object__;
935                var id          = obj.id;
936                var canvas      = obj.canvas;
937                var context     = obj.context;
938                var mouseCoords = RGraph.getMouseXY(e);
939
940
941                // Determine if the mouse is near a point
942                for (var j=0; j<obj.coords.length; ++j) {
943                    for (var i=0; i<obj.coords[j].length; ++i) {
944                       
945                        var dx = Math.abs(mouseCoords[0] - obj.coords[j][i][0]);
946                        var dy = Math.abs(mouseCoords[1] - obj.coords[j][i][1]);
947                        var a  = Math.atan(dy / dx);
948   
949                       
950                        var hyp = Math.sqrt((dx * dx) + (dy * dy));
951   
952                        if (hyp <= 5) {
953                            canvas.style.cursor = 'pointer';
954                            RGraph.FireCustomEvent(obj, 'onadjustbegin');
955
956                            RGraph.Registry.Set('chart.adjusting.radar.' + id, [j, i, obj.coords[j][i][0] > obj.centerx, obj.coords[j][i][1] > obj.centery]);
957                            return;
958                        }
959                    }
960                }
961                   
962                canvas.style.cursor = 'default';
963            }
964            canvas.addEventListener('mousedown', canvas_onmousedown, false);
965            RGraph.AddEventListener(canvas.id, 'mousedown', canvas_onmousedown);
966
967
968            var canvas_onmouseup = function (e)
969            {
970                var id = e.target.id;
971
972                if (RGraph.Registry.Get('chart.adjusting.radar.' + id)) {
973                    RGraph.FireCustomEvent(e.target.__object__, 'onadjustend');
974                }
975
976                RGraph.Registry.Set('chart.adjusting.radar.' + id, null);
977                canvas.style.cursor = 'default';
978            }
979            canvas.addEventListener('mouseup', canvas_onmouseup, false);
980            RGraph.AddEventListener(canvas.id, 'mouseup', canvas_onmouseup);
981   
982   
983            var canvas_onmouseout = function (e)
984            {
985                canvas_onmouseup(e);
986            }
987            canvas.addEventListener('mouseout', canvas_onmouseout, false);
988            RGraph.AddEventListener(canvas.id, 'mouseout', canvas_onmouseout);
989       
990        /**
991        * Gantt chart
992        */
993        } else if (obj.type == 'gantt') {
994
995
996            /**
997            * The onmousedown event handler
998            */
999            var canvas_onmousedown = function (e)
1000            {
1001                var canvas      = e.target;
1002                var id          = canvas.id;
1003                var obj         = canvas.__object__;
1004                var mouseCoords = RGraph.getMouseXY(e);
1005                var mouseX      = mouseCoords[0];
1006                var mouseY      = mouseCoords[1];
1007
1008                for (var i=0; i<obj.coords.length; ++i) {
1009                   
1010                    var coordX = obj.coords[i][0];
1011                    var coordY = obj.coords[i][1];
1012                    var coordW = obj.coords[i][2];
1013                    var coordH = obj.coords[i][3];
1014
1015                    if (mouseX > coordX
1016                        && mouseX < (coordX + coordW)
1017                        && mouseY > coordY
1018                        && mouseY < (coordY + coordH)
1019                       ) {
1020                       
1021                        var mode = (mouseX >= (coordX + coordW - 5) ? 'resize' : 'move');
1022
1023                        RGraph.Registry.Set('chart.adjusting.gantt', {'index': i,'object': obj,'mousex': mouseX,'mousey': mouseY,'event_start': obj.Get('chart.events')[i][0],'event_duration': obj.Get('chart.events')[i][1],'mode': mode});
1024
1025                        RGraph.FireCustomEvent(obj, 'onadjustbegin');
1026                        return;
1027                    }
1028                }
1029            }
1030            canvas.addEventListener('mousedown', canvas_onmousedown, false);
1031            RGraph.AddEventListener(canvas.id, 'mousedown', canvas_onmousedown);
1032
1033           
1034            /**
1035            * Change the pointer
1036            */
1037            var canvas_onmousemove = function (e)
1038            {
1039                var canvas      = e.target;
1040                var id          = canvas.id;
1041                var obj         = canvas.__object__;
1042                var mouseCoords = RGraph.getMouseXY(e);
1043                var mouseX      = mouseCoords[0];
1044                var mouseY      = mouseCoords[1];
1045               
1046                for (var i=0; i<obj.coords.length; ++i) {
1047                   
1048                    var coordX = obj.coords[i][0];
1049                    var coordY = obj.coords[i][1];
1050                    var coordW = obj.coords[i][2];
1051                    var coordH = obj.coords[i][3];
1052
1053                    if (mouseX > coordX
1054                        && mouseX < (coordX + coordW)
1055                        && mouseY > coordY
1056                        && mouseY < (coordY + coordH)
1057                       ) {
1058
1059                       canvas.style.cursor = 'ew-resize';                       
1060                        return;
1061                    }
1062                }
1063               
1064                canvas.style.cursor = 'default';
1065            }
1066            canvas.addEventListener('mousemove', canvas_onmousemove, false);
1067            RGraph.AddEventListener(canvas.id, 'mousemove', canvas_onmousemove);
1068
1069
1070
1071
1072
1073
1074
1075
1076            var window_onmousemove = function (e)
1077            {
1078                var conf = RGraph.Registry.Get('chart.adjusting.gantt');
1079
1080                if (conf) {
1081
1082                    var obj         = conf['object'];
1083                    var id          = obj.id;
1084                    var index       = conf['index'];
1085                    var startX      = conf['mousex'];
1086                    var startY      = conf['mousey'];
1087                    var eventStart  = conf['event_start'];
1088                    var duration    = conf['event_duration'];
1089                    var mode        = conf['mode'];
1090                    var mouseCoords = RGraph.getMouseXY(e);
1091                    var mouseX      = mouseCoords[0];
1092                    var mouseY      = mouseCoords[1];
1093                   
1094                    RGraph.FireCustomEvent(obj, 'onadjust');
1095
1096                    if (mode == 'resize') {
1097                   
1098                        /**
1099                        * Account for the right hand gutter. Appears to be a FF bug
1100                        */
1101                        if (mouseX > (RGraph.GetWidth(obj) - obj.gutterRight)) {
1102                            mouseX = RGraph.GetWidth(obj) - obj.gutterRight;
1103                        }
1104                       
1105
1106                        var diff = ((mouseX - startX) / (RGraph.GetWidth(obj) - obj.gutterLeft - obj.gutterRight)) * obj.Get('chart.xmax');
1107                            diff = Math.round(diff);
1108                       
1109                        obj.Get('chart.events')[index][1] = duration + diff;
1110                       
1111                        if (obj.Get('chart.events')[index][1] < 0) {
1112                            obj.Get('chart.events')[index][1] = 1;
1113                        }
1114
1115                    } else {
1116
1117                        var diff = ((mouseX - startX) / (RGraph.GetWidth(obj) - obj.gutterLeft - obj.gutterRight)) * obj.Get('chart.xmax');
1118                        diff = Math.round(diff);
1119
1120                        if (   eventStart + diff > 0
1121                            && (eventStart + diff + obj.Get('chart.events')[index][1]) < obj.Get('chart.xmax')) {
1122   
1123                            obj.Get('chart.events')[index][0] = eventStart + diff;
1124                       
1125                        } else if (eventStart + diff < 0) {
1126                            obj.Get('chart.events')[index][0] = 0;
1127                       
1128                        } else if ((eventStart + diff + obj.Get('chart.events')[index][1]) > obj.Get('chart.xmax')) {
1129                            obj.Get('chart.events')[index][0] = obj.Get('chart.xmax') - obj.Get('chart.events')[index][1];
1130                        }
1131                    }
1132                   
1133                    RGraph.Redraw();
1134                    RGraph.FireCustomEvent(obj, 'onadjust');
1135                }
1136            }
1137            window.addEventListener('mousemove', window_onmousemove, false);
1138            RGraph.AddEventListener('window_' + canvas.id, 'mousemove', window_onmousemove);
1139
1140
1141
1142
1143
1144
1145
1146
1147       
1148       
1149            var window_onmouseup = function (e)
1150            {
1151                if (RGraph.Registry.Get('chart.adjusting.gantt')) {
1152               
1153                    var conf = RGraph.Registry.Get('chart.adjusting.gantt');
1154                    var obj  = conf['object'];
1155                    var id   = obj.id;
1156
1157                    RGraph.FireCustomEvent(obj, 'onadjustend');
1158                    RGraph.Registry.Set('chart.adjusting.gantt', null);
1159                }
1160            }
1161            window.addEventListener('mouseup', window_onmouseup, false);
1162            RGraph.AddEventListener('window_' + canvas.id, 'mouseup', window_onmouseup);
1163        }
1164    }
Note: See TracBrowser for help on using the repository browser.