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.

Tue Jan 15, 2019 11:57 am

Please tell me how to format(color,size...) the text in ChartDataLabel. Is it possible at all?

RostykS
 
Posts: 18
Joined: Wed Dec 26, 2018 12:57 pm

Wed Jan 16, 2019 7:18 am

Hi,

Thanks for your inquiry.
Please refer to following code:
Code: Select all
             foreach (ChartSeriesDataFormat series in chart.Series)
             {
                 //set the font
                 series.DataLabels.TextProperties.Paragraphs[0].DefaultCharacterProperties.LatinFont = new TextFont("Arial");
                 //set the font size
                 series.DataLabels.TextProperties.Paragraphs[0].DefaultCharacterProperties.FontHeight = 18;
                 //set the font color
                 series.DataLabels.TextProperties.Paragraphs[0].DefaultCharacterProperties.Fill.FillType = FillFormatType.Solid;
                 series.DataLabels.TextProperties.Paragraphs[0].DefaultCharacterProperties.Fill.SolidColor.Color = Color.Red;
             }


Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Wed Jan 16, 2019 8:41 am

I created a presentation
1.png

Then I run your code
1.png

But Datalabels has not changed!

RostykS
 
Posts: 18
Joined: Wed Dec 26, 2018 12:57 pm

Wed Jan 16, 2019 9:16 am

Hi,

Thanks for your prompt response.
Could you please provide your input PPT file for further investigation?

Many thanks,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Wed Jan 16, 2019 3:33 pm

My code and pptx

RostykS
 
Posts: 18
Joined: Wed Dec 26, 2018 12:57 pm

Thu Jan 17, 2019 3:22 am

Hi,

Thanks for your file and code.
Kindly note you need to save to a new file after changing. Besides, I found the ChartDataLable "2.7" in your file has already been formatted, so I change my previous code to below code:
Code: Select all
             Presentation presentation = new Presentation();
             presentation.LoadFromFile(@"F:\Test16172.pptx", FileFormat.Pptx2013);
             IChart chart = presentation.Slides[0].Shapes[0] as IChart;
             foreach (ChartSeriesDataFormat series in chart.Series)
             {
                 //set the format for the label "0.8" and "3.2"
                 series.DataLabels.TextProperties.Paragraphs[0].DefaultCharacterProperties.FontHeight = 18;
                 series.DataLabels.TextProperties.Paragraphs[0].DefaultCharacterProperties.LatinFont = new TextFont("Arial");
                 series.DataLabels.TextProperties.Paragraphs[0].DefaultCharacterProperties.Fill.FillType = FillFormatType.Solid;
                 series.DataLabels.TextProperties.Paragraphs[0].DefaultCharacterProperties.Fill.SolidColor.Color = Color.Red;
               
                 //get and set the format for the lable "2.7"
                 ChartDataLabelCollection labels = series.DataLabels;
                 foreach (ChartDataLabel label in labels)
                 {
                     label.TextProperties.Paragraphs[0].DefaultCharacterProperties.FontHeight = 18;
                     label.TextProperties.Paragraphs[0].DefaultCharacterProperties.LatinFont = new TextFont("Arial");
                     label.TextProperties.Paragraphs[0].DefaultCharacterProperties.Fill.FillType = FillFormatType.Solid;
                     label.TextProperties.Paragraphs[0].DefaultCharacterProperties.Fill.SolidColor.Color = Color.Red;
                 }
             }
            //save the file
            presentation.SaveToFile("DatalableFont16172.pptx",FileFormat.Pptx2013);

Attached is my result file for your kind reference. If there is any question, welcome to get it back to us.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Thu Jan 17, 2019 11:57 am

Okay.But I create a presentation,I add a dataLabel and the format this label ...I do not have some changes(((

RostykS
 
Posts: 18
Joined: Wed Dec 26, 2018 12:57 pm

Fri Jan 18, 2019 5:45 am

Hi,

Thanks for your detailed information.
Kindly note if you add the label in cd1.TextFrame.Text via code, please simply set the format in TextFrame when changing the settings.
Code: Select all
            Presentation presentation = new Presentation();
            RectangleF rect = new RectangleF(presentation.SlideSize.Size.Width / 2 - 200, 100, 400, 400);
            IChart chart = presentation.Slides[0].Shapes.AppendChart(Spire.Presentation.Charts.ChartType.ScatterStraightLinesAndMarkers, rect);
            chart.ChartTitle.TextProperties.Text = "Report";
            chart.ChartTitle.TextProperties.IsCentered = true;
            chart.ChartTitle.Height = 30;
            chart.HasTitle = true;
            var sers = chart.Series;
            ChartDataLabel cd1 = sers[0].DataLabels.Add();
            cd1.PercentageVisible = true;
            cd1.TextFrame.Text = "Label";
            //change the format
            cd1.TextFrame.Paragraphs[0].DefaultCharacterProperties.FontHeight = 18;
            cd1.TextFrame.Paragraphs[0].DefaultCharacterProperties.LatinFont = new TextFont("Arial");
            cd1.TextFrame.Paragraphs[0].DefaultCharacterProperties.Fill.FillType = FillFormatType.Solid;
            cd1.TextFrame.Paragraphs[0].DefaultCharacterProperties.Fill.SolidColor.Color = Color.Red;
            cd1.Position = ChartDataLabelPosition.Left;           
            presentation.SaveToFile("16172Label.pptx", FileFormat.Pptx2013);

Any question, welcome to get it back to us.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Fri Jan 25, 2019 8:41 am

Hi,

Greetings from E-iceblue.
Did the code I provided help you solve your issue?
Could you please give us some feedback at your convenience?

Thanks,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Return to Spire.Presentation