Apache POI散点图创建

Zyzyx:

当前使用POI生成散点图,但是图表图表却很奇怪:左上角的一个是使用我的代码生成的,另一个是在Excel中手动生成的。它们都是“带有直线和标记的散点图”类型,但是由于某种原因,生成的图表显示了曲线。另一个问题是,每个数据点在图例中分别列出,并指定了另一种颜色。

public void GenerateChart(XSSFSheet sheet) {
    XSSFDrawing drawing = sheet.createDrawingPatriarch();
    XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 10, 15);

    XSSFChart chart = drawing.createChart(anchor);
    XSSFChartLegend legend = chart.getOrCreateLegend();
    legend.setPosition(LegendPosition.TOP_RIGHT);

    XSSFValueAxis bottomAxis = chart.createValueAxis(AxisPosition.BOTTOM);
    XSSFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);
    leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);

    CellRangeAddress crXData = new CellRangeAddress(1, sheet.getLastRowNum(), 0, 0);
    CellRangeAddress crYData = new CellRangeAddress(1, sheet.getLastRowNum(), 1, 1);
    CellReference crTitle = new CellReference(0,1);
    Cell cell = sheet.getRow(crTitle.getRow()).getCell(crTitle.getCol());

    ChartDataSource<Number> dsXData = DataSources.fromNumericCellRange(sheet, crXData);
    ChartDataSource<Number> dsYData = DataSources.fromNumericCellRange(sheet, crYData);

    XSSFScatterChartData data = chart.getChartDataFactory().createScatterChartData();
    ScatterChartSeries seriesTitler = data.addSerie(dsXData, dsYData);

    seriesTitler.setTitle(cell.getStringCellValue());
    chart.plot(data, bottomAxis, leftAxis);
}

使用Apache POI 3.17 此处的文档显示XSSFScatterChartData不推荐使用其他POI ,而应XDDFScatterChartData改用。但是我不知道在哪里可以使用.jar来使用它。我以为它是Beta版?

What I want to do is to generate something like the chart on the right of the image, the manually created one. Since POI allowing chart creation seems to be a relatively new thing, I haven't been able to find any clues. Anyone know the trick?

Axel Richter :

The problems are changed defaults for Excel charts in newer Excel versions.

There is a setting for smooth the line in scatter charts. Apache poi does not set this. But now in newer Excelversions this option defaults to true if it is not set.

Also there is a setting for vary the colors of each data point. Also apache poi does not set this. But now in newer Excelversions this option defaults to true if not set.

So we need setting both of those options to false as wanted:

...
  chart.plot(data, bottomAxis, leftAxis);

  //set properties of first scatter chart data series to not smooth the line:
  ((XSSFChart)chart).getCTChart().getPlotArea().getScatterChartArray(0).getSerArray(0)
   .addNewSmooth().setVal(false);

  //set properties of first scatter chart to not vary the colors:
  ((XSSFChart)chart).getCTChart().getPlotArea().getScatterChartArray(0)
   .addNewVaryColors().setVal(false);
...

不幸的是,https//poi.apache.org/apidocs/ 不是最新稳定版本的POI API文档,而是当前的“ Nightly Build”。因此,对于使用夜间构建XDDF而不是XSSF一个需求的情况,夜间构建当然不是稳定版本,应该在生产代码中使用。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章