POI 生成气泡图

ApachePOI5.2.2默认不支持气泡图,但可以通过散点图并设置MarkerSize来模拟气泡图效果。代码示例展示了如何构建气泡图,包括设置字体颜色、轴样式、数据源以及标记颜色等,最终实现气泡图的展示。
摘要由CSDN通过智能技术生成

poi版本5.2.2 

poi默认不支持气泡图,不过可以通过散点图设置MarkerSize来实现气泡图效果,代码如下

public class WordBubbleChart implements WordChart{

	@Override
	public void bulider(XWPFChart chart, ChartOption options) {

		String sheetName = "sheet0";
		XSSFWorkbook workbook = null;
		try {
			workbook = chart.getWorkbook();
		} catch (Exception e) {
			e.printStackTrace();
			return;
		}
		XSSFSheet sheet = workbook.getSheet(sheetName);
		refreshSheet(sheet, options);
		
		// 字体颜色
		XDDFSolidFillProperties fontColor = new XDDFSolidFillProperties(XDDFColor.from(new byte[] {(byte)96,(byte)98,(byte)102}));
		
		// 样式线
		XDDFLineProperties line = new XDDFLineProperties();
		line.setFillProperties(new XDDFSolidFillProperties(XDDFColor.from(new byte[] {(byte)228,(byte)231,(byte)237})));
		line.setWidth(0.5);
		
		XDDFLineProperties noLine = new XDDFLineProperties();
		noLine.setFillProperties(new XDDFNoFillProperties());
		
		
		// X轴
		XDDFValueAxis  xAxis = chart.createValueAxis(AxisPosition.BOTTOM);
		if(!options.isShowXAxis()) {
			xAxis.setVisible(false);// X轴不显示
		}
		xAxis.setMajorTickMark(AxisTickMark.NONE);// 轴刻度线
		// 标签样式
		XDDFRunProperties xTextProperties = xAxis.getOrAddTextProperties();
		xTextProperties.setFillProperties(fontColor);
		// 轴线样式
		XDDFShapeProperties xAxisLineProperties = xAxis.getOrAddShapeProperties();
		if(options.isShowXAxisLine()) {
			xAxisLineProperties.setLineProperties(line);
		} else {
			xAxisLineProperties.setLineProperties(noLine);
		}
		// 网格线
		if(options.isShowXGrid()) {
			XDDFShapeProperties xGridProperties = xAxis.getOrAddMajorGridProperties();
			xGridProperties.setLineProperties(line);
		}
		
		// Y轴
		XDDFValueAxis  yAxis = chart.createValueAxis(AxisPosition.LEFT);
		if(!options.isShowYAxis()) {
			yAxis.setVisible(false);// Y轴不显示
		}
		yAxis.setMajorTickMark(AxisTickMark.NONE);// 轴刻度线
		// 标签样式
		XDDFRunProperties yTextProperties = yAxis.getOrAddTextProperties();
		yTextProperties.setFillProperties(fontColor);
		// 轴线样式
		XDDFShapeProperties yAxisLineProperties = yAxis.getOrAddShapeProperties();
		if(options.isShowYAxisLine()) {
			yAxisLineProperties.setLineProperties(line);
		} else {
			yAxisLineProperties.setLineProperties(noLine);
		}
		// 网格线
		if(options.isShowYGrid()) {
			XDDFShapeProperties yGridProperties = yAxis.getOrAddMajorGridProperties();
			yGridProperties.setLineProperties(line);
		}
				
		XDDFScatterChartData scatter = (XDDFScatterChartData) chart.createData(ChartTypes.SCATTER, xAxis, yAxis);
		
		XDDFLineProperties chartLine = new XDDFLineProperties();
		chartLine.setFillProperties(new XDDFNoFillProperties());
		
		String[] dimensions = options.getDimensions();
		JSONArray array = options.getDataSet();
		double min = 0;
		double max = 0;
		for (int i = 0; i < array.size(); i++) {
			JSONObject obj = array.getJSONObject(i);
			Double d = obj.getDouble(dimensions[2]);
			if(d != null && max < d) {
				max = d;
			}
			if(d != null && min > d) {
				min = d;
			}
		}
		CTScatterChart  c = chart.getCTChart().getPlotArea().getScatterChartArray(0);
		for (int i = 0; i < array.size(); i++) {
			XDDFNumericalDataSource<Double> x = XDDFDataSourcesFactory.fromNumericCellRange(sheet,new CellRangeAddress(i+1, i+1, 1, 1));
			XDDFNumericalDataSource<Double> y = XDDFDataSourcesFactory.fromNumericCellRange(sheet,new CellRangeAddress(i+1, i+1, 2, 2));
			XDDFScatterChartData.Series series = (XDDFScatterChartData.Series) scatter.addSeries(x, y);
			
			JSONObject obj = array.getJSONObject(i);
			Double d = obj.getDouble(dimensions[2]);
			if(d != null) {
				if(min == max){
					series.setMarkerSize((short)5);
			    } else {
			    	Double size = (d - min) * (40 - 5) / (max - min) + 5;
			    	series.setMarkerSize((short)size.intValue());
			    }
			} else {
				series.setMarkerSize((short)5);
			}
			series.setLineProperties(chartLine);
			series.setMarkerStyle(MarkerStyle.CIRCLE);
			// 设置标记颜色
			CTMarker marker = c.getSerArray(i).getMarker();
			setMarkerColor(marker, options.getColor(0));
		}
		chart.plot(scatter);
	}
	
	/**
	 * 	设置标记点颜色
	 */
	private void setMarkerColor(CTMarker marker, byte[] color) {
		CTShapeProperties shapeProperties = marker.addNewSpPr();
		// 边框颜色
		CTLineProperties borderProperties = shapeProperties.addNewLn();
		CTSolidColorFillProperties borderColor = borderProperties.addNewSolidFill();
		CTSRgbColor borderRgb = borderColor.addNewSrgbClr();
		borderRgb.addNewAlpha().setVal(80000);  // 透明度 0- 100000
		borderRgb.setVal(color);
		// 填充颜色
		CTSolidColorFillProperties fillProperties = shapeProperties.addNewSolidFill();
		CTSRgbColor fillRgb = fillProperties.addNewSrgbClr();
		fillRgb.addNewAlpha().setVal(80000);  // 透明度 0- 100000
		fillRgb.setVal(color);
	}
}

效果如下图:

本功能所有代码实现,摘自开源项目https://gitee.com/lenvoe2019/demo-ureport目录src/main/java/com/bstek/ureport/export/word/chart下

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值