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

查看:85
本文介绍了如何更改由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天全站免登陆