Coordinates X,Y and crosshair

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
cajimene
Posts: 2
Joined: Thu Jan 18, 2007 1:32 pm

Coordinates X,Y and crosshair

Post by cajimene » Fri Jan 19, 2007 10:17 am

Hi,

Sorry, my english is very bad.

I have a problem drawing a crosshair because the coordinate Y isn't good.
I see others topics related, but not work fine.

Code: Select all

Point2D p = panelGrafica.translateScreenToJava2D(new Point(x, y));
        
        XYPlot plot = (XYPlot) panelGrafica.getChart().getPlot();
        ChartRenderingInfo info = panelGrafica.getChartRenderingInfo();
        Rectangle2D dataArea = info.getPlotInfo().getDataArea();
        
        ValueAxis domainAxis = plot.getDomainAxis(); 
        RectangleEdge domainAxisEdge = plot.getDomainAxisEdge(); 
        ValueAxis rangeAxis = plot.getRangeAxis(); 
        RectangleEdge rangeAxisEdge = plot.getRangeAxisEdge(); 

        double chartX = domainAxis.java2DToValue(p.getX(), dataArea, 
                domainAxisEdge); 
        double chartY = rangeAxis.java2DToValue(p.getY(), dataArea, 
                rangeAxisEdge); 
        System.out.println("Chart: x = " + chartX + ", y = " + chartY); 
I see a topic where david.gilbert say what there is a bug, how i can do this fine?

thanks

Carlos.

fookchen
Posts: 1
Joined: Fri Jan 26, 2007 3:04 pm

Post by fookchen » Fri Jan 26, 2007 3:17 pm

Hi,

I managed to add a quick fix to the XYPlot.java, since I do not have the time to trace through the rest of the code to find where the error occured exactly.

For some reasons which I am unsure, the Data Area is unstable, (looks likely that it is modified by some other parts of the code). What I have done is to add a public Rectangle2D variable to capture the value of dataArea after the XYPlot's draw method is called. It is somewhat inelegant, but it works :).

After recompiling the jar, you should be able to do something like:
double chartY = rangeAxis.java2DToValue(p.getY(), xyplot.dataAreaFixed, rangeAxisEdge);

Modifications are as follow:
//Add a new public variable dataAreaFixed
public Rectangle2D dataAreaFixed = null;

//within the function draw(Graphics2D g2, Rectangle2D area, Point2D anchor, PlotState parentState, PlotRenderingInfo info);
//add the following line just before -> drawBackground(g2, dataArea);
dataAreaFixed = dataArea; //obtain the dataArea whenever draw is called

Hope that helps,

regards,
Eddie

cajimene
Posts: 2
Joined: Thu Jan 18, 2007 1:32 pm

Post by cajimene » Fri Jan 26, 2007 3:20 pm

Thanks a lot,

Carlos.

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Fri Jan 26, 2007 4:36 pm

The dataArea in the ChartRenderingInfo is the area into which the data is drawn, but if the ChartPanel is doing any scaling (which sometimes is the case) then that won't necessarily correspond to where the data area appears on the panel.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

wzwei28
Posts: 105
Joined: Wed Mar 16, 2005 9:17 am
Location: Malaysia, Earth
Contact:

Post by wzwei28 » Fri Oct 12, 2007 3:35 pm

Simple algorithm/patch/source code available at following link:
http://www.jfree.org/phpBB2/viewtopic.php?p=64888#64888

Locked