0

我使用 Apache POI 创建了一个图表,如下所示:

// creata anchor
    XSSFDrawing drawing = sheet.createDrawingPatriarch();
    XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, FROM_COLUMN, FROM_ROW, TO_COLUMN, TO_ROW);

    // create chart
    XSSFChart chart = drawing.createChart(anchor);

现在,我需要在 java 中将图表区域的填充属性设置为“NO FILL”。有没有办法做到这一点?

4

1 回答 1

2

图表是具有形状属性的形状。其中一个形状属性是填充属性。所以我们需要获取图表的形状属性来设置填充属性。

XDDFChart提供XDDFChart.getOrAddShapeProperties。但这会获取图表绘图区域的形状属性。但是要设置图表的形状属性,我们需要图表空间的形状属性。由于没有一种方法可以得到它,因此必须像这样得到它:

private XDDFShapeProperties getOrAddChartSpaceShapeProperties(XDDFChart chart) {
  if (chart.getCTChartSpace().getSpPr() == null) chart.getCTChartSpace().addNewSpPr();
  return new XDDFShapeProperties(chart.getCTChartSpace().getSpPr());
}

如果我们有XDDFShapeProperties,我们可以将填充属性设置为XDDFNoFillProperties

完整示例:

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

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xddf.usermodel.chart.LegendPosition;
import org.apache.poi.xddf.usermodel.chart.XDDFChart;
import org.apache.poi.xddf.usermodel.chart.XDDFChartData;
import org.apache.poi.xddf.usermodel.chart.XDDFChartLegend;
import org.apache.poi.xddf.usermodel.chart.XDDFDataSource;
import org.apache.poi.xddf.usermodel.chart.XDDFDataSourcesFactory;
import org.apache.poi.xddf.usermodel.chart.XDDFNumericalDataSource;
import org.apache.poi.xddf.usermodel.chart.XDDFPieChartData;
import org.apache.poi.xddf.usermodel.chart.ChartTypes;
import org.apache.poi.xddf.usermodel.XDDFNoFillProperties;
import org.apache.poi.xddf.usermodel.XDDFShapeProperties;
import org.apache.poi.xssf.usermodel.XSSFChart;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFDrawing;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class PieChart {
    
  private static void setRoundedCorners(XDDFChart chart, boolean setVal) {
    if (chart.getCTChartSpace().getRoundedCorners() == null) chart.getCTChartSpace().addNewRoundedCorners();
    chart.getCTChartSpace().getRoundedCorners().setVal(setVal);
  }
  
  private static XDDFShapeProperties getOrAddChartSpaceShapeProperties(XDDFChart chart) {
    if (chart.getCTChartSpace().getSpPr() == null) chart.getCTChartSpace().addNewSpPr();
    return new XDDFShapeProperties(chart.getCTChartSpace().getSpPr());
  }
  
  public static void main(String[] args) throws IOException {
    try (XSSFWorkbook wb = new XSSFWorkbook()) {
      XSSFSheet sheet = wb.createSheet("piechart");
      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 < NUM_OF_ROWS; rowIndex++) {
        row = sheet.createRow((short) rowIndex);
        for (int colIndex = 0; colIndex < NUM_OF_COLUMNS; colIndex++) {
          cell = row.createCell((short) colIndex);
          if (rowIndex == 0) cell.setCellValue("Cat " + (colIndex + 1));
          else cell.setCellValue((colIndex + 1) * (rowIndex + 1));
        }
      }

      XSSFDrawing drawing = sheet.createDrawingPatriarch();
      XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 4, 10, 25);

      XSSFChart chart = drawing.createChart(anchor);
      //XDDFShapeProperties shapeProperties = chart.getOrAddShapeProperties(); //gets shape properties of plot area
      XDDFShapeProperties shapeProperties = getOrAddChartSpaceShapeProperties(chart);
      shapeProperties.setFillProperties(new XDDFNoFillProperties());
      
      chart.setTitleText("Pie Chart");
      chart.setTitleOverlay(false);
      setRoundedCorners(chart, false);
      
      XDDFChartLegend legend = chart.getOrAddLegend();
      legend.setPosition(LegendPosition.TOP_RIGHT);

      XDDFDataSource<String> cat = XDDFDataSourcesFactory.fromStringCellRange(sheet,
          new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1));
      XDDFNumericalDataSource<Double> val = XDDFDataSourcesFactory.fromNumericCellRange(sheet,
          new CellRangeAddress(1, 1, 0, NUM_OF_COLUMNS - 1));

      XDDFChartData data = chart.createData(ChartTypes.PIE, null, null);
      data.setVaryColors(true);
      data.addSeries(cat, val);
      chart.plot(data);

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

这适用于当前apache poi版本4.1.25.0.0.

于 2021-03-04T13:33:32.387 回答