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>

2、POI折线图

package 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.PresetLineDash;
import org.apache.poi.xddf.usermodel.XDDFLineProperties;
import org.apache.poi.xddf.usermodel.XDDFPresetLineDash;
import org.apache.poi.xddf.usermodel.chart.AxisPosition;
import org.apache.poi.xddf.usermodel.chart.ChartTypes;
import org.apache.poi.xddf.usermodel.chart.LegendPosition;
import org.apache.poi.xddf.usermodel.chart.MarkerStyle;
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.XDDFLineChartData;
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;

/**
 * POI折线图
 */
public class ApachePoiLineChart4 {

	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));
			// 数据1,单元格范围位置[1, 0]到[1, 6]
			XDDFNumericalDataSource<Double> area = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(1, 1, 0, 6));
			// 数据1,单元格范围位置[2, 0]到[2, 6]
			XDDFNumericalDataSource<Double> population = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(2, 2, 0, 6));

			// LINE:折线图,
			XDDFLineChartData data = (XDDFLineChartData) chart.createData(ChartTypes.LINE, bottomAxis, leftAxis);

			// 图表加载数据,折线1
			XDDFLineChartData.Series series1 = (XDDFLineChartData.Series) data.addSeries(countries, area);
			// 折线图例标题
			series1.setTitle("面积", null);
			// 直线
			series1.setSmooth(false);
			// 设置标记大小
			series1.setMarkerSize((short) 6);
			// 设置标记样式,星星
			series1.setMarkerStyle(MarkerStyle.STAR);

			// 图表加载数据,折线2
			XDDFLineChartData.Series series2 = (XDDFLineChartData.Series) data.addSeries(countries, population);
			// 折线图例标题
			series2.setTitle("人口", null);
			// 曲线
			series2.setSmooth(true);
			// 设置标记大小
			series2.setMarkerSize((short) 6);
			// 设置标记样式,正方形
			series2.setMarkerStyle(MarkerStyle.SQUARE);

			// 图表加载数据,平均线3
			// 数据1,单元格范围位置[2, 0]到[2, 6]
			XDDFNumericalDataSource<Double> population3 = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(3, 3, 0, 6));
			XDDFLineChartData.Series series3 = (XDDFLineChartData.Series) data.addSeries(countries, population3);
			// 折线图例标题
			series3.setTitle("面积平均", null);
			// 直线
			series3.setSmooth(false);
			// 设置标记大小
			// series3.setMarkerSize((short) 3);
			// 设置标记样式,正方形
			series3.setMarkerStyle(MarkerStyle.NONE);
			// 折线图LineChart
			// XDDFSolidFillProperties fill = new XDDFSolidFillProperties(XDDFColor.from(PresetColor.CHARTREUSE));
			XDDFLineProperties line = new XDDFLineProperties();
			// line.setFillProperties(fill);
			// line.setLineCap(LineCap.ROUND);
			line.setPresetDash(new XDDFPresetLineDash(PresetLineDash.DOT));// 虚线
			// XDDFShapeProperties shapeProperties = new XDDFShapeProperties();
			// shapeProperties.setLineProperties(line);
			// series3.setShapeProperties(shapeProperties);
			series3.setLineProperties(line);

			// 图表加载数据,平均线3
			// 数据1,单元格范围位置[2, 0]到[2, 6]
			XDDFNumericalDataSource<Double> population4 = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(4, 4, 0, 6));
			XDDFLineChartData.Series series4 = (XDDFLineChartData.Series) data.addSeries(countries, population4);
			// 折线图例标题
			series4.setTitle("人口平均", null);
			// 直线
			series4.setSmooth(false);
			// 设置标记大小
			// series4.setMarkerSize((short) 3);
			// 设置标记样式,正方形
			series4.setMarkerStyle(MarkerStyle.NONE);
			XDDFLineProperties line4 = new XDDFLineProperties();
			line4.setPresetDash(new XDDFPresetLineDash(PresetLineDash.DOT));// 虚线
			series4.setLineProperties(line);

			// 绘制
			chart.plot(data);

			// 打印图表的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();
			}
		}

	}

}

 

 

 

 

 

 

 

您可以使用POI库来导出Excel折线图,并且可以设置线条为虚线。下面是一个示例代码,展示了如何使用POI来实现这个功能: ```java import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.*; import java.io.FileOutputStream; import java.io.IOException; public class ExcelChartExample { public static void main(String[] args) { // 创建一个新的Excel工作簿 Workbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet("折线图示例"); // 创建一个数据源 Row row1 = sheet.createRow(0); row1.createCell(0).setCellValue(1); row1.createCell(1).setCellValue(2); row1.createCell(2).setCellValue(3); row1.createCell(3).setCellValue(4); Row row2 = sheet.createRow(1); row2.createCell(0).setCellValue(5); row2.createCell(1).setCellValue(6); row2.createCell(2).setCellValue(7); row2.createCell(3).setCellValue(8); // 创建一个折线图 Drawing drawing = sheet.createDrawingPatriarch(); ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 4, 0, 10, 20); Chart chart = drawing.createChart(anchor); ChartLegend legend = chart.getOrCreateLegend(); legend.setPosition(LegendPosition.BOTTOM); LineChartData data = chart.getChartDataFactory().createLineChartData(); // 创建折线 ChartAxis bottomAxis = chart.getChartAxisFactory().createCategoryAxis(AxisPosition.BOTTOM); ValueAxis 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)); LineChartSeries series1 = data.addSeries(xs, ys1); series1.setTitle(sheet.getRow(1).getCell(0).getStringCellValue()); LineChartSeries series2 = data.addSeries(xs, ys2); series2.setTitle(sheet.getRow(2).getCell(0).getStringCellValue()); // 设置线条样式为虚线 CTChartLines lines1 = series1.getCTLineSer().addNewSpPr().addNewLn().addNewPr(); lines1.addNewDash().setVal(new byte[]{4, 4}); CTChartLines lines2 = series2.getCTLineSer().addNewSpPr().addNewLn().addNewPr(); lines2.addNewDash().setVal(new byte[]{4, 4}); // 将图表添加到工作表 chart.plot(data, bottomAxis, leftAxis); // 保存Excel文件 try { FileOutputStream fileOut = new FileOutputStream("折线图示例.xlsx"); workbook.write(fileOut); fileOut.close(); workbook.close(); System.out.println("Excel文件导出成功!"); } catch (IOException e) { e.printStackTrace(); } } } ``` 这个示例代码会创建一个包含折线图的Excel文件,并且将线条样式设置为虚线。您可以根据需要修改数据源和线条样式的设置。请确保将POI库添加到项目的依赖中。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值