如何限制传奇规模并使其与Piechart滚动?和Javafx布局

 2023-02-15    321  

问题描述

我将javafx piechart集成在我的秋千面板上,其工作正常,但是我的数据列表太大了,无法适应传奇,传说正在扩展,这会导致Piechart越来越小. ID喜欢使其滚动,但找不到任何解决方案.我是Javafx的新手.

您还建议使用Piechart面板和场景适合JPanel的场景哪种布局?如您所见,我的Piechart面板没有填满场景.我不希望我的图表收缩.

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

一列图例

一列Legend

2列图例导致图表缩小

 2列Legend会导致图表到缩小

推荐答案

如果扩展PieChart,则可以按照protected直接访问Legend,请参见:图表

通过这样做,您可以按照以下方式进行操作:

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

注意:如果您采用这种方法,我建议更新以下内容:legend.setPrefWidth(100);更具体地是您LegendItem的特定内容.因为这不会是大型显示名称的最佳解决方案


通过将上述替换为您的代码并添加一些测试项目以引起滚动条的需求,我得到了以下内容:

在此处输入图像说明


明智的布局我认为您不需要Group和您当前正在使用的滚动窗格.如果您将getScene()方法更新到下面,则应看到一个小改进:

public Scene getScene() {
    VBox root = new VBox();
    Scene scene = new Scene(root, Color.ALICEBLUE);
    root.getChildren().addAll(getPieChart());
    return scene;
}
  • javafx布局与父
  • https://stackoverflow.com/questions/9816099/javafx-how-to-make-make-my-custom-custom-compont-component-use-use-use-use-all———— :如何使我的自定义组件使用父级布局中的所有可用空间?
  • 图表resization resization

其他推荐答案

我认为传说放在底部.由于面板的宽度更多,您可以以默认位置显示传奇.

您将JFXPanel添加到我认为不需要的JSCrollpane中.

add(new JScrollPane(getJfxPanel()), BorderLayout.CENTER);

您可以直接添加面板

add(getJfxPanel(), BorderLayout.CENTER);

和您要添加的JFXPanel的面板将占据右侧的整个区域.我不确定该面板的布局是什么,所以我不确定它是否占据整个右侧区域.

,如果您真的希望它可以滚动,则需要根据PIECHART的数据设置JFXPANEL的大小,然后像您一样将其放置在JSCrollpane中.

以上所述是小编给大家介绍的如何限制传奇规模并使其与Piechart滚动?和Javafx布局,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对77isp云服务器技术网的支持!

原文链接:https://77isp.com/post/33773.html

=========================================

https://77isp.com/ 为 “云服务器技术网” 唯一官方服务平台,请勿相信其他任何渠道。