java POI 设置导出柱状图间隔
时间: 2023-10-27 15:20:54 浏览: 156
POI 导出excel
要设置导出柱状图间隔,可以通过设置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个柱子显示一个刻度。可以根据实际需求修改此值。
阅读全文