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