修复JavaFX折线图中不同系列之间的间距 [英] Fix spacing between different series in JavaFX line chart
问题描述
我希望项目间距在恒定的间距下显得更有序
I want the item spacing to appear more orderly with a constant spacing
我该怎么做?我梳理了JavaFX CSS属性和Modena CSS,但没有发现任何似乎有用的相关内容.我只找到以下属性:
How can I do this? I combed through the JavaFX CSS properties and modena css, and I didn't see anything relevant that seemed to work. I only find these properties:
.chart-legend {
-fx-background-color: white
-fx-background-insets: 0 0 -1 0, 0,1;
-fx-background-radius: 4,4,3;
-fx-padding: 6px;
}
推荐答案
图表的图例是一个任意节点.它的默认实现是专门的TilePane,即其子对象在可用宽度上的大小均一.对于其他布局,可以将默认实现替换为自定义图例f.i. FlowPane(或HBox).
The legend of a Chart is an arbitrary node. It's default implementation is a specialized TilePane, that is its children are sized uniformly across the available width. For a different layout that default implementation can be replaced with a custom legend, f.i. a FlowPane (or HBox).
一种快速的方法是将LineChart子类化,覆盖updateLegend并将其替换为自定义窗格.下面的示例很脏,因为它依赖于默认实现的实现细节
A quick approach is to subclass LineChart, override updateLegend and replace the default with a custom pane. The example below is dirty in that it relies on implementation details of the default implementation
自定义LineChart:
The custom LineChart:
public static class MyLineChart<X, Y> extends LineChart<X, Y> {
public MyLineChart(Axis<X> xAxis, Axis<Y> yAxis) {
super(xAxis, yAxis);
}
private TilePane legendAlias;
private FlowPane legendReplacement;
@Override
protected void updateLegend() {
// let super do the setup
super.updateLegend();
Node legend = getLegend();
if (legend instanceof TilePane) {
legendAlias = (TilePane) legend;
legendReplacement = new FlowPane(10, 10);
setLegend(legendReplacement);
}
if (legendAlias != null && legendAlias.getChildren().size() > 0) {
legendReplacement.getChildren().setAll(legendAlias.getChildren());
legendAlias.getChildren().clear();
setLegend(legendReplacement);
}
}
}
这篇关于修复JavaFX折线图中不同系列之间的间距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!