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 Nov 09, 2021 8:17 pm

Hello,

I want to change chart data as I mention in attachment?
Series has Values list and each value is CellRange and CellRange has Text property. Can I set Text property like attachment?

ChartCapture.PNG

numankizil
 
Posts: 26
Joined: Tue Nov 09, 2021 8:00 pm

Wed Nov 10, 2021 1:50 am

Hello Numan,

Thank you for your inquiry.
Do you want to show data labels on chart? If so, please try the following code.
Code: Select all
  //Get chart on the first slide
            IChart Chart = ppt.Slides[0].Shapes[0] as IChart;
            //Show data labels
            Chart.Series[0].DataLabels.LabelValueVisible = true;


I apologize if I misunderstood your requirement. In order to help me better understand your needs, please share your source ppt file and expected ppt file. Then I will do an investigation and work out a demo for you.

Sincerely,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2774
Joined: Wed Jun 27, 2012 8:50 am

Wed Nov 10, 2021 6:05 pm

Hello,

Yes I want to show datalabels but my problem is not showing it . The numbers where datalabels shown is actually data and when I debug DataLabels has no count.But where the numbers shown are in
Code: Select all
chart.Series[x].Values
not in
Code: Select all
chart.Series[x].DataLabels
.

I also set data like
Code: Select all
chart.ChartData[x, 0].Value="2.9"

numankizil
 
Posts: 26
Joined: Tue Nov 09, 2021 8:00 pm

Thu Nov 11, 2021 3:27 am

Dear Numan,

Thank you for your reply.
By default, chart.Series[x].DataLabels collection is empty. Please use chart.Series[x].DataLabels.Add() like the following code to add the data lables.
Code: Select all
  for (int i = 0; i < chart.Categories.Count; i++)
            {
                chart.Series[0].DataLabels.Add();
            }


In your expected template(expected_chart.pptx), the data lables's values are not derived from the chart's data source, but instead are customized.You can use chart.Series[0].DataLabels[0].TextFrame.Text = "123" to set the custom value.

Feel free to contact us if any questions.

Sincerely,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2774
Joined: Wed Jun 27, 2012 8:50 am

Thu Nov 11, 2021 7:07 pm

Dear amy.zhao,

thank you for your support, I can set text to datalabels and also how can I set more than one text to a datalabel ? As below insert text to below,above, left and right.

ChartCapture.PNG

numankizil
 
Posts: 26
Joined: Tue Nov 09, 2021 8:00 pm

Fri Nov 12, 2021 7:26 am

Dear Numan,

Thanks for your further inquiry.
Please refer to the following code to insert text to below,above, left and right regions of a data label.
Code: Select all
 IChart chart = presentation.Slides[0].Shapes[0] as IChart;

            for (int i = 0; i < chart.Categories.Count; i++)
            {
                chart.Series[0].DataLabels.Add();
            }
            chart.Series[0].DataLabels.LabelValueVisible = true;
            float fontHeight = 10;
            TextFont latinFont = new TextFont("Calibri");
            FillFormatType fillType = Spire.Presentation.Drawing.FillFormatType.Solid;
            Color color = System.Drawing.Color.Black;


            ChartDataLabelCollection ss = chart.Series[0].DataLabels;
            ITextFrameProperties textFrame = chart.Series[0].DataLabels[0].TextFrame;
            TextParagraph paragraph = textFrame.Paragraphs[0];

            TextRange topTextRange = new TextRange("Top");
            topTextRange.LatinFont = latinFont;
            topTextRange.FontHeight = fontHeight;
            topTextRange.Fill.FillType = fillType;
            topTextRange.Fill.SolidColor.Color = color;
            paragraph.TextRanges.Append(topTextRange);

            TextParagraph newPara1= new TextParagraph();
            TextRange leftTextRange = new TextRange("left");
            leftTextRange.LatinFont = latinFont;
            leftTextRange.FontHeight = fontHeight;
            leftTextRange.Fill.FillType = fillType;
            leftTextRange.Fill.SolidColor.Color = color;
            newPara1.TextRanges.Insert(0, leftTextRange);

            TextRange middleTextRange = new TextRange(" ↑ ");
            middleTextRange.LatinFont = latinFont;
            middleTextRange.FontHeight = fontHeight;
            middleTextRange.Fill.FillType = fillType;
            middleTextRange.Fill.SolidColor.Color = Color.Blue;
            newPara1.TextRanges.Append(middleTextRange);

            TextRange rightTextRange = new TextRange("right");
            rightTextRange.LatinFont = latinFont;
            rightTextRange.FontHeight = fontHeight;
            rightTextRange.Fill.FillType = fillType;
            rightTextRange.Fill.SolidColor.Color = color;
            newPara1.TextRanges.Append(rightTextRange);

            textFrame.Paragraphs.Append(newPara1);

            TextParagraph newPara2 = new TextParagraph();
            TextRange bottomtextRange = new TextRange("bottom");
            bottomtextRange.LatinFont = latinFont;
            bottomtextRange.FontHeight = fontHeight;
            bottomtextRange.Fill.FillType = fillType;
            bottomtextRange.Fill.SolidColor.Color = color;
            newPara2.TextRanges.Append(bottomtextRange);

            textFrame.Paragraphs.Append(newPara2);

