如何使用 apache poi 4.0 XDDFChartData, XDDFPieChartData 生成饼图;在使用java的excel表中,我不想使用jfree [英] how to generate piechart using apache poi 4.0 XDDFChartData, XDDFPieChartData; in excel sheet using java, i dont want to use jfree
问题描述
我需要一个 Java 示例代码,目前我可以在从工作表读取值后绘制折线图,我也想生成饼图.
我尝试过的用于 .ppt
格式的示例代码.
我想在 excel 中为 Java 中的饼图提供相同的内容.
String[] Categories = listCategories.toArray(new String[listCategories.size()]);Double[] values = listValues.toArray(new Double[listValues.size()]);最终 int numOfPoints = Categories.length;最终字符串 categoryDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, 0, 0));final String valuesDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, 1, 1));最终的 XDDFDataSource>categoryData = XDDFDataSourcesFactory.fromArray(categories, categoryDataRange);最终 XDDFNumericalDataSourcevaluesData = XDDFDataSourcesFactory.fromArray(values, valuesDataRange);XDDFPieChartData.Series firstSeries = (XDDFPieChartData.Series) pie.getSeries().get(0);firstSeries.replaceData(categoriesData, valuesData);firstSeries.setTitle(chartTitle, chart.setSheetTitle(chartTitle, 0));firstSeries.setExplosion(25);chart.plot(饼图);
源自 https://svn.apache.org/repos/asf/poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/ 这是一个使用 XDDF
内容的 PieChart
示例:
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.xddf.usermodel.chart.XDDFPieChartData;导入 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;公共类饼图{public static void main(String[] args) 抛出 IOException {试试 (XSSFWorkbook wb = new XSSFWorkbook()) {XSSFSheet sheet = wb.createSheet("piechart");最终 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));XDDFChartData 数据 = 新 XDDFPieChartData(chart.getCTChart().getPlotArea().addNewPieChart());data.setVaryColors(true);data.addSeries(cat, val);图表(数据);//将输出写入文件试试 (FileOutputStream fileOut = new FileOutputStream("ooxml-pie-chart.xlsx")) {wb.write(fileOut);}}}}
<小时>
以上是仅使用 XDDF
内容的最小示例.这是一个更扩展的版本,它设置数据标签并使图表在 LibreOffice/OpenOffice Calc
中可见.但这需要使用低级 org.openxmlformats.schemas.drawingml.x2006.chart.*
的东西.
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.xddf.usermodel.chart.XDDFPieChartData;导入 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;公共类饼图{public static void main(String[] args) 抛出 IOException {试试 (XSSFWorkbook wb = new XSSFWorkbook()) {XSSFSheet sheet = wb.createSheet("piechart");最终 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));XDDFChartData 数据 = 新 XDDFPieChartData(chart.getCTChart().getPlotArea().addNewPieChart());data.setVaryColors(true);XDDFChartData.Series 系列 = data.addSeries(cat, val);图表(数据);//添加数据标签如果 (!chart.getCTChart().getPlotArea().getPieChartArray(0).getSerArray(0).isSetDLbls())chart.getCTChart().getPlotArea().getPieChartArray(0).getSerArray(0).addNewDLbls();chart.getCTChart().getPlotArea().getPieChartArray(0).getSerArray(0).getDLbls().addNewDLblPos().setVal(org.openxmlformats.schemas.drawingml.x2006.chart.STDLblPos.OUT_END);chart.getCTChart().getPlotArea().getPieChartArray(0).getSerArray(0).getDLbls().addNewShowLegendKey().setVal(true);chart.getCTChart().getPlotArea().getPieChartArray(0).getSerArray(0).getDLbls().addNewShowPercent().setVal(true);//不要自动删除标题;在 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
此代码需要 ooxml-schemas-1.4.jar
中提到的所有模式的完整 jar.html#faq-N10025" rel="nofollow noreferrer">FAQ-N10025.
从 apache poi 4.1.1
版本开始,XDDFChartData 数据
的创建必须像:
I need a sample code in Java, currently I am able to draw line charts after reading values from sheet, and I also want to generate pie chart.
Example code I have tried which is for .ppt
format.
I want the same in excel for pie chart in java.
String[] categories = listCategories.toArray(new String[listCategories.size()]);
Double[] values = listValues.toArray(new Double[listValues.size()]);
final int numOfPoints = categories.length;
final String categoryDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, 0, 0));
final String valuesDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, 1, 1));
final XDDFDataSource<?> categoriesData = XDDFDataSourcesFactory.fromArray(categories, categoryDataRange);
final XDDFNumericalDataSource<? extends Number> valuesData = XDDFDataSourcesFactory.fromArray(values, valuesDataRange);
XDDFPieChartData.Series firstSeries = (XDDFPieChartData.Series) pie.getSeries().get(0);
firstSeries.replaceData(categoriesData, valuesData);
firstSeries.setTitle(chartTitle, chart.setSheetTitle(chartTitle, 0));
firstSeries.setExplosion(25);
chart.plot(pie);
Derived from the chart examples in https://svn.apache.org/repos/asf/poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/ here is a PieChart
example using the XDDF
stuff:
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.xddf.usermodel.chart.XDDFPieChartData;
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 {
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);
chart.setTitleText("Pie 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));
XDDFChartData data = new XDDFPieChartData(chart.getCTChart().getPlotArea().addNewPieChart());
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);
}
}
}
}
The above is the minimal example using only the XDDF
stuff. Here is a more extended version which sets data labels and makes the chart visible in LibreOffice/OpenOffice Calc
. But this needs using the low level org.openxmlformats.schemas.drawingml.x2006.chart.*
stuff.
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.xddf.usermodel.chart.XDDFPieChartData;
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 PieChart {
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);
chart.setTitleText("Pie 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));
XDDFChartData data = new XDDFPieChartData(chart.getCTChart().getPlotArea().addNewPieChart());
data.setVaryColors(true);
XDDFChartData.Series series = data.addSeries(cat, val);
chart.plot(data);
// Add data labels
if (!chart.getCTChart().getPlotArea().getPieChartArray(0).getSerArray(0).isSetDLbls())
chart.getCTChart().getPlotArea().getPieChartArray(0).getSerArray(0).addNewDLbls();
chart.getCTChart().getPlotArea().getPieChartArray(0).getSerArray(0).getDLbls()
.addNewDLblPos().setVal(org.openxmlformats.schemas.drawingml.x2006.chart.STDLblPos.OUT_END);
chart.getCTChart().getPlotArea().getPieChartArray(0).getSerArray(0).getDLbls()
.addNewShowLegendKey().setVal(true);
chart.getCTChart().getPlotArea().getPieChartArray(0).getSerArray(0).getDLbls()
.addNewShowPercent().setVal(true);
// 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().getPieChartArray(0).getSerArray(0).addNewDPt().addNewIdx().setVal(p);
chart.getCTChart().getPlotArea().getPieChartArray(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-pie-chart.xlsx")) {
wb.write(fileOut);
}
}
}
}
This code needs the full jar of all of the schemas ooxml-schemas-1.4.jar
as mentioned in FAQ-N10025.
Since version apache poi 4.1.1
creation of the XDDFChartData data
must be changed like:
...
import org.apache.poi.xddf.usermodel.chart.ChartTypes;
...
//XDDFChartData data = new XDDFPieChartData(chart.getCTChart().getPlotArea().addNewPieChart());
XDDFChartData data = chart.createData(ChartTypes.PIE, null, null);
...
这篇关于如何使用 apache poi 4.0 XDDFChartData, XDDFPieChartData 生成饼图;在使用java的excel表中,我不想使用jfree的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!