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

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

问题描述

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

public void GenerateChart(XSSFSheet sheet) {XSSFDrawing 绘图 = sheet.createDrawingPatriarch();XSSFClientAnchor 锚点 =drawing.createAnchor(0, 0, 0, 0, 0, 5, 10, 15);XSSFChart 图表 =drawing.createChart(anchor);XSSFChartLegend 图例 = 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);单元格单元格 = sheet.getRow(crTitle.getRow()).getCell(crTitle.getCol());ChartDataSource<数字>dsXData = DataSources.fromNumericCellRange(sheet, crXData);ChartDataSource<数字>dsYData = DataSources.fromNumericCellRange(sheet, crYData);XSSFScatterChartData 数据 = 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 来使用它.我假设它处于测试阶段?

我想要做的是生成类似于图像右侧的图表,手动创建的图表.由于允许创建图表的 POI 似乎是一个相对较新的事物,我一直无法找到任何线索.有谁知道诀​​窍吗?

解决方案

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

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

还有一个设置可以改变每个数据点的颜色.apache poi 也没有设置这个.但现在在较新的 Excel 版本中,如果未设置,则此选项默认为 true.

所以我们需要根据需要将这两个选项设置为 false:

<预><代码>...chart.plot(data, bottomAxis, leftAxis);//将第一个散点图数据系列的属性设置为不平滑线:((XSSFChart)chart).getCTChart().getPlotArea().getScatterChartArray(0).getSerArray(0).addNewSmooth().setVal(false);//设置第一个散点图的属性不改变颜色:((XSSFChart)chart).getCTChart().getPlotArea().getScatterChartArray(0).addNewVaryColors().setVal(false);...

不幸的是 https://poi.apache.org/apidocs/不是最新稳定版本的 POI API 文档,而是当前的Nightly Build".因此,要使用 XDDF 而不是 XSSF 需要使用夜间构建,这当然是稳定版本,应该在生产代码中使用.

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

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?

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?

解决方案

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

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天全站免登陆