gpt4 book ai didi

java - 如何使用 apache poi 4.0 XDDFChartData、XDDFPieChartData 生成饼图;在使用 java 的 excel 表中,我不想使用 jfree

转载 作者:行者123 更新时间:2023-11-30 07:42:18 31 4
gpt4 key购买 nike

我需要一个Java示例代码,目前我可以在从sheet读取值后绘制折线图,​​我还想生成饼图。

我试过的示例代码是 .ppt 格式的。

我希望在 excel 中使用 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);

最佳答案

源自 https://svn.apache.org/repos/asf/poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/ 中的图表示例这是一个使用 XDDF 内容的 PieChart 示例:

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);
}
}
}
}

以上是仅使用 XDDF 内容的最小示例。这是一个更扩展的版本,它设置数据标签并使图表在 LibreOffice/OpenOffice Calc 中可见。但这需要使用低级 org.openxmlformats.schemas.drawingml.x2006.chart.* 东西。

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);
}
}
}
}

此代码需要所有模式的完整 jar ooxml-schemas-1.4.jar,如 FAQ-N10025 中所述.


自版本 apache poi 4.1.1 以来,XDDFChartData 数据 的创建必须更改为:

...
import org.apache.poi.xddf.usermodel.chart.ChartTypes;
...
//XDDFChartData data = new XDDFPieChartData(chart.getCTChart().getPlotArea().addNewPieChart());
XDDFChartData data = chart.createData(ChartTypes.PIE, null, null);
...

关于java - 如何使用 apache poi 4.0 XDDFChartData、XDDFPieChartData 生成饼图;在使用 java 的 excel 表中,我不想使用 jfree,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54885970/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com