【问题标题】:Fix spacing between different series in JavaFX line chart修复 JavaFX 折线图中不同系列之间的间距
【发布时间】:2018-08-07 07:34:03
【问题描述】:

我希望项目间距以恒定间距显得更有序

我该怎么做?我梳理了 JavaFX CSS 属性和 modena css,但没有发现任何相关的东西似乎有效。我只找到这些属性:

.chart-legend {
   -fx-background-color: white
   -fx-background-insets: 0 0 -1 0, 0,1;
   -fx-background-radius: 4,4,3;
   -fx-padding: 6px;
}

【问题讨论】:

  • 看起来每个标签的数量相同,即适合最宽的最小值。你到底想要什么/如何改变,这就是你所说的“更老套”是什么意思?
  • @kleopatra 我希望每个人之间有相同的空间。因为如果你看到图像,第三个和第四个参数非常接近。
  • hmm ... 不支持:Legend is-a TilePane,它的子级大小一致。如果我理解正确,您需要 FlowPane 或 HBox - 您将不得不弄脏,子类化图表并将其换成自定义实现
  • @kleopatra 是的,比如 HBox 或类似的。好的,我会尝试用肮脏的方式来做:D。还是谢谢你!

标签: java css javafx


【解决方案1】:

图表的图例是任意节点。它的默认实现是一个专门的 TilePane,即它的子级在可用宽度上统一调整大小。对于默认实现可以用自定义图例替换的不同布局,f.i. FlowPane(或 HBox)。

一种快速的方法是继承 LineChart,覆盖 updateLegend 并用自定义窗格替换默认值。下面的例子很脏,因为它依赖于默认实现的实现细节

自定义折线图:

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);
        }
    }

}

【讨论】:

  • 你太棒了!它完美地工作!谢谢大家
猜你喜欢
  • 2021-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-12
相关资源
最近更新 更多