POI 重叠、并列柱状图(条形图),显示数据,自定义颜色

1、pom.xml

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.1.2</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml-schemas</artifactId>
    <version>4.1.2</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.1.2</version>
</dependency>
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-collections4</artifactId>
    <version>4.4</version>
</dependency>
<dependency>
    <groupId>commons-codec</groupId>
    <artifactId>commons-codec</artifactId>
    <version>1.13</version>
</dependency>
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-compress</artifactId>
    <version>1.19</version>
</dependency>
<dependency>
    <groupId>org.apache.xmlbeans</groupId>
    <artifactId>xmlbeans</artifactId>
    <version>3.1.0</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>ooxml-schemas</artifactId>
    <version>1.4</version>
</dependency>

 

2、poi 柱状图、条形图自定义颜色,重叠条形图,显示数值。

package com.poi.test;


import java.io.FileOutputStream;
import java.io.IOException;
 
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xddf.usermodel.PresetColor;
import org.apache.poi.xddf.usermodel.XDDFColor;
import org.apache.poi.xddf.usermodel.XDDFSolidFillProperties;
import org.apache.poi.xddf.usermodel.chart.AxisCrossBetween;
import org.apache.poi.xddf.usermodel.chart.AxisCrosses;
import org.apache.poi.xddf.usermodel.chart.AxisPosition;
import org.apache.poi.xddf.usermodel.chart.BarDirection;
import org.apache.poi.xddf.usermodel.chart.BarGrouping;
import org.apache.poi.xddf.usermodel.chart.ChartTypes;
import org.apache.poi.xddf.usermodel.chart.LegendPosition;
import org.apache.poi.xddf.usermodel.chart.XDDFBarChartData;
import org.apache.poi.xddf.usermodel.chart.XDDFCategoryAxis;
import org.apache.poi.xddf.usermodel.chart.XDDFChartLegend;
import org.apache.poi.xddf.usermodel.chart.XDDFDataSource;
import org.apache.poi.xddf.usermodel.chart.XDDFDataSourcesFactory;
import org.apache.poi.xddf.usermodel.chart.XDDFNumericalDataSource;
import org.apache.poi.xddf.usermodel.chart.XDDFValueAxis;
import org.apache.poi.xssf.usermodel.XSSFChart;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFDrawing;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTBarSer;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTDPt;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTPlotArea;
import org.openxmlformats.schemas.drawingml.x2006.chart.STDLblPos;
 
public class ApachePoiBarChart {
 
