Apache POI。如何仅显示折线图的正面部分?

时间:2017-09-30 13:26:16

标签: java excel charts apache-poi

我想用apache poi在Excel中制作折线图。我想剪切或制作0图形的那些部分,这是负面的。我从apache poi示例中得到了这个例子。这有什么解决方案吗?

我的代码。

try (Workbook wb = new XSSFWorkbook()) {
        Sheet sheet = wb.createSheet("linechart");
        final int NUM_OF_ROWS = 2;
        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 < 1; rowIndex++) {
            row = sheet.createRow((short) rowIndex);
            for (int colIndex = 0; colIndex < NUM_OF_COLUMNS; colIndex++) {
                cell = row.createCell((short) colIndex);
                cell.setCellValue(4*colIndex * (rowIndex + 1));
            }
        }
        for (int rowIndex = 1; 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(ThreadLocalRandom.current().nextInt(3));
            }
        }

        Drawing<?> drawing = sheet.createDrawingPatriarch();
        ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 0, 20, 15);

        Chart chart = drawing.createChart(anchor);
        ChartLegend legend = chart.getOrCreateLegend();
        legend.setPosition(LegendPosition.TOP_RIGHT);

        LineChartData data = chart.getChartDataFactory().createLineChartData();

        // Use a category axis for the bottom axis.
        ChartAxis bottomAxis = chart.getChartAxisFactory().createCategoryAxis(AxisPosition.BOTTOM);
        ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT);
        leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
        bottomAxis.setCrosses(AxisCrosses.AUTO_ZERO);

        ChartDataSource<Number> xs = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1));
        ChartDataSource<Number> ys1 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(1, 1, 0, NUM_OF_COLUMNS - 1));


        LineChartSeries chartSerie = data.addSeries(xs, ys1);
        chartSerie.setTitle("My Title");

        chart.plot(data, bottomAxis, leftAxis);

        // Write the output to a file
        try (FileOutputStream fileOut = new FileOutputStream("ooxml-line-chart.xlsx")) {
            wb.write(fileOut);
        }
    }

这就是我现在所拥有的:

my line chart

this is what i want

1 个答案:

答案 0 :(得分:0)

正如人们可以在apache poi的API文档中找到ValueAxis扩展ChartAxisChartAxis有方法ChartAxis.setMinimum

所以

...
  ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT);

  leftAxis.setMinimum(0);

  leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
...