要创建Java POI条形图结合两个条形值,可以按照以下步骤进行:
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Bar Chart");
Row row1 = sheet.createRow(0);
row1.createCell(0).setCellValue("Value 1");
row1.createCell(1).setCellValue(10);
Row row2 = sheet.createRow(1);
row2.createCell(0).setCellValue("Value 2");
row2.createCell(1).setCellValue(20);
Drawing drawing = sheet.createDrawingPatriarch();
ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 10, 15);
Chart chart = drawing.createChart(anchor);
ChartLegend legend = chart.getOrCreateLegend();
legend.setPosition(LegendPosition.BOTTOM);
BarChartData data = chart.getChartDataFactory().createBarChartData();
ValueAxis bottomAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.BOTTOM);
ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT);
leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
ChartDataSource<Number> xs = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(0, 1, 0, 0));
ChartDataSource<Number> ys1 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(0, 0, 1, 1));
ChartDataSource<Number> ys2 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(1, 1, 1, 1));
data.addSeries(xs, ys1);
data.addSeries(xs, ys2);
chart.plot(data, bottomAxis, leftAxis);
FileOutputStream fileOut = new FileOutputStream("bar_chart.xlsx");
workbook.write(fileOut);
fileOut.close();
workbook.close();
这样,就创建了一个包含两个条形值的Java POI条形图。可以根据需要调整图表的样式和其他属性。
请注意,这里没有提及任何特定的云计算品牌商,但你可以根据自己的需求选择适合的云计算平台来部署和运行Java代码。
领取专属 10元无门槛券
手把手带您无忧上云