如何更改Anchore的背景颜色

问题描述 投票:0回答:1

我正在实现一个 Apache POI 图表,它创建三个图表。图表的背景颜色为白色。但我想将其更改为自定义 RGB 颜色。

在上图中,我想将 XSSFClientAnchore 的整个红色标记区域的颜色更改为自定义颜色。

 private void createDounughtChart(final XSSFWorkbook workbook,String sheetName){
    final XSSFSheet uebersicht = workbook.getSheet(sheetName);
    String[] categories = new String[]{"one","two","three","four","five","six","seven","eight","nine","ten"};
    Number[] values = new Number[]{ 11L,2L,3L,4L,5L,6L,7L,8L,9L,10L };
    XSSFChart chart1 = createDoughnutChart(uebersicht, categories, values, "Zusammensetzung Abfallfraktionen\n", 2, 5, 5, 26);

    categories = new String[]{"one","two"};
    values = new Number[]{ 60L,40L };
    XSSFChart chart2 = createDoughnutChart(uebersicht, categories, values, "Anteil gefährliche / nicht gefährliche Abfälle\n", 5, 5, 8, 24);

    categories = new String[]{"one","two","three","four","five","six","seven","eight","nine","ten"};
    values = new Number[]{ 11L,2L,3L,4L,5L,6L,7L,8L,9L,10L };
    XSSFChart chart3 = createDoughnutChart(uebersicht, categories, values, "Zusammensetzung Entsorgungswege\n", 8, 5, 11, 26);
}
static XSSFChart createDoughnutChart(XSSFSheet sheet, String[] categories, Number[] values, String titleText, int col1, int row1, int col2, int row2) {
    XSSFDrawing drawing = sheet.createDrawingPatriarch();
    XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, col1, row1, col2, row2);

    XSSFChart chart = drawing.createChart(anchor);
    chart.setTitleText(titleText);
    chart.setTitleOverlay(false);

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

    XDDFDataSource<String> cat = XDDFDataSourcesFactory.fromArray(categories);
    XDDFNumericalDataSource<Number> val = XDDFDataSourcesFactory.fromArray(values);

    XDDFDoughnutChartData data = (XDDFDoughnutChartData)chart.createData(ChartTypes.DOUGHNUT, null, null);
    //XDDFDoughnutChartData data = new XDDFDoughnutChartData(chart, chart.getCTChart().getPlotArea().addNewDoughnutChart());
    data.setVaryColors(true);
    //data.setHoleSize(10);
    //data.setHoleSize(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(XlsxService.getColor(p));
    }

    // Add data labels
    if (!chart.getCTChart().getPlotArea().getDoughnutChartArray(0).getSerArray(0).isSetDLbls()) {
        chart.getCTChart().getPlotArea().getDoughnutChartArray(0).getSerArray(0).addNewDLbls();
    }
    chart.getCTChart().getPlotArea().getDoughnutChartArray(0).getSerArray(0).getDLbls().addNewShowVal().setVal(true);
    chart.getCTChart().getPlotArea().getDoughnutChartArray(0).getSerArray(0).getDLbls().addNewShowSerName().setVal(false);
    chart.getCTChart().getPlotArea().getDoughnutChartArray(0).getSerArray(0).getDLbls().addNewShowCatName().setVal(false);
    chart.getCTChart().getPlotArea().getDoughnutChartArray(0).getSerArray(0).getDLbls().addNewShowPercent().setVal(false);
    chart.getCTChart().getPlotArea().getDoughnutChartArray(0).getSerArray(0).getDLbls().addNewShowLegendKey().setVal(false);


    // chart area (chartspace) without border line
    chart.getCTChartSpace().addNewSpPr().addNewLn().addNewNoFill();

    return chart;
}
java spring-boot charts apache-poi
1个回答
0
投票

您似乎想为图表空间着色。这与锚点无关,而是图表中的设置。

怎么看?像现在一样创建图表。之后解压创建的

*.xlsx
文件。看看
/xl/charts/chart*.xml
。记住 XML。然后在 Excel GUI 中打开该文件并执行您想要的操作。保存更改。之后再次解压
*.xlsx
文件并查看
/xl/charts/chart*.xml
。您会发现类似这样的附加 XML:

<c:chartSpace>
...
 <c:spPr>
  <a:solidFill>
   <a:srgbClr val="EB8531"/>
  </a:solidFill>
...
</c:chartSpace>

使用 Apache POI 代码:

...
XSSFChart chart ...
...
// chartspace having colored fill
if (chart.getCTChartSpace().getSpPr() == null) chart.getCTChartSpace().addNewSpPr();
if (chart.getCTChartSpace().getSpPr().isSetSolidFill()) chart.getCTChartSpace().getSpPr().unsetSolidFill();
chart.getCTChartSpace().getSpPr().addNewSolidFill().addNewSrgbClr().setVal(new byte[]{(byte)255, (byte)255, 0});
...

此外,您还需要取消设置图表空间的圆角,这是自 Excel 2017 以来的默认设置。

...
// no rounded corners chartspace
if (chart.getCTChartSpace().getRoundedCorners() == null) chart.getCTChartSpace().addNewRoundedCorners();
chart.getCTChartSpace().getRoundedCorners().setVal(false);
...
© www.soinside.com 2019 - 2024. All rights reserved.