I try to create a pivot chart from from a pivot table with apache poi. I am able to create a chart from a fixed area in the sheet, where the pivot table should be. But i need to be able to determine the area from the pivot table.
This function creates a pivot chart from a pivot table, where the range is hard coded and this works so far:
public void createPivotChart(XSSFWorkbook workbook, XSSFPivotTable pivotTable, String pivotSheetName, ReportConfigDTO config) {
XSSFSheet chartSheet = workbook.createSheet(pivotSheetName);
//set default style for cells -> erste spalte
chartSheet.setDefaultColumnStyle(0, cellStyle);
List<ReportConfigDTO.Chart> chartsFields = config.getReportDefinition().getCharts();
int startCol = 0;
for (ReportConfigDTO.Chart chartFields : chartsFields) {
XSSFDrawing drawing = chartSheet.createDrawingPatriarch();
XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, startCol, 1, 10, 25);
XSSFChart chart = drawing.createChart(anchor);
PivotChartLegendPositionENUM legendPosition = PivotChartLegendPositionENUM.fromString(chartFields.getLegendPosition());
XDDFChartLegend legend = chart.getOrAddLegend();
legend.setPosition(LegendPosition.valueOf(legendPosition.toString()));
XDDFCategoryAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);
XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);
leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
leftAxis.setCrossBetween(AxisCrossBetween.BETWEEN);
XSSFSheet pivotSheet = (XSSFSheet) pivotTable.getParent();
//the cell range needs to be determined dynamically and should not be hard coded
XDDFDataSource<String> categories = XDDFDataSourcesFactory.fromStringCellRange(
pivotSheet, new CellRangeAddress(9, 15, 0, 0));
XDDFNumericalDataSource<Double> values = XDDFDataSourcesFactory.fromNumericCellRange(
pivotSheet, new CellRangeAddress(9, 15, 1, 1));
XDDFChartData data = chart.createData(ChartTypes.BAR, bottomAxis, leftAxis);
Series series = data.addSeries(categories, values);
series.setTitle("Charttitle", null);
if (data instanceof XDDFBarChartData) {
((XDDFBarChartData) data).setBarDirection(BarDirection.COL);
((XDDFBarChartData) data).setBarGrouping(BarGrouping.STACKED);
}
chart.plot(data);
}
How can i determine the area of the pivot table in the sheet to create the CellRangeAddress?
I tried to determine the cell reference with:
new CellReference(pivotTable.getCTPivotTableDefinition().getLocation().getRef()
but this gives me some incorrect area. (mostly [A3:B4]).
I also tried to loop through the sheet, but the sheet seems to be empty, but the creation of the chart works so far, so it can not be empty:
chartSheet.getLastRowNum() //returns -1
I read that Apache POI does not know how much space a pivot table representation takes in the sheet. is that true? Can it be determined somehow or is there another way to create the chart based on a pivot table of unknown size?
sonja is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1