如何在饼图中使用Apache POI显示百分比。

huangapple go评论116阅读模式
英文:

how to display a percentage in pie chart using apache poi

问题

以下是翻译好的内容:

我正在尝试使用 Apache POI 制作一个饼图。我希望在饼图中显示像这样的百分比。

(图片已省略)

原始数据

(图片已省略)

以下是我的代码,我漏掉了什么吗?

XSSFDrawing drawing = sheet.createDrawingPatriarch();
XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 4, 7, 20);

XSSFChart chart = drawing.createChart(anchor);
chart.setTitleText("summary");
chart.setTitleOverlay(true);

XDDFChartLegend legend = chart.getOrAddLegend();
legend.setPosition(LegendPosition.TOP_RIGHT);

XDDFDataSource<String> status = XDDFDataSourcesFactory.fromStringCellRange(sheet,
    new CellRangeAddress(0, 0, 0, 2));

XDDFNumericalDataSource<Double> values = XDDFDataSourcesFactory.fromNumericCellRange(sheet,
    new CellRangeAddress(1, 1, 0, 2));

XDDFChartData data = chart.createData(ChartTypes.PIE3D, null, null);
data.setVaryColors(true);
XDDFChartData.Series series = data.addSeries(status, values);
chart.plot(data);
英文:

I'm trying to make a pie chart with apache poi. I want to display the percentage in pie chart like this.

如何在饼图中使用Apache POI显示百分比。

raw data

如何在饼图中使用Apache POI显示百分比。

here my code, what did i miss ??

XSSFDrawing drawing = sheet.createDrawingPatriarch();
XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 4, 7, 20);

XSSFChart chart = drawing.createChart(anchor);
chart.setTitleText(&quot;summary&quot;);
chart.setTitleOverlay(true);

XDDFChartLegend legend = chart.getOrAddLegend();
legend.setPosition(LegendPosition.TOP_RIGHT);

XDDFDataSource &lt; String &gt; status = XDDFDataSourcesFactory.fromStringCellRange(sheet,
    new CellRangeAddress(0, 0, 0, 2));

XDDFNumericalDataSource &lt; Double &gt; values = XDDFDataSourcesFactory.fromNumericCellRange(sheet,
    new CellRangeAddress(1, 1, 0, 2));

XDDFChartData data = chart.createData(ChartTypes.PIE3D, null, null);
data.setVaryColors(true);
XDDFChartData.Series series = data.addSeries(status, values);
chart.plot(data);

答案1

得分: 2

你缺少的是数据标签。

为图表设置数据标签在apche poiXDDF图表中直到版本4.1.2仍不支持。因此,我们需要使用ooxml-schemas类来实现这个功能。

以下代码生成您想要的图表。它需要apache poi 4.1.2以及额外的ooxml-schemas 1.4

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.*;
import org.apache.poi.xssf.usermodel.*;

public class PieChart {

  public static void main(String[] args) throws IOException {

    Object[][] data = new Object[][] {
     new Object[] {"A", "B", "C"},
     new Object[] {5d, 10d, 34d}
    };

    try (XSSFWorkbook wb = new XSSFWorkbook()) {
      XSSFSheet sheet = wb.createSheet("piechart");

      // 创建工作表数据
      Row row;
      Cell cell;
      for (int rowIndex = 0; rowIndex < 2; rowIndex++) {
        row = sheet.createRow((short) rowIndex);
        for (int colIndex = 0; colIndex < 3; colIndex++) {
          cell = row.createCell((short) colIndex);
          Object cellValue = data[rowIndex][colIndex];
          if (cellValue instanceof String) {
            cell.setCellValue((String)cellValue);
          } else if (cellValue instanceof Double) {
            cell.setCellValue((Double)cellValue);
          }
        }
      }

      // 创建绘图和锚点
      XSSFDrawing drawing = sheet.createDrawingPatriarch();
      XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 4, 0, 15, 15);

      // 创建图表
      XSSFChart chart = drawing.createChart(anchor);
      chart.setTitleText("summary");
      chart.setTitleOverlay(false);
      XDDFChartLegend legend = chart.getOrAddLegend();
      legend.setPosition(LegendPosition.TOP);

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

      XDDFChartData chartData = chart.createData(ChartTypes.PIE3D, null, null);
      chartData.setVaryColors(true);
      XDDFChartData.Series series = chartData.addSeries(cat, val);
      chart.plot(chartData);

      // 添加数据标签
      if (!chart.getCTChart().getPlotArea().getPie3DChartArray(0).getSerArray(0).isSetDLbls()) 
        chart.getCTChart().getPlotArea().getPie3DChartArray(0).getSerArray(0).addNewDLbls();
      chart.getCTChart().getPlotArea().getPie3DChartArray(0).getSerArray(0).getDLbls()
        .addNewShowLegendKey().setVal(false);
      chart.getCTChart().getPlotArea().getPie3DChartArray(0).getSerArray(0).getDLbls()
        .addNewShowPercent().setVal(true);
      chart.getCTChart().getPlotArea().getPie3DChartArray(0).getSerArray(0).getDLbls()
        .addNewShowLeaderLines().setVal(false);
      chart.getCTChart().getPlotArea().getPie3DChartArray(0).getSerArray(0).getDLbls()
        .addNewShowVal().setVal(false);
      chart.getCTChart().getPlotArea().getPie3DChartArray(0).getSerArray(0).getDLbls()
        .addNewShowCatName().setVal(false);
      chart.getCTChart().getPlotArea().getPie3DChartArray(0).getSerArray(0).getDLbls()
       .addNewShowSerName().setVal(false);
      chart.getCTChart().getPlotArea().getPie3DChartArray(0).getSerArray(0).getDLbls()
       .addNewShowBubbleSize().setVal(false);

      // 不自动删除标题;在Calc中显示标题是必要的
      if (chart.getCTChart().getAutoTitleDeleted() == null) chart.getCTChart().addNewAutoTitleDeleted();
      chart.getCTChart().getAutoTitleDeleted().setVal(false);

      // 数据点颜色
      byte[][] rgb = new byte[][]{
       new byte[] {127, 127, (byte)255},
       new byte[] {(byte)255, 127, 127},
       new byte[] {127, 127, 127}
      };
      for (int p = 0; p < 3; p++) {
        chart.getCTChart().getPlotArea().getPie3DChartArray(0).getSerArray(0).addNewDPt().addNewIdx().setVal(p);
        chart.getCTChart().getPlotArea().getPie3DChartArray(0).getSerArray(0).getDPtArray(p)
          .addNewSpPr().addNewSolidFill().addNewSrgbClr().setVal(rgb[p]);
      }

      // 将输出写入文件
      try (FileOutputStream fileOut = new FileOutputStream("ooxml-pie-chart.xlsx")) {
        wb.write(fileOut);
      }
    }
  }
}
英文:

What you are missing are the data labels.

Setting data labels for charts is not yet supported by apche poi XDDF charts up ti ver sion 4.1.2. So we need using the ooxml-schemas classes for this.

Following code generates your wanted chart. It needs apache poi 4.1.2 and additionally ooxml-schemas 1.4.

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.*;
import org.apache.poi.xssf.usermodel.*;
public class PieChart {
public static void main(String[] args) throws IOException {
Object[][] data = new Object[][] {
new Object[] {&quot;A&quot;, &quot;B&quot;, &quot;C&quot;},
new Object[] {5d, 10d, 34d}
};
try (XSSFWorkbook wb = new XSSFWorkbook()) {
XSSFSheet sheet = wb.createSheet(&quot;piechart&quot;);
// create sheet data
Row row;
Cell cell;
for (int rowIndex = 0; rowIndex &lt; 2; rowIndex++) {
row = sheet.createRow((short) rowIndex);
for (int colIndex = 0; colIndex &lt; 3; colIndex++) {
cell = row.createCell((short) colIndex);
Object cellValue = data[rowIndex][colIndex];
if (cellValue instanceof String) {
cell.setCellValue((String)cellValue);
} else if (cellValue instanceof Double) {
cell.setCellValue((Double)cellValue);
}
}
}
// create drawing and anchor
XSSFDrawing drawing = sheet.createDrawingPatriarch();
XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 4, 0, 15, 15);
// create chart
XSSFChart chart = drawing.createChart(anchor);
chart.setTitleText(&quot;summary&quot;);
chart.setTitleOverlay(false);
XDDFChartLegend legend = chart.getOrAddLegend();
legend.setPosition(LegendPosition.TOP);
XDDFDataSource&lt;String&gt; cat = XDDFDataSourcesFactory.fromStringCellRange(sheet,
new CellRangeAddress(0, 0, 0, 2));
XDDFNumericalDataSource&lt;Double&gt; val = XDDFDataSourcesFactory.fromNumericCellRange(sheet,
new CellRangeAddress(1, 1, 0, 2));
XDDFChartData chartData = chart.createData(ChartTypes.PIE3D, null, null);
chartData.setVaryColors(true);
XDDFChartData.Series series = chartData.addSeries(cat, val);
chart.plot(chartData);
// add data labels
if (!chart.getCTChart().getPlotArea().getPie3DChartArray(0).getSerArray(0).isSetDLbls()) 
chart.getCTChart().getPlotArea().getPie3DChartArray(0).getSerArray(0).addNewDLbls();
chart.getCTChart().getPlotArea().getPie3DChartArray(0).getSerArray(0).getDLbls()
.addNewShowLegendKey().setVal(false);
chart.getCTChart().getPlotArea().getPie3DChartArray(0).getSerArray(0).getDLbls()
.addNewShowPercent().setVal(true);
chart.getCTChart().getPlotArea().getPie3DChartArray(0).getSerArray(0).getDLbls()
.addNewShowLeaderLines().setVal(false);
chart.getCTChart().getPlotArea().getPie3DChartArray(0).getSerArray(0).getDLbls()
.addNewShowVal().setVal(false);
chart.getCTChart().getPlotArea().getPie3DChartArray(0).getSerArray(0).getDLbls()
.addNewShowCatName().setVal(false);
chart.getCTChart().getPlotArea().getPie3DChartArray(0).getSerArray(0).getDLbls()
.addNewShowSerName().setVal(false);
chart.getCTChart().getPlotArea().getPie3DChartArray(0).getSerArray(0).getDLbls()
.addNewShowBubbleSize().setVal(false);
// 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
byte[][] rgb = new byte[][]{
new byte[] {127, 127, (byte)255},
new byte[] {(byte)255, 127, 127},
new byte[] {127, 127, 127}
};
for (int p = 0; p &lt; 3; p++) {
chart.getCTChart().getPlotArea().getPie3DChartArray(0).getSerArray(0).addNewDPt().addNewIdx().setVal(p);
chart.getCTChart().getPlotArea().getPie3DChartArray(0).getSerArray(0).getDPtArray(p)
.addNewSpPr().addNewSolidFill().addNewSrgbClr().setVal(rgb

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

huangapple
  • 本文由 发表于 2020年9月21日 16:03:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63988310.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定