问题1.我只在条形图上显示数据标签。
问题2.图表被剪掉了。第一类和最后一类。
您可以看到混合标签,“右轴”+“底轴”+“数据值”。
我只想查看数据标签。
我想显示类别“1”底部的三个。还有类别“64”底部的三个。
我该怎么做?
语言 - Java
依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.1.2</version>
</dependency>
private void createChart(XSSFSheet sheet,
String chartTitle,
String leftTitle,
String bottomAxisTitle,
List<Map<String, Object>> reportData,
Map<String, String> capMap,
String rwRatio,
String resultType,
String name,
Integer startRownum,
Integer startCellIndex,
Integer lastRownum,
Integer lastCellIndex) {
XSSFDrawing drawing = (XSSFDrawing)sheet.createDrawingPatriarch();
XSSFClientAnchor anchor = drawing.createAnchor(
10, 10, 110, 110, startCellIndex,
startRownum, lastCellIndex, lastRownum); //int dx1, int dy1, int dx2,
//int dy2, int col1, int row1,
//int col2, int row2
XSSFChart chart = drawing.createChart(anchor);
chart.setTitleText(chartTitle);
chart.setTitleOverlay(false);
XDDFChartLegend legend = chart.getOrAddLegend();
legend.setPosition(LegendPosition.TOP_RIGHT);
// Use a category axis for the bottom axis.
XDDFCategoryAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);
bottomAxis.setTitle(bottomAxisTitle);
XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);
leftAxis.setTitle(leftTitle);
leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
XDDFChartData data = chart.createData(ChartTypes.BAR, bottomAxis, leftAxis); // Chart type
XDDFDataSource<String> xs = XDDFDataSourcesFactory.fromArray(qdList);
String title = "";
for(Map<String, Object> report : reportData) {
List <Double>iops = new ArrayList<>();
List<Map<String, Object>> fios = (List<Map<String, Object>>) report.get("myData");
TestMain tm = (TestMain) report.get("testMain");
title = capMap.get(tm.getId().toString());
for(String qd : qdList) {
for(Map<String, Object> fio : fios) {
iops.add(Double.parseDouble(fio.get(name).toString()));
}
}
Double [] values = new Double[iops.size()];
for(int i = 0 ; i < iops.size(); i++) {
values[i] = iops.get(i);
}
XDDFNumericalDataSource<Double> ys1 = XDDFDataSourcesFactory.fromArray(values);
XDDFChartData.Series series1 = data.addSeries(xs, ys1);
series1.setTitle(title, null);
series1.setShowLeaderLines(true);
}
chart.plot(data);
chart.displayBlanksAs(DisplayBlanks.SPAN);
XDDFBarChartData bar = (XDDFBarChartData) data;
bar.setBarDirection(BarDirection.COL);
List<PresetColor> color = colors();
for(int idx = 0 ; idx < reportData.size() ; idx++) {
solidFillSeries(data, idx, color.get(idx));
}
private static void solidFillSeries(XDDFChartData data, int index, PresetColor color) {
XDDFSolidFillProperties fill = new XDDFSolidFillProperties(XDDFColor.from(color));
XDDFChartData.Series series = data.getSeries().get(index);
XDDFShapeProperties properties = series.getShapeProperties();
if (properties == null) {
properties = new XDDFShapeProperties();
}
properties.setFillProperties(fill);
series.setShapeProperties(properties);
series.setShowLeaderLines(true);
}
最佳答案
我找到了第一个答案!!!
我添加了一个依赖项:
<dependency> <groupId>org.apache.poi</groupId> <artifactId>ooxml-schemas</artifactId> <version>1.3</version> </dependency>
我添加了一些代码。
int seriesNr = 0; chart.getCTChart().getPlotArea().getBarChartArray(0).getSerArray(seriesNr).getDLbls().addNewDLblPos().setVal(org.openxmlformats.schemas.drawingml.x2006.chart.STDLblPos.CTR); chart.getCTChart().getPlotArea().getBarChartArray(0).getSerArray(seriesNr).getDLbls().addNewShowVal().setVal(true); chart.getCTChart().getPlotArea().getBarChartArray(0).getSerArray(seriesNr).getDLbls().addNewShowLegendKey().setVal(false); chart.getCTChart().getPlotArea().getBarChartArray(0).getSerArray(seriesNr).getDLbls().addNewShowCatName().setVal(false); chart.getCTChart().getPlotArea().getBarChartArray(0).getSerArray(seriesNr).getDLbls().addNewShowSerName().setVal(false); chart.getCTChart().getPlotArea().getBarChartArray(0).getSerArray(1).getDLbls().addNewDLblPos().setVal(org.openxmlformats.schemas.drawingml.x2006.chart.STDLblPos.CTR); chart.getCTChart().getPlotArea().getBarChartArray(0).getSerArray(1).getDLbls().addNewShowVal().setVal(true); chart.getCTChart().getPlotArea().getBarChartArray(0).getSerArray(1).getDLbls().addNewShowLegendKey().setVal(false); chart.getCTChart().getPlotArea().getBarChartArray(0).getSerArray(1).getDLbls().addNewShowCatName().setVal(false); chart.getCTChart().getPlotArea().getBarChartArray(0).getSerArray(1).getDLbls().addNewShowSerName().setVal(false);
这是更改后的图表图像。
seriesNr
是索引getSerArray(seriesNr)
的数据。
关于java - Apache POI Excel 仅显示数据标签和切割图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62148824/