How to Set the Percent Format as "0.0%" for a Pie Chart in Java?

Sharing a expected file here. Need help to set the given format for datalabels in pie chart.

testPieChartAspose.zip (48.0 KB)

@VaradS

To set the data labels in a pie chart to display percentages in the format “0.0%”, you can use the following Java code snippet. This example demonstrates how to create a pie chart and format the data labels accordingly:

import com.aspose.slides.*;
import java.awt.*;

public class SetDataLabelsPercentageSign {
    public static void main(String[] args) {
        // Create an instance of the Presentation class
        Presentation presentation = new Presentation();
        
        // Get reference to the first slide
        ISlide slide = presentation.getSlides().get_Item(0);
        
        // Add a pie chart to the slide
        IChart chart = slide.getShapes().addChart(ChartType.Pie, 20, 20, 500, 400);
        
        // Clear existing series
        chart.getChartData().getSeries().clear();
        int defaultWorksheetIndex = 0;
        
        // Getting the chart data worksheet
        IChartDataWorkbook workbook = chart.getChartData().getChartDataWorkbook();
        
        // Add a new series
        IChartSeries series = chart.getChartData().getSeries().add(workbook.getCell(defaultWorksheetIndex, 0, 1, "Series 1"), chart.getType());
        series.getDataPoints().addDataPointForPieSeries(workbook.getCell(defaultWorksheetIndex, 1, 1, 0.30));
        series.getDataPoints().addDataPointForPieSeries(workbook.getCell(defaultWorksheetIndex, 2, 1, 0.50));
        series.getDataPoints().addDataPointForPieSeries(workbook.getCell(defaultWorksheetIndex, 3, 1, 0.20));
        
        // Setting LabelFormat properties
        series.getLabels().getDefaultDataLabelFormat().setShowValue(true);
        series.getLabels().getDefaultDataLabelFormat().setNumberFormatLinkedToSource(false);
        series.getLabels().getDefaultDataLabelFormat().setNumberFormat("0.0%"); // Set the desired format
        
        // Save the presentation
        presentation.save("SetDataLabelsPercentageSign_out.pptx", SaveFormat.Pptx);
    }
}

In this code:

  • A pie chart is created on the first slide of the presentation.
  • The data points are added to the series.
  • The data label format is set to “0.0%” using the setNumberFormat method.

Make sure to adjust the file paths and other parameters as needed for your specific use case. This should help you achieve the desired formatting for your pie chart data labels.

testPiePercentage.zip (47.5 KB)

I tried to set it as suggested, but it is not reflecting in the slide. The changes are visible in the format panel on the right, but they are not reflected in the chart.

@VaradS,
Could you please clarify whether you’re trying to create the exact same chart from scratch or simply change the number format of its data labels? Could you please also tell us how that chart was created?

Thanks, I was able to achieve it @andrey.potapov .

Also if for specific numbers like less than 1, I need to show value up to 3 decimal places. How can I handle such scenarios. I’m sharing a sample file here. For all percentage number less than 1 I need to show value up to 3 decimal places and for rest of the number up to 1 decimal place.
testPiePercentagelessthanzero.zip (48.3 KB)

@VaradS,
Thank you for posting your requirements. Please try using the following code snippet:

IChartDataPoint dataPoint = series.getDataPoints().get_Item(pointIndex);
double pointValue = dataPoint.getValue().toDouble();

if(pointValue < 1) {
    IDataLabel dataLabel = series.getLabels().get_Item(pointIndex);
    dataLabel.getDataLabelFormat().setNumberFormat("0.000%");
}

Create or Update PowerPoint Presentation Charts in Java|Aspose.Slides Documentation
Chart Data Label|Aspose.Slides Documentation

@andrey.potapov ,
Thank you for your response.

I’m attaching a sample code snippet for your reference. In our case, we are directly providing numeric values like 3, 150, 230, etc. Based on these values, MS PowerPoint automatically calculates the percentages when we set:

lbl.getDataLabelFormat().setShowPercentage(true);

import com.aspose.slides.*;
import com.aspose.slides.ChartType;

import java.awt.*;


public class PieChart {
    public static void main(String[] args) {
        //ExStart:PieChart
        // The path to the documents directory.

        // Instantiate Presentation class that represents PPTX file
        Presentation presentation = new Presentation();

        // Access first slide
        ISlide slides = presentation.getSlides().get_Item(0);

        // Add chart with default data
        IChart chart = slides.getShapes().addChart(ChartType.Pie, 100, 100, 400, 400);

        // Setting chart Title
        chart.getChartTitle().addTextFrameForOverriding("Sample Title");
        chart.getChartTitle().getTextFrameForOverriding().getTextFrameFormat().setCenterText(NullableBool.True);
        chart.getChartTitle().setHeight(20);
        chart.setTitle(true);

        // Set first series to Show Values
        chart.getChartData().getSeries().get_Item(0).getLabels().getDefaultDataLabelFormat().setShowValue(true);

        // Setting the index of chart data sheet
        int defaultWorksheetIndex = 0;

        // Getting the chart data worksheet
        IChartDataWorkbook fact = chart.getChartData().getChartDataWorkbook();

        // Delete default generated series and categories
        chart.getChartData().getSeries().clear();
        chart.getChartData().getCategories().clear();

        // Adding new categories
        chart.getChartData().getCategories().add(fact.getCell(0, 1, 0, "First Qtr"));
        chart.getChartData().getCategories().add(fact.getCell(0, 2, 0, "2nd Qtr"));
        chart.getChartData().getCategories().add(fact.getCell(0, 3, 0, "3rd Qtr"));

        // Adding new series
        IChartSeries series = chart.getChartData().getSeries().add(fact.getCell(0, 0, 1, "Series 1"), chart.getType());

        // Now populating series data
        series.getDataPoints().addDataPointForPieSeries(fact.getCell(defaultWorksheetIndex, 1, 1, 3));
        series.getDataPoints().addDataPointForPieSeries(fact.getCell(defaultWorksheetIndex, 2, 1, 150));
        series.getDataPoints().addDataPointForPieSeries(fact.getCell(defaultWorksheetIndex, 3, 1, 230));

        // Not working in new version
        // Adding new points and setting sector color
        // series.IsColorVaried = true;
        chart.getChartData().getSeriesGroups().get_Item(0).setColorVaried(true);

        IChartDataPoint point = series.getDataPoints().get_Item(0);
        double pointValue = point.getValue().toDouble();
        if(pointValue < 1) {
            IDataLabel dataLabel = series.getLabels().get_Item(0);
            dataLabel.getDataLabelFormat().setNumberFormat("0.000%");
        }
        point.getFormat().getFill().setFillType(FillType.Solid);
        point.getFormat().getFill().getSolidFillColor().setColor(new Color(PresetColor.Cyan));
        // Setting Sector border


        IChartDataPoint point1 = series.getDataPoints().get_Item(1);
        point1.getFormat().getFill().setFillType(FillType.Solid);
        point1.getFormat().getFill().getSolidFillColor().setColor(new Color(PresetColor.Brown));

        // Setting Sector border
//        point1.getFormat().getLine().getFillFormat().setFillType(FillType.Solid);
//        point1.getFormat().getLine().getFillFormat().getSolidFillColor().setColor(Color.BLUE);
//        point1.getFormat().getLine().setWidth(3.0);
//        point1.getFormat().getLine().setStyle(LineStyle.Single);
//        point1.getFormat().getLine().setDashStyle(LineDashStyle.LargeDashDot);

        IChartDataPoint point2 = series.getDataPoints().get_Item(2);
        point2.getFormat().getFill().setFillType(FillType.Solid);
        point2.getFormat().getFill().getSolidFillColor().setColor(new Color(PresetColor.Coral));

        // Setting Sector border
//        point2.getFormat().getLine().getFillFormat().setFillType(FillType.Solid);
//        point2.getFormat().getLine().getFillFormat().getSolidFillColor().setColor(Color.RED);
//        point2.getFormat().getLine().setWidth(2.0);
//        point2.getFormat().getLine().setStyle(LineStyle.ThinThin);
//        point2.getFormat().getLine().setDashStyle(LineDashStyle.LargeDashDotDot);

        // Create custom labels for each of categories for new series
        IDataLabel lbl1 = series.getDataPoints().get_Item(0).getLabel();

        // lbl.setShowCategoryName(true);
        lbl1.getDataLabelFormat().setShowValue(false);
        lbl1.getDataLabelFormat().setShowPercentage(true);
        //lbl1.getDataLabelFormat().setNumberFormat("0.0%");

        IDataLabel lbl2 = series.getDataPoints().get_Item(1).getLabel();
        lbl2.getDataLabelFormat().setShowValue(false);
        lbl2.getDataLabelFormat().setShowPercentage(true);
        //lbl2.getDataLabelFormat().setNumberFormat("0.0%");

        IDataLabel lbl3 = series.getDataPoints().get_Item(2).getLabel();
        lbl3.getDataLabelFormat().setShowValue(false);
        lbl3.getDataLabelFormat().setShowPercentage(true);
        //lbl3.getDataLabelFormat().setNumberFormat("0.0%");

        // Showing Leader Lines for Chart
        series.getLabels().getDefaultDataLabelFormat().setShowLeaderLines(true);
        series.getLabels().getDefaultDataLabelFormat().setNumberFormatLinkedToSource(false);
        series.getLabels().getDefaultDataLabelFormat().setNumberFormat("0.0%");

        // Setting Rotation Angle for Pie Chart Sectors
        chart.getChartData().getSeriesGroups().get_Item(0).setFirstSliceAngle(180);

        // Save presentation with chart
        presentation.save( "PieChart_outDecimal.pptx", SaveFormat.Pptx);
    }
}

However, in the code snippet you shared, it appears the logic checks the actual data point value to determine if it is less than 1.
Instead, our requirement is to check the percentage value (calculated by MS PowerPoint) — and if that percentage is less than 1%, then apply the format "0.000%".

Could you please confirm if this approach is feasible? If so, could you also share a code snippet demonstrating this?
Please note that we are not calculating the percentage values manually before setting the number format — it is handled by MS PowerPoint itself.

Thanks again for your support, and looking forward to your guidance.

@VaradS,
Thank you for posting the details for your requirements. Unfortunately, it cannot be done automatically, but you can try using the IDataLabel.getActualLabelText method to check the actual percentage value calculated by PowerPoint.

IDataLabel dataLabel = series.getLabels().get_Item(pointIndex);
String labelText = dataLabel.getActualLabelText();

I hope this will help you.

thanks for the response @andrey.potapov. This is helpful.

@VaradS,
We are glad that the issue has been resolved on your end. Thank you for using Aspose.Slides.

@andrey.potapov ,
One more question, Is there any other optimized way to set such conditional formats?

like in Aspose Cells implementations we have achieved this by

chartSeries.getDataLabels().setNumberFormat("[>0.01]0.0%;0.000%");

@VaradS,
I need some time to answer your question. I will get back to you as soon as possible.

@VaradS,
Thank you for your patience. Yes, PowerPoint supports the same conditional formats as Excel, so you can use them in Aspose.Slides as well.