Spire.Presentation is a professional PowerPoint® compatible library that enables developers to create, read, write, modify, convert and Print PowerPoint documents. Get free and professional technical support for Spire.Presentation for .NET, Java, Android, C++, Python.

Mon Nov 02, 2020 2:00 pm

Hi Team,

As part of our requirement, we are supposed to change the color of the data labels in stacked column chart in ppt. Could anyone help me with that?
Goal:
goal.jpg

the text here I changed manually using MS powerpoint to make it white. I need to do that from code using spire.

sruthibalachandran
 
Posts: 4
Joined: Mon Nov 02, 2020 1:53 pm

Tue Nov 03, 2020 7:07 am

Hello,

Thanks for your feedback.
Please refer to the following code to change the color of the data labels in stacked column chart. If there are any questions, please provide your input file for further investigation.
Code: Select all
            Presentation ppt = new Presentation();
            ppt.LoadFromFile(@"sample.pptx");
            IChart chart = (IChart)ppt.Slides[0].Shapes[0];

            foreach (ChartSeriesDataFormat chartSeries in chart.Series) {
                chartSeries.DataLabels.TextProperties.Paragraphs[0].DefaultCharacterProperties.Fill.SolidColor.KnownColor = KnownColors.White;
            }
            ppt.SaveToFile("Labels_result.pptx", FileFormat.Pptx2010);


Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Wed Nov 04, 2020 7:21 am

Hi Brian,

Thank you for your response.Really appreciate your quick turnaround. I tried the code you posted but unfortunately it did not work for me.

However, I found the solution for the issue.
Here is the code for formatting datalabels -
Code: Select all
               for (Int32 c = 0; c < cols.Count(); ++c)
                {
                    chart.Series[c].Values = chart.ChartData[1, c + 1, columnlabels.Count(), c + 1];
                    chart.Series[c].Fill.FillType = FillFormatType.Solid;
                    chart.Series[c].InvertIfNegative = false;

                    for (Int32 r = 0; r < rows.Count(); ++r)
                    {
                        var label = chart.Series[c].DataLabels.Add();
                        label.LabelValueVisible = true;
                        label.TextProperties.Paragraphs[0].DefaultCharacterProperties.FontHeight = 12;
                        label.TextProperties.Paragraphs[0].DefaultCharacterProperties.LatinFont = new TextFont("DM Sans");
                        label.Position = ChartDataLabelPosition.Center;
                        chart.Series[c].DataLabels[r].HasDataSource = false;
                        chart.Series[c].DataLabels[r].NumberFormat = "##.##";
                        //change to white color
                        label.TextProperties.Paragraphs[0].DefaultCharacterProperties.Fill.FillType = FillFormatType.Solid;
                        label.TextProperties.Paragraphs[0].DefaultCharacterProperties.Fill.SolidColor.Color = Color.White;
                    }
                }


This part of code did the trick for me -

label.TextProperties.Paragraphs[0].DefaultCharacterProperties.Fill.FillType = FillFormatType.Solid;
label.TextProperties.Paragraphs[0].DefaultCharacterProperties.Fill.SolidColor.Color = Color.White;

sruthibalachandran
 
Posts: 4
Joined: Mon Nov 02, 2020 1:53 pm

Wed Nov 04, 2020 9:53 am

Hello,

OK, thanks for your feedback.
I'm glad to hear that you finally found the solution. If you encounter any issues related to our products in the future, please feel free to contact us.
Wish you all the best!

Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Return to Spire.Presentation

cron