圆环图 Apache-POI [英] Doughnut Chart Apache-POI

查看:73
本文介绍了圆环图 Apache-POI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 apache-poi 创建甜甜圈图,但没有任何信息或示例.我试图使用饼图的例子,但没有成功.你能帮忙解决这个问题吗?

解决方案

首先请注意,对于以下每个解决方案,所有架构的完整 jar ooxml-schemas-1.4.jar(较低版本如FAQ N10025中所述,需要使用旧版本.>

apache poi 中还没有圆环图数据类型.因此,在大多数 apache poi 版本中最简单和可用的方法将使用 org.openxmlformats.schemas.drawingml.x2006.chart.

的低级类

示例:

import java.io.FileOutputStream;导入 org.apache.poi.ss.usermodel.*;导入 org.apache.poi.ss.util.*;导入 org.apache.poi.xssf.usermodel.XSSFWorkbook;导入 org.apache.poi.xssf.usermodel.XSSFChart;导入 org.apache.poi.xssf.usermodel.XSSFDrawing;导入 org.openxmlformats.schemas.drawingml.x2006.chart.CTChart;导入 org.openxmlformats.schemas.drawingml.x2006.chart.CTPlotArea;导入 org.openxmlformats.schemas.drawingml.x2006.chart.CTDoughnutChart;导入 org.openxmlformats.schemas.drawingml.x2006.chart.CTBoolean;导入 org.openxmlformats.schemas.drawingml.x2006.chart.CTPieSer;导入 org.openxmlformats.schemas.drawingml.x2006.chart.CTAxDataSource;导入 org.openxmlformats.schemas.drawingml.x2006.chart.CTNumDataSource;导入 org.openxmlformats.schemas.drawingml.x2006.chart.CTNumRef;导入 org.openxmlformats.schemas.drawingml.x2006.chart.CTStrRef;导入 org.apache.poi.xssf.usermodel.DefaultIndexedColorMap;公共类 DoughnutChart {public static void main(String[] args) 抛出异常 {工作簿 wb = new XSSFWorkbook();Sheet sheet = wb.createSheet(Sheet1");行行;细胞细胞;for (int r = 0; r <3; r++) {row = sheet.createRow(r);单元格 = row.createCell(0);cell.setCellValue("S" + r);单元格 = row.createCell(1);cell.setCellValue(r+1);}XSSFDrawing 绘图 = (XSSFDrawing)sheet.createDrawingPatriarch();ClientAnchor 锚点 =drawing.createAnchor(0, 0, 0, 0, 0, 5, 5, 20);XSSFChart 图表 =drawing.createChart(anchor);CTChart ctChart = ((XSSFChart)chart).getCTChart();CTPlotArea ctPlotArea = ctChart.getPlotArea();CTDoughnutChart ctDoughnutChart = ctPlotArea.addNewDoughnutChart();ctDoughnutChart.addNewVaryColors().setVal(true);ctDoughnutChart.addNewHoleSize().setVal((short)50);CTPieSer ctPieSer = ctDoughnutChart.addNewSer();ctPieSer.addNewIdx().setVal(0);CTAxDataSource cttAxDataSource = ctPieSer.addNewCat();CTStrRef ctStrRef = cttAxDataSource.addNewStrRef();ctStrRef.setF("Sheet1!$A$1:$A$3");CTNumDataSource ctNumDataSource = ctPieSer.addNewVal();CTNumRef ctNumRef = ctNumDataSource.addNewNumRef();ctNumRef.setF("Sheet1!$B$1:$B$3");//数据点颜色;在 Calc 中显示数据点是必要的int pointCount = 3;for (int p = 0; p 


使用当前的 apache poi 4.1.1 可以提供自己的 XDDFDoughnutChartData 如下:

import org.apache.poi.xddf.usermodel.chart.*;导入 org.apache.poi.util.Beta;导入 org.apache.poi.xddf.usermodel.XDDFShapeProperties;导入 org.openxmlformats.schemas.drawingml.x2006.chart.CTAxDataSource;导入 org.openxmlformats.schemas.drawingml.x2006.chart.CTNumDataSource;导入 org.openxmlformats.schemas.drawingml.x2006.chart.CTDoughnutChart;导入 org.openxmlformats.schemas.drawingml.x2006.chart.CTPieSer;导入 org.openxmlformats.schemas.drawingml.x2006.chart.CTSerTx;@测试版公共类 XDDFDoughnutChartData 扩展 XDDFChartData {私人 CTDoughnutChart 图表;公共 XDDFDoughnutChartData(XDDFChart 父级,CTDoughnutChart 图表){超级(父母);this.chart = 图表;for (CTPieSer 系列:chart.getSerList()) {this.series.add(new Series(series, series.getCat(), series.getVal()));}}@覆盖protected void removeCTSeries(int n) {chart.removeSer(n);}@覆盖公共无效 setVaryColors(布尔变化颜色){如果(不同颜色 == 空){如果(chart.isSetVaryColors()){chart.unsetVaryColors();}} 别的 {如果(chart.isSetVaryColors()){chart.getVaryColors().setVal(varyColors);} 别的 {chart.addNewVaryColors().setVal(varyColors);}}}公共整数 getFirstSliceAngle() {如果(chart.isSetFirstSliceAng()){返回 chart.getFirstSliceAng().getVal();} 别的 {返回空;}}公共无效setFirstSliceAngle(整数角度){如果(角度==空){如果(chart.isSetFirstSliceAng()){chart.unsetFirstSliceAng();}} 别的 {如果(角度 <0 || 360 <角度){throw new IllegalArgumentException(角度必须在 0 到 360 度之间");}如果(chart.isSetFirstSliceAng()){chart.getFirstSliceAng().setVal(angle);} 别的 {chart.addNewFirstSliceAng().setVal(angle);}}}公共短 getHoleSize() {如果(chart.isSetHoleSize()){返回 chart.getHoleSize().getVal();} 别的 {返回空;}}公共无效setHoleSize(短尺寸){如果(大小==空){如果(chart.isSetHoleSize()){chart.unsetHoleSize();}} 别的 {如果(大小 <0 || 100 <大小){throw new IllegalArgumentException("size must be between 0 and 100");}如果(chart.isSetHoleSize()){chart.getHoleSize().setVal(size);} 别的 {chart.addNewHoleSize().setVal(size);}}}@覆盖公共 XDDFChartData.Series addSeries(XDDFDataSource category,XDDFNumericalDataSource值){最终长索引 = this.series.size();最终 CTPieSer ctSer = this.chart.addNewSer();ctSer.addNewCat();ctSer.addNewVal();ctSer.addNewIdx().setVal(index);ctSer.addNewOrder().setVal(index);添加最终系列 = 新系列(ctSer,类别,值);this.series.add(add);返回添加;}公共类系列扩展 XDDFChartData.Series {私人CTPieSer系列;受保护系列(CTPieSer 系列,XDDFDataSource 类别,XDDFNumericalDataSource值){超级(类别,值);this.series = 系列;}受保护系列(CTPieSer 系列,CTAxDataSource 类别,CTNumDataSource 值){super(XDDFDataSourcesFactory.fromDataSource(category), XDDFDataSourcesFactory.fromDataSource(values));this.series = 系列;}@覆盖受保护的 CTSeTx getSeriesText() {如果(series.isSetTx()){返回 series.getTx();} 别的 {返回 series.addNewTx();}}@覆盖公共无效 setShowLeaderLines(boolean showLeaderLines) {如果 (!series.isSetDLbls()) {series.addNewDLbls();}如果 (series.getDLbls().isSetShowLeaderLines()) {series.getDLbls().getShowLeaderLines().setVal(showLeaderLines);} 别的 {series.getDLbls().addNewShowLeaderLines().setVal(showLeaderLines);}}@覆盖公共 XDDFShapeProperties getShapeProperties() {如果(series.isSetSpPr()){返回新的 XDDFShapeProperties(series.getSpPr());} 别的 {返回空;}}@覆盖公共无效 setShapeProperties(XDDFShapeProperties 属性){如果(属性 == 空){如果(series.isSetSpPr()){系列.unsetSpPr();}} 别的 {如果(series.isSetSpPr()){series.setSpPr(properties.getXmlObject());} 别的 {series.addNewSpPr().set(properties.getXmlObject());}}}公共长 getExplosion() {如果(series.isSetExplosion()){返回 series.getExplosion().getVal();} 别的 {返回空;}}public void setExplosion(长爆炸){如果(爆炸==空){如果(series.isSetExplosion()){系列.unsetExplosion();}} 别的 {如果(series.isSetExplosion()){series.getExplosion().setVal(explosion);} 别的 {series.addNewExplosion().setVal(explosion);}}}@覆盖受保护的 CTAxDataSource getAxDS() {返回 series.getCat();}@覆盖受保护的 CTNumDataSource getNumDS() {返回 series.getVal();}@覆盖protected void setIndex(long val) {series.getIdx().setVal(val);}@覆盖受保护的无效 setOrder(long val) {series.getOrder().setVal(val);}}}

使用它可以使用 XDDF 东西创建一个圆环图.

示例:

import java.io.FileOutputStream;导入 java.io.IOException;导入 org.apache.poi.ss.usermodel.Cell;导入 org.apache.poi.ss.usermodel.Row;导入 org.apache.poi.ss.util.CellRangeAddress;导入 org.apache.poi.xddf.usermodel.chart.LegendPosition;导入 org.apache.poi.xddf.usermodel.chart.XDDFChartData;导入 org.apache.poi.xddf.usermodel.chart.XDDFChartLegend;导入 org.apache.poi.xddf.usermodel.chart.XDDFDataSource;导入 org.apache.poi.xddf.usermodel.chart.XDDFDataSourcesFactory;导入 org.apache.poi.xddf.usermodel.chart.XDDFNumericalDataSource;导入 org.apache.poi.xssf.usermodel.XSSFChart;导入 org.apache.poi.xssf.usermodel.XSSFClientAnchor;导入 org.apache.poi.xssf.usermodel.XSSFDrawing;导入 org.apache.poi.xssf.usermodel.XSSFSheet;导入 org.apache.poi.xssf.usermodel.XSSFWorkbook;导入 org.apache.poi.xssf.usermodel.DefaultIndexedColorMap;公共类 DoughnutChartXDDF {public static void main(String[] args) 抛出 IOException {试试 (XSSFWorkbook wb = new XSSFWorkbook()) {XSSFSheet sheet = wb.createSheet(doughnutChart");最终 int NUM_OF_ROWS = 2;最终 int NUM_OF_COLUMNS = 10;//创建一行并在其中放置一些单元格.行是基于 0 的.行行;细胞细胞;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 绘图 = sheet.createDrawingPatriarch();XSSFClientAnchor 锚点 =drawing.createAnchor(0, 0, 0, 0, 0, 4, 10, 25);XSSFChart 图表 =drawing.createChart(anchor);chart.setTitleText(甜甜圈图");chart.setTitleOverlay(false);XDDFChartLegend 图例 = chart.getOrAddLegend();Legend.setPosition(LegendPosition.TOP_RIGHT);XDDFDataSourcecat = XDDFDataSourcesFactory.fromStringCellRange(sheet,新的 CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1));XDDFNumericalDataSourceval = XDDFDataSourcesFactory.fromNumericCellRange(sheet,新的 CellRangeAddress(1, 1, 0, NUM_OF_COLUMNS - 1));XDDFDoughnutChartData data = new XDDFDoughnutChartData(chart, chart.getCTChart().getPlotArea().addNewDoughnutChart());data.setVaryColors(true);data.setHoleSize((short)50);XDDFChartData.Series 系列 = data.addSeries(cat, val);图表(数据);//不要自动删除标题;在 Calc 中显示标题是必要的if (chart.getCTChart().getAutoTitleDeleted() == null) chart.getCTChart().addNewAutoTitleDeleted();chart.getCTChart().getAutoTitleDeleted().setVal(false);//数据点颜色;在 Calc 中显示数据点是必要的int pointCount = series.getCategoryData().getPointCount();for (int p = 0; p 

I'am trying to create Doughnut Chart with apache-poi, but there's no information or examples. I tried to use example of pie chart but unsuccesful. Can you help with this problem?

解决方案

At first note, for each following solution the full jar of all of the schemas ooxml-schemas-1.4.jar (lower versions for older releases) is needed as mentioned in FAQ N10025.

There is not a doughnut chart data type already in apache poi. So the simplest method and usable in most apache poi versions will be using the low level classes of org.openxmlformats.schemas.drawingml.x2006.chart.

Example:

import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFChart;
import org.apache.poi.xssf.usermodel.XSSFDrawing;

import org.openxmlformats.schemas.drawingml.x2006.chart.CTChart;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTPlotArea;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTDoughnutChart;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTBoolean;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTPieSer;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTAxDataSource;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumDataSource;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumRef;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTStrRef;

import org.apache.poi.xssf.usermodel.DefaultIndexedColorMap;

public class DoughnutChart {

    public static void main(String[] args) throws Exception {
        Workbook wb = new XSSFWorkbook();
        Sheet sheet = wb.createSheet("Sheet1");

        Row row;
        Cell cell;
        for (int r = 0; r < 3; r++) {
            row = sheet.createRow(r);
            cell = row.createCell(0);
            cell.setCellValue("S" + r);
            cell = row.createCell(1);
            cell.setCellValue(r+1);
        }

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

        XSSFChart chart = drawing.createChart(anchor);

        CTChart ctChart = ((XSSFChart)chart).getCTChart();
        CTPlotArea ctPlotArea = ctChart.getPlotArea();
        CTDoughnutChart ctDoughnutChart = ctPlotArea.addNewDoughnutChart();
        ctDoughnutChart.addNewVaryColors().setVal(true);
        ctDoughnutChart.addNewHoleSize().setVal((short)50);
        CTPieSer ctPieSer = ctDoughnutChart.addNewSer();

        ctPieSer.addNewIdx().setVal(0);     

        CTAxDataSource cttAxDataSource = ctPieSer.addNewCat();
        CTStrRef ctStrRef = cttAxDataSource.addNewStrRef();
        ctStrRef.setF("Sheet1!$A$1:$A$3"); 
        CTNumDataSource ctNumDataSource = ctPieSer.addNewVal();
        CTNumRef ctNumRef = ctNumDataSource.addNewNumRef();
        ctNumRef.setF("Sheet1!$B$1:$B$3"); 

        // Data point colors; is necessary for showing data points in Calc
        int pointCount = 3; 
        for (int p = 0; p < pointCount; p++) {
            ctChart.getPlotArea().getDoughnutChartArray(0).getSerArray(0).addNewDPt().addNewIdx().setVal(p);
            ctChart.getPlotArea().getDoughnutChartArray(0).getSerArray(0).getDPtArray(p)
                .addNewSpPr().addNewSolidFill().addNewSrgbClr().setVal(DefaultIndexedColorMap.getDefaultRGB(p+10));
        }

System.out.println(ctChart);

        FileOutputStream fileOut = new FileOutputStream("workbook.xlsx");
        wb.write(fileOut);
        fileOut.close();
        wb.close();
    }
}


Using the current apache poi 4.1.1 one could provide an own XDDFDoughnutChartData as follows:

import org.apache.poi.xddf.usermodel.chart.*;

import org.apache.poi.util.Beta;
import org.apache.poi.xddf.usermodel.XDDFShapeProperties;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTAxDataSource;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumDataSource;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTDoughnutChart;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTPieSer;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTSerTx;

@Beta
public class XDDFDoughnutChartData extends XDDFChartData {
    private CTDoughnutChart chart;

    public XDDFDoughnutChartData(XDDFChart parent, CTDoughnutChart chart) {
        super(parent);
        this.chart = chart;
        for (CTPieSer series : chart.getSerList()) {
            this.series.add(new Series(series, series.getCat(), series.getVal()));
        }
    }

    @Override
    protected void removeCTSeries(int n) {
        chart.removeSer(n);
    }

    @Override
    public void setVaryColors(Boolean varyColors) {
        if (varyColors == null) {
            if (chart.isSetVaryColors()) {
                chart.unsetVaryColors();
            }
        } else {
            if (chart.isSetVaryColors()) {
                chart.getVaryColors().setVal(varyColors);
            } else {
                chart.addNewVaryColors().setVal(varyColors);
            }
        }
    }

    public Integer getFirstSliceAngle() {
        if (chart.isSetFirstSliceAng()) {
            return chart.getFirstSliceAng().getVal();
        } else {
            return null;
        }
    }

    public void setFirstSliceAngle(Integer angle) {
        if (angle == null) {
            if (chart.isSetFirstSliceAng()) {
                chart.unsetFirstSliceAng();
            }
        } else {
            if (angle < 0 || 360 < angle) {
                throw new IllegalArgumentException("angle must be between 0 and 360");
            }
            if (chart.isSetFirstSliceAng()) {
                chart.getFirstSliceAng().setVal(angle);
            } else {
                chart.addNewFirstSliceAng().setVal(angle);
            }
        }
    }

    public Short getHoleSize() {
        if (chart.isSetHoleSize()) {
            return chart.getHoleSize().getVal();
        } else {
            return null;
        }
    }

    public void setHoleSize(Short size) {
        if (size == null) {
            if (chart.isSetHoleSize()) {
                chart.unsetHoleSize();
            }
        } else {
            if (size < 0 || 100 < size) {
                throw new IllegalArgumentException("size must be between 0 and 100");
            }
            if (chart.isSetHoleSize()) {
                chart.getHoleSize().setVal(size);
            } else {
                chart.addNewHoleSize().setVal(size);
            }
        }
    }

    @Override
    public XDDFChartData.Series addSeries(XDDFDataSource<?> category,
            XDDFNumericalDataSource<? extends Number> values) {
        final long index = this.series.size();
        final CTPieSer ctSer = this.chart.addNewSer();
        ctSer.addNewCat();
        ctSer.addNewVal();
        ctSer.addNewIdx().setVal(index);
        ctSer.addNewOrder().setVal(index);
        final Series added = new Series(ctSer, category, values);
        this.series.add(added);
        return added;
    }

    public class Series extends XDDFChartData.Series {
        private CTPieSer series;

        protected Series(CTPieSer series, XDDFDataSource<?> category,
                XDDFNumericalDataSource<? extends Number> values) {
            super(category, values);
            this.series = series;
        }

        protected Series(CTPieSer series, CTAxDataSource category, CTNumDataSource values) {
            super(XDDFDataSourcesFactory.fromDataSource(category), XDDFDataSourcesFactory.fromDataSource(values));
            this.series = series;
        }

        @Override
        protected CTSerTx getSeriesText() {
            if (series.isSetTx()) {
                return series.getTx();
            } else {
                return series.addNewTx();
            }
        }

        @Override
        public void setShowLeaderLines(boolean showLeaderLines) {
            if (!series.isSetDLbls()) {
                series.addNewDLbls();
            }
            if (series.getDLbls().isSetShowLeaderLines()) {
                series.getDLbls().getShowLeaderLines().setVal(showLeaderLines);
            } else {
                series.getDLbls().addNewShowLeaderLines().setVal(showLeaderLines);
            }
        }

        @Override
        public XDDFShapeProperties getShapeProperties() {
            if (series.isSetSpPr()) {
                return new XDDFShapeProperties(series.getSpPr());
            } else {
                return null;
            }
        }

        @Override
        public void setShapeProperties(XDDFShapeProperties properties) {
            if (properties == null) {
                if (series.isSetSpPr()) {
                    series.unsetSpPr();
                }
            } else {
                if (series.isSetSpPr()) {
                    series.setSpPr(properties.getXmlObject());
                } else {
                    series.addNewSpPr().set(properties.getXmlObject());
                }
            }
        }

        public Long getExplosion() {
            if (series.isSetExplosion()) {
                return series.getExplosion().getVal();
            } else {
                return null;
            }
        }

        public void setExplosion(Long explosion) {
            if (explosion == null) {
                if (series.isSetExplosion()) {
                    series.unsetExplosion();
                }
            } else {
                if (series.isSetExplosion()) {
                    series.getExplosion().setVal(explosion);
                } else {
                    series.addNewExplosion().setVal(explosion);
                }
            }
        }

        @Override
        protected CTAxDataSource getAxDS() {
            return series.getCat();
        }

        @Override
        protected CTNumDataSource getNumDS() {
            return series.getVal();
        }

        @Override
        protected void setIndex(long val) {
            series.getIdx().setVal(val);
        }

        @Override
        protected void setOrder(long val) {
            series.getOrder().setVal(val);
        }
    }
}

And using that one can create a doughnut chart using the XDDF stuff.

Example:

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

import org.apache.poi.xssf.usermodel.DefaultIndexedColorMap;


public class DoughnutChartXDDF {

  public static void main(String[] args) throws IOException {
    try (XSSFWorkbook wb = new XSSFWorkbook()) {
      XSSFSheet sheet = wb.createSheet("doughnutChart");
      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);
      chart.setTitleText("Doughnut Chart");
      chart.setTitleOverlay(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));

      XDDFDoughnutChartData data = new XDDFDoughnutChartData(chart, chart.getCTChart().getPlotArea().addNewDoughnutChart());
      data.setVaryColors(true);
      data.setHoleSize((short)50);
      XDDFChartData.Series series = data.addSeries(cat, val);
      chart.plot(data);

      // Do not auto delete the title; is necessary for showing title in Calc
      if (chart.getCTChart().getAutoTitleDeleted() == null) chart.getCTChart().addNewAutoTitleDeleted();
      chart.getCTChart().getAutoTitleDeleted().setVal(false);

      // Data point colors; is necessary for showing data points in Calc
      int pointCount = series.getCategoryData().getPointCount(); 
      for (int p = 0; p < pointCount; p++) {
        chart.getCTChart().getPlotArea().getDoughnutChartArray(0).getSerArray(0).addNewDPt().addNewIdx().setVal(p);
        chart.getCTChart().getPlotArea().getDoughnutChartArray(0).getSerArray(0).getDPtArray(p)
          .addNewSpPr().addNewSolidFill().addNewSrgbClr().setVal(DefaultIndexedColorMap.getDefaultRGB(p+10));
      }
 
      // Write the output to a file
      try (FileOutputStream fileOut = new FileOutputStream("ooxml-doughnut-chart.xlsx")) {
        wb.write(fileOut);
      }
    }
  }
}

这篇关于圆环图 Apache-POI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