	public static void main(String[] args) throws IOException {
		XSSFWorkbook wb = new XSSFWorkbook();
		String sheetName = "Sheet1";
		FileOutputStream fileOut = null;
		try {
			XSSFSheet sheet = wb.createSheet(sheetName);
			//第一行,国家名称
			Row row = sheet.createRow(0);
			Cell cell = row.createCell(0);
			cell.setCellValue("俄罗斯");
			cell = row.createCell(1);
			cell.setCellValue("加拿大");
			cell = row.createCell(2);
			cell.setCellValue("美国");
			cell = row.createCell(3);
			cell.setCellValue("中国");
			cell = row.createCell(4);
			cell.setCellValue("巴西");
			cell = row.createCell(5);
			cell.setCellValue("澳大利亚");
			cell = row.createCell(6);
			cell.setCellValue("印度");
			// 第二行,乡村地区
			row = sheet.createRow(1);
			cell = row.createCell(0);
			cell.setCellValue(17098242);
			cell = row.createCell(1);
			cell.setCellValue(9984670);
			cell = row.createCell(2);
			cell.setCellValue(9826675);
			cell = row.createCell(3);
			cell.setCellValue(9596961);
			cell = row.createCell(4);
			cell.setCellValue(8514877);
			cell = row.createCell(5);
			cell.setCellValue(7741220);
			cell = row.createCell(6);
			cell.setCellValue(3287263);
			// 第三行,农村人口
			row = sheet.createRow(2);
			cell = row.createCell(0);
			cell.setCellValue(14590041);
			cell = row.createCell(1);
			cell.setCellValue(35151728);
			cell = row.createCell(2);
			cell.setCellValue(32993302);
			cell = row.createCell(3);
			cell.setCellValue(14362887);
			cell = row.createCell(4);
			cell.setCellValue(21172141);
			cell = row.createCell(5);
			cell.setCellValue(25335727);
			cell = row.createCell(6);
			cell.setCellValue(13724923);
			// 第四行,面积平局
			row = sheet.createRow(3);
			cell = row.createCell(0);
			cell.setCellValue(9435701.143);
			cell = row.createCell(1);
			cell.setCellValue(9435701.143);
			cell = row.createCell(2);
			cell.setCellValue(9435701.143);
			cell = row.createCell(3);
			cell.setCellValue(9435701.143);
			cell = row.createCell(4);
			cell.setCellValue(9435701.143);
			cell = row.createCell(5);
			cell.setCellValue(9435701.143);
			cell = row.createCell(6);
			cell.setCellValue(9435701.143);
			// 第四行,人口平局
			row = sheet.createRow(4);
			cell = row.createCell(0);
			cell.setCellValue(22475821.29);
			cell = row.createCell(1);
			cell.setCellValue(22475821.29);
			cell = row.createCell(2);
			cell.setCellValue(22475821.29);
			cell = row.createCell(3);
			cell.setCellValue(22475821.29);
			cell = row.createCell(4);
			cell.setCellValue(22475821.29);
			cell = row.createCell(5);
			cell.setCellValue(22475821.29);
			cell = row.createCell(6);
			cell.setCellValue(22475821.29);
 
			//创建一个画布
			XSSFDrawing drawing = sheet.createDrawingPatriarch();
			//前四个默认0,[0,5]:从0列5行开始;[7,26]:到7列26行结束
			//默认宽度(14-8)*12
			XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 7, 26);
			//创建一个chart对象
			XSSFChart chart = drawing.createChart(anchor);
			//标题
			chart.setTitleText("地区排名前七的国家");
			//标题覆盖
			chart.setTitleOverlay(false);
			
			//图例位置
			XDDFChartLegend legend = chart.getOrAddLegend();
			legend.setPosition(LegendPosition.TOP);
			
			//分类轴标(X轴),标题位置
			XDDFCategoryAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);
			bottomAxis.setTitle("国家");
			//值(Y轴)轴,标题位置
			XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);
			leftAxis.setTitle("面积和人口");
 
			//CellRangeAddress(起始行号,终止行号, 起始列号,终止列号)
			//分类轴标(X轴)数据,单元格范围位置[0, 0]到[0, 6]
			XDDFDataSource<String> countries = XDDFDataSourcesFactory.fromStringCellRange(sheet, new CellRangeAddress(0, 0, 0, 6));
//			XDDFCategoryDataSource countries = XDDFDataSourcesFactory.fromArray(new String[] {"俄罗斯","加拿大","美国","中国","巴西","澳大利亚","印度"});
			//数据1,单元格范围位置[1, 0]到[1, 6]
			XDDFNumericalDataSource<Double> area = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(1, 1, 0, 6));
