How can I change charts generated by apache poi to not use smoothed lines and show empty cells as gaps?

Thanks to Etienne for the code to set blanks as gaps. I got help from a POI developer and here is the solution that solves both issues mentioned in the original question.

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

Leave a Comment