如何限制图例大小并使其可与饼图一起滚动?和 javafx 布局 [英] How to restrict legend size and making it scrollable with piechart? and javafx layouts

查看:103
本文介绍了如何限制图例大小并使其可与饼图一起滚动?和 javafx 布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的摆动面板上集成了 javafx 饼图,它工作正常,但我的数据列表太大而无法放入图例,并且图例正在扩大,这导致饼图变小.我想让它滚动但找不到任何解决方案.我是 javafx 的新手.

您还建议饼图面板和场景采用什么布局以适合 jpanel?如您所见,我的饼图面板没有填满整个场景.我不希望我的图表缩小.

public PieChartPanel() {this.setLayout(new BorderLayout());添加(新 JScrollPane(getJfxPanel()),BorderLayout.CENTER);}公共 JFXPanel getJfxPanel() {如果(jfxPanel == null){jfxPanel = 新的 JFXPanel();jfxPanel.setScene(getScene());}返回 jfxPanel;}公共场景 getScene() {如果(场景==空){组根 = new Group();场景 = 新场景(根,颜色.ALICEBLUE);javafx.scene.control.ScrollPane scroll = new ScrollPane();scroll.setFitToHeight(true);scroll.setFitToWidth(true);scroll.setContent(getPieChart());root.getChildren().addAll(scroll);}返回场景;}私人饼图 getPieChart() {如果(饼图 == 空){ObservableListpieChartData = FXCollections.observableArrayList();饼图 = 新饼图(饼图数据);pieChart.setTitle("Toplam Talep (Ünite) Grafiği");pieChart.setLabelLineLength(20);pieChart.setLegendSide(Side.RIGHT);pieChart.setClockwise(true);}返回饼图;}

一列图例

2 列图例导致图表缩小

解决方案

如果你扩展 PieChart 你可以直接访问 Legend 因为它是 protected代码>,参见:

<小时>布局明智我认为您不需要 Group 和您当前使用的滚动窗格.如果您将 getScene() 方法更新为以下内容,您应该会看到一个小的改进:

公共场景 getScene() {VBox root = new VBox();场景场景 = 新场景(root, Color.ALICEBLUE);root.getChildren().addAll(getPieChart());返回场景;}

为了进一步改进,希望以下帖子可以提供帮助:

I integrated javafx piechart on my swing panel its working fine, but my data list is too big to fit in the legend and the legend is expanding which causes the piechart getting smaller. Id like to make it scrollable but couldnt find any solution. Im new to javafx.

Also what layout would you suggest for piechart panel and scene to fit in the jpanel? As you see my piechart panel doesnt fill the scene. I dont want my chart to shrink.

public PieChartPanel() {
    this.setLayout(new BorderLayout());
    add(new JScrollPane(getJfxPanel()), BorderLayout.CENTER);
}

public JFXPanel getJfxPanel() {
    if (jfxPanel == null) {
        jfxPanel = new JFXPanel();
        jfxPanel.setScene(getScene());
    }
    return jfxPanel;
}

public Scene getScene() {
    if (scene == null) {
        Group root = new Group();
        scene = new Scene(root, Color.ALICEBLUE);

        javafx.scene.control.ScrollPane scroll = new ScrollPane();
        scroll.setFitToHeight(true);
        scroll.setFitToWidth(true);
        scroll.setContent(getPieChart());

        root.getChildren().addAll(scroll);

    }
    return scene;
}

private PieChart getPieChart() {
    if (pieChart == null) {
        ObservableList<PieChart.Data> pieChartData = FXCollections.observableArrayList();
        pieChart = new PieChart(pieChartData);
        pieChart.setTitle("Toplam Talep (Ünite) Grafiği");
        pieChart.setLabelLineLength(20);
        pieChart.setLegendSide(Side.RIGHT);
        pieChart.setClockwise(true);
    }
    return pieChart;
}

one column legend

2 column legend causes chart to shrink

解决方案

If you extend PieChart you can access the Legend directly as it is protected, see : Chart

By doing that you can do something along these lines:

public class CustomPieChart extends PieChart {
    public CustomPieChart(ObservableList<PieChart.Data> data){
        super(data);
        setLabelLineLength(20);
        createScrollableLegend();
        setLegendSide(Side.RIGHT);
        setClockwise(true);
    }

    private void createScrollableLegend(){
        Legend legend = (Legend) getLegend();
        if(legend != null){
            legend.setPrefWidth(100);
            ScrollPane scrollPane = new ScrollPane(legend);
            scrollPane.setHbarPolicy(ScrollBarPolicy.NEVER);
            scrollPane.setVbarPolicy(ScrollBarPolicy.AS_NEEDED);
            scrollPane.maxHeightProperty().bind(heightProperty());
            setLegend(scrollPane);
        }
    }
}

Note: If you go for this approach, I recommend updating this: legend.setPrefWidth(100); to be more specific to your LegendItem's. As this won't be the best solution for large display names


By substituting the above into your code and adding a few test items to cause the need for the scroll bars, I got the following:


Layout wise I don't think you need the Group and scroll panes you're currently using. If you update your getScene() method to the below you should see a small improvement:

public Scene getScene() {
    VBox root = new VBox();
    Scene scene = new Scene(root, Color.ALICEBLUE);
    root.getChildren().addAll(getPieChart());
    return scene;
}

For further improvements hopefully the following posts can help:

这篇关于如何限制图例大小并使其可与饼图一起滚动?和 javafx 布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