Apache POI将一个系列名称添加到LineChart中

我正在Excel文档中使用Apache POI创build一个LineChart。 据我所能达到的是在下面的图像:

在这里输入图像说明

我使用Apache svn的例子编写代码,所以我现在的方法是这样的:

Drawing drawing = question.createDrawingPatriarch(); ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 4, 8, 14, 18); 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); bottomAxis.setCrosses(AxisCrosses.AUTO_ZERO); ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT); leftAxis.setCrosses(AxisCrosses.AUTO_ZERO); List<ReportQuestionModel> questionModels = groupModel.getQuestionModels(); for (ReportQuestionModel questionModel : questionModels) { List<ReportOptionModel> optionModels = questionModel.getOptionModels(); for (ReportOptionModel optionModel : optionModels) { rowNum++; XSSFRow optionRow = question.createRow(rowNum); XSSFCell optionsCell = optionRow.createCell(0); optionsCell.setCellValue(optionModel.getAnswerText()); long count = optionModel.getCount(); totalResponses += count; XSSFCell optionsCountCell = optionRow.createCell(1); optionsCountCell.setCellValue(count); XSSFCell optionsPercentageCell = optionRow.createCell(2); optionsPercentageCell.setCellValue(optionModel.getPercentage()); } } ChartDataSource<Number> xs = DataSources.fromNumericCellRange(question, new CellRangeAddress(8, 8, 0, 1)); for (int i = 9; i <= rowNum; i ++) { ChartDataSource<Number> ys = DataSources.fromNumericCellRange(question, new CellRangeAddress(i, i, 0, 1)); data.addSerie(xs, ys); } chart.plot(data, bottomAxis, leftAxis); 

我找不到默认的"Series 1", "Series 2", ..., "Series n"名称作为我的值从列中取得,在这种情况下从“答案选项” 。 而且在当前的API中似乎没有任何方法来指定这个系列的名字。

有人可以帮我吗?

这很直接,而不是使用:

 data.addSerie(xs, ys); 

我不得不使用:

 LineChartSerie chartSerie = data.addSerie(xs, ys); chartSerie.setTitle("My Title"); 

没有看看使用data.addSerie(xs, ys);的API data.addSerie(xs, ys); 返回一个LineChartSerie对象,我可以在其中设置标题。

从Apache POI版本3.16开始,请改用setTitleText(“Title Name”)。 另外正确的类名是LineChartSeries而不是LineChartSerie。 所以,上面的解决scheme看起来像:

 LineChartSeries chartSeries = data.addSeries(xs,ys); chartSeries.setTitleText("My Title"); 

为Apache POI 3.16起。