如何更改 apache poi 生成的图表,使其不使用平滑线并将空单元格显示为间隙?

2024-05-01

我正在使用 POI 3.12-beta1,并拥有可创建包含多个数据集和图例中命名系列的折线图的代码。但是,poi 中折线图的默认设置会生成一条已在数据点上平滑的线。空值也被绘制为 0,但我们希望线条停在有空单元格的第一列。

在 xlsx 文件中呈现图表属性后,我可以进入图表属性并更改这些设置,但我们需要使用这些设置呈现 xlsx。我在可用的 API 中找不到任何可以更改这些设置的内容。

我使用这个示例类作为下面代码的起点http://svn.apache.org/repos/asf/poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/LineChart.java http://svn.apache.org/repos/asf/poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/LineChart.java

        Drawing drawing = sheet.createDrawingPatriarch();
        ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 17, 18, 30);
        Chart chart = drawing.createChart(anchor);
        ChartLegend legend = chart.getOrCreateLegend();
        legend.setPosition(LegendPosition.RIGHT);
        LineChartData data = chart.getChartDataFactory().createLineChartData();
        ChartAxis bottomAxis = chart.getChartAxisFactory().createCategoryAxis(AxisPosition.BOTTOM);
        ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT);
        leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);

        int row = 2;
        int startCol = 3;
        int endCol = 17;
        boolean abs = false;
        ChartDataSource<Number> xs = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(row, row, startCol, endCol));

        row = 10;
        int seriesCol = 0;
        ChartDataSource<Number> ys1 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(row, row, startCol, endCol));
        LineChartSerie ser1 = data.addSerie(xs, ys1);
        ser1.setTitle(new CellReference(sheet.getSheetName(), row, seriesCol, abs, abs));

        row = 11;
        ChartDataSource<Number> ys2 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(row, row, startCol, endCol));
        LineChartSerie ser2 = data.addSerie(xs, ys2);
        ser2.setTitle(new CellReference(sheet.getSheetName(), row, seriesCol, abs, abs));

        row = 12;
        ChartDataSource<Number> ys3 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(row, row, startCol, endCol));
        LineChartSerie ser3 = data.addSerie(xs, ys3);
        ser3.setTitle(new CellReference(sheet.getSheetName(), row, seriesCol, abs, abs));

        chart.plot(data, new ChartAxis[] { bottomAxis, leftAxis });

感谢 Etienne 提供将空白设置为间隙的代码。我得到了 POI 开发人员的帮助,这是解决原始问题中提到的两个问题的解决方案。

    XSSFChart chart = (XSSFChart)drawing.createChart(anchor);

    // this will set blank values as gaps in the chart so you 
    // can accurately plot data series of different lengths
    CTDispBlanksAs disp = CTDispBlanksAs.Factory.newInstance();
    disp.setVal(STDispBlanksAs.GAP);
    chart.getCTChart().setDispBlanksAs(disp);


    // setup chart, axes, data series, etc


    chart.plot(data, new ChartAxis[] { bottomAxis, leftAxis });

    // this must occur after the call to chart.plot above
    CTPlotArea plotArea = chart.getCTChart().getPlotArea();
    for (CTLineChart ch : plotArea.getLineChartList()) {
        for (CTLineSer ser : ch.getSerList()) {
            CTBoolean ctBool = CTBoolean.Factory.newInstance();
            ctBool.setVal(false);
            ser.setSmooth(ctBool);
        }
    }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何更改 apache poi 生成的图表,使其不使用平滑线并将空单元格显示为间隙? 的相关文章

随机推荐