generated effect.png


Sincerely,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2774
Joined: Wed Jun 27, 2012 8:50 am

Sat Nov 13, 2021 1:15 pm

Dear amy.zhao,

thanks, it works when I give it to 0 index series but I need to implement it to all bars in the chart.
When I try to iterate on categories and series and add dataLabels to series the result is not what I need because I add it to just series.
how can I implement it to each bar?
code:

Code: Select all
for (int x = 0; x < chart.Series.Count; x++)
            {
                for (int i = 0; i < chart.Categories.Count; i++)
                {
                    chart.Series[x].DataLabels.Add();
                    ITextFrameProperties textFrame = chart.Series[x].DataLabels[0].TextFrame;
                    TextParagraph paragraph = textFrame.Paragraphs[0];

                    TextRange topTextRange = new TextRange("Top");
                    paragraph.TextRanges.Append(topTextRange);

                    TextParagraph newPara1 = new TextParagraph();
                    TextRange leftTextRange = new TextRange("left");
                    newPara1.TextRanges.Insert(0, leftTextRange);

                    TextRange middleTextRange = new TextRange(" ↑ ");
                    newPara1.TextRanges.Append(middleTextRange);

                    TextRange rightTextRange = new TextRange("right");
                    newPara1.TextRanges.Append(rightTextRange);

                    textFrame.Paragraphs.Append(newPara1);

                    TextParagraph newPara2 = new TextParagraph();
                    TextRange bottomtextRange = new TextRange("bottom");
                    newPara2.TextRanges.Append(bottomtextRange);

                    textFrame.Paragraphs.Append(newPara2);
                }
            }



result:

each_bar.PNG

numankizil
 
Posts: 26
Joined: Tue Nov 09, 2021 8:00 pm

Mon Nov 15, 2021 3:42 am

Dear Numan,

Please refer to the following code.
Code: Select all
  static void AddMultipleTextRange()
        {
             Presentation presentation = new Presentation();
            presentation.LoadFromFile(@"source_template_chart.pptx");

            //Get chart on the first slide
            IChart chart = presentation.Slides[0].Shapes[0] as IChart;

            float fontHeight = 10;
            TextFont latinFont = new TextFont("Calibri");
            FillFormatType fillType = Spire.Presentation.Drawing.FillFormatType.Solid;
            Color color = System.Drawing.Color.Black;

            for (int s = 0; s < chart.Series.Count; s++)
            {
                chart.Series[s].DataLabels.LabelValueVisible = true;
                for (int j = 0; j < chart.Categories.Count; j++)
                {
                    ChartDataLabel dataLabel = chart.Series[s].DataLabels.Add();
                    ITextFrameProperties textFrame = dataLabel.TextFrame;
                    TextParagraph paragraph = textFrame.Paragraphs[0];

                    TextRange topTextRange = new TextRange("Top");
                    topTextRange.LatinFont = latinFont;
                    topTextRange.FontHeight = fontHeight;
                    topTextRange.Fill.FillType = fillType;
                    topTextRange.Fill.SolidColor.Color = color;
                    paragraph.TextRanges.Append(topTextRange);

                    TextParagraph newPara1 = new TextParagraph();
                    TextRange leftTextRange = new TextRange("left");
                    leftTextRange.LatinFont = latinFont;
                    leftTextRange.FontHeight = fontHeight;
                    leftTextRange.Fill.FillType = fillType;
                    leftTextRange.Fill.SolidColor.Color = color;
                    newPara1.TextRanges.Insert(0, leftTextRange);

                    TextRange middleTextRange = new TextRange(" ↑ ");
                    middleTextRange.LatinFont = latinFont;
                    middleTextRange.FontHeight = fontHeight;
                    middleTextRange.Fill.FillType = fillType;
                    middleTextRange.Fill.SolidColor.Color = Color.Blue;
                    newPara1.TextRanges.Append(middleTextRange);

                    TextRange rightTextRange = new TextRange("right");
                    rightTextRange.LatinFont = latinFont;
                    rightTextRange.FontHeight = fontHeight;
                    rightTextRange.Fill.FillType = fillType;
                    rightTextRange.Fill.SolidColor.Color = color;
                    newPara1.TextRanges.Append(rightTextRange);

                    textFrame.Paragraphs.Append(newPara1);

                    TextParagraph newPara2 = new TextParagraph();
                    TextRange bottomtextRange = new TextRange("bottom");
                    bottomtextRange.LatinFont = latinFont;
                    bottomtextRange.FontHeight = fontHeight;
                    bottomtextRange.Fill.FillType = fillType;
                    bottomtextRange.Fill.SolidColor.Color = color;
                    newPara2.TextRanges.Append(bottomtextRange);

                    textFrame.Paragraphs.Append(newPara2);
                }               
            }         
           
            String result = "source_template_chart-out.pptx";
            presentation.SaveToFile(result, Spire.Presentation.FileFormat.Pptx2013);
        }


