我需要设置类别轴标记和标签间隔。我找不到这样做的方法。我只是找到了以下方法,但无法使用或测试它们:
CTCatAx catAx = chart.getCTChart().getPlotArea().getCatAxArray(0);
catAx.setTickLblSkip(...);
catAx.setTickMarkSkip(...);
catAx.addNewLblOffset()
不清楚您的问题到底在哪里。可以使用以下代码设置您要设置的属性:
...
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);
}
}
}
}
该代码还显示了XDDF类别轴. set最小值
和XDDF类别轴.setMaximum
如何适用于类别轴。这两个属性也会影响类别轴,尽管它们不应该适用于类别轴。