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

后端 未结 2 1707
眼角桃花
眼角桃花 2021-01-20 14:00

I am using POI 3.12-beta1 and have code that creates a line chart with multiple datasets and named series in the legend. However, the default settings for line charts in poi

相关标签:
2条回答
  • 2021-01-20 14:27

    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);
            }
        }
    
    0 讨论(0)
  • 2021-01-20 14:28

    I couldn't find any solution to that same problem so I ended up trying to find some "creative workaround". It's not a clean approach at all, but I'll share it just in case.

    It turns out that the default Chart POI interface doesn't allow to do a lot of operations (modify Axis and Legend, and that's about it).

    I do work with XLSX files however, and the implementing XSSFChart class provides a bit more features, such as a setPlotOnlyVisibleCells method that does work in enabling/disabling that option in the "Hidden & Empty Cells" menu. However, that's not what we want!

    If you look at the XLSX file format (which is basically XML), you'll notice there's a "dispBlanksAs" XML element as a child of the Chart element, and we'll want to set it to "gap". (POI won't set it, which is equivalent to set it to "zero").

    Unfortunately, there's no such method in the POI API currently, so I had to resort to the following dirty code to get it working:

    XSSFChart chart = (XSSFChart)drawing.createChart(anchor);
    CTDispBlanksAs disp = CTDispBlanksAs.Factory.newInstance();
    disp.setVal(STDispBlanksAs.GAP);
    chart.getCTChart().setDispBlanksAs(disp);
    

    This code is very likely reprehensible in many ways, but it works for me :)

    An important note is that you'll need to have the ooxml-schemas 1.1 jar on the classpath for this to work. It won't work even with poi-ooxml-schemas 3.12, as it doesn't contain the definition for CTDispBlanksAs class.

    Hopefully this feature will be supported in a future version of POI.

    0 讨论(0)
提交回复
热议问题