2014-02-18 9 views
9

Ich erstelle ein LineChart mit Apache POI in Excel-Dokument. Soweit ich es geschafft habe, ist in dem Bild unten zu erreichen:Apache POI fügen Sie einen Seriennamen in LineChart hinzu

enter image description here

ich den Code schrieb Beispiele von Apache SVN verwenden, so meine aktuelle Ansatz wie folgt aussieht:

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

Was Ich kann nicht finden, wie Standard-"Series 1", "Series 2", ..., "Series n" Namen als meine Werte aus den Spalten übernommen werden, in diesem Fall von: "Antwortoptionen". Und es scheint keine Methoden in der aktuellen API zu geben, wie man Namen der Serie spezifiziert.

Kann mir bitte jemand helfen?

Antwort

13

Es war ziemlich straighforward, nur statt mit:

data.addSerie(xs, ys); 

ich verwenden musste:

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

nicht an der API aussah, das data.addSerie(xs, ys); gibt ein einzelnes LineChartSerie-Objekt auf womit ich einen Titel setzen kann.

2

Verwenden Sie ab Apache POI Version 3.16 stattdessen setTitleText ("Titelname"). Auch der korrekte Klassenname ist LineChartSeries nicht LineChartSerie. So würde die obige Lösung wie folgt aussehen:

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

für Apache POI 3,16 ab.