//			XDDFNumericalDataSource<Integer> area = XDDFDataSourcesFactory.fromArray(new Integer[] {17098242,9984670,9826675,9596961,8514877,7741220,3287263});
			
			//数据2,单元格范围位置[2, 0]到[2, 6]
			XDDFNumericalDataSource<Double> population = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(2, 2, 0, 6));
 
			//bar:条形图,
			XDDFBarChartData bar = (XDDFBarChartData) chart.createData(ChartTypes.BAR, bottomAxis, leftAxis);
			
			leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
			leftAxis.setCrossBetween(AxisCrossBetween.BETWEEN);
			//设置为可变颜色
			bar.setVaryColors(true);
			//条形图方向,纵向/横向:纵向
			bar.setBarDirection(BarDirection.COL);
 
			//图表加载数据,条形图1
			XDDFBarChartData.Series series1 = (XDDFBarChartData.Series) bar.addSeries(countries, area);
			//条形图例标题
			series1.setTitle("面积", null);
			XDDFSolidFillProperties fill = new XDDFSolidFillProperties(XDDFColor.from(PresetColor.RED));
			//条形图,填充颜色
			series1.setFillProperties(fill);
			
 
			//图表加载数据,条形图2
			XDDFBarChartData.Series series2 = (XDDFBarChartData.Series) bar.addSeries(countries, population);
			//条形图例标题
			series2.setTitle("人口", null);
			XDDFSolidFillProperties fill2 = new XDDFSolidFillProperties(XDDFColor.from(PresetColor.BLUE));
			//条形图,填充颜色
			series2.setFillProperties(fill2);
			
			CTPlotArea plotArea = chart.getCTChart().getPlotArea();
			//绘制
			chart.plot(bar);
			
			
			// 堆积条形图,将2个条形图重叠起来
			bar.setBarGrouping(BarGrouping.STACKED);
			//修正重叠,使钢筋真正堆叠而不是并排
			plotArea.getBarChartArray(0).addNewOverlap().setVal((byte) 100);
			
			
			//柱状图1上显示数值
			plotArea.getBarChartArray(0).getSerArray(0).addNewDLbls();
			plotArea.getBarChartArray(0).getSerArray(0).getDLbls().addNewShowVal().setVal(true);
			plotArea.getBarChartArray(0).getSerArray(0).getDLbls().addNewShowLegendKey().setVal(false);
			plotArea.getBarChartArray(0).getSerArray(0).getDLbls().addNewShowCatName().setVal(false);
			plotArea.getBarChartArray(0).getSerArray(0).getDLbls().addNewShowSerName().setVal(false);
			
			//柱状图2上显示数值
			plotArea.getBarChartArray(0).getSerArray(1).addNewDLbls();
			plotArea.getBarChartArray(0).getSerArray(1).getDLbls().addNewShowVal().setVal(true);
			plotArea.getBarChartArray(0).getSerArray(1).getDLbls().addNewShowLegendKey().setVal(false);
			plotArea.getBarChartArray(0).getSerArray(1).getDLbls().addNewShowCatName().setVal(false);
			plotArea.getBarChartArray(0).getSerArray(1).getDLbls().addNewShowSerName().setVal(false);
			
			
			//给每个条形图设置颜色
			CTBarSer ser = plotArea.getBarChartArray(0).getSerArray(0);
			//黑色
			CTDPt dpt = ser.addNewDPt();
			dpt.addNewIdx().setVal(0);
			dpt.addNewSpPr().addNewSolidFill().addNewSrgbClr().setVal(new byte[] { (byte) 0, (byte) 0, (byte) 0 });
			//红色
			dpt = ser.addNewDPt();
			dpt.addNewIdx().setVal(1);
			dpt.addNewSpPr().addNewSolidFill().addNewSrgbClr().setVal(new byte[] { (byte) 255, (byte) 0, (byte) 0 });
			//蓝色
			dpt = ser.addNewDPt();
			dpt.addNewIdx().setVal(2);
			dpt.addNewSpPr().addNewSolidFill().addNewSrgbClr().setVal(new byte[] { (byte) 0, (byte) 0, (byte) 255 });
			//绿色
			dpt = ser.addNewDPt();
			dpt.addNewIdx().setVal(3);
			dpt.addNewSpPr().addNewSolidFill().addNewSrgbClr().setVal(new byte[] { (byte) 0, (byte) 128, (byte) 0 });
			//黄色
			dpt = ser.addNewDPt();
			dpt.addNewIdx().setVal(4);
			dpt.addNewSpPr().addNewSolidFill().addNewSrgbClr().setVal(new byte[] { (byte) 255, (byte) 255, (byte) 0 });
			//紫色
			dpt = ser.addNewDPt();
			dpt.addNewIdx().setVal(5);
			dpt.addNewSpPr().addNewSolidFill().addNewSrgbClr().setVal(new byte[] { (byte) 80, (byte) 0, (byte) 80 });
			//棕色
			dpt = ser.addNewDPt();
			dpt.addNewIdx().setVal(6);
			dpt.addNewSpPr().addNewSolidFill().addNewSrgbClr().setVal(new byte[] { (byte) 165, (byte) 42, (byte) 42 });
			
			//边框
			//ser.getSpPr().addNewLn().addNewSolidFill().addNewSrgbClr().setVal(new byte[] { (byte) 0, (byte) 0, (byte) 0 });
			
			// 打印图表的xml
			//System.out.println(chart.getCTChart());
			
			// 将输出写入excel文件
			String filename = "排行榜前七的国家.xlsx";
			fileOut = new FileOutputStream(filename);
			wb.write(fileOut);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			wb.close();
			if (fileOut != null) {
				fileOut.close();
			}
		}
 
	}
 
}

 

 

 

