Zoom In and Zoom out functionality on a button click on JfreeChart line graph?
Asked Answered
M

1

2

Basically i want the line graph to be zoomed in and Zoomed out(total 4 buttons,2 for X-axis(Zoom in and Zoom out) and other two for Y-axis) on a button click along any axis like if the graph drawn on negative x-axis and negative Y-axis area ,depending on data points then on button click the graph should be Zoomed in and Zoomed out along that negative x-axis or negative Y-axis based on button click.

How can i achieve this ?Any sample code with detail Explanation is much helpful!!

 private JButton createZoom()
 {
        final JButton auto = new JButton("ZOOMIN");
        auto.setActionCommand("ZOOM_IN_DOMAIN");
        auto.addActionListener(new ChartPanel(chart));
        return auto;
    }
Mohamedmohammad answered 26/12, 2016 at 20:21 Comment(0)
G
4

Each button's Action implementation should invoke the corresponding method used by ChartPanel to create it's popup menu of zoom commands. The implementation of actionPerformed() is a convenient guide to the available zooming functionality. For example, the ZOOM_IN_DOMAIN_COMMAND is handled by invoking zoomInDomain(). Based on this example, a typical Zoom X handler relative to the origin is shown below:

private JButton createZoom() {
    final JButton zoomX = new JButton(new AbstractAction("Zoom X") {

        @Override
        public void actionPerformed(ActionEvent e) {
            chartPanel.zoomInDomain(0, 0);
        }
    });
    return zoomX;
}

If the default zoomPoint is sufficient, you can use the chart panel's implementation:

private JButton createZoom() {
    final JButton zoomX = new JButton("Zoom X");
    zoomX.setActionCommand(ChartPanel.ZOOM_IN_DOMAIN_COMMAND);
    zoomX.addActionListener(chartPanel);
    return zoomX;
}

zoomed image

In contrast, the createZoom() method in the original example shows how to evoke the ChartPanel method restoreAutoBounds(), which restores the auto-range calculation on both axes.

image

Gossipry answered 26/12, 2016 at 23:26 Comment(12)
In the example reference you provided "chartPanel.setMouseWheelEnabled(true);" this line is causing the graph to ZoomIn and ZoomOut along the x axis, y axis and both. but i want that functionality on a button click like if i press the button "xZoom" zooming should be done along x-axis only.similarly on pressing the "yZoom" button zooming should be done along the y-axis only.and when "zoom" button is pressed zooming should be done w.r.t both the axes. This is my requirement can you please help with some sample code on how to do this.Mohamedmohammad
I've added a specific example above; if you have problems, please edit your question to include a minimal reproducible example that shows your revised approach.Gossipry
Thanks for edited answer :) But One of the Link was broken out of the 2 you have provided.Mohamedmohammad
I think this one "ZOOM_IN_HORIZONTAL_ACTION_COMMAND" will serve my purpose but i don't understand where to use it, means with what method, i Should use.Any clue ?Please help..Mohamedmohammad
If you mean ZOOM_IN_DOMAIN_COMMAND , the createZoom() method above replaces the same method in the complete example. If this does this not work for you, please edit your question to include a minimal reproducible example that shows your revised approach.Gossipry
I have updated My question with the sample i tried before asking the question. I made ChartPanel to listen for the zoomIn action. but am getting Null pointer Exception.I went this way bcz i am confused of what co-ordinate points i could give to the method you mentioned to achieve my functionality.you just given (0,0) as the co-ordinate points for ZoomInDomain() method. but i didn't understand what points i should pass to achieve my requirement.Mohamedmohammad
The zooming is "centered about the given coordinate on the screen." I used the origin to show the effect; your values will depend on your design. Why don't you just use the built-in popup?Gossipry
No i can't use the popmenu as per my requirement button i should use. Can you just tell me why am getting the null pointer exception after i made the ChartPanel as listener?What is the wrong in my approach?Mohamedmohammad
Sorry, I can't reproduce the NPE, but I see that button.addActionListener(chartPanel) works, too.Gossipry
Sorry i am troubling you for a long time.Thanks for being so patient.ya its working fine for me as well.auto.setActionCommand(chartPanel.ZOOM_IN_DOMAIN_COMMAND); auto.addActionListener(chartPanel); setting this way zooming happening only along x-axis. but can you tell what can i do to make the zooming along the y-axis as well? also how can i control this zooming scale?Mohamedmohammad
Some ideas on your new questions: 1) use the ZOOM_IN_RANGE_COMMAND; 2) use the corresponding zoom factor method. If you have problems, please open a new question which includes a minimal reproducible example that shows your revised approach.Gossipry
Please have a look at this question #41529891Mohamedmohammad

© 2022 - 2024 — McMap. All rights reserved.