java – Apache POI将一个系列名称添加到LineChart中

前端之家收集整理的这篇文章主要介绍了java – Apache POI将一个系列名称添加到LineChart中前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在Excel文档中使用Apache POI创建一个LineChart.据我所设想的,在下图中:

我使用Apache的svn中的示例编写了代码,所以我目前的方法看起来像这样:

  1. Drawing drawing = question.createDrawingPatriarch();
  2. ClientAnchor anchor = drawing.createAnchor(0,4,8,14,18);
  3.  
  4. Chart chart = drawing.createChart(anchor);
  5. ChartLegend legend = chart.getOrCreateLegend();
  6. legend.setPosition(LegendPosition.TOP_RIGHT);
  7.  
  8. LineChartData data = chart.getChartDataFactory().createLineChartData();
  9.  
  10. ChartAxis bottomAxis = chart.getChartAxisFactory().createCategoryAxis(AxisPosition.BOTTOM);
  11. bottomAxis.setCrosses(AxisCrosses.AUTO_ZERO);
  12. ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT);
  13. leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
  14.  
  15. List<ReportQuestionModel> questionModels = groupModel.getQuestionModels();
  16. for (ReportQuestionModel questionModel : questionModels) {
  17.  
  18. List<ReportOptionModel> optionModels = questionModel.getOptionModels();
  19. for (ReportOptionModel optionModel : optionModels) {
  20. rowNum++;
  21.  
  22. XSSFRow optionRow = question.createRow(rowNum);
  23.  
  24. XSSFCell optionsCell = optionRow.createCell(0);
  25. optionsCell.setCellValue(optionModel.getAnswerText());
  26.  
  27. long count = optionModel.getCount();
  28. totalResponses += count;
  29.  
  30. XSSFCell optionsCountCell = optionRow.createCell(1);
  31. optionsCountCell.setCellValue(count);
  32.  
  33. XSSFCell optionsPercentageCell = optionRow.createCell(2);
  34. optionsPercentageCell.setCellValue(optionModel.getPercentage());
  35. }
  36. }
  37.  
  38. ChartDataSource<Number> xs = DataSources.fromNumericCellRange(question,new CellRangeAddress(8,1));
  39. for (int i = 9; i <= rowNum; i ++) {
  40. ChartDataSource<Number> ys = DataSources.fromNumericCellRange(question,new CellRangeAddress(i,i,1));
  41. data.addSerie(xs,ys);
  42. }
  43. chart.plot(data,bottomAxis,leftAxis);

我找不到的是如何从列中获取默认的“系列1”,“系列2”,…,“系列n”名称作为我的值,在这种情况下是从“答案选项” .而目前的API中似乎没有任何方法来指定系列的名称.

有人可以帮我吗?

解决方法

这是非常直截了当的,而不是使用:
  1. data.addSerie(xs,ys);

我不得不使用:

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

没有看到使用data.addSerie(xs,ys)的API;返回一个可以设置标题的LineChartSerie对象.

猜你在找的Java相关文章