3、poi 柱状图、条形图自定义颜色,并列条形图,显示数值。

package com.poi.test;


import java.io.FileOutputStream;
import java.io.IOException;
 
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xddf.usermodel.PresetColor;
import org.apache.poi.xddf.usermodel.XDDFColor;
import org.apache.poi.xddf.usermodel.XDDFSolidFillProperties;
import org.apache.poi.xddf.usermodel.chart.AxisCrossBetween;
import org.apache.poi.xddf.usermodel.chart.AxisCrosses;
import org.apache.poi.xddf.usermodel.chart.AxisPosition;
import org.apache.poi.xddf.usermodel.chart.BarDirection;
import org.apache.poi.xddf.usermodel.chart.BarGrouping;
import org.apache.poi.xddf.usermodel.chart.ChartTypes;
import org.apache.poi.xddf.usermodel.chart.LegendPosition;
import org.apache.poi.xddf.usermodel.chart.XDDFBarChartData;
import org.apache.poi.xddf.usermodel.chart.XDDFCategoryAxis;
import org.apache.poi.xddf.usermodel.chart.XDDFChartLegend;
import org.apache.poi.xddf.usermodel.chart.XDDFDataSource;
import org.apache.poi.xddf.usermodel.chart.XDDFDataSourcesFactory;
import org.apache.poi.xddf.usermodel.chart.XDDFNumericalDataSource;
import org.apache.poi.xddf.usermodel.chart.XDDFValueAxis;
import org.apache.poi.xssf.usermodel.XSSFChart;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFDrawing;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTBarSer;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTDPt;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTPlotArea;
import org.openxmlformats.schemas.drawingml.x2006.chart.STDLblPos;
 
public class ApachePoiBarChart {
 
