I am creating a LineChart using Apache POI in Excel document. As far as I've managed to achieve is in the below image:
我在Excel文档中创建了一个使用Apache POI的LineChart。就我所能做到的而言,如下图所示:
I wrote the code using examples from Apache's svn, so my current approach looks like this:
我使用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);
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.
我找不到的是如何得到默认的“系列1”,“系列2”,……,从列中取“系列n”名称作为我的值,在本例中取“回答选项”。在当前的API中似乎没有任何方法可以指定系列的名称。
Can anybody assist me with this, please?
有人能帮我一下吗?
2 个解决方案
#1
13
It was pretty straighforward, just instead of using:
它非常直接,只是代替了:
data.addSerie(xs, ys);
I had to be using:
我必须使用:
LineChartSerie chartSerie = data.addSerie(xs, ys);
chartSerie.setTitle("My Title");
Didn't look at the API that using data.addSerie(xs, ys);
returns a single LineChartSerie
object onto which I can set a title.
没有查看使用数据的API。addSerie(x,y);返回一个LineChartSerie对象,我可以在其中设置标题。
#2
2
From Apache POI version 3.16 onwards, use setTitleText("Title Name") instead. Also the correct class name is LineChartSeries not LineChartSerie. So, the above solution would look like:
从Apache POI 3.16版本开始,使用setTitleText(“Title Name”)代替。正确的类名是LineChartSeries而不是LineChartSerie。因此,上面的解应该是:
LineChartSeries chartSeries = data.addSeries(xs,ys);
chartSeries.setTitleText("My Title");
for Apache POI 3.16 onwards.
Apache POI 3.16开始。
#1
13
It was pretty straighforward, just instead of using:
它非常直接,只是代替了:
data.addSerie(xs, ys);
I had to be using:
我必须使用:
LineChartSerie chartSerie = data.addSerie(xs, ys);
chartSerie.setTitle("My Title");
Didn't look at the API that using data.addSerie(xs, ys);
returns a single LineChartSerie
object onto which I can set a title.
没有查看使用数据的API。addSerie(x,y);返回一个LineChartSerie对象,我可以在其中设置标题。
#2
2
From Apache POI version 3.16 onwards, use setTitleText("Title Name") instead. Also the correct class name is LineChartSeries not LineChartSerie. So, the above solution would look like:
从Apache POI 3.16版本开始,使用setTitleText(“Title Name”)代替。正确的类名是LineChartSeries而不是LineChartSerie。因此,上面的解应该是:
LineChartSeries chartSeries = data.addSeries(xs,ys);
chartSeries.setTitleText("My Title");
for Apache POI 3.16 onwards.
Apache POI 3.16开始。