本文介绍了JavaFX:如何在XY线图的Y轴上正确实现`getValueForDisplay()`?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在折线图上实现工具提示以显示X和Y轴的值,我正确地获得X轴的值,但是没有正确计算Y轴值。我试着做数学,但是当我们调整窗口大小时,到目前为止没有任何帮助。是否有任何逻辑可以帮助我们正确计算Y轴上的值?
I am trying to implement tooltip on a line graph to show values of X and Y Axis, i am getting values at X axis correctly, but Y axis values are not getting calculated properly. I tried to do the Math, but nothing helped so far values get incorrect when we resize the window. Is there any logic which help us to calculate value on Y axis correctly?
// lineChart is an object of AreaChart Or XYChart
lineChart.setOnMouseMoved(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
Tooltip t= new Tooltip("X:"+lineChart.getXAxis().getValueForDisplay(event.getX()-lineChart.getXAxis().getLayoutX())+", Y:"+
lineChart.getYAxis().getValueForDisplay(event.getY()));
t.show(stage);
}
});
推荐答案
这应该可以满足您的需求:
This should do what you need:
public void handle(MouseEvent event) {
Point2D pointInScene = new Point2D(event.getSceneX(), event.getSceneY());
Axis<Number> xAxis = lineChart.getXAxis();
Axis<Number> yAxis = lineChart.getYAxis();
double xPosInAxis = xAxis.sceneToLocal(new Point2D(pointInScene.getX(), 0)).getX();
double yPosInAxis = yAxis.sceneToLocal(new Point2D(0, pointInScene.getY())).getY();
double x = xAxis.getValueForDisplay(xPosInAxis).doubleValue();
double y = yAxis.getValueForDisplay(yPosInAxis).doubleValue();
Tooltip t = new Tooltip("X: "+x+", Y:"+y);
t.show(stage);
}
这篇关于JavaFX:如何在XY线图的Y轴上正确实现`getValueForDisplay()`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!