我在我的 swing 面板上集成了 javafx 饼图,它工作正常,但我的数据列表太大,无法放入图例,图例正在扩展,导致饼图变小。我想让它可滚动但找不到任何解决方案。我是 javafx 的新手。
另外,对于饼图面板和场景,您建议采用何种布局以适应 jpanel?如您所见,我的饼图面板没有填满整个场景。我不想让我的图表缩小。
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;
}
一列图例
2列图例导致图表缩小
最佳答案
如果您扩展 PieChart
,您可以直接访问 Legend
,因为它是 protected
,参见:Chart
通过这样做,您可以按照以下方式做一些事情:
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
和滚动 Pane 。如果您将 getScene()
方法更新为以下内容,您应该会看到一个小改进:
public Scene getScene() {
VBox root = new VBox();
Scene scene = new Scene(root, Color.ALICEBLUE);
root.getChildren().addAll(getPieChart());
return scene;
}
为了进一步改进,希望以下帖子可以提供帮助:
关于java - 如何限制图例大小并使其可以用饼图滚动?和 javafx 布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37742836/