Java – Apache POI adds a series name to linechart

I am using Apache POI to create a linechart in an excel document As far as I can imagine, in the following figure:

I wrote the code using the examples in Apache's SVN, so my current method looks like this:

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);

What I can't find is how to get the default "series 1", "series 2",..., "series N" names from the column as my values, in this case from "answer options" There seems to be no way to specify the name of the series in the current API

Can someone help me?

Solution

This is very straightforward, not using:

data.addSerie(xs,ys);

I have to use:

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

I don't see using data API of addserie (XS, YS); Returns a linechartserie object that can set a title

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>