繁体   English   中英

如何在JavaFx中获得LineChart的刻度标签的宽度?

[英]How to get the width of the tick label of a LineChart in JavaFx?

在下面的屏幕截图中,如何获得红色框突出显示的部分的宽度?

图表

我努力了

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

filtered是价格变动的列表,但是node没有任何像getPrefWidth()这样的方法来返回价格变动的宽度。

好吧,所以我在JKostikiadis的帮助下弄清楚了。 就像他/她提到的那样,基本上,您将获得绘图区域的layoutX ,然后只需添加绘图区域的左侧填充即可。

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

在大多数情况下,yAxis.getWidth()绰绰有余,但是如果您想精确一点,就没有直接的方法来获取该信息(如果我没记错的话),所以您必须找到一种解决方法,我建议访问chart-plot-background,然后找到该Region的layoutX,它实际上是标记区域的宽度。 这是您可以实现的方法:

Region r = (Region) lineChart.lookup("Region"); // access chart-plot-background
double yAxisWidth = r.getLayoutX(); // find out where it starts
System.out.println(yAxisWidth);

为了解释这里的想法是一张图片(chart-plot-background背景为黑色):

在此处输入图片说明

PS。 仍然我不确定这是否是正确的方法,并且可能会失败,具体取决于您可能会在图表上应用的不同CSS规则

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM