如何更改由 apache poi 生成的图表以不使用平滑线并将空单元格显示为间隙? [英] How can I change charts generated by apache poi to not use smoothed lines and show empty cells as gaps?

查看:28
本文介绍了如何更改由 apache poi 生成的图表以不使用平滑线并将空单元格显示为间隙?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 generate a line that has been smoothed over the data points. Empty values are also being plotted as 0, but we want the lines to stop at the first column where there is an empty cell.

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

I can go into the chart properties once it is rendered in an xlsx file and change these settings, but we need to have the xlsx rendered with these settings. I can't find anything in the available API to change these settings.

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

I used this sample class as a starting point for my code below 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 开发人员那里得到了帮助,这是解决原始问题中提到的两个问题的解决方案.

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

这篇关于如何更改由 apache poi 生成的图表以不使用平滑线并将空单元格显示为间隙?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