javaspring-bootchartsapache-poi

How to change the background color of Anchore


I am implementing an Apache POI chart which creates three Chart. The background color of the chart is WHITE. But i would like to change it to custom RGB color.

enter image description here

In the above image I want to change color of the whole red marking area which are XSSFClientAnchore to custom color.

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

Solution

  • Seems you want to color the chart space. This has nothing to do with the anchor but is a setting in the chart.

    How to see? Create the chart as you do it now. After that unzip the *.xlsx file created. Have a look into /xl/charts/chart*.xml. Remember the XML. Then open the file in Excel GUI and do what you want. Save the changes. After that unzip the *.xlsx file again and have a look into /xl/charts/chart*.xml. You will find additional XML like this:

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

    Using Apache POI code that would be:

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

    Additional you will need to unset rounded corners for chart space, which is the default since Excel 2017.

    ...
    // no rounded corners chartspace
    if (chart.getCTChartSpace().getRoundedCorners() == null) chart.getCTChartSpace().addNewRoundedCorners();
    chart.getCTChartSpace().getRoundedCorners().setVal(false);
    ...