Jfreechart获取鼠标坐标 [英] Jfreechart get Mouse Co-ordinates
问题描述
我一直在尝试在JfreeChart中获取当前的鼠标坐标,发现以下解决方案部分起作用
I have been trying to get current Mouse Co-ordinates in a JfreeChart and found that the following solution was working partially
我一直在使用OHLC数据集绘制图表,尽管我可以正确地获得RangeAxis(在子图值中表示),但是我不能从以上示例中为X轴获得的值中做出任何事情.
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 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?
推荐答案
经过几个小时的实验,解决了.这是完整的MouseMotionListener的代码.刚刚将其添加到chartPanel中,瞧! - 有用! chartY返回Y轴的正确值,dateString返回完整日期.在OHLC图表中进行了尝试,看起来很合适.
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;
}
};
这篇关于Jfreechart获取鼠标坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!