Disabling default mouse behaviour

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
avanidhar
Posts: 16
Joined: Wed Jun 11, 2008 10:47 pm

Disabling default mouse behaviour

Post by avanidhar » Wed Oct 22, 2008 6:14 pm

Dear All,

I am using mouseclicks to zoom into my chart. When i do a regular click with an isShiftDown() function call, the zoom in functionality works fine. But when i try to zoom out using a shift + right click combination, it does not happen. This is my sample code

Code: Select all

public void mouseClicked(MouseEvent e) {
                       
            if(e.isShiftDown())
            {
                this.chartPanel.setZoomAroundAnchor(true);
                this.chartPanel.setZoomInFactor(.5);
                this.chartPanel.zoomInRange(e.getX(), e.getY());
                this.chartPanel.zoomInDomain(e.getX(),e.getY());
                
            }
            else if(e.isMetaDown()&&e.isShiftDown()){
            
                this.chartPanel.setZoomAroundAnchor(true);
                this.chartPanel.setZoomOutFactor(.1);
                this.chartPanel.zoomOutRange(e.getX(), e.getY());
                this.chartPanel.zoomOutDomain(e.getX(),e.getY());
            }
        }
Could you guide me on how to get over this problem? How do i override default behaviour? I am new to JfreeChart and your inputs would be very valuable.

Avanidhar

RichardWest
Posts: 844
Joined: Fri Oct 13, 2006 9:29 pm
Location: Sunnyvale, CA

Post by RichardWest » Wed Oct 22, 2008 6:51 pm

This seems to be a simple code ordering problem. The if-condition is less stringent than the else-if-condition, so an event that would meet the condition of the else-if-statement would also meet the condition of the if-statement. Try changing the order of the conditions or rewrite the code to:

Code: Select all

public void mouseClicked(MouseEvent e) {
    if (e.isShiftDown()) {
        if (e.isMetaDown()) {
            this.chartPanel.setZoomAroundAnchor(true); 
            this.chartPanel.setZoomOutFactor(.1); 
            this.chartPanel.zoomOutRange(e.getX(), e.getY()); 
            this.chartPanel.zoomOutDomain(e.getX(),e.getY()); 
        }
        else {
            this.chartPanel.setZoomAroundAnchor(true); 
            this.chartPanel.setZoomInFactor(.5); 
            this.chartPanel.zoomInRange(e.getX(), e.getY()); 
            this.chartPanel.zoomInDomain(e.getX(),e.getY()); 
        }
    }
}
Richard West
Design Engineer II
Advanced Micro Devices
Sunnyvale, CA

avanidhar
Posts: 16
Joined: Wed Jun 11, 2008 10:47 pm

Post by avanidhar » Wed Oct 22, 2008 7:51 pm

I tried re-ordering the code. The zoom works fine as usual. But the zoomout gives the same problem. When i do a right click with shift enabled, the zoomout fails and the default menu appears on right click. Could this be solved if I could set the default mouse behaviour to null in some way?

I tried to add zoom-in behaviour to shift+Click and zoomout behaviour to control+Click. Heres the sample code for that

Code: Select all

if(e.isShiftDown())
            {
                this.chartPanel.setZoomAroundAnchor(true);
                this.chartPanel.setZoomInFactor(.5);
                this.chartPanel.zoomInBoth(e.getX(), e.getY());
                
            }
       else if(e.isControlDown()){
            
                
                this.chartPanel.setZoomInFactor(0.2);
                this.chartPanel.zoomOutBoth(e.getX(), e.getY());
            }
Note here that i have used setZoomInFactor for controldown, even though i am zooming out. When i use setZoomOutFactor(0.2) for this, the zoomout fails. Both zoomin and zoomout seem to work by setting the ZoomInFactor. Is this usual?

RichardWest
Posts: 844
Joined: Fri Oct 13, 2006 9:29 pm
Location: Sunnyvale, CA

Post by RichardWest » Wed Oct 22, 2008 11:27 pm

avanidhar wrote:I tried re-ordering the code. The zoom works fine as usual. But the zoomout gives the same problem.
The code needed to be reordered (the error was so blatent that I missed the subtler error). The problem is that isMetaDown() && isShiftDown() is not the equivalent of Shift+right-click. Using the isMetaDown() method does not determine if a click is a right click unless you are using a very strange environment. You should probably use isPopupTrigger() instead.
avanidhar wrote:Note here that i have used setZoomInFactor for controldown, even though i am zooming out. When i use setZoomOutFactor(0.2) for this, the zoomout fails. Both zoomin and zoomout seem to work by setting the ZoomInFactor. Is this usual?
Zooming-out and zooming-in use the same basic zoom method based on a factor. You can zoom-out with a factor <1.0, but that is effectively a zoom-in. Conversely, you can zoom-in with a factor >1.0, but that is effectively a zoom-out. The default factors for zoom-in and zoom-out are 0.5 and 2.0 (half-size and double-size), respectively.

Since you never set the zoom-out factor in your updated code, the default value of 2.0 is used. You can prove this to yourself by changing the zoom-in factor in the else-if-statement and see that it has no affect on how the plot resizes. You can also see how this works by looking at the ChartPanel source code.
Richard West
Design Engineer II
Advanced Micro Devices
Sunnyvale, CA

Locked