	public static void main(String[] args) throws IOException {
		XSSFWorkbook wb = new XSSFWorkbook();
		String sheetName = "Sheet1";
		FileOutputStream fileOut = null;
		try {
			XSSFSheet sheet = wb.createSheet(sheetName);
			//第一行,国家名称
			Row row = sheet.createRow(0);
			Cell cell = row.createCell(0);
			cell.setCellValue("俄罗斯");
			cell = row.createCell(1);
			cell.setCellValue("加拿大");
			cell = row.createCell(2);
			cell.setCellValue("美国");
			cell = row.createCell(3);
			cell.setCellValue("中国");
			cell = row.createCell(4);
			cell.setCellValue("巴西");
			cell = row.createCell(5);
			cell.setCellValue("澳大利亚");
			cell = row.createCell(6);
			cell.setCellValue("印度");
			// 第二行,乡村地区
			row = sheet.createRow(1);
			cell = row.createCell(0);
			cell.setCellValue(17098242);
			cell = row.createCell(1);
			cell.setCellValue(9984670);
			cell = row.createCell(2);
			cell.setCellValue(9826675);
			cell = row.createCell(3);
			cell.setCellValue(9596961);
			cell = row.createCell(4);
			cell.setCellValue(8514877);
			cell = row.createCell(5);
			cell.setCellValue(7741220);
			cell = row.createCell(6);
			cell.setCellValue(3287263);
			// 第三行,农村人口
			row = sheet.createRow(2);
			cell = row.createCell(0);
			cell.setCellValue(14590041);
			cell = row.createCell(1);
			cell.setCellValue(35151728);
			cell = row.createCell(2);
			cell.setCellValue(32993302);
			cell = row.createCell(3);
			cell.setCellValue(14362887);
			cell = row.createCell(4);
			cell.setCellValue(21172141);
			cell = row.createCell(5);
			cell.setCellValue(25335727);
			cell = row.createCell(6);
			cell.setCellValue(13724923);
			// 第四行,面积平局
			row = sheet.createRow(3);
			cell = row.createCell(0);
			cell.setCellValue(9435701.143);
			cell = row.createCell(1);
			cell.setCellValue(9435701.143);
			cell = row.createCell(2);
			cell.setCellValue(9435701.143);
			cell = row.createCell(3);
			cell.setCellValue(9435701.143);
			cell = row.createCell(4);
			cell.setCellValue(9435701.143);
			cell = row.createCell(5);
			cell.setCellValue(9435701.143);
			cell = row.createCell(6);
			cell.setCellValue(9435701.143);
			// 第四行,人口平局
			row = sheet.createRow(4);
			cell = row.createCell(0);
			cell.setCellValue(22475821.29);
			cell = row.createCell(1);
			cell.setCellValue(22475821.29);
			cell = row.createCell(2);
			cell.setCellValue(22475821.29);
			cell = row.createCell(3);
			cell.setCellValue(22475821.29);
			cell = row.createCell(4);
			cell.setCellValue(22475821.29);
			cell = row.createCell(5);
			cell.setCellValue(22475821.29);
			cell = row.createCell(6);
			cell.setCellValue(22475821.29);
 
			//创建一个画布
			XSSFDrawing drawing = sheet.createDrawingPatriarch();
			//前四个默认0,[0,5]:从0列5行开始;[7,26]:到7列26行结束
			//默认宽度(14-8)*12
			XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 7, 26);
			//创建一个chart对象
			XSSFChart chart = drawing.createChart(anchor);
			//标题
			chart.setTitleText("地区排名前七的国家");
			//标题覆盖
			chart.setTitleOverlay(false);
			
			//图例位置
			XDDFChartLegend legend = chart.getOrAddLegend();
			legend.setPosition(LegendPosition.TOP);
			
			//分类轴标(X轴),标题位置
			XDDFCategoryAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);
			bottomAxis.setTitle("国家");
			//值(Y轴)轴,标题位置
			XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);
			leftAxis.setTitle("面积和人口");
 
			//CellRangeAddress(起始行号,终止行号, 起始列号,终止列号)
			//分类轴标(X轴)数据,单元格范围位置[0, 0]到[0, 6]
			XDDFDataSource<String> countries = XDDFDataSourcesFactory.fromStringCellRange(sheet, new CellRangeAddress(0, 0, 0, 6));
//			XDDFCategoryDataSource countries = XDDFDataSourcesFactory.fromArray(new String[] {"俄罗斯","加拿大","美国","中国","巴西","澳大利亚","印度"});
			//数据1,单元格范围位置[1, 0]到[1, 6]
			XDDFNumericalDataSource<Double> area = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(1, 1, 0, 6));
