如何在JavaFx中获得折线图刻度标签的宽度?

如何在JavaFx中获得折线图刻度标签的宽度?,java,javafx,javafx-8,javafx-2,Java,Javafx,Javafx 8,Javafx 2,如何获得下面屏幕截图中红色外壳突出显示的部分宽度 我试过了 LineChart<Number, Number> sourceChart = ...; NumberAxis axis = (NumberAxis) sourceChart.getXAxis(); FilteredList<Node> filtered = axis.getChildrenUnmodifiable().filtered(node -> node instanceof Text); node

如何获得下面屏幕截图中红色外壳突出显示的部分宽度

我试过了

LineChart<Number, Number> sourceChart = ...;
NumberAxis axis = (NumberAxis) sourceChart.getXAxis();
FilteredList<Node> filtered = axis.getChildrenUnmodifiable().filtered(node -> node instanceof Text);
node = filtered.get(0);

filtered是一个勾号列表,但节点没有任何返回勾号宽度的方法,如getPrefWidth或任何此类方法。

在大多数情况下,yAxis.getWidth就足够了,但如果您想精确,如果我没有弄错,就没有直接的方法来获取该信息,因此您必须找到解决方法,我建议访问图表绘图背景,然后找到该区域的布局X,它实际上是标记区域的宽度。以下是如何实现这一目标:

Region r = (Region) lineChart.lookup("Region"); // access chart-plot-background
double yAxisWidth = r.getLayoutX(); // find out where it starts
System.out.println(yAxisWidth);
为了解释这个想法,这里有一张图片,图表的背景颜色是黑色的:


另外,我仍然不确定这是正确的方法,它可能会失败,这取决于你可能在图表上应用的不同CSS规则。好吧,所以我在JKostikiadis的帮助下找到了答案。基本上,正如s/他所提到的,你得到绘图区域的布局X,然后只添加绘图区域的左填充

Region plotArea = (Region) sourceChart.lookup("Region");
double plotAreaLayoutX = plotArea.getLayoutX();
double chartLeftPad = sourceChart.getPadding().getLeft();
double labelWidth = plotAreaLayoutX + chartLeftPad;

嗨,你的回答帮我弄明白了。非常感谢你。请检查我的答案,以获得更精确的方式获得绘图区域的起始位置。