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.

Fri Jan 17, 2020 12:54 am

Hello, I like the example you provide in the how-to at the link below.

https://www.e-iceblue.com/Tutorials/Spi ... B.NET.html

What I'm trying to figure out is how to format the data labels for font size. I've looked through some of the stuff here and nothing I tried changes the size of the label text. Can I get a hand please?
Dan Brewerton, MIS
Cybersecurity Specialist
.NET Programmer
User avatar

dbrewerton
 
Posts: 12
Joined: Thu Nov 14, 2019 4:07 pm
Location: Honeoye, NY, USA

Fri Jan 17, 2020 7:53 am

Hi,

Thanks for your inquiry.
Please refer to the code below to set the font size for data labels in PowerPoint file.
Code: Select all
            Presentation ppt = new Presentation();
            ppt.LoadFromFile("Test.pptx");

            //Get the first chart
            IChart chart = ppt.Slides[0].Shapes[0] as IChart;

            //Get the data label of the first chart series.
            ChartDataLabelCollection dataLabels = chart.Series[0].DataLabels;

            //Set the font size for data label.
            dataLabels.TextProperties.Paragraphs[0].DefaultCharacterProperties.FontHeight = 20;

            //Save to file
            ppt.SaveToFile("Result.pptx", Spire.Presentation.FileFormat.Pptx2013);

If there is any question, please offer us the following information for further investigation.
1. Your input PowerPoint file.
2. Your desired result file.

You could upload them here or send us([email protected]) via email.

Best wishes,
Amber
E-iceblue support team
User avatar

Amber.Gu
 
Posts: 525
Joined: Tue Jun 04, 2019 3:16 am

Mon Jan 20, 2020 2:11 pm

Hello, I tried modifying my c# code as you suggested; however, the code change does not impact the labels of the data on the chart. Here is my code with your suggestions:

Code: Select all
     
                RectangleF rec = new RectangleF(25, 40, 350, 215);
                IChart chart1 = ppt.Slides[0].Shapes.AppendChart(Spire.Presentation.Charts.ChartType.BarClustered, rec);
                chart1.ChartTitle.TextProperties.Text = V2.ToString();
                chart1.ChartTitle.TextProperties.IsCentered = false;
                chart1.ChartTitle.Height = 30;
                chart1.HasLegend = false;
                chart1.HasTitle = true;

                ChartDataLabelCollection dataLabels = chart1.Series[0].DataLabels;

                dataLabels.TextProperties.Paragraphs[0].DefaultCharacterProperties.FontHeight = 10;
                dataLabels.TextProperties.Paragraphs[0].DefaultCharacterProperties.LatinFont = new TextFont("Times New Roman");
                chart1.PrimaryValueAxis.TextProperties.Paragraphs[0].DefaultCharacterProperties.FontHeight = 10;
                chart1.PrimaryValueAxis.TextProperties.Paragraphs[0].DefaultCharacterProperties.LatinFont = new TextFont("Arial");
                chart1.PrimaryCategoryAxis.TextProperties.Paragraphs[0].DefaultCharacterProperties.FontHeight = 10;
                chart1.PrimaryCategoryAxis.TextProperties.Paragraphs[0].DefaultCharacterProperties.LatinFont = new TextFont("Arial");


The X and Y axis font value is correct, however the text identifying the values on the chart is unaffected.
Dan Brewerton, MIS
Cybersecurity Specialist
.NET Programmer
User avatar

dbrewerton
 
Posts: 12
Joined: Thu Nov 14, 2019 4:07 pm
Location: Honeoye, NY, USA

Tue Jan 21, 2020 2:36 am

Hi,

Thanks for your information.
After checking your code, I found you created a chart and there was no data label added, so the collection dataLabels is null and the setting doesn't work. Please add data label firstly, then set the format. Sample code for your kind reference:
Code: Select all
           for (int i = 0; i < chart1.Series.Count; i++)
           {
               ChartSeriesDataFormat cs = chart1.Series[i];
               for (int j = 0; j < 4; j++)
               {
                //add datalabel
                ChartDataLabel dl =  chart1.Series[i].DataLabels.Add();
                dl.LabelValueVisible = true;
                //set font and size
                dl.TextProperties.Paragraphs[0].DefaultCharacterProperties.FontHeight = 10;
                dl.TextProperties.Paragraphs[0].DefaultCharacterProperties.LatinFont = new TextFont("Times New Roman");
               }
           }


Sincerely,
Betsy
E-iceblue support team
User avatar

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

Tue Jan 21, 2020 3:34 pm

I figured out where the problem was occurring. In the helper class for chart data, I have to set the text properties there.

Code: Select all
        private static void InitChartData(IChart chart1, DataTable dt1)
        {
            //set series name
            ChartSeriesFormatCollection ctf = chart1.Series;
            for (int c = 0; c < dt1.Columns.Count; c++)
            {
                chart1.ChartData[0, c].Text = dt1.Columns[c].Caption;
                if (c > 0)
                {
                    ctf.Append(chart1.ChartData[0, c]);
                }
            }
            ChartCategoryCollection catg = chart1.Categories;
            for (int r = 0; r < dt1.Rows.Count; r++)
            {
                //set category
                catg.Append(chart1.ChartData[r + 1, 0]);

                //set chartData value
                object[] data = dt1.Rows[r].ItemArray;

                for (int c = 0; c < data.Length; c++)
                {
                    chart1.ChartData[r + 1, c].Value = data[c];
                }
                //set series value
                for (int i = 0; i < ctf.Count; i++)
                {
                    chart1.Series[i].Values.Add(chart1.ChartData[r + 1, i + 1]);
                }
            }
            for (int i = 0; i < chart1.Series.Count; i++)
            {
                ChartSeriesDataFormat series = chart1.Series[i];
                for (int j = 0; j < series.Values.Count; j++)
                {
                    ChartDataLabel label = series.DataLabels.Add();                   
                    label.ID = j;
                    label.LabelValueVisible = true;
                    label.TextProperties.Paragraphs[0].DefaultCharacterProperties.FontHeight = 10;
                    label.TextProperties.Paragraphs[0].DefaultCharacterProperties.LatinFont = new TextFont("Arial");
                }
            }
        }

Dan Brewerton, MIS
Cybersecurity Specialist
.NET Programmer
User avatar

dbrewerton
 
Posts: 12
Joined: Thu Nov 14, 2019 4:07 pm
Location: Honeoye, NY, USA

Wed Jan 22, 2020 1:17 am

Hi,

Glad to hear that your issue is solved.
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

Return to Spire.Presentation