英文:
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<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: 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<String> cat = XDDFDataSourcesFactory.fromStringCellRange(sheet,
new CellRangeAddress(1, 5, 0, 0));
XDDFNumericalDataSource<Double> val = XDDFDataSourcesFactory.fromNumericCellRange(sheet,
new CellRangeAddress(1, 5, 1, 1));
That is cat
data source A2:A6
and val
data source B2:B6
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论