2011-08-26 37 views
2

我有用JFreeChart构建的XYLineChart。鉴于该图表和ChartMouseEvent,我需要检索最靠近点击鼠标点的displayde系列的X值。JFreeChart交互式图表编辑:将鼠标坐标转换为系列值

多亏了previous post我已经能够获取的灰度图的偏移量(图像中的绿色点的坐标)及其以下方法维度:

Rectangle2D greyChartArea = chartPanel.getChartRenderingInfo().getPlotInfo().getDataArea(); 

我也知道了所显示的系列的最大X值:

double maxXValue = seriesCollection.getDomainUpperBound(true); //where seriesCollection is an XYSeriesCollection object 

XYLineChart

现在的问题在于,用于将鼠标coordin吃(点)到图表中的相应值,我需要知道多少单位(双)与屏幕上的像素相对应。 不幸的是,最大X值(在这种情况下为60)和灰色图表宽度(看起来很大的蓝色线)之间存在差距,所以我无法实现完美的转换。

然后我有两个问题:

  1. 如何精确计算像素最后显示的x值和整个灰色图表之间的差距? (大蓝线长度)
  2. 我做错了什么?有没有更简单的方法来实现这个目标,可能避免所有这些微积分?我是JFreeChart新手,该库的文档还不够,所以也许我错过了一些可以帮助我的功能。
+0

http://meta.stackexchange.com/questions/99734/how-do-i-create-a-screenshot-to-illustrate-a-post +1 – mKorbel

+0

@mKorbel:你曾经使用了JFreeChart ?你有什么想法吗? – Heisenbug

+0

从来没有,我认为这只是垃圾区(在这个论坛上),因为我知道对不起的人... – mKorbel

回答

2
final XYPlot plot = getChart().getXYPlot(); 
    final ValueAxis domainAxis = plot.getDomainAxis(); 
    final ValueAxis rangeAxis = plot.getRangeAxis(); 
    final Rectangle2D plotRectangle = SWTUtils.toAwtRectangle(getScreenDataArea()); 
    final double chartX = domainAxis.java2DToValue(relativeX, plotRectangle, plot.getDomainAxisEdge()); 
    final double chartY = rangeAxis.java2DToValue(relativeY, plotRectangle, plot.getRangeAxisEdge()); 

我们已经使用它从鼠标坐标获取数据坐标。

+0

非常感谢,很有效! – Heisenbug

1

看看这个JFreeChart get mouse coordinates。如果你知道坐标,你可以从你的情节x和y坐标,并从轴系得到相应的值:

JFreeChart chart = yourChart; 
Rectangle2D greyChartArea = chartPanel.getChartRenderingInfo().getPlotInfo().getDataArea(); 
XYPlot plot = (XYPlot) chart.getPlot(); 

double valueX = ((NumberAxis) plot.getRangeAxis()).java2DToValue(chartY,plot.getRangeAxisEdge(); 
double valueY = ((NumberAxis) plot.getDomainAxis()).java2DToValue(chartX,plot.getDomainAxisEdge(); 

应该这样做。

+0

+1:也谢谢你。我将Kathir的答案设置为正确的,因为你已经将valueX和valueY颠倒过来了。没有什么个人的,但我不得不选择一个答案。再次感谢,你们都非常乐于助人! – Heisenbug

+0

没问题的队友;) – Jes

+0

这是不可建立的。 –

4

回顾这个example,您可以从ChartProgressListener中的十字线值中获得模型坐标。十字线不一定是可见的。

chartPanel.getChart().addProgressListener(new ChartProgressListener() { 

    @Override 
    public void chartProgress(ChartProgressEvent e) { 
     XYPlot xyPlot = (XYPlot) chartPanel.getChart().getPlot(); 
     System.out.println(e.getType() 
      + ": " + xyPlot.getDomainCrosshairValue() 
      + ", " + xyPlot.getRangeCrosshairValue()); 
    } 
}); 
+0

谢谢..我也试试。 – Heisenbug

+0

我在说什么+1 – mKorbel

+0

伟大而简单的想法。 –