简体   繁体   中英

How to set category axis mark interval and label interval in apache poi-5.0.0?

I need to set the category axis mark and label interval. I couldn't find a way to do that. I just found below methods, but couldn't use use or test them:

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

Not clear where exactly is your problem. The properties you wants to set can be set using following code:

...
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
...

Let's have a complete example again:

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

The code also shows how XDDFCategoryAxis.setMinimun and XDDFCategoryAxis.setMaximum works for category axes. Both properties affect category axes too although they should not work for a category axis.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM