提问者:小点点

对于Apache poi折线图,如何使第一列不显示在y轴上


我使用poi版本是4.1.1。现在我想让第一列不显示在y轴上,意味着我想在y轴和第一列之间添加一些空间。我注意到x轴上有一个选项“轴位置”,当它设置为“勾号之间”时,它会按照我的意愿工作。但是我不知道如何使用apache-poi制作它。有可能制作吗?

这里是“excel中的Asis位置”

我想要的

谢谢大家!!!

这是Apache网站的代码示例:

public class LineChart {

    public static void main(String[] args) throws IOException {
        try (XSSFWorkbook wb = new XSSFWorkbook()) {
            XSSFSheet sheet = wb.createSheet("linechart");
            final int NUM_OF_ROWS = 3;
            final int NUM_OF_COLUMNS = 10;

            // Create a row and put some cells in it. Rows are 0 based.
            Row row;
            Cell cell;
            for (int rowIndex = 0; rowIndex < NUM_OF_ROWS; rowIndex++) {
                row = sheet.createRow((short) rowIndex);
                for (int colIndex = 0; colIndex < NUM_OF_COLUMNS; colIndex++) {
                    cell = row.createCell((short) colIndex);
                    cell.setCellValue(colIndex * (rowIndex + 1.0));
                }
            }

            XSSFDrawing drawing = sheet.createDrawingPatriarch();
            XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 10, 15);

            XSSFChart chart = drawing.createChart(anchor);
            XDDFChartLegend legend = chart.getOrAddLegend();
            legend.setPosition(LegendPosition.BOTTOM);

            // Use a category axis for the bottom axis.
            XDDFCategoryAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);
            //bottomAxis.setTitle("x"); // https://stackoverflow.com/questions/32010765
            XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);
            //leftAxis.setTitle("f(x)");
            leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);


            XDDFDataSource<Double> xs = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1));
            XDDFNumericalDataSource<Double> ys1 = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(1, 1, 0, NUM_OF_COLUMNS - 1));
            XDDFNumericalDataSource<Double> ys2 = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(2, 2, 0, NUM_OF_COLUMNS - 1));

            XDDFLineChartData data = (XDDFLineChartData) chart.createData(ChartTypes.LINE, bottomAxis, leftAxis);
            XDDFLineChartData.Series series1 = (XDDFLineChartData.Series) data.addSeries(xs, ys1);
            series1.setTitle("a", null); // https://stackoverflow.com/questions/21855842
            //series1.setSmooth(false); // https://stackoverflow.com/questions/29014848
            series1.setMarkerStyle(MarkerStyle.NONE); // https://stackoverflow.com/questions/39636138
            XDDFLineChartData.Series series2 = (XDDFLineChartData.Series) data.addSeries(xs, ys2);
            series2.setTitle("b", null);
            //series2.setSmooth(true);
            //series2.setMarkerSize((short) 6);
            series2.setMarkerStyle(MarkerStyle.NONE); // https://stackoverflow.com/questions/39636138

            CTBoolean ctBoolean = CTBoolean.Factory.newInstance();
            ctBoolean.setVal(false);
            CTPlotArea plotArea = chart.getCTChart().getPlotArea();
            chart.getCTChartSpace().setRoundedCorners(ctBoolean);
            for (CTLineSer ser : plotArea.getLineChartArray()[0].getSerArray()) {
                CTDLbls ctdLbls = ser.addNewDLbls();

                ctdLbls.setShowSerName(ctBoolean);
                ctdLbls.setShowLegendKey(ctBoolean);
                ctdLbls.setShowLeaderLines(ctBoolean);
                ctdLbls.setShowCatName(ctBoolean);
                CTDLblPos ctdLblPos = CTDLblPos.Factory.newInstance();
                ctdLblPos.setVal(STDLblPos.CTR);
                CTDLblPos.Factory.newInstance();
                ctdLbls.setDLblPos(ctdLblPos);
            }

            chart.plot(data);

            // if your series have missing values like https://stackoverflow.com/questions/29014848
            // chart.displayBlanksAs(DisplayBlanks.GAP);

            // https://stackoverflow.com/questions/24676460
            solidLineSeries(data, 0, PresetColor.LIGHT_GREEN);
            solidLineSeries(data, 1, PresetColor.DARK_RED);

            // Write the output to a file
            try (FileOutputStream fileOut = new FileOutputStream("ooxml-line-chart.xlsx")) {
                wb.write(fileOut);
            }
        }
    }
    //CTPresetLineDashProperties
    private static void solidLineSeries(XDDFChartData data, int index, PresetColor color) {
        XDDFSolidFillProperties fill = new XDDFSolidFillProperties(XDDFColor.from(color));
        XDDFLineProperties line = new XDDFLineProperties();
        if (index == 0) {
            line.setPresetDash(new XDDFPresetLineDash(PresetLineDash.DOT));
        }
        line.setFillProperties(fill);
        XDDFChartData.Series series = data.getSeries().get(index);
        XDDFShapeProperties properties = series.getShapeProperties();
        if (properties == null) {
            properties = new XDDFShapeProperties();
        }
        properties.setLineProperties(line);
        series.setShapeProperties(properties);
    }
}

共1个答案

匿名用户

默认值轴和类别轴在类别点上完全交叉。因此,如果值轴在点0处与类别轴交叉,那么它现在看起来像您的结果。

但是如果x轴是类别轴,则有一个设置XDDFValueAxis. setCrossBetween。如果设置为AxisCrossBetween.BETWEEN,则值轴在类别点之间穿过类别轴。这看起来像你想要的。

所以在你的情况下:

...
            // Use a category axis for the bottom axis.
            XDDFCategoryAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);
            //bottomAxis.setTitle("x"); // https://stackoverflow.com/questions/32010765
            XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);
            //leftAxis.setTitle("f(x)");
            leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);

            leftAxis.setCrossBetween(AxisCrossBetween.BETWEEN);
...

顺便说一句:您应该摆脱已弃用的方法。

例如,ooxml-schemasget… Array()方法已被弃用。要么使用get…Array(item)从数组中获取具体项目,要么使用get…List()获取List

在您的情况下:

...
            //for (CTLineSer ser : plotArea.getLineChartArray()[0].getSerArray()) {
            for (CTLineSer ser : plotArea.getLineChartArray(0).getSerList()) {
...

XDDFChartData. getSeries()也已弃用。如果您需要一个系列,请使用XDDFChartData.getSeries(index)

在您的情况下:

...
        //XDDFChartData.Series series = data.getSeries().get(index);
        XDDFChartData.Series series = data.getSeries(index);
...