如何在使用Apache POI 4.1.2时仅设置XSSFChart中的绘图区域高度?

huangapple 未分类评论47阅读模式
英文:

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);

预期输出:

如何在使用Apache POI 4.1.2时仅设置XSSFChart中的绘图区域高度?

英文:

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?
如何在使用Apache POI 4.1.2时仅设置XSSFChart中的绘图区域高度?

         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&lt;?&gt; category = XDDFDataSourcesFactory.fromStringCellRange(dataSheet, categoryCells);
         for (int i = 0; i &lt; addressList.size(); i++) {
            XDDFNumericalDataSource&lt;?&gt; 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 &gt;= 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:

如何在使用Apache POI 4.1.2时仅设置XSSFChart中的绘图区域高度?

huangapple
  • 本文由 发表于 2020年7月27日 13:30:25
  • 转载请务必保留本文链接:https://java.coder-hub.com/63109181.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定