JFreeChart交互式图表编辑:将鼠标坐标转换为系列值

发布于 2024-12-01 11:48:03 字数 907 浏览 1 评论 0 原文

我有一个用 JFreeChart 构建的 XYLineChart。我需要,给定该图表和 ChartMouseEvent,检索最接近鼠标单击点的显示系列的 X 值。

感谢 上一篇文章 我已经能够检索灰色的偏移量使用以下方法获取图表(图像中绿点的坐标)及其尺寸:

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

我还知道显示系列的最大 X 值:

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

XYLineChart

现在的问题是,为了将鼠标坐标(点)转换为图表中的相应值,我需要知道如何许多单位(双)对应屏幕上的一个像素。 不幸的是,最大 X 值(本例中为 60)和灰色图表宽度(看大蓝线)之间存在差距,因此我无法实现完美的转换。

然后我有两个问题:

  1. 如何精确计算最后显示的x值与整个灰色图表之间的像素差距? (大蓝线长度)
  2. 我做错了什么吗?有没有更简单的方法来实现这个目标,可能避免所有这些微积分?我是 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

XYLineChart

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:

  1. How to calculate exactly the gap in pixel between the last displayed x value and the whole grey chart ? ( big blue line length)
  2. 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

铁轨上的流浪者 2024-12-08 11:48:03

回顾一下这个示例,您可以从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());
    }
});

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.

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());
    }
});
落花随流水 2024-12-08 11:48:03
  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());

我们用它来从鼠标坐标中获取数据坐标。

  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());

We have used this to get the data coordinates from the mouse coordinates.

风流物 2024-12-08 11:48:03

看看这个 JFreeChart 获取鼠标坐标。如果您知道坐标,则可以从绘图中获取 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();

这样就可以了。

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:

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();

That should do it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文