Apache POI – 饼图 – java.lang.IllegalStateException: 类别和值必须具有相同的点计数

huangapple go评论82阅读模式
英文:

Apache POI - PIE Chart - java.lang.IllegalStateException: Category and values must have the same point count

问题

我遇到了 java.lang.IllegalStateException: 分类和值必须具有相同的数据点数。在下面的代码中,我错过了什么?谢谢。

XDDFDataSource<String> cat = XDDFDataSourcesFactory.fromStringCellRange(sheet,
                            new CellRangeAddress(0, 0, 0, 1));
XDDFNumericalDataSource<Double> val = XDDFDataSourcesFactory.fromNumericCellRange(sheet, 
                    new CellRangeAddress(1, 6, 0, 1));

XDDFChartData chartData = chart.createData(ChartTypes.PIE, null,
                    null);
chartData.setVaryColors(true);
chartData.addSeries(cat, val);
chart.plot(chartData);

java.lang.IllegalStateException: 分类和值必须具有相同的数据点数。
    at org.apache.poi.xddf.usermodel.chart.XDDFChartData$Series.replaceData(XDDFChartData.java:170)
    at org.apache.poi.xddf.usermodel.chart.XDDFChartData$Series.<init>(XDDFChartData.java:161)
    at org.apache.poi.xddf.usermodel.chart.XDDFPieChartData$Series.<init>(XDDFPieChartData.java:107)
    at org.apache.poi.xddf.usermodel.chart.XDDFPieChartData.addSeries(XDDFPieChartData.java:97)
英文:

I am getting java.lang.IllegalStateException: Category and values must have the same point count. What am i missing here in the below code? Thanks.

XDDFDataSource&lt;String&gt; cat = XDDFDataSourcesFactory.fromStringCellRange(sheet,
							new CellRangeAddress(0, 0, 0, 1));
			XDDFNumericalDataSource&lt;Double&gt; val = XDDFDataSourcesFactory.fromNumericCellRange(sheet, 
					new CellRangeAddress(1, 6, 0, 1));

			XDDFChartData chartData = chart.createData(ChartTypes.PIE, null,
					null);
			chartData.setVaryColors(true);
			chartData.addSeries(cat, val);
			chart.plot(chartData);

Apache POI – 饼图 – java.lang.IllegalStateException: 类别和值必须具有相同的点计数

java.lang.IllegalStateException: Category and values must have the same point count.
at org.apache.poi.xddf.usermodel.chart.XDDFChartData$Series.replaceData(XDDFChartData.java:170)
at org.apache.poi.xddf.usermodel.chart.XDDFChartData$Series.<init>(XDDFChartData.java:161)
at org.apache.poi.xddf.usermodel.chart.XDDFPieChartData$Series.<init>(XDDFPieChartData.java:107)
at org.apache.poi.xddf.usermodel.chart.XDDFPieChartData.addSeries(XDDFPieChartData.java:97)

答案1

得分: 1

错误非常明显。有关构造函数参数的含义,请参阅CellRangeAddress

因此,你的cat数据源是从第0行到第0行,从第0列到第1列。即为A1:B1。但是你的val数据源是从第1行到第6行,从第0列到第1列。即为A2:B7。所以cat数据源包含2个单元格,而val数据源包含12个单元格。

根据你的Excel表格截图,应该是:

XDDFDataSource<String> cat = XDDFDataSourcesFactory.fromStringCellRange(sheet,
                             new CellRangeAddress(1, 5, 0, 0));
XDDFNumericalDataSource<Double> val = XDDFDataSourcesFactory.fromNumericCellRange(sheet, 
                                      new CellRangeAddress(1, 5, 1, 1));

cat数据源为A2:A6,而val数据源为B2:B6

英文:

The error is pretty clear. See CellRangeAddress for what the constructor parameters mean.

So your cat data source is from row 0 to row 0 and column 0 to column 1. That is A1:B1. But your val data source is form row 1 to row 6 and column 0 to column 1. That is A2:B7. So cat data source contains 2 cells but val data source contains 12 cells.

According to your screen shot of Excel sheet, it should be:

XDDFDataSource&lt;String&gt; cat = XDDFDataSourcesFactory.fromStringCellRange(sheet,
                             new CellRangeAddress(1, 5, 0, 0));
XDDFNumericalDataSource&lt;Double&gt; val = XDDFDataSourcesFactory.fromNumericCellRange(sheet, 
                                      new CellRangeAddress(1, 5, 1, 1));

That is cat data source A2:A6 and val data source B2:B6.

huangapple
  • 本文由 发表于 2020年8月20日 04:06:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/63494256.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定