//			XDDFNumericalDataSource<Integer> area = XDDFDataSourcesFactory.fromArray(new Integer[] {17098242,9984670,9826675,9596961,8514877,7741220,3287263});
			
			//数据2,单元格范围位置[2, 0]到[2, 6]
			XDDFNumericalDataSource<Double> population = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(2, 2, 0, 6));
 
			//bar:条形图,
			XDDFBarChartData bar = (XDDFBarChartData) chart.createData(ChartTypes.BAR, bottomAxis, leftAxis);
			
			leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
			leftAxis.setCrossBetween(AxisCrossBetween.BETWEEN);
			//设置为可变颜色
			bar.setVaryColors(true);
			//条形图方向,纵向/横向:纵向
			bar.setBarDirection(BarDirection.COL);
 
			//图表加载数据,条形图1
			XDDFBarChartData.Series series1 = (XDDFBarChartData.Series) bar.addSeries(countries, area);
			//条形图例标题
			series1.setTitle("面积", null);
			byte[] cbyte = new byte[] { (byte) 0, (byte) 255, (byte) 255 };
			XDDFSolidFillProperties fill = new XDDFSolidFillProperties(XDDFColor.from(cbyte));
			//XDDFSolidFillProperties fill = new XDDFSolidFillProperties(XDDFColor.from(PresetColor.RED));
			//条形图,填充颜色
			series1.setFillProperties(fill);
			
 
			//图表加载数据,条形图2
			XDDFBarChartData.Series series2 = (XDDFBarChartData.Series) bar.addSeries(countries, population);
			//条形图例标题
			series2.setTitle("人口", null);
			XDDFSolidFillProperties fill2 = new XDDFSolidFillProperties(XDDFColor.from(PresetColor.BLUE));
			//条形图,填充颜色
			series2.setFillProperties(fill2);
			
			CTPlotArea plotArea = chart.getCTChart().getPlotArea();
			//绘制
			chart.plot(bar);
			
			
			// 堆积条形图,将2个条形图重叠起来
			//bar.setBarGrouping(BarGrouping.STACKED);
			//修正重叠,使钢筋真正堆叠而不是并排
			//plotArea.getBarChartArray(0).addNewOverlap().setVal((byte) 100);
			
			
			//柱状图1上显示数值
			plotArea.getBarChartArray(0).getSerArray(0).addNewDLbls();
			plotArea.getBarChartArray(0).getSerArray(0).getDLbls().addNewShowVal().setVal(true);
			plotArea.getBarChartArray(0).getSerArray(0).getDLbls().addNewShowLegendKey().setVal(false);
			plotArea.getBarChartArray(0).getSerArray(0).getDLbls().addNewShowCatName().setVal(false);
			plotArea.getBarChartArray(0).getSerArray(0).getDLbls().addNewShowSerName().setVal(false);
			
			//柱状图2上显示数值
			plotArea.getBarChartArray(0).getSerArray(1).addNewDLbls();
			plotArea.getBarChartArray(0).getSerArray(1).getDLbls().addNewShowVal().setVal(true);
			plotArea.getBarChartArray(0).getSerArray(1).getDLbls().addNewShowLegendKey().setVal(false);
			plotArea.getBarChartArray(0).getSerArray(1).getDLbls().addNewShowCatName().setVal(false);
			plotArea.getBarChartArray(0).getSerArray(1).getDLbls().addNewShowSerName().setVal(false);
			
			
			//给每个条形图设置颜色
			CTBarSer ser = plotArea.getBarChartArray(0).getSerArray(0);
			//黑色
			CTDPt dpt = ser.addNewDPt();
			dpt.addNewIdx().setVal(0);
			dpt.addNewSpPr().addNewSolidFill().addNewSrgbClr().setVal(new byte[] { (byte) 0, (byte) 0, (byte) 0 });
			//红色
			dpt = ser.addNewDPt();
			dpt.addNewIdx().setVal(1);
			dpt.addNewSpPr().addNewSolidFill().addNewSrgbClr().setVal(new byte[] { (byte) 255, (byte) 0, (byte) 0 });
			//蓝色
			dpt = ser.addNewDPt();
			dpt.addNewIdx().setVal(2);
			dpt.addNewSpPr().addNewSolidFill().addNewSrgbClr().setVal(new byte[] { (byte) 0, (byte) 0, (byte) 255 });
			//绿色
			dpt = ser.addNewDPt();
			dpt.addNewIdx().setVal(3);
			dpt.addNewSpPr().addNewSolidFill().addNewSrgbClr().setVal(new byte[] { (byte) 0, (byte) 128, (byte) 0 });
			//黄色
			dpt = ser.addNewDPt();
			dpt.addNewIdx().setVal(4);
			dpt.addNewSpPr().addNewSolidFill().addNewSrgbClr().setVal(new byte[] { (byte) 255, (byte) 255, (byte) 0 });
			//紫色
			dpt = ser.addNewDPt();
			dpt.addNewIdx().setVal(5);
			dpt.addNewSpPr().addNewSolidFill().addNewSrgbClr().setVal(new byte[] { (byte) 80, (byte) 0, (byte) 80 });
			//棕色
			dpt = ser.addNewDPt();
			dpt.addNewIdx().setVal(6);
			dpt.addNewSpPr().addNewSolidFill().addNewSrgbClr().setVal(new byte[] { (byte) 165, (byte) 42, (byte) 42 });
			
			//边框
			//ser.getSpPr().addNewLn().addNewSolidFill().addNewSrgbClr().setVal(new byte[] { (byte) 0, (byte) 0, (byte) 0 });
			
			// 打印图表的xml
			//System.out.println(chart.getCTChart());
 
			// 将输出写入excel文件
			String filename = "排行榜前七的国家.xlsx";
			fileOut = new FileOutputStream(filename);
			wb.write(fileOut);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			wb.close();
			if (fileOut != null) {
				fileOut.close();
			}
		}
 
	}
 
}

 

