我有一个用 JFreeChart 构建的 XYLineChart。我需要,给定该图表和 ChartMouseEvent,检索最接近鼠标单击点的显示系列的 X 值。
感谢 上一篇文章 我已经能够检索灰色的偏移量使用以下方法获取图表(图像中绿点的坐标)及其尺寸:
Rectangle2D greyChartArea = chartPanel.getChartRenderingInfo().getPlotInfo().getDataArea();
我还知道显示系列的最大 X 值:
double maxXValue = seriesCollection.getDomainUpperBound(true); //where seriesCollection is an XYSeriesCollection object
现在的问题是,为了将鼠标坐标(点)转换为图表中的相应值,我需要知道如何许多单位(双)对应屏幕上的一个像素。
不幸的是,最大 X 值(本例中为 60)和灰色图表宽度(看大蓝线)之间存在差距,因此我无法实现完美的转换。
然后我有两个问题:
- 如何精确计算最后显示的x值与整个灰色图表之间的像素差距? (大蓝线长度)
- 我做错了什么吗?有没有更简单的方法来实现这个目标,可能避免所有这些微积分?我是 JFreeChart 新手,该库的文档还不够,所以也许我缺少一些可以帮助我的功能。
I have an XYLineChart built with JFreeChart. I need, given that chart and a ChartMouseEvent, retrieve the X value of the displayde series closest to the point where the mouse has been clicked.
Thanks to a previous post I have been able to retrieve the offset of the grey chart (the coordinates of the green point in the image) and its dimension with the following method:
Rectangle2D greyChartArea = chartPanel.getChartRenderingInfo().getPlotInfo().getDataArea();
I also know the max X values of the displayed serie:
double maxXValue = seriesCollection.getDomainUpperBound(true); //where seriesCollection is an XYSeriesCollection object
Now the problem is that for converting a mouse coordinate (Point) into a corresponding value in the chart, I need to know at how many units (double) correspond a pixel on the screen.
Unfortunately there is a gap between the maximum X value (60 in this case) and the grey chart width (look at the big blue line), so I can't achieve a perfect conversion.
Then I have two questions:
- How to calculate exactly the gap in pixel between the last displayed x value and the whole grey chart ? ( big blue line length)
- Am I doing something wrong? Is there any simpler way to achieve this goals, possibly avoiding all this calculus? I'm a JFreeChart newbie and the documentation of that library isn't enough, so maybe I'm missing some features that could help me.
发布评论
评论(3)
回顾一下这个示例,您可以从
ChartProgressListener
。十字线不一定是可见的。Recalling this example, you can obtain model coordinates from the cross-hair values in a
ChartProgressListener
. The cross-hairs don't have to be visible.我们用它来从鼠标坐标中获取数据坐标。
We have used this to get the data coordinates from the mouse coordinates.
看看这个 JFreeChart 获取鼠标坐标。如果您知道坐标,则可以从绘图中获取 x 和 y 坐标,并从轴中获取相应的值:
这样就可以了。
Have a look at this JFreeChart get mouse coordinates. If you know the coordinate, you can take the x and y-coordinates from your plot, and get the corresponding values from the axises:
That should do it.