java - Apache POI 4 - 创建带有多个空单元格的 LINE 图表
问题描述
我确实喜欢基于我的数据源创建折线图,但不幸的是,当折线图 y 系列跨多个单元格时,它在绘图聊天时遇到 NullPointException。
我发现该列为空白时发生错误,并强制设置“0”以使其工作。
无论如何,我可以在没有硬编码“0”的空单元格上绘制图表?
下面是我如何硬编码默认值的方式,
for (int i = 0; headers.size() > i; i++) {
row.createCell(i, CellType.NUMERIC);
}
下面是线图代码:
int rows = numberOfRows - 1;
int cols = headers.size();
XSSFDrawing drawing = sheet.createDrawingPatriarch();
XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 10, 15);
XSSFChart chart = drawing.createChart(anchor);
chart.displayBlanksAs(DisplayBlanks.GAP);
XDDFChartLegend legend = chart.getOrAddLegend();
legend.setPosition(LegendPosition.TOP_RIGHT);
// Use a category axis for the bottom axis.
XDDFCategoryAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);
bottomAxis.setOrientation(AxisOrientation.MAX_MIN);
bottomAxis.setTitle("Date");
XDDFValueAxis rightAxis = chart.createValueAxis(AxisPosition.RIGHT);
rightAxis.setTitle("Rates");
rightAxis.setCrosses(AxisCrosses.AUTO_ZERO);
XDDFChartLegend chartLegend = chart.getOrAddLegend();
chartLegend.setPosition(LegendPosition.TOP_RIGHT);
chartLegend.setOverlay(false);
XDDFLineChartData lineChartData = (XDDFLineChartData) chart.createData(ChartTypes.LINE, bottomAxis, rightAxis);
lineChartData.setVaryColors(false);
XDDFDataSource<String> xs = XDDFDataSourcesFactory.fromStringCellRange(sheet, new CellRangeAddress(1, rows, 1, 1));
for (int col = 2; cols > col; col++) {
XDDFNumericalDataSource<Double> ys = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(1, rows, col, col));
XDDFLineChartData.Series series = (XDDFLineChartData.Series) lineChartData.addSeries(xs, ys);
series.setTitle(headers.get(col), null);
series.setSmooth(false);
series.setMarkerStyle(MarkerStyle.NONE);
}
chart.plot(lineChartData);
解决方案
推荐阅读
- c# - 有没有办法在渲染帧之前执行循环?
- javascript - 表单组问题Angular
- python - 如何从字典列表中删除字典(使用给定值)?
- python - 有没有什么方法可以改变 Z 轴上的点而不在 Matplotlib 3D 上自动缩放?
- javascript - 如何防止 ipcRenderer 侦听器的倍增?
- reactjs - 使用 React 测试库设置 React 上下文值
- word2vec - 为什么 Google word2vec 不包含“the”、“a”等常用词
- azure - 我可以在 Azure 集成运行时警报中设置时间范围吗?
- r - R 上的 GVIZ 未从 UCSC 加载 mm10
- traefik - ingressroute 如何连接到 traefik 的入口控制器