[英]Why is the parent node of an axis in an XYChart not the chart itself? (JavaFX)
我一直在嘗試使用XYCharts做一些工作,特別是在它們上面或周圍放置東西。 我一直需要相對於整個圖表定義的坐標空間使用軸的邊界框的坐標。 但是,我已經意識到.getBoundsInParent()方法不會返回應用於軸的期望值。 似乎是因為軸的父級不是圖表,並且我不知道父級是什么。 例如,以下布爾值是false,對我來說這似乎是糟糕的設計(假設圖表是LineChart的一個實例):
chart.getXAxis()。getParent()==圖表
這里發生了什么? 謝謝。
沒有直接的API可以獲取圖表繪制區域的邊界。 一種方法是使用查找,但這不是很可靠。
如果要在圖表繪圖區域中添加其他內容,建議的方法是覆蓋圖表的layoutPlotChildren
方法,如本問題所述 。
在圖表區域外添加內容的最佳方法可能是對Region
進行子類化,並重寫layoutChildren
方法以對圖表和所需的額外內容進行布局。 您可以通過使用ValueAxis.getDisplayPosition(value)
來獲取相對於軸的坐標,然后使用幾個轉換來獲取相對於區域的坐標,從而確定特定“圖表值”的坐標。
這是一個簡單的示例:
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javafx.application.Application;
import javafx.geometry.Point2D;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart.Data;
import javafx.scene.chart.XYChart.Series;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Region;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class LineChartWithRightPanel extends Application {
private final Color[] RECT_COLORS =
new Color[] {Color.RED, Color.ORANGE, Color.YELLOW, Color.GREEN, Color.BLUE};
private final double RECT_WIDTH = 20 ;
@Override
public void start(Stage primaryStage) {
NumberAxis xAxis = new NumberAxis();
NumberAxis yAxis = new NumberAxis(0, 1, 0.2);
LineChart<Number, Number> chart = new LineChart<>(xAxis, yAxis);
Region chartWithPanel = new Region() {
private List<Rectangle> rectangles =
Stream.of(RECT_COLORS)
.map(c -> new Rectangle(RECT_WIDTH, 0, c))
.collect(Collectors.toList());
{
getChildren().add(chart);
getChildren().addAll(rectangles);
}
@Override
protected void layoutChildren() {
double w = getWidth();
double h = getHeight();
chart.resizeRelocate(0, 0, w-RECT_WIDTH, h);
chart.layout();
double chartMinY = yAxis.getLowerBound();
double chartMaxY = yAxis.getUpperBound();
double chartYRange = chartMaxY - chartMinY ;
for (int i = 0 ; i < rectangles.size(); i++) {
// easier ways to do this in this example,
// but this technique shows how to map "chart coords"
// to coords in this pane:
double rectMinYInChart = chartMinY + i * chartYRange / rectangles.size();
double rectMaxYInChart = chartMinY + (i+1) * chartYRange / rectangles.size();
double rectMinYInAxis = yAxis.getDisplayPosition(rectMinYInChart);
double rectMaxYInAxis = yAxis.getDisplayPosition(rectMaxYInChart);
double rectMinY = sceneToLocal(yAxis.localToScene(new Point2D(0, rectMinYInAxis))).getY();
double rectMaxY = sceneToLocal(yAxis.localToScene(new Point2D(0, rectMaxYInAxis))).getY();
rectangles.get(i).setHeight(Math.abs(rectMaxY - rectMinY));
rectangles.get(i).setX(w - RECT_WIDTH);
rectangles.get(i).setY(Math.min(rectMinY, rectMaxY));
Tooltip.install(rectangles.get(i),
new Tooltip(String.format("%.2f - %.2f", rectMinYInChart, rectMaxYInChart)));
}
}
};
Series<Number, Number> series = new Series<>();
series.setName("Data");
Random rng = new Random();
for (int i = 0; i < 20; i++) {
series.getData().add(new Data<>(i, rng.nextDouble()));
}
chart.getData().add(series);
BorderPane root = new BorderPane(chartWithPanel);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
請注意,您可以使用相同的思路獲得整個繪圖區域的邊界:
double minXInAxis = xAxis.getDisplayPosition(xAxis.getLowerBound());
double maxXInAxis = xAxis.getDisplayPosition(xAxis.getUpperBound());
double minYInAxis = yAxis.getDisplayPosition(yAxis.getLowerBound());
double maxYInAxis = yAxis.getDisplayPosition(yAxis.getUpperBound());
double minX = sceneToLocal(xAxis.localToScene(new Point2D(minXInAxis,0))).getX();
double maxX = sceneToLocal(xAxis.localToScene(new Point2D(maxXInAxis,0))).getX();
double minY = sceneToLocal(yAxis.localToScene(new Point2D(0,minYInAxis))).getY();
double maxY = sceneToLocal(yAxis.localToScene(new Point2D(0,maxYInAxis))).getY();
然后, minX
, maxX
, minY
, maxY
定義繪圖區域的邊界。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.