要设置导出柱状图间隔,可以通过设置CategoryAxis的CategoryMargin属性来实现。具体代码如下: ```java // 创建Workbook和Sheet对象 Workbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet("柱状图"); // 创建柱状图并设置数据 Drawing drawing = sheet.createDrawingPatriarch(); ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 10, 20); Chart chart = drawing.createChart(anchor); ChartLegend legend = chart.getOrCreateLegend(); legend.setPosition(LegendPosition.TOP_RIGHT); LineChartData data = chart.getChartDataFactory().createLineChartData(); ChartAxis bottomAxis = chart.getChartAxisFactory().createCategoryAxis(AxisPosition.BOTTOM); ChartAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT); leftAxis.setCrosses(AxisCrosses.AUTO_ZERO); ChartDataSource<Number> xs = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(0, 0, 0, 3)); ChartDataSource<Number> ys1 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(1, 1, 0, 3)); ChartDataSource<Number> ys2 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(2, 2, 0, 3)); data.addSeries(xs, ys1); data.addSeries(xs, ys2); chart.plot(data, bottomAxis, leftAxis); // 设置柱状图间隔 CategoryAxis axis = chart.getChartAxisFactory().createCategoryAxis(AxisPosition.BOTTOM); axis.setCrosses(AxisCrosses.AUTO_ZERO); axis.setTickLabelPosition(TickLabelPosition.NEXT_TO); axis.setTickMarkPosition(TickMarkPosition.BELOW); axis.setTickLabelSpacing(2); axis.setTickLabelRotation(45); // 输出Excel文件 FileOutputStream fileOut = new FileOutputStream("柱状图.xlsx"); workbook.write(fileOut); fileOut.close(); ``` 在上述代码中,通过设置CategoryAxis的setTickLabelSpacing方法来设置柱状图间隔,这里设置为2,即每隔2个柱子显示一个刻度。可以根据实际需求修改此值。
评论 36
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值