How to get a mouse click domain value ?

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
zinZ
Posts: 19
Joined: Mon Jul 09, 2007 2:18 pm

How to get a mouse click domain value ?

Post by zinZ » Tue Jul 17, 2007 8:13 am

Hello,

I would find a method that get the domaine value (on a TimeSeriesChart) of a mouse click.

I find that code on a former thread but I don't find it in the librairy.
You can add a ChartMouseListner to achieve that. You can capture the anchor point (mouse click) and the distance (mouse move). There are some methods in the Axis classes that let you translate screen coordinates do data coordinates.


Code:

Code: Select all

    /** 
     * Returns the domain data value given a screen coordinate. 
     * @param x the x-coordinate of the screen 
     * @return the corresponding domain data value 
     */ 
    private double getDomainDataValue(final int x) { 
        // get the current domain value 
        XYPlot plot = chart.getXYPlot(); 
        double xx = plot.getDomainAxis().java2DToValue( 
                x, getScreenDataArea(), plot.getDomainAxisEdge() 
            ); 
        return xx; 
    } 
    
    /** 
     * Returns the X screen coordinate given a  domain data value. 
     * @param xDataValue the domain data value 
     * @return the corresponding x-coordinate of the screen 
     */ 
    private int getXScreenCoordinate(double xDataValue) { 
        // get the current domain value 
        XYPlot plot = chart.getXYPlot(); 
        double xx = plot.getDomainAxis().valueToJava2D( 
                xDataValue, getScreenDataArea(), plot.getDomainAxisEdge() 
            ); 
        
        // apply scale if any and return (SCALE MESS UP EVERYTHING) 
        //return translateJava2DToScreen(new Point2D.Double(xx,0)).x; 
        return (int) Math.round(xx); 
    } 

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 » Tue Jul 17, 2007 9:06 am

I don't know where you found that code, so I won't comment on it. MouseListenerDemo4.java is an example that shows how to calculate the x and y data values from a mouse click.
David Gilbert
JFreeChart Project Leader

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

zinZ
Posts: 19
Joined: Mon Jul 09, 2007 2:18 pm

Post by zinZ » Tue Jul 17, 2007 9:54 am

Thank you for your reply David.

I found this code on this forum in the subject : Crosshairs, tracelines, markers
But I thank it was code.

I didn't see the source of MouseListenerDemo because they are not in the Demo. I am working with it.

I will use the method java2DToValue, and some methods of CrosshairState to determinate the clicked DomainAxis value.

Or perhaps there is a better way.

Locked