Sincerely,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2774
Joined: Wed Jun 27, 2012 8:50 am

Mon Nov 15, 2021 8:10 pm

Dear amy.zhao,

it is working thanks for your support.

numankizil
 
Posts: 26
Joined: Tue Nov 09, 2021 8:00 pm

Tue Nov 16, 2021 1:34 am

Dear Numan,

You are welcome.
Feel free to contact us if any problems.

Sincerely,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2774
Joined: Wed Jun 27, 2012 8:50 am

Thu Dec 02, 2021 5:58 am

Dear amy.zhao,

I set new added datalebel's setting as below code to be the same with original datalabel settings and it works when I open with google docs and WPS but not working in microsoft powerpoint.
is there something wrong in that code.

Code: Select all
middleTextRange.Fill.SolidColor.Color = chart.Series[s].DataLabels.TextProperties.Paragraphs[0].DefaultCharacterProperties.Fill.SolidColor.Color;
middleTextRange.Fill.FillType= chart.Series[s].DataLabels.TextProperties.Paragraphs[0].DefaultCharacterProperties.Fill.FillType;
middleTextRange.FontHeight = chart.Series[s].DataLabels.TextProperties.Paragraphs[0].DefaultCharacterProperties.FontHeight;
middleTextRange.LatinFont = chart.Series[s].DataLabels.TextProperties.Paragraphs[0].DefaultCharacterProperties.LatinFont;



microsoft powerpoint:

micosoft_power_point.PNG



google docs:

googledocs.PNG

numankizil
 
Posts: 26
Joined: Tue Nov 09, 2021 8:00 pm

Thu Dec 02, 2021 7:03 am

Hello,

Thanks for your inquiry!

Kindly note that in the code my colleague provided before, we set the text format for the middleTextRange directly by TextRange. If you want to create a new chart and copy only the middleTextRange style to the new middleTextRange of the new chart, you should use the format of the corresponding TextRange, instead of the default properties. Please refer to the following code.

Code: Select all
                    middleTextRange.Fill.SolidColor.Color = chart.Series[s].DataLabels[j].TextFrame.Paragraphs[1].TextRanges[1].Fill.SolidColor.Color;
                    middleTextRange.Fill.FillType = chart.Series[s].DataLabels[j].TextFrame.Paragraphs[1].TextRanges[1].Fill.FillType;
                    middleTextRange.FontHeight = chart.Series[s].DataLabels[j].TextFrame.Paragraphs[1].TextRanges[1].FontHeight;
                    middleTextRange.LatinFont = chart.Series[s].DataLabels[j].TextFrame.Paragraphs[1].TextRanges[1].LatinFont;


Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Fri Dec 03, 2021 6:41 pm

Hello,

Code: Select all
chart.Series[s].DataLabels
just has one datalabel that I add with
Code: Select all
 chart.Series[s].DataLabels.Add();
as your colleague mentioned.
Because of this I can apply this :

Code: Select all
middleTextRange.Fill.SolidColor.Color = chart.Series[s].DataLabels[0].TextFrame.Paragraphs[0].TextRanges[0].Fill.SolidColor.Color;
middleTextRange.Fill.FillType = chart.Series[s].DataLabels[0].TextFrame.Paragraphs[0].TextRanges[0].Fill.FillType;
middleTextRange.FontHeight = chart.Series[s].DataLabels[0].TextFrame.Paragraphs[0].TextRanges[0].FontHeight;
middleTextRange.LatinFont = chart.Series[s].DataLabels[0].TextFrame.Paragraphs[0].TextRanges[0].LatinFont;


as expected it apply the color and font that I applied for the new datalabel.

Example: I added blue label and also middle is being blue.

chart_color.PNG

numankizil
 
Posts: 26
Joined: Tue Nov 09, 2021 8:00 pm

Mon Dec 06, 2021 6:33 am

Hello,

Thanks for your feedback!

Glad to hear that you have applied the color and font you want.

If you encounter any issues related to our product in the future, just feel free to contact us.

Have a nice day!

Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Sun Dec 12, 2021 10:59 am

Hello,

Sorry but I did not apply. I can not apply the existing settings when try your suggestion I apply the new added settings thats not I want.

numankizil
 
Posts: 26
Joined: Tue Nov 09, 2021 8:00 pm

Return to Spire.Presentation