Fix spacing between different series in JavaFX line chart

后端 未结 1 789
孤城傲影
孤城傲影 2020-12-21 11:31

I want the item spacing to appear more orderly with a constant spacing

How can I do this? I combed through the JavaFX CSS properties and modena css, and I d

相关标签:
1条回答
  • 2020-12-21 12:21

    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).

    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

    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);
            }
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题