You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Apache POI 4.1.1气泡图表创建与已有图表数据修改问询

Apache POI 4.1.1 气泡图表操作指南

一、创建带X/Y/Z系列数据的气泡图表

气泡图表属于散点图的变体,Apache POI中通过XDDFScatterChartData.BubbleSeries实现,具体操作步骤如下:

  1. 先在工作表中准备好X轴、Y轴、气泡大小(Z轴)的数据源,示例数据结构:

    X值Y值Z值
    123
    456
    789
  2. 创建绘图对象并初始化气泡图表框架:

// 假设已获取XSSFWorkbook对象wb、XSSFSheet对象sheet
XSSFDrawing drawing = sheet.createDrawingPatriarch();
// 定义图表在工作表中的位置(参数:dx1, dy1, dx2, dy2, col1, row1, col2, row2)
XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 5, 1, 15, 20); 

XSSFChart chart = drawing.createChart(anchor);
chart.setTitleText("气泡图表示例");
chart.setTitleOverlay(false);

// 创建坐标轴
XDDFValueAxis bottomAxis = chart.createValueAxis(AxisPosition.BOTTOM);
bottomAxis.setTitle("X轴");
XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);
leftAxis.setTitle("Y轴");

// 初始化散点图数据容器(气泡图基于散点图扩展)
XDDFScatterChartData data = (XDDFScatterChartData) chart.createData(ChartTypes.SCATTER, bottomAxis, leftAxis);
data.setVaryColors(true);
  1. 绑定X/Y/Z系列数据并生成气泡系列:
int lastDataRow = 3; // 数据最后一行的索引(表头在第0行,数据从第1行开始)
// 绑定X/Y/Z轴的数据范围
XDDFDataSource<Double> xData = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(1, lastDataRow, 0, 0));
XDDFNumericalDataSource<Double> yData = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(1, lastDataRow, 1, 1));
XDDFNumericalDataSource<Double> zData = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(1, lastDataRow, 2, 2));

// 创建气泡系列并添加到图表
XDDFScatterChartData.BubbleSeries series = (XDDFScatterChartData.BubbleSeries) data.addSeries(xData, yData);
series.setSize(zData); // 设置气泡大小对应的Z轴数据
series.setTitle("数据系列1", null);

// 绘制图表
chart.plot(data);

二、修改已有气泡图表的坐标轴系列数据

修改已有气泡图数据的核心是定位目标图表,更新其系列的数据源,代码示例如下:

// 假设已获取XSSFWorkbook对象wb、目标工作表sheet
XSSFDrawing drawing = sheet.getDrawingPatriarch();
if (drawing == null) return;

// 遍历工作表中的所有图表,定位目标气泡图
for (XSSFShape shape : drawing.getShapes()) {
    if (shape instanceof XSSFChart) {
        XSSFChart chart = (XSSFChart) shape;
        // 判断是否为气泡图(散点图类型)
        if (chart.getChartType() == ChartTypes.SCATTER) {
            XDDFScatterChartData data = (XDDFScatterChartData) chart.getChartData();
            if (data.getSeriesCount() > 0) {
                // 获取第一个数据系列(可根据需求指定目标系列)
                XDDFScatterChartData.BubbleSeries series = (XDDFScatterChartData.BubbleSeries) data.getSeries(0);
                
                // 准备新的X/Y/Z数据范围
                int newLastRow = 5;
                XDDFDataSource<Double> newXData = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(1, newLastRow, 0, 0));
                XDDFNumericalDataSource<Double> newYData = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(1, newLastRow, 1, 1));
                XDDFNumericalDataSource<Double> newZData = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(1, newLastRow, 2, 2));
                
                // 更新系列数据
                series.replaceData(newXData, newYData);
                series.setSize(newZData);
                
                // 刷新图表
                chart.plot(data);
                break;
            }
        }
    }
}

关键注意事项

  • Apache POI 4.1.1中,气泡图必须通过XDDFScatterChartData.BubbleSeries处理,不能使用普通散点系列。
  • 数据范围的行列索引需对应实际数据位置(列索引从0开始,行索引注意区分表头行和数据行)。

内容的提问来源于stack exchange,提问作者BabyGluk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.23 14:30:24