微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我使用的是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

Drawing drawing = sheet.createDrawingPatriarch();
        ClientAnchor anchor = drawing.createAnchor(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,endCol));
        LineChartSerie ser1 = data.addSerie(xs,ys1);
        ser1.setTitle(new CellReference(sheet.getSheetName(),seriesCol,abs,abs));

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

        row = 12;
        ChartDataSource<Number> ys3 = DataSources.fromNumericCellRange(sheet,endCol));
        LineChartSerie ser3 = data.addSerie(xs,ys3);
        ser3.setTitle(new CellReference(sheet.getSheetName(),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,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);
        }
    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