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:
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;
}
}
}