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 May 06, 2022 12:55 pm

I want to add a Text Range to the chart DataLabel without losing connection to the data itself.

When I open the following PowerPoint file with the Spire Presentation I can reach the chart text range which contains the value as follows. Text is [VALUE] and Field is Spire.Presentation.Field.

If I edit data behind the chart using Powerpoint itself data label text range value changes accordingly to the data.

Template Fİle:
datalabel-test.zip


01.png


Source Code:

Code: Select all
using Spire.Presentation;
using System.Linq;
using Spire.Presentation.Charts;
using System;
using System.Collections.Generic;

namespace PptxTesterPaidVersion
{
    class Program
    {
        static void Main(string[] args)
        {
            DatalabelTextRange();
        }
       
        private static void DatalabelTextRange()
        {
            //Load template presentation
            Presentation templatePresentation = new Presentation();
            templatePresentation.LoadFromFile("datalabel-test.pptx");

            //Create New Presentation
            Spire.Presentation.Presentation presentation = new Spire.Presentation.Presentation();
            presentation.Slides.RemoveAt(0);

            //Get slide from template presentation. index:1
            ISlide cloneSlide = templatePresentation.Slides.ToArray().ElementAtOrDefault(0);

            //---
            //Get Chart                       
            //IChart chart = templateSlide.Shapes.ToArray().FirstOrDefault(x => x.Name == "Content Placeholder 15") as IChart;
            IChart chart = cloneSlide.Shapes.ToArray().FirstOrDefault(x => x.Name == "Chart 7") as IChart;

            //---
            for (int s = 0; s < chart.Series.Count; s++)
            {
                var serieIndex = s;
                IEnumerable<ChartDataLabel> dataLabels = chart.Series[s].DataLabels.Cast<ChartDataLabel>();

                for (int j = 0; j < chart.Categories.Count; j++)
                {
                    var categoryIndex = j;
                    ChartDataLabel dl = null;
                    bool existingDataLabelFound;

                    //Get data label
                    var existingDataLabel = dataLabels.FirstOrDefault(c => c.ID == categoryIndex);
                    existingDataLabelFound = false;

                    if (existingDataLabel != null)
                    {
                        existingDataLabelFound = true;
                        Console.WriteLine($"textframe field:{existingDataLabel.TextFrame.Paragraphs[0].TextRanges[4].Field}");
                        Console.WriteLine($"textframe text:{existingDataLabel.TextFrame.Paragraphs[0].TextRanges[4].Text}");
                    }
                }
            }

        }               
    }
}


When I do it from scratch using by Spire Presentation library text range value doesn't change according to the data behind that chart. I think this behavior is related to that field "existingDataLabel.TextFrame.Paragraphs[0].TextRanges[4].Field" but can't find a way to assign this field properly. How can I assign the data value itself to a text range points to the data behind chart ?


I want to do it at that line but it doesn't work.

Code: Select all
TextRange middleTextRange = new TextRange("[VALUE]");
paragraph.TextRanges.Append(middleTextRange);


Template File:
datalabel-test-02.zip


Source Code:

Code: Select all
using Spire.Presentation;
using System.Linq;
using Spire.Presentation.Charts;
using System;
using System.Collections.Generic;

namespace PptxTesterPaidVersion
{
    class Program
    {
        static void Main(string[] args)
        {
            DatalabelNumberFormat_2();
        }


        private static void DatalabelNumberFormat_2()
        {
            //Load template presentation
            Presentation templatePresentation = new Presentation();
            templatePresentation.LoadFromFile("datalabel-test-02.pptx");

            //Create New Presentation
            Spire.Presentation.Presentation presentation = new Spire.Presentation.Presentation();
            presentation.Slides.RemoveAt(0);

            //Get slide from template presentation. index:1
            ISlide cloneSlide = templatePresentation.Slides.ToArray().ElementAtOrDefault(0);

            //---
            //Get Chart                       
            //IChart chart = templateSlide.Shapes.ToArray().FirstOrDefault(x => x.Name == "Content Placeholder 15") as IChart;
            IChart chart = cloneSlide.Shapes.ToArray().FirstOrDefault(x => x.Name == "Chart 5") as IChart;

            //---
            for (int s = 0; s < chart.Series.Count; s++)
            {
                var serieIndex = s;
                IEnumerable<ChartDataLabel> dataLabels = chart.Series[s].DataLabels.Cast<ChartDataLabel>();

                for (int j = 0; j < chart.Categories.Count; j++)
                {
                    var categoryIndex = j;
                    ChartDataLabel dl = null;
                    bool existingDataLabelFound;
                    TextRange existingDataLabelTextRange = null;

                    //Get data label
                    var existingDataLabel = dataLabels.FirstOrDefault(c => c.ID == categoryIndex);
                    existingDataLabelFound = false;

                    if (existingDataLabel != null)
                    {
                        existingDataLabelFound = true;
                        dl = existingDataLabel;
                        existingDataLabelTextRange = dl.TextFrame.Paragraphs[0].FirstTextRange;
                    }
                    else
                    {
                        existingDataLabelFound = false;
                        dl = chart.Series[serieIndex].DataLabels.Add();
                        dl.ID = categoryIndex;
                    }


                    ITextFrameProperties textFrame = dl.TextFrame;
                    TextParagraph paragraph = textFrame.Paragraphs[0];

                    TextRange topTextRange = new TextRange("top\v");
                    paragraph.TextRanges.Append(topTextRange);

                    TextRange leftTextRange = new TextRange("left");
                    paragraph.TextRanges.Append(leftTextRange);

                    TextRange middleTextRange = new TextRange("[VALUE]");
                    paragraph.TextRanges.Append(middleTextRange);

                    TextRange rightTextRange = new TextRange("right\v");
                    paragraph.TextRanges.Append(rightTextRange);

                    TextRange bottomTextRange = new TextRange("bottom");
                    paragraph.TextRanges.Append(bottomTextRange);
                }


                presentation.Slides.Append(cloneSlide);

                //Save and launch to view the PPTX document.
                presentation.SaveToFile("DatalabelNumberFormat_2_01.pptx", Spire.Presentation.FileFormat.Pptx2010);
            }

        }
    }
}

omur.ertanis
 
Posts: 22
Joined: Mon Feb 21, 2022 6:49 am

Sat May 07, 2022 11:21 am

Hello,

Thanks for your inquiry.
Sorry that our Spire.Presentation at present doesn’t support assigning chart data value to chart data label text range. We will add a new feature into our upgrade list to achieve the screenshot effect.
effect.png

I have logged it into our tracking system with the ticket SPIREPPT-1948. Once it is achieved, I will inform you asap.

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1261
Joined: Wed Apr 25, 2018 3:20 am

Mon May 30, 2022 2:37 am

Hello,

Glad to inform that we just released Spire.Presentation Pack Hotfix Version:7.5.3 which supported this feature now, please download it from the following links and refer to the sample code to have a test.
Website link:https://www.e-iceblue.com/Download/download-presentation-for-net-now.html
Nuget link: https://www.nuget.org/packages/Spire.Presentation/7.5.3
Sample code:
Code: Select all
   //Load template presentation
   Presentation templatePresentation = new Presentation();
   templatePresentation.LoadFromFile(@"datalabel-test-02.pptx");

   //Create New Presentation
   Spire.Presentation.Presentation presentation = new Spire.Presentation.Presentation();
   presentation.Slides.RemoveAt(0);

   //Get slide from template presentation. index:1
   ISlide cloneSlide = templatePresentation.Slides.ToArray().ElementAtOrDefault(0);

   //---
   //Get Chart                       
   //IChart chart = templateSlide.Shapes.ToArray().FirstOrDefault(x => x.Name == "Content Placeholder 15") as IChart;
   IChart chart = cloneSlide.Shapes.ToArray().FirstOrDefault(x => x.Name == "Chart 5") as IChart;

   //---
   for (int s = 0; s < chart.Series.Count; s++)
   {
       var serieIndex = s;
       IEnumerable<ChartDataLabel> dataLabels = chart.Series[s].DataLabels.Cast<ChartDataLabel>();

       for (int j = 0; j < chart.Categories.Count; j++)
       {
           var categoryIndex = j;
           ChartDataLabel dl = null;
           bool existingDataLabelFound;
           TextRange existingDataLabelTextRange = null;

           //Get data label
           var existingDataLabel = dataLabels.FirstOrDefault(c => c.ID == categoryIndex);
           existingDataLabelFound = false;

           if (existingDataLabel != null)
           {
               existingDataLabelFound = true;
               dl = existingDataLabel;
               existingDataLabelTextRange = dl.TextFrame.Paragraphs[0].FirstTextRange;
           }
           else
           {
               existingDataLabelFound = false;
               dl = chart.Series[serieIndex].DataLabels.Add();
               dl.ID = categoryIndex;
           }

           dl.UseValuePlaceholder = true;
           ITextFrameProperties textFrame = dl.TextFrame;
           TextParagraph paragraph = textFrame.Paragraphs[0];

           TextRange topTextRange = new TextRange("top\v");
           paragraph.TextRanges.Append(topTextRange);
           topTextRange.Fill.FillType = FillFormatType.Solid;
           topTextRange.Fill.SolidColor.Color = Color.Green;

           TextRange leftTextRange = new TextRange("left");
           paragraph.TextRanges.Append(leftTextRange);
           leftTextRange.Fill.FillType = FillFormatType.Solid;
           leftTextRange.Fill.SolidColor.Color = Color.Red;

           TextRange middleTextRange = new TextRange(ChartDataLabel.ValuePlaceholder);
           paragraph.TextRanges.Append(middleTextRange);

           TextRange rightTextRange = new TextRange("right\v");
           paragraph.TextRanges.Append(rightTextRange);
           rightTextRange.Fill.FillType = FillFormatType.Solid;
           rightTextRange.Fill.SolidColor.Color = Color.Blue;

           TextRange bottomTextRange = new TextRange("bottom");
           paragraph.TextRanges.Append(bottomTextRange);
           bottomTextRange.Fill.FillType = FillFormatType.Solid;
           bottomTextRange.Fill.SolidColor.Color = Color.Orange;
       }

       presentation.Slides.Append(cloneSlide);
       //Save and launch to view the PPTX document.
       presentation.SaveToFile(@"output.pptx", Spire.Presentation.FileFormat.Pptx2010);

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1261
Joined: Wed Apr 25, 2018 3:20 am

Mon Aug 08, 2022 11:37 am

I tried it and it works. Thank you.

But, the middle data label's visual settings can't be changed eg font size and font height. You can check the following lines in which I set these settings. How can I change the middle data label's visual settings? Also, there is no problem with the top, left, right, and bottom labels.

Code: Select all
TextRange middleTextRange = new TextRange(ChartDataLabel.ValuePlaceholder);
middleTextRange.Fill.SolidColor.Color = chart.Series[0].DataLabels.TextProperties.Paragraphs[0].DefaultCharacterProperties.Fill.SolidColor.Color;
middleTextRange.Fill.FillType = chart.Series[0].DataLabels.TextProperties.Paragraphs[0].DefaultCharacterProperties.Fill.FillType;
middleTextRange.FontHeight = chart.Series[0].DataLabels.TextProperties.Paragraphs[0].DefaultCharacterProperties.FontHeight;
middleTextRange.LatinFont = chart.Series[0].DataLabels.TextProperties.Paragraphs[0].DefaultCharacterProperties.LatinFont;
paragraph.TextRanges.Append(middleTextRange);


2022-08-08 14_24_23-output.pptx - PowerPoint.png


Output File:
output.zip


Sample Code:

Code: Select all
using Spire.Presentation;
using System.Linq;
using Spire.Presentation.Charts;
using System;
using System.Drawing;
using Spire.Presentation.Drawing;
using System.Collections.Generic;

namespace PptxTesterPaidVersion2
{
    class Program
    {
        static void Main(string[] args)
        {
            SetChartDataLabelByValue();

        }


        private static void SetChartDataLabelByValue()
        {
            //Load template presentation
            Presentation templatePresentation = new Presentation();
            templatePresentation.LoadFromFile(@"datalabel-test-02.pptx");

            //Create New Presentation
            Spire.Presentation.Presentation presentation = new Spire.Presentation.Presentation();
            presentation.Slides.RemoveAt(0);

            //Get slide from template presentation. index:1
            ISlide cloneSlide = templatePresentation.Slides.ToArray().ElementAtOrDefault(0);

            //---
            //Get Chart                       
            //IChart chart = templateSlide.Shapes.ToArray().FirstOrDefault(x => x.Name == "Content Placeholder 15") as IChart;
            IChart chart = cloneSlide.Shapes.ToArray().FirstOrDefault(x => x.Name == "Chart 5") as IChart;

            //---
            for (int s = 0; s < chart.Series.Count; s++)
            {
                var serieIndex = s;
                IEnumerable<ChartDataLabel> dataLabels = chart.Series[s].DataLabels.Cast<ChartDataLabel>();

                for (int j = 0; j < chart.Categories.Count; j++)
                {
                    var categoryIndex = j;
                    ChartDataLabel dl = null;
                    bool existingDataLabelFound;
                    TextRange existingDataLabelTextRange = null;

                    //Get data label
                    var existingDataLabel = dataLabels.FirstOrDefault(c => c.ID == categoryIndex);
                    existingDataLabelFound = false;

                    if (existingDataLabel != null)
                    {
                        existingDataLabelFound = true;
                        dl = existingDataLabel;
                        existingDataLabelTextRange = dl.TextFrame.Paragraphs[0].FirstTextRange;
                    }
                    else
                    {
                        existingDataLabelFound = false;
                        dl = chart.Series[serieIndex].DataLabels.Add();
                        dl.ID = categoryIndex;
                    }

                    dl.UseValuePlaceholder = true;
                    ITextFrameProperties textFrame = dl.TextFrame;
                    TextParagraph paragraph = textFrame.Paragraphs[0];

                    TextRange topTextRange = new TextRange("top\v");
                    paragraph.TextRanges.Append(topTextRange);
                    topTextRange.Fill.FillType = FillFormatType.Solid;
                    topTextRange.Fill.SolidColor.Color = Color.Green;
                    topTextRange.FontHeight = chart.Series[0].DataLabels.TextProperties.Paragraphs[0].DefaultCharacterProperties.FontHeight;
                    topTextRange.LatinFont = chart.Series[0].DataLabels.TextProperties.Paragraphs[0].DefaultCharacterProperties.LatinFont;

                    TextRange leftTextRange = new TextRange("left");
                    paragraph.TextRanges.Append(leftTextRange);
                    leftTextRange.Fill.FillType = FillFormatType.Solid;
                    leftTextRange.Fill.SolidColor.Color = Color.Red;
                    leftTextRange.FontHeight = chart.Series[0].DataLabels.TextProperties.Paragraphs[0].DefaultCharacterProperties.FontHeight;
                    leftTextRange.LatinFont = chart.Series[0].DataLabels.TextProperties.Paragraphs[0].DefaultCharacterProperties.LatinFont;

                    TextRange middleTextRange = new TextRange(ChartDataLabel.ValuePlaceholder);
                    middleTextRange.Fill.SolidColor.Color = chart.Series[0].DataLabels.TextProperties.Paragraphs[0].DefaultCharacterProperties.Fill.SolidColor.Color;
                    middleTextRange.Fill.FillType = chart.Series[0].DataLabels.TextProperties.Paragraphs[0].DefaultCharacterProperties.Fill.FillType;
                    middleTextRange.FontHeight = chart.Series[0].DataLabels.TextProperties.Paragraphs[0].DefaultCharacterProperties.FontHeight;
                    middleTextRange.LatinFont = chart.Series[0].DataLabels.TextProperties.Paragraphs[0].DefaultCharacterProperties.LatinFont;
                    paragraph.TextRanges.Append(middleTextRange);

                    TextRange rightTextRange = new TextRange("right\v");
                    paragraph.TextRanges.Append(rightTextRange);
                    rightTextRange.Fill.FillType = FillFormatType.Solid;
                    rightTextRange.Fill.SolidColor.Color = Color.Blue;
                    rightTextRange.FontHeight = chart.Series[0].DataLabels.TextProperties.Paragraphs[0].DefaultCharacterProperties.FontHeight;
                    rightTextRange.LatinFont = chart.Series[0].DataLabels.TextProperties.Paragraphs[0].DefaultCharacterProperties.LatinFont;

                    TextRange bottomTextRange = new TextRange("bottom");
                    paragraph.TextRanges.Append(bottomTextRange);
                    bottomTextRange.Fill.FillType = FillFormatType.Solid;
                    bottomTextRange.Fill.SolidColor.Color = Color.Orange;
                    bottomTextRange.FontHeight = chart.Series[0].DataLabels.TextProperties.Paragraphs[0].DefaultCharacterProperties.FontHeight;
                    bottomTextRange.LatinFont = chart.Series[0].DataLabels.TextProperties.Paragraphs[0].DefaultCharacterProperties.LatinFont;
                }
            }

            presentation.Slides.Append(cloneSlide);
            //Save and launch to view the PPTX document.
            presentation.SaveToFile(@"output.pptx", Spire.Presentation.FileFormat.Pptx2010);
        }
    }
}

omur.ertanis
 
Posts: 22
Joined: Mon Feb 21, 2022 6:49 am

Tue Aug 09, 2022 7:32 am

Hello Omur Ertanis,

Thanks for your inquiry.
I did reproduce the behavior you mentioned. This issue has been logged into our bug tracking system with the ticket SPIREPPT-2034. Our Dev team will do more investigations to fix it. Once there is any update, I will inform you asap. Sorry for the inconvenience caused.

Sincerely,
Simple
E-iceblue support team
User avatar

Simple.Li
 
Posts: 248
Joined: Fri Jul 01, 2022 2:33 am

Wed Sep 28, 2022 2:00 am

Hello,

Thanks for your patience.
Glad to inform you that we just released Spire.Presentation Pack Hotfix Version:7.9.2 which fixes the issue with SPIREPPT-2034. Please download the new version from the following links to test.
In addition, you need to add the following code to the middleTextRange section.
Code: Select all
middleTextRange.Fill.SolidColor.Color = Color.YellowGreen;
middleTextRange.FontHeight = 20;

Website download link: https://www.e-iceblue.com/Download/down ... t-now.html
Nuget download link: https://www.nuget.org/packages/Spire.Presentation/7.9.2

Sincerely,
Simple
E-iceblue support team
User avatar

Simple.Li
 
Posts: 248
Joined: Fri Jul 01, 2022 2:33 am

Return to Spire.Presentation