Apache POI散点图创建 [英] Apache POI scatter chart creation

查看:349
本文介绍了Apache POI散点图创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Currently generating a scatterplot using POI, however the chart comes out weird: The topleft one is generated using my code, the other one is made manually in Excel. They are both of the type "scatter with straight lines and markers", the generated chart however shows curved lines for some reason. Another issue is that each of the data-points is listed separately in the legend as well as given another colour.

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和其他,我应该改用XDDFScatterChartData.但是我不知道在哪里可以获取.jar来使用它.我以为它是Beta版?

Using Apache POI 3.17 Documentation here shows that XSSFScatterChartData and others are deprecated, and that I should be using XDDFScatterChartData instead. However I cannot figure out where to get the .jar to use this. I'm assuming it's in beta?

我想要做的是生成类似图像右侧的图表(手动创建的图表)的内容.由于POI允许创建图表似乎是一个相对较新的事物,因此我一直找不到任何线索.有人知道这个窍门吗?

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?

推荐答案

在新版本的Excel中,问题已更改为Excel图表的默认值.

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

有一个设置可以使散点图中的线条平滑. Apache poi没有设置.但现在在Excel的较新版本中,如果未设置此选项,则默认为true.

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.

还有一个用于更改每个数据点颜色的设置.同样,apache poi不会设置它.但是,在新的Excel版本中,如果未设置,则此选项默认为true.

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.

因此我们需要根据需要将这两个选项都设置为false:

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文档,而是用于当前夜间构建"的POI API文档.因此,对于使用XDDF而不是XSSF,需要使用夜间构建,该构建当然不是不是稳定版本,应在生产代码中使用.

Unfortunately the https://poi.apache.org/apidocs/ is not the POI API Documentation for the latest stable release but for the current "Nightly Build". So for using the XDDF instead of XSSF one needs using the nightly builds which of course are not stable releases which should be used in productive code.

这篇关于Apache POI散点图创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