PieChart示例

105 篇文章 5 订阅

PieChart是JavaFX中的饼图,示例如下:

PieChartUtil.java文件,饼图数据设置。

package javafx8.ch29;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.chart.PieChart;

/**
 * @copyright 2023-2022
 * @package   javafx8.ch29
 * @file      PieChartUtil.java
 * @date      2023-08-31 14:22
 * @author    qiao wei
 * @version   1.0
 * @brief     饼图数据集合。
 * @history
 */
public class PieChartUtil {
    
    public static ObservableList<PieChart.Data> getChartData() {
        ObservableList<PieChart.Data> data = FXCollections. observableArrayList();
        
        /**
         * 将要显示的饼图数据添加到列表中,参数1是表头显示的内容,参数2是饼图要显示的数据。
         */
        data.add(new PieChart.Data("China", 1275));
        data.add(new PieChart.Data("India", 1017));
        data.add(new PieChart.Data("Brazil", 172));
        data.add(new PieChart.Data("UK", 59));
        data.add(new PieChart.Data("USA", 285));
        
        // 表头显示数据比例。
//        data.add(new PieChart.Data("China : " + (1275 * 100 / (1275 + 1017 + 172 + 59 + 285) + "%"), 1275));
//        data.add(new PieChart.Data("India : " + (1017 * 100 / (1275 + 1017 + 172 + 59 + 285) + "%"), 1017));
//        data.add(new PieChart.Data("Brazil : " + (172 * 100 / (1275 + 1017 + 172 + 59 + 285) + "%"), 172));
//        data.add(new PieChart.Data("UK : " + (59 * 100 / (1275 + 1017 + 172 + 59 + 285) + "%"), 59));
//        data.add(new PieChart.Data("USA : " + (285 * 100 / (1275 + 1017 + 172 + 59 + 285) + "%"), 285));
        
        return data;
    }
}

PieChartTest.java文件,显示饼图数据

package javafx8.ch29;

import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.geometry.Side;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.chart.PieChart;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
 * @copyright 2023-2022
 * @package   javafx8.ch29
 * @file      PieSliceTest.java
 * @date      2023-08-31 16:23
 * @author    qiao wei
 * @version   1.0
 * @brief     
 * @history
 */
public class PieSliceTest extends Application {

    public static void main(String[] args) {
        Application.launch(PieSliceTest.class, args);
    }
    
    @Override
    public void start(Stage stage) throws Exception {
        PieChart chart = new PieChart();
        chart.setTitle("Population in 2000");
        
        // Place the legend on the left side
        chart.setLegendSide(Side.LEFT);
        
        // Set the data for the chart
        ObservableList<PieChart.Data> chartData = PieChartUtil.getChartData();
        chart.setData(chartData);
        
        // Add a Tooltip to all pie slices
        this.addSliceTooltip(chart);
        StackPane root = new StackPane(chart);
        Scene scene = new Scene(root);
        
        stage.setScene(scene);
        stage.setTitle("Customizing Pie Slices");
        stage.show();
    }

    private void addSliceTooltip(PieChart chart) {
        // Compute the total pie value
        double totalPieValue = 0.0d;
        
        // Traverse items to compute total value.
        for (PieChart.Data d : chart.getData()) {
            totalPieValue += d.getPieValue();
        }
        
        // Add a tooltip to all pie slices
        for (PieChart.Data d : chart.getData()) {
            Node sliceNode = d.getNode();
            double pieValue = d.getPieValue();
            double percentPieValue = (pieValue / totalPieValue) * 100;
            
            // Create and install a Tooltip for the slice
            String message = d.getName() + "=" + pieValue + " (" + String.format("%.2f", percentPieValue) + "%)";
            Tooltip tooltip = new Tooltip(message);
            tooltip.setStyle("-fx-background-color: yellow;" + "-fx-text-fill: black;");
            tooltip.setShowDelay(new Duration(50));
            Tooltip.install(sliceNode, tooltip);
        }
    }
}

运行结果如下:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值