How to use mouse wheel to zoom in(out) ?

Discussion about JFreeChart related to stockmarket charts.
Locked
Tony Young
Posts: 14
Joined: Fri Nov 02, 2007 8:21 am

How to use mouse wheel to zoom in(out) ?

Post by Tony Young » Thu Nov 08, 2007 3:10 am

Hi:
JFreeChart default to zoom in(out) when the mouse is dragged, but I want to use mouse wheel to control this, so I read the source code of ChartPanel class, and copyed some methods to my class,there are

getScreenDataArea()
translateScreenToJava2D(Point screenPoint)
zoomInBoth(double x, double y)
zoomInDomain(double x, double y)
zoomInRange(double x, double y)
zoomOutBoth(double x, double y)
zoomOutDomain(double x, double y)
zoomOutRange(double x, double y)

Then I add MouseWheelListener to the panel, write class mouseWheelHandle to handle the event

Code: Select all

private class mouseWheelHandle implements MouseWheelListener 
    { 
        public void mouseWheelMoved(MouseWheelEvent e) 
        { 
                zoomPoint=e.getPoint();

        	if(e.getWheelRotation()>0){
        		zoomInBoth(zoomPoint.getX(), zoomPoint.getY());}
        	else if(e.getWheelRotation()<0){
        		zoomOutBoth(zoomPoint.getX(), zoomPoint.getY());
        	}

    } 
But it didn't work! :evil:
I don't know what to do next, would someone give me some help?

LucaSirri
Posts: 15
Joined: Tue Sep 16, 2008 11:48 am

Post by LucaSirri » Mon Oct 27, 2008 12:09 pm

We use this code

Code: Select all

public class DefaultChartPanel extends ChartPanel implements MouseWheelListener
....
    public void mouseWheelMoved(MouseWheelEvent e) {
        if (e.getScrollType() != MouseWheelEvent.WHEEL_UNIT_SCROLL) return;
        if (e.getWheelRotation()< 0) increaseZoom(getFocusedChart(), true);
        else        	               decreaseZoom(getFocusedChart(), true);
    }
    
    public synchronized void increaseZoom(JComponent chart, boolean saveAction){
        ChartPanel ch = (ChartPanel)chart;
        zoomChartAxis(ch, true);
    }  
    
    public synchronized void decreaseZoom(JComponent chart, boolean saveAction){
        ChartPanel ch = (ChartPanel)chart;
        zoomChartAxis(ch, false);
    }  
    
    private void zoomChartAxis(ChartPanel chartP, boolean increase){    	    	
        int width = chartP.getMaximumDrawWidth() - chartP.getMinimumDrawWidth();
        int height = chartP.getMaximumDrawHeight() - chartP.getMinimumDrawWidth();        
        if(increase){
        	chartP.zoomInBoth(width/2, height/2);
        }else{
        	chartP.zoomOutBoth(width/2, height/2);
        }

    } 

bztom33
Posts: 35
Joined: Tue Jul 11, 2006 10:35 pm

Post by bztom33 » Tue Oct 28, 2008 12:41 am

I modified the original code provided by LucaSirri as follows...

Code: Select all

 
addListener(chartPanel);
...


protected void  addListener(ChartPanel chartPanel)) {
 chartPanel.addMouseWheelListener(new MouseWheelListener() {

    public void mouseWheelMoved(MouseWheelEvent e) {
        if (e.getScrollType() != MouseWheelEvent.WHEEL_UNIT_SCROLL) return;
        if (e.getWheelRotation()< 0) increaseZoom((ChartPanel)e.getComponent(), true);
        else                          decreaseZoom((ChartPanel)e.getComponent(), true);
    }
   
    public synchronized void increaseZoom(JComponent chart, boolean saveAction){
        ChartPanel ch = (ChartPanel)chart;
        zoomChartAxis(ch, true);
    } 
   
    public synchronized void decreaseZoom(JComponent chart, boolean saveAction){
        ChartPanel ch = (ChartPanel)chart;
        zoomChartAxis(ch, false);
    } 
   
    private void zoomChartAxis(ChartPanel chartP, boolean increase){              
        int width = chartP.getMaximumDrawWidth() - chartP.getMinimumDrawWidth();
        int height = chartP.getMaximumDrawHeight() - chartP.getMinimumDrawWidth();       
        if(increase){
           chartP.zoomInBoth(width/2, height/2);
        }else{
           chartP.zoomOutBoth(width/2, height/2);
        }

    }

tukosito
Posts: 3
Joined: Mon Aug 19, 2013 10:34 am
antibot: No, of course not.

Re: How to use mouse wheel to zoom in(out) ?

Post by tukosito » Mon Aug 19, 2013 10:38 am

Since 1.0.13, ChartPanel has an setMouseWheelEnabled(boolean) property that enables the wheel.

PS: I know that the thread is old, but it is one of the first results in Google, so it may be interesting.

Locked