![](/img/trans.png)
[英]Is possible to create horizontal scrolling line chart with a locked y axis on JAVAFX?
[英]JavaFX: How to correctly implement `getValueForDisplay()` on Y Axis of a XY line Graph?
我正在嘗試在折線圖上實現工具提示以顯示X和Y軸的值,我正在正確獲取X軸的值,但是Y軸的值未正確計算。 我嘗試進行數學運算,但是到目前為止,當我們調整窗口大小時,值仍然不正確。 是否有任何邏輯可以幫助我們正確計算Y軸上的值?
// 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);
}
});
這應該可以滿足您的需求:
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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.