英文:
How can I set only the plot area height in XSSFChart using Apache POI 4.1.2
问题
我使用模板文件创建了一个堆叠条形图。在我的图表中,有一个系列和100多个图例。为了显示所有图例,我调整了绘图区域的Y轴值,现在条形的大小比预期的要大。是否有方法来调整绘图区域的高度?
int additionalRows = barCount * barSize;
additionalRows = additionalRows + (addressList.size()/3);
chart = graphSheet.createDrawingPatriarch().createChart(new XSSFClientAnchor(100, 100, 100, 100, 1, currentIndex, 2, currentIndex+= additionalRows));
XSSFChart stackedFullBarChart = templateSheet.getDrawingPatriarch().getCharts().get(ChartType.STACKED_FULL_CHART.getValue());
chart.importContent(stackedFullBarChart);
chart.setTitleText(title);
XDDFChartData chartData = chart.getChartSeries().get(0);
XDDFDataSource<?> category = XDDFDataSourcesFactory.fromStringCellRange(dataSheet, categoryCells);
for (int i = 0; i < addressList.size(); i++) {
XDDFNumericalDataSource<?> values = XDDFDataSourcesFactory.fromNumericCellRange(dataSheet, addressList.get(i));
int seriesNameRow = addressList.get(i).getFirstRow() - 2;
int seriesNameCol = addressList.get(i).getFirstColumn();
XSSFCell seriesNameCell = dataSheet.getRow(seriesNameRow).getCell(seriesNameCol);
String seriesName = seriesNameCell.getStringCellValue();
if (i >= chartData.getSeriesCount()) {
XDDFChartData.Series series1 = chartData.addSeries(category, values);
series1.setTitle(seriesName, new CellReference(seriesNameCell));
} else {
chartData.getSeries(i).setTitle(seriesName, new CellReference(seriesNameCell));
chartData.getSeries(i).replaceData(category, values);
}
}
chart.getOrAddManualLayout().setWidthRatio(0.8);
**chart.getOrAddManualLayout().setHeightRatio(0.9);**
chart.getOrAddManualLayout().setY(1.1);
chart.plot(chartData);
chart.getOrAddLegend().setPosition(LegendPosition.TOP);
CTLegend ctLegend = chart.getCTChart().addNewLegend();
ctLegend.addNewLegendPos().setVal(STLegendPos.T);
CTBoolean ctBoolean = CTBoolean.Factory.newInstance();
ctBoolean.setVal(false);
ctLegend.setOverlay(ctBoolean);
CTDouble ctdouble = CTDouble.Factory.newInstance();
ctdouble.setVal(0.1);
chart.getCTChart().getPlotArea().getLayout().getManualLayout().setH(ctdouble);
预期输出:
英文:
I have created a stacked bar chart using a template file. In my chart, there is one series and more than 100 legends. To show all legends I have adjusted the plot area Y-axis value, now the bar size bigger than expected. Are there any methods to adjust the Plot area height?
int additionalRows = barCount * barSize;
additionalRows = additionalRows + (addressList.size()/3);
chart = graphSheet.createDrawingPatriarch().createChart(new XSSFClientAnchor(100, 100, 100, 100, 1, currentIndex, 2, currentIndex+= additionalRows));
XSSFChart stackedFullBarChart = templateSheet.getDrawingPatriarch().getCharts().get(ChartType.STACKED_FULL_CHART.getValue());
chart.importContent(stackedFullBarChart);
chart.setTitleText(title);
XDDFChartData chartData = chart.getChartSeries().get(0);
XDDFDataSource<?> category = XDDFDataSourcesFactory.fromStringCellRange(dataSheet, categoryCells);
for (int i = 0; i < addressList.size(); i++) {
XDDFNumericalDataSource<?> values = XDDFDataSourcesFactory.fromNumericCellRange(dataSheet, addressList.get(i));
int seriesNameRow = addressList.get(i).getFirstRow() - 2;
int seriesNameCol = addressList.get(i).getFirstColumn();
XSSFCell seriesNameCell = dataSheet.getRow(seriesNameRow).getCell(seriesNameCol);
String seriesName = seriesNameCell.getStringCellValue();
if (i >= chartData.getSeriesCount()) {
XDDFChartData.Series series1 = chartData.addSeries(category, values);
series1.setTitle(seriesName, new CellReference(seriesNameCell));
} else {
chartData.getSeries(i).setTitle(seriesName, new CellReference(seriesNameCell));
chartData.getSeries(i).replaceData(category, values);
}
}
chart.getOrAddManualLayout().setWidthRatio(0.8);
**chart.getOrAddManualLayout().setHeightRatio(0.9);
chart.getOrAddManualLayout().setY(1.1);**
chart.plot(chartData);
chart.getOrAddLegend().setPosition(LegendPosition.TOP);
CTLegend ctLegend = chart.getCTChart().addNewLegend();
ctLegend.addNewLegendPos().setVal(STLegendPos.T);
CTBoolean ctBoolean = CTBoolean.Factory.newInstance();
ctBoolean.setVal(false);
ctLegend.setOverlay(ctBoolean);
CTDouble ctdouble = CTDouble.Factory.newInstance();
ctdouble.setVal(0.1);
chart.getCTChart().getPlotArea().getLayout().getManualLayout().setH(ctdouble);
Expected Output:
专注分享java语言的经验与见解,让所有开发者获益!
评论