我正在Excel文档中使用Apache POI创建一个LineChart.据我所设想的,在下图中:
我使用Apache的svn中的示例编写了代码,所以我目前的方法看起来像这样:
Drawing drawing = question.createDrawingPatriarch();
ClientAnchor anchor = drawing.createAnchor(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,1));
for (int i = 9; i <= rowNum; i ++) {
ChartDataSource<Number> ys = DataSources.fromNumericCellRange(question,new CellRangeAddress(i,i,1));
data.addSerie(xs,ys);
}
chart.plot(data,bottomAxis,leftAxis);
我找不到的是如何从列中获取默认的“系列1”,“系列2”,…,“系列n”名称作为我的值,在这种情况下是从“答案选项” .而目前的API中似乎没有任何方法来指定系列的名称.
有人可以帮我吗?
解决方法
这是非常直截了当的,而不是使用:
data.addSerie(xs,ys);
我不得不使用:
LineChartSerie chartSerie = data.addSerie(xs,ys);
chartSerie.setTitle("My Title");
没有看到使用data.addSerie(xs,ys)的API;返回一个可以设置标题的LineChartSerie对象.