We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Doughnut Chart Style Is Lost after Saving to PPTX

Hi,

I have a pptx containing a doughnut chart, styled as I want.
I open the pptx with Aspose Slides, update the data in the doughnut chart workbook and then save the pptx.

The saved doughnut chart loses all the style that I’ve previously set, I mean it loses sector colors, font colors, label settings, and others.

Is there a way to keep the doughnut chart style?

Thank you.

@kylanee,
Thank you for your query. To investigate this case, please share and specify the following:

  • The input PPTX file
  • The code example reproducing the problem
  • The output presentation file
  • The version of Aspose.Slides you used

But first, please check the issue with the latest version of Aspose.Slides.

Hi @Andrey_Potapov!

Thank you,
I’m using the 21.4 version (the last one).

Here is the code to reproduce the fact:

@Test
void doughnutChartTest() throws Exception {

    Presentation pt = new Presentation(getClass().getResource("/templates/doughnut-chart-test.pptx").getPath());
    ISlide slide = pt.getSlides().get_Item(0);

    for (IShape shape : slide.getShapes()) {

        if (shape instanceof IChart) {

            IChart ptChart = (IChart) shape;
            IChartDataWorkbook chartData = ptChart.getChartData().getChartDataWorkbook();

            if (chartData.getCell(0, 0, 0).getValue().toString().equals("{{CHART_TEST}}")) {

                // Clear chart sample data:

                chartData.clear(0);
                ptChart.getChartData().getSeries().clear();
                ptChart.getChartData().getCategories().clear();

                // Fill chart with new data:

                ptChart.getChartData().getSeries().add(chartData.getCell(0, 0, 1, "Number of"), ptChart.getType());

                IChartSeries series = ptChart.getChartData().getSeries().get_Item(0);
                series.getDataPoints().addDataPointForDoughnutSeries(chartData.getCell(0, 1, 1, 5));
                series.getDataPoints().addDataPointForDoughnutSeries(chartData.getCell(0, 2, 1, 10));
                series.getDataPoints().addDataPointForDoughnutSeries(chartData.getCell(0, 3, 1, 1));

                ptChart.getChartData().getCategories().add(chartData.getCell(0, 1, 0, "Apple"));
                ptChart.getChartData().getCategories().add(chartData.getCell(0, 2, 0, "Pineapple"));
                ptChart.getChartData().getCategories().add(chartData.getCell(0, 3, 0, "Pineapplepen"));
            }
        }
    }

    pt.save("doughnut-chart-test-result.pptx", SaveFormat.Pptx);
} 

Here are the input and the output file:
doughnut-chart-test.zip (70.7 KB)

The resulting chart has lost every style settings, as colors, labels and so on.

I know that I can code the style in the following way:

ptChart.getChartData().getSeriesGroups().get_Item(0).setDoughnutHoleSize((byte) 60);
ptChart.getChartData().getSeries().get_Item(0).getLabels().getDefaultDataLabelFormat().setShowValue(false);
ptChart.getChartData().getSeries().get_Item(0).getLabels().getDefaultDataLabelFormat().setShowPercentage(true);
ptChart.getChartData().getSeries().get_Item(0).getLabels().getDefaultDataLabelFormat().setShowCategoryName(true); 
ptChart.getChartData().getSeries().get_Item(0).getLabels().getDefaultDataLabelFormat().setShowLeaderLines(true); 
ptChart.getChartData().getSeries().get_Item(0).getParentSeriesGroup().setColorVaried(true);

but I could have different charts with different styles in my presentation, so is there a way to keep the original style settings without writing them in the code?

@kylanee,
Thank you for the additional information. I reproduced the problem and logged the issue in our tracking system with ID SLIDESJAVA-38533. Our development team will investigate this case. You will be notified when it is fixed.

1 Like

@kylanee,
Our development team investigated the issue. The line below removes a series, its data, and format settings:

chart.getChartData().getSeries().clear();

To keep the chart style as the original one, you should clear data points instead:

if (chartData.getCell(0, 0, 0).getValue().toString().equals("{{CHART_TEST}}"))
{
    chartData.clear(0);

    IChartSeries series = chart.getChartData().getSeries().get_Item(0);
    series.getDataPoints().clear(); //clear data points but preserve the chart.ChartData.Series[0] object.
    chart.getChartData().getCategories().clear();

    series.getName().setData(chartData.getCell(0, 0, 1, "Number of")); //set new name

    //add new data points
    series.getDataPoints().addDataPointForDoughnutSeries(chartData.getCell(0, 1, 1, 5));
    series.getDataPoints().addDataPointForDoughnutSeries(chartData.getCell(0, 2, 1, 10));
    series.getDataPoints().addDataPointForDoughnutSeries(chartData.getCell(0, 3, 1, 1));

    //add new categories
    chart.getChartData().getCategories().add(chartData.getCell(0, 1, 0, "Apple"));
    chart.getChartData().getCategories().add(chartData.getCell(0, 2, 0, "Pineapple"));
    chart.getChartData().getCategories().add(chartData.getCell(0, 3, 0, "Pineapplepen"));
}

More examples: Chart Formatting
API Reference: IChartSeries interface, IChartDataPointCollection interface

Hi @Andrey_Potapov, thank you very much, problem solved! :slight_smile: