java - 如何将本地坐标转换为JavaFX图表坐标

标签 java javafx charts

对于业务应用程序,要求用户能够选择 JavaFX LineChart 的特定部分。目前,我已经使用 onMousePressed、onMouseDragged 和 onMouseReleased 监听器结合 JavaFX 矩形和一些坐标魔法来使用选择框。当我对这些事件执行 .getX() 和 .getY() 时,我得到了在图表上绘制矩形的适当坐标。这些是我假设的相对于 JavaFX 图表的坐标?

当用户做出选择时,此矩形会覆盖图表,并允许用户根据自己的选择进行任何操作或绘制新的选择。这一切都有效,但是当用户开始从先前的选择范围内绘制时,这将采用相对于矩形的坐标。我的问题如下:如何使矩形对鼠标事件“透明”并将这些事件发送到底层组件(折线图)。或者有没有办法将这些坐标转换为折线图坐标。

请注意,我已经在所有不同部分尝试了 localToParent、localToScene、sceneToLocal 和 sceneToLocal。此外,即使我不完全了解该属性的确切工作原理,我也尝试以不同的方式更改 pickOnBounds。

谢谢

最佳答案

Currently I've got the selection box working using the onMousePressed, onMouseDragged and onMouseReleased listeners in combination with a JavaFX Rectangle and some coordinate magic. When I do .getX() and .getY() on these events I get the appropriate coordinates for drawing the Rectangle on the chart. These are the coordinates relative to the JavaFX chart I assume?

不,它与图表无关,它是您单击的目标节点的本地坐标。请参阅 MouseEvent::getX 的文档:

Horizontal position of the event relative to the origin of the MouseEvent's source.

图表是一个由许多节点和 Pane 组成的复杂事物,每个节点和 Pane 都可以有自己的本地坐标系。因此 getX 将返回的内容取决于您单击的实际内容。例如,如果您在图表顶部覆盖一个半透明填充的矩形,然后在矩形内部单击,则坐标将是矩形内的局部坐标。

how do I make the Rectangle "transparent" for mouse events and send these events to the underlying component (the LineChart).

制作矩形mouse transparent :

rectangle.setMouseTransparent(true);

Or is there a way to transform these coordinates to the LineChart coordinates.

您可以调用类似 mouseEvent.getSceneX() 的电话 获取场景坐标,而不是本地坐标。

然后,您可以使用node.sceneToLocal(sceneX, sceneY)将坐标转换为您想要的任何节点的本地坐标。 :

node.sceneToLocal(mouseEvent.getSceneX(), mouseEvent.getSceneY());

因此,只要您引用折线图中想要确定鼠标单击相对坐标的相关节点,就可以做到这一点。

关于java - 如何将本地坐标转换为JavaFX图表坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58221758/

相关文章:

javascript - D3 气泡图

java - (Java RPG) if 语句中的对象构造

java - 如何读取文件并返回字符串及其值

java - OS X 上 Windows 环境下的测试程序

JavaFx图表多个系列,如何使它们保持独立

JavaFX为ProgressIndicator和Spinner设置不同的样式

java - 以编程方式向布局添加多个按钮

java - 连接具有 StringConverter 的 slider 和微调器

javascript - 如何在dimple js中创建气泡饼图

performance - 提高具有高数据速率的图形图表的性能