【问题标题】:How to set category axis mark interval and label interval in apache poi-5.0.0?如何在apache poi-5.0.0中设置类别轴标记间隔和标签间隔?
【发布时间】:2021-03-17 19:56:45
【问题描述】:

我需要设置类别轴标记和标签间隔。我找不到办法做到这一点。我刚刚找到以下方法,但无法使用或测试它们:

CTCatAx catAx = chart.getCTChart().getPlotArea().getCatAxArray(0);
catAx.setTickLblSkip(...);
catAx.setTickMarkSkip(...);
catAx.addNewLblOffset()

【问题讨论】:

  • 到底是什么问题?您是否遇到错误或异常,或者您的代码没有编译?也许这可以帮助您找到解决问题的方法:stackoverflow.com/questions/38913412/…
  • @MarioVarchmin 我只是不能使用提到的方法。我认为您的示例没有为我提供解决方案,我之前检查过。

标签: java excel apache-poi


【解决方案1】:

不清楚您的问题到底出在哪里。您要设置的属性可以使用以下代码进行设置:

...
XSSFChart chart ...
...
chart.getCTChart().getPlotArea().getCatAxArray(0).addNewTickLblSkip().setVal(2); // label only every second mark
chart.getCTChart().getPlotArea().getCatAxArray(0).addNewTickMarkSkip().setVal(2); // show only every second mark  
chart.getCTChart().getPlotArea().getCatAxArray(0).addNewLblOffset().setVal(200); // label offset to the axis, possible values: 0 to 1000
...

让我们再举一个完整的例子:

import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xddf.usermodel.*;
import org.apache.poi.xddf.usermodel.chart.*;
import org.apache.poi.xssf.usermodel.*;

public class LineChartAxisUnits {

    public static void main(String[] args) throws IOException {
        try (XSSFWorkbook wb = new XSSFWorkbook()) {
            XSSFSheet sheet = wb.createSheet("Sheet 1");
            final int NUM_OF_COLUMNS = 20;

            // Create a row and put some cells in it. Rows are 0 based.
            Row row;
            Cell cell;
            for (int rowIndex = 0; rowIndex < 2; rowIndex++) {
                row = sheet.createRow((short) rowIndex);
                for (int colIndex = 0; colIndex < NUM_OF_COLUMNS; colIndex++) {
                    cell = row.createCell(colIndex);
                    if (rowIndex == 0) cell.setCellValue(colIndex+1);
                    //if (rowIndex == 0) cell.setCellValue(((new java.util.Random()).nextDouble()+colIndex)/20d);
                    else cell.setCellValue((new java.util.Random()).nextDouble());
                }
            }

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

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

            XDDFCategoryAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM); 
            bottomAxis.setTitle("x");
            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> ys = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(1, 1, 0, NUM_OF_COLUMNS - 1));
 

            XDDFLineChartData data = (XDDFLineChartData) chart.createData(ChartTypes.LINE, bottomAxis, leftAxis);
            data.setVaryColors(false);
            XDDFLineChartData.Series series = (XDDFLineChartData.Series) data.addSeries(xs, ys);
            series.setTitle("Title", null);
  
            series.setMarkerStyle(MarkerStyle.CIRCLE);
            series.setMarkerSize((short)5);

            chart.getCTChart().getPlotArea().getCatAxArray(0).addNewTickLblSkip().setVal(2); // label only every second mark
            chart.getCTChart().getPlotArea().getCatAxArray(0).addNewTickMarkSkip().setVal(2); // show only every second mark
            chart.getCTChart().getPlotArea().getCatAxArray(0).addNewLblOffset().setVal(200); // label offset to the axis, possible values: 0 to 1000

            bottomAxis.setMinimum(5); // this sets where axis starts, at fifth category in this case, this works too although it should not work for a category axis
            bottomAxis.setMaximum(15); // this sets where axis ends, at fifteenth category in this case, this works too although it should not work for a category axis

            chart.plot(data);

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

代码还显示了XDDFCategoryAxis.setMinimunXDDFCategoryAxis.setMaximum 如何用于类别轴。这两个属性也会影响类别轴,尽管它们不适用于类别轴。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多