问题描述
我正在 Excel 文档中使用 Apache POI 创建折线图.就我设法实现的而言,如下图所示:
我使用 Apache 的 svn 中的示例编写了代码,因此我目前的方法如下所示:
绘图绘图 = question.createDrawingPatriarch();ClientAnchor 锚点 =drawing.createAnchor(0, 0, 0, 0, 4, 8, 14, 18);图表图表=drawing.createChart(anchor);ChartLegend 图例 = chart.getOrCreateLegend();Legend.setPosition(LegendPosition.TOP_RIGHT);LineChartData 数据 = 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);列表<ReportQuestionModel>questionModels = groupModel.getQuestionModels();for (ReportQuestionModel questionModel : questionModels) {列表<ReportOptionModel>optionModels = questionModel.getOptionModels();for (ReportOptionModel optionModel : optionModels) {行数++;XSSFRow optionRow = question.createRow(rowNum);XSSFCell optionsCell = optionRow.createCell(0);optionsCell.setCellValue(optionModel.getAnswerText());长计数 = optionModel.getCount();totalResponses += 计数;XSSFCell optionsCountCell = optionRow.createCell(1);optionsCountCell.setCellValue(count);XSSFCell optionsPercentageCell = optionRow.createCell(2);optionsPercentageCell.setCellValue(optionModel.getPercentage());}}ChartDataSourcexs = DataSources.fromNumericCellRange(question, new CellRangeAddress(8, 8, 0, 1));for (int i = 9; i <= rowNum; i ++) {ChartDataSourceys = 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.addSeries(xs, ys);
我必须使用:
LineChartSeries chartSeries = data.addSeries(xs, ys);chartSeries.setTitle("我的标题");
没有查看使用 data.addSeries(xs, ys);
返回单个 LineChartSeries
对象的 API,我可以在其上设置标题.>
I am creating a LineChart using Apache POI in Excel document. As far as I've managed to achieve is in the below image:
I wrote the code using examples from Apache's svn, so my current approach looks like this:
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);
What I can't find is how to get default "Series 1", "Series 2", ..., "Series n"
names to be taken from as my values from the columns, in this case from: "Answer options". And there doesn't seem to be any methods in the current API how to specify names of the series.
Can anybody assist me with this, please?
It was pretty straight forward, just instead of using:
data.addSeries(xs, ys);
I had to be using:
LineChartSeries chartSeries = data.addSeries(xs, ys);
chartSeries.setTitle("My Title");
Didn't look at the API that using data.addSeries(xs, ys);
returns a single LineChartSeries
object onto which I can set a title.
这篇关于Apache POI 将系列名称添加到 LineChart的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!