word poi5.1.0 How to make chart add a series

24 Views Asked by At

`My process is to locate the specified chart according to the title of the chart, then modify the corresponding Excel table data, and dynamically display a series of data

public static void replaceCharts(XWPFDocument doc, Map<String, List<Object[]>> params) throws IOException, InvalidFormatException {
        List<XWPFChart> charts = doc.getCharts();
        int z = 0;
        for (XWPFChart chart : charts) {
            z++;
            XDDFTitle title = chart.getTitle();
            List<XDDFTextParagraph> paragraphs = chart.getTitle().getBody().getParagraphs();
            for (XDDFTextParagraph paragraph : paragraphs) {
                System.out.println(paragraph.getText());
                List<Object[]> tableData = params.get(paragraph.getText());
                if (tableData != null) {
                    XSSFWorkbook workbook = chart.getWorkbook();
                    XSSFSheet itemSheet = workbook.getSheetAt(0);
                    int lastNum = itemSheet.getLastRowNum() + 1;
                    int start = 0;
                    while (start <= lastNum) {
                        XSSFRow row = itemSheet.getRow(start);
                        if (row != null) {
                            itemSheet.removeRow(row);
                        }
                        start++;
                    }
                    for (int i = 0; i < tableData.size(); i++) {
                        Object[] tableItem = tableData.get(i);
                        XSSFRow row = itemSheet.createRow(i);
                        for (int j = 0; j < tableItem.length; j++) {
                            XSSFCell cell = row.createCell(j);
                            if (tableItem[j] instanceof Integer) {
                                cell.setCellValue((Integer) tableItem[j]);
                            } else if (tableItem[j] instanceof String) {
                                cell.setCellValue(String.valueOf(tableItem[j]));
                            }
                            System.out.println(row.getCell(j));

                        }
                    }


                    // 定义新的数据范围
                    XDDFChartData chartData = chart.getChartSeries().get(0);
                    System.out.println(chartData.getSeriesCount());
                    for (int i = 0; i < 4; i++) {
                        XDDFDataSource cat0 = XDDFDataSourcesFactory.fromStringCellRange(itemSheet,
                                new CellRangeAddress(1, 6, 0, 0));
                        XDDFNumericalDataSource val0 = XDDFDataSourcesFactory.fromNumericCellRange(itemSheet,
                                new CellRangeAddress(1, 6, i + 1, i + 1));
                        XDDFChartData.Series series = chartData.addSeries(cat0, val0);
                        series.setTitle("series" + i, null);
                        int pointCount = val0.getPointCount();
                        System.out.println(pointCount);
                        XDDFNumericalDataSource<? extends Number> valuesData = series.getValuesData();
                        series.plot();

                    }


                    // 刷新图表
                    chart.plot(chartData);


                }


            }

        }
    }

Exception

java.lang.IndexOutOfBoundsException at org.openxmlformats.schemas.drawingml.x2006.chart.impl.CTNumDataImpl.getPtArray(CTNumDataImpl.java:204) at org.apache.poi.xddf.usermodel.chart.XDDFDataSourcesFactory$5.getPointAt(XDDFDataSourcesFactory.java:238) at org.apache.poi.xddf.usermodel.chart.XDDFDataSourcesFactory$5.getPointAt(XDDFDataSourcesFactory.java:202) at org.apache.poi.xddf.usermodel.chart.XDDFDataSource.fillNumericalCache(XDDFDataSource.java:76) at org.apache.poi.xddf.usermodel.chart.XDDFChartData$Series.plot(XDDFChartData.java:228) at org.apache.poi.xddf.usermodel.chart.XDDFChart.plot(XDDFChart.java:414)

0

There are 0 best solutions below