Spire.Presentation is a professional PowerPoint® compatible library that enables developers to create, read, write, modify, convert and Print PowerPoint documents from any .NET(C#, VB.NET, ASP.NET, .NET Core) application and Java (J2SE and J2EE) application.

Fri Mar 11, 2022 8:14 am

Hello,
I have an existing chart and I want to add overlays to that chart.
Chart data label is on at the template file but I can't get the chart data label at the following code. It throws an System.ArgumentOutOfRangeException.

Code: Select all
var dataLabel = chart.Series[0].DataLabels[0];


How can I get an existing chart data label?

Template File:
template-overlays.zip


Source Code:

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

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

        private static void TestChartDataLabels_Overlays()
        {
            //Load template presentation
            Presentation templatePresentation = new Presentation();
            templatePresentation.LoadFromFile("template-overlays.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 = cloneSlide.Shapes.ToArray().FirstOrDefault(x => x.Name == "Chart 20") as IChart;
            var dataLabel = chart.Series[0].DataLabels[0];

            ITextFrameProperties textFrame = dataLabel.TextFrame;
            TextParagraph paragraph = textFrame.Paragraphs[0];
            TextRange topTextRange = new TextRange("-->");
            paragraph.TextRanges.Append(topTextRange);
            presentation.Slides.Append(cloneSlide);

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

        public static string GetColumnName(int index)
        {
            const string letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

            var value = "";

            if (index >= letters.Length)
                value += letters[index / letters.Length - 1];

            value += letters[index % letters.Length];

            return value;
        }
    }
}
Last edited by omur.ertanis on Fri Mar 11, 2022 10:28 am, edited 1 time in total.

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

Fri Mar 11, 2022 8:50 am

Hello,

Thanks for your inquiry.
I tested your case and did reproduce the issue you mentioned. I have logged the issue into our bug tracking system with the ticket number SPIREPPT-1880. Our development team will investigate and fix it. Once it is resolved, I will inform you in time. Sorry for the inconvenience caused.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1141
Joined: Wed Apr 07, 2021 2:50 am

Mon Mar 14, 2022 4:52 pm

Hi,

Do you have a rough estimate of when this issue will be resolved? Sorry for chasing you but a timeline would really help.

Thanks

majeed_s
 
Posts: 45
Joined: Thu Mar 25, 2021 4:13 pm

Tue Mar 15, 2022 9:20 am

Hello,

Thanks for following up.
After further investigation, our developers found that all the "datalable" properties of the chart in your Powerpoint file are the same, and the relevant style properties are stored directly in "chart.Series[0].DataLabels", so "chart.Series[0]. DataLabels[0]" cannot get the value and will throw an exception.
Please refer to the following code to set the style of "DataLabels[0]":
Code: Select all
...
IChart chart = cloneSlide.Shapes.ToArray().FirstOrDefault(x => x.Name == "Chart 20") as IChart;
//Initialize the instances of series label and set parameters of the first label
ChartDataLabel cd1 = chart.Series[0].DataLabels.Add();
cd1.ID = 0;
cd1.PercentageVisible = true;
ITextFrameProperties textFrame = cd1.TextFrame;
TextParagraph paragraph = textFrame.Paragraphs[0];
TextRange topTextRange = new TextRange("-->");
paragraph.TextRanges.Append(topTextRange);
presentation.Slides.Append(cloneSlide);
presentation.SaveToFile("TestChartDataLabels_Overlays_01.pptx", Spire.Presentation.FileFormat.Pptx2013);

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1141
Joined: Wed Apr 07, 2021 2:50 am

Tue Mar 15, 2022 11:02 am

Unfortunately, this is not what I want. I want to concatenate data label text frame content with another text. For my specific example, the output should be "20% -->".
How can I achieve this?

The output should be "20% -->" but it is " -->"

01.jpg


Source Code:

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

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

        private static void TestChartDataLabels_Overlays()
        {
            //Load template presentation
            Presentation templatePresentation = new Presentation();
            templatePresentation.LoadFromFile("template-overlays.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 = cloneSlide.Shapes.ToArray().FirstOrDefault(x => x.Name == "Chart 20") as IChart;

            //Initialize the instances of series label and set parameters of the first label
            ChartDataLabel cd1 = chart.Series[0].DataLabels.Add();
            cd1.ID = 0;

            cd1.PercentageVisible = true;
            ITextFrameProperties textFrame = cd1.TextFrame;
            TextParagraph paragraph = textFrame.Paragraphs[0];
            TextRange topTextRange = new TextRange("-->");
            paragraph.TextRanges.Append(topTextRange);

            presentation.Slides.Append(cloneSlide);

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

        public static string GetColumnName(int index)
        {
            const string letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

            var value = "";

            if (index >= letters.Length)
                value += letters[index / letters.Length - 1];

            value += letters[index % letters.Length];

            return value;
        }
    }
}
Last edited by omur.ertanis on Tue Mar 15, 2022 4:47 pm, edited 1 time in total.

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

Tue Mar 15, 2022 12:17 pm

Just to clarify, we want to access a given data label and concat a new piece of string to the existing string.

We don't want to create a brand new data label.

Here is a before and after.

majeed_s
 
Posts: 45
Joined: Thu Mar 25, 2021 4:13 pm

Wed Mar 16, 2022 10:05 am

Hello,

Thanks for your feedback.
By parsing the source file you provided, we found that "DataLabel" is not found in chart.xml (as shown in screenshot 1), so "chart.Series[0].DataLabels[0]" throws an exception. If the style of a certain "DataLabel" is modified, the parsing file can find "DataLabel" in chart.xml (as shown in screenshot 2), and load this file "chart.Series[0].DataLabels[0]" to obtain correct value.
So, for your source file, if you add strings to "chart.Series[0].DataLabels[0]", you can only use the methods provided to you earlier. And you need to change "TextRange topTextRange = new TextRange("-->")" to "TextRange topTextRange = new TextRange("20% -->")" to achieve your needs. Hope you can understand.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1141
Joined: Wed Apr 07, 2021 2:50 am

Mon Mar 21, 2022 3:23 pm

Thank you Annika,
I want to reach that XML files. How can I reach that XML files with the Spire Presentation library?

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

Mon Mar 21, 2022 4:54 pm

Thanks Annika. We ran further tests and you are right, the `dLbl` node does not exist unless you manually edit one of the data label in PowerPoint. This is a major problem for us.

Is there a workaround? Perhaps, we can inject our own xml to this particular node?

majeed_s
 
Posts: 45
Joined: Thu Mar 25, 2021 4:13 pm

Tue Mar 22, 2022 1:31 am

omur.ertanis wrote:Thank you Annika,
I want to reach that XML files. How can I reach that XML files with the Spire Presentation library?


Hi omur.ertanis,

You are welcome.
These XML files are obtained after parsing PowerPoint files, and the data obtained by our Spire.Presentation is consistent with the data in XML, but Spire.Presentation does not support manipulating XML files. Hope you can understand.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1141
Joined: Wed Apr 07, 2021 2:50 am

Tue Mar 22, 2022 1:51 am

majeed_s wrote:Thanks Annika. We ran further tests and you are right, the `dLbl` node does not exist unless you manually edit one of the data label in PowerPoint. This is a major problem for us.

Is there a workaround? Perhaps, we can inject our own xml to this particular node?


Hello majeed_s,

Thanks for your feedback.
I'm sorry that I don't have a solution to this issue other than the solution I gave you earlier. Our Spire.Presentation does not support manipulating XML files. If you want to inject your own XML files into the "DataLabel" node, you need to do it yourself. Thanks for your understanding.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1141
Joined: Wed Apr 07, 2021 2:50 am

Return to Spire.Presentation