开发者

Jfreechart get Mouse Co-ordinates

I have been trying to get current Mouse Co-ordinates in a JfreeChart and found that the following solution was working partially

JFreeChart get mouse coordinates

I have been using OHLC Dataset to draw the chart and while I could get the RangeAxis properly (meaning in the subplot values), I couldn't make anything开发者_如何学Go out of the value recieved for the X-Axis from the above example.

I am sure that I am receiving the values in some other format (not the displayed date format), anyone could point out what I am doing wrong?


Got it solved after few hours of experiment. Here is the code for a complete MouseMotionListener. Just added this to the chartPanel and voila! - it works! The chartY returns proper value of Y-Axis and the dateString returns complete date. Tried it in an OHLC Chart and seems proper.

 MouseMotionListener mouselisten = new MouseMotionListener() {

    public void mouseDragged(MouseEvent e) {
        //
    }

    public void mouseMoved(MouseEvent e) {
        Point2D p = e.getPoint();
        Rectangle2D plotArea = chartPanel.getScreenDataArea();
        XYPlot plot = (XYPlot) chart.getPlot(); // your plot
        double chartX = plot.getDomainAxis().java2DToValue(p.getX(), plotArea, plot.getDomainAxisEdge());
        double chartY = plot.getRangeAxis().java2DToValue(p.getY(), plotArea, plot.getRangeAxisEdge());

        DecimalFormat dfT   = new DecimalFormat("00");
        GregorianCalendar gc = new GregorianCalendar();
        long lDte = (long)chartX;
        Date dtXX = new Date(lDte);
        gc.setTime(dtXX);
        String sDD  = dfT.format(Double.valueOf(String.valueOf(gc.get(GregorianCalendar.DAY_OF_MONTH))));
        String sMM  = dfT.format(Double.valueOf(String.valueOf(gc.get(GregorianCalendar.MONTH)+1)));
        String sYY  = dfT.format(Double.valueOf(String.valueOf(gc.get(GregorianCalendar.YEAR))));
        String dateString = sDD +"/"+ sMM +"/"+ sYY;


    }
};
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