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;
    }
JAVA
  • 524
  • 11
  • 23

1 Answers1

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

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • 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. – JAVA Dec 27 '16 at 09:23
  • I've added a specific example above; if you have problems, please edit your question to include a [mcve] that shows your revised approach. – trashgod Dec 27 '16 at 12:31
  • Thanks for edited answer :) But One of the Link was broken out of the 2 you have provided. – JAVA Dec 28 '16 at 01:22
  • 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.. – JAVA Dec 28 '16 at 04:44
  • If you mean `ZOOM_IN_DOMAIN_COMMAND `, the `createZoom()` method above replaces the same method in the complete [example](http://stackoverflow.com/a/5522583/230513). If this does this not work for you, please edit your question to include a [mcve] that shows your revised approach. – trashgod Dec 28 '16 at 10:21
  • 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. – JAVA Dec 30 '16 at 05:59
  • 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? – trashgod Dec 30 '16 at 10:59
  • 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? – JAVA Dec 30 '16 at 12:21
  • Sorry, I can't reproduce the NPE, but I see that `button.addActionListener(chartPanel)` works, too. – trashgod Dec 30 '16 at 20:02
  • 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? – JAVA Dec 31 '16 at 03:07
  • 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 [mcve] that shows your revised approach. – trashgod Dec 31 '16 at 09:50
  • Please have a look at this question http://stackoverflow.com/questions/41529391/abnormal-behaviour-of-the-zoom-in-and-zoom-out-functionality-of-the-jfreechart – JAVA Jan 08 '17 at 04:14