What is wrong with chartPanel.getScreenDataArea()?

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
patb
Posts: 11
Joined: Sun Aug 03, 2008 7:55 pm

What is wrong with chartPanel.getScreenDataArea()?

Post by patb » Mon Apr 26, 2010 5:30 pm

Hello,

I just wanted to calculate the Plot value according to the mouse coordinates in a XYPlot. A task, which has been solved a lot of times when one googles for it. I also managed to calculate the right value, although there is a question left behind.

This is how I calculate the plot value (vertical x-axis), the code just prints the value to the console on a mouse click on the chart:

Code: Select all

chartPanel.addMouseListener( new MouseAdapter() {
  @Override public void mouseClicked( MouseEvent e ) {
    
    Point2D p = chartPanel.translateScreenToJava2D(e.getPoint());
    
    // (*)
    Rectangle2D plotArea = chartPanel.getChartRenderingInfo().getPlotInfo().getDataArea();
    
    XYPlot plot = (XYPlot)chartPanel.getChart().getPlot();
    double yValue = plot.getRangeAxis().java2DToValue(p.getY(), plotArea, plot.getRangeAxisEdge());
    System.out.println("yValue: " + yValue);
  }
} );
This seems to work. But, in a lot of examples, I saw that instead of asking the plotInfo for the dataArea (see line maked with (*)), the "getScreenDataArea()" of the "chartPanel" has been used:

Code: Select all

// Rectangle2D plotArea = chartPanel.getChartRenderingInfo().getPlotInfo().getDataArea();
Rectangle2D plotArea = chartPanel.getScreenDataArea();
This also works to some degree - but the real curious thing with this was, when I resized my window (in which my ChartPanel is) to a much greater height, so the plot also got very high (let's say ~ 800 pixels), the y-Value calculation failed! I'm not sure which the height-limit was, I at some heigh, the calculation yield a wrong value.

So my question is, does anyone know, why the "chartPanel.getChartRenderingInfo().getPlotInfo().getDataArea()" works, while the use of "chartPanel.getScreenDataArea()" does not?

(If you miss any information about my plot/chart, please say so and I will post some more info.)


Thanks a lot for your help!


Best Regards,
Timo

gengar
Posts: 26
Joined: Thu Mar 18, 2010 12:28 am
antibot: No, of course not.

Re: What is wrong with chartPanel.getScreenDataArea()?

Post by gengar » Mon Apr 26, 2010 7:02 pm

How did the calculation fail? Did it return a negative number?

skunk
Posts: 1087
Joined: Thu Jun 02, 2005 10:14 pm
Location: Brisbane, Australia

Re: What is wrong with chartPanel.getScreenDataArea()?

Post by skunk » Mon Apr 26, 2010 7:06 pm

Read the source for org.jfree.chart.ChartPanel
I suspect "both methods" will work as you expect if you increase the maximum draw height and width using these methods

Code: Select all

public void setMaximumDrawWidth(int width)
public void setMaximumDrawHeight(int height)

patb
Posts: 11
Joined: Sun Aug 03, 2008 7:55 pm

Re: What is wrong with chartPanel.getScreenDataArea()?

Post by patb » Mon Apr 26, 2010 10:27 pm

Hello,

thanks a lot for your replies.
gengar wrote:How did the calculation fail? Did it return a negative number?
Let's say I click on the plot where the y-value is "25". Then my code really returns a 25. When I now change the height of the plot enough (maybe 800 pixels high) and click again where the "25" is in the plot, my code returns a ~ 120. But only with "chartPanel.getScreenDataArea()", of course.
skunk wrote:Read the source for org.jfree.chart.ChartPanel
I suspect "both methods" will work as you expect if you increase the maximum draw height and width using these methods

Code: Select all

public void setMaximumDrawWidth(int width)
public void setMaximumDrawHeight(int height)
I did not know that there were those properties. I read the docs and I now understand them - what I do not understand is, why the calculation is wrong when the plot is beeing scaled, because the "chartPanel.getScreenDataArea()" apparently takes the scale into account (which can be seen in the sources of ChartPanel).

Locked