Java 甜甜圈图ApachePOI
我正在尝试用ApachePOI创建甜甜圈图,但没有任何信息或示例。我尝试使用饼图的例子,但没有成功。你能帮我解决这个问题吗 首先请注意,对于下面的每个解决方案,都需要完整的jar,其中包含所有模式ooxml-schemas-1.4.jar较低版本,用于较旧的版本,如中所述 apache poi中没有甜甜圈图表数据类型。因此,在大多数apache poi版本中使用的最简单方法是使用org.openxmlformats.schemas.drawingml.x2006.chart的低级类 例如:Java 甜甜圈图ApachePOI,java,excel,apache-poi,Java,Excel,Apache Poi,我正在尝试用ApachePOI创建甜甜圈图,但没有任何信息或示例。我尝试使用饼图的例子,但没有成功。你能帮我解决这个问题吗 首先请注意,对于下面的每个解决方案,都需要完整的jar,其中包含所有模式ooxml-schemas-1.4.jar较低版本,用于较旧的版本,如中所述 apache poi中没有甜甜圈图表数据类型。因此,在大多数apache poi版本中使用的最简单方法是使用org.openxmlformats.schemas.drawingml.x2006.chart的低级类 例如: im
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();
}
}
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);
}
}
}
}
使用它可以创建一个使用XDDF的甜甜圈图表
例如:
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();
}
}
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);
}
}
}
}
嗨,阿克塞尔,你能提供一个4.1.2甜甜圈图表版本吗?@Doron Levi:不,我不能,因为org.apache.poi.xddf.usermodel.chart.xddf甜甜圈图表数据到目前为止是不完整和无用的。但是我的ApachePOI4.1.1示例使用我自己的XDDFDoughnutChartData,也可以使用ApachePOI4.1.2。@Doron Levi:请阅读我答案中的第一句话。您需要所有模式的完整jar ooxml-schemas-1.4.jar。