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 Mar 08, 2022 2:08 pm

Hello,
I want to add additional data behind an existing chart and I want the chart to retain all its visual formatting. Note - I do not want to copy the properties from one series to another using spire.

I added one more series to the chart but I am losing visual settings such as Data Labels, Data Labels Format, etc.

02.jpg


I also want to retain Data Format Labels settings for the newly added series.

03.jpg


Sample Template File:
template-chart-data-labels.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_02();
        }

                             
        private static void TestChartDataLabels_02()
        {
            //Load template presentation
            Presentation templatePresentation = new Presentation();
            templatePresentation.LoadFromFile("template-chart-data-labels.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 5") as IChart;

            int seriesCount = 4;
            int categoryCount = 3;

            chart.ChartData.Clear(0, 0, categoryCount + 1, seriesCount + 1);

            chart.ChartData[0, 1].Text = "Total";
            chart.ChartData[0, 2].Text = "Male";
            chart.ChartData[0, 3].Text = "Female";
            chart.ChartData[0, 4].Text = "Intense Demand";
           

            chart.ChartData[1, 0].Text = "Read a printed newspaper";
            chart.ChartData[2, 0].Text = "Visited a newspaper website";
            chart.ChartData[3, 0].Text = "Read a printed magazine";

            chart.ChartData[1, 1].NumberValue = 0.4315;
            chart.ChartData[1, 2].NumberValue = 0.4413;
            chart.ChartData[1, 3].NumberValue = 0.4222;
            chart.ChartData[1, 4].NumberValue = 0.2587;

            chart.ChartData[2, 1].NumberValue = 0.4136;
            chart.ChartData[2, 2].NumberValue = 0.4402;
            chart.ChartData[2, 3].NumberValue = 0.3883;
            chart.ChartData[2, 4].NumberValue = 0.4905;

            chart.ChartData[3, 1].NumberValue = 0.2539;
            chart.ChartData[3, 2].NumberValue = 0.2369;
            chart.ChartData[3, 3].NumberValue = 0.2699;
            chart.ChartData[3, 4].NumberValue = 0.1449;


            var startIndexAddSeries = chart.Series.Count + 1;
            for (int i = startIndexAddSeries; i <= seriesCount; i++)
            {
                chart.Series.Append(chart.ChartData[0, i]);
            }

            chart.Categories.CategoryLabels = chart.ChartData["A2", "A" + (categoryCount + 1)];
            for (int i = 0; i < seriesCount; i++)
            {
                string letter = GetColumnName(i + 1);
                chart.Series[i].Values = chart.ChartData[letter + "2", letter + (categoryCount + 1)];
            }

            presentation.Slides.Append(cloneSlide);

            //Save and launch to view the PPTX document.
            presentation.SaveToFile("TestChartDataLabels_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;
        }
    }
}

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

Wed Mar 09, 2022 9:51 am

Hello,

Thanks for your inquiry!
Even doing the same operation in MS powerpoint, the data labels format of the new added series also weren't retained. However, you can add the same format manually with the following modified code.
Code: Select all
private static void TestChartDataLabels_02()
{
    //Load template presentation
    Presentation templatePresentation = new Presentation();
    templatePresentation.LoadFromFile(@"template-chart-data-labels.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 5") as IChart;

    int seriesCount = 4;
    int categoryCount = 3;
   
    chart.ChartData.Clear(0, 0, categoryCount + 1, seriesCount + 1);

    chart.ChartData[0, 1].Text = "Total";
    chart.ChartData[0, 2].Text = "Male";
    chart.ChartData[0, 3].Text = "Female";
    chart.ChartData[0, 4].Text = "Intense Demand";


    chart.ChartData[1, 0].Text = "Read a printed newspaper";
    chart.ChartData[2, 0].Text = "Visited a newspaper website";
    chart.ChartData[3, 0].Text = "Read a printed magazine";

    chart.ChartData[1, 1].NumberValue = 0.4315;
    chart.ChartData[1, 2].NumberValue = 0.4413;
    chart.ChartData[1, 3].NumberValue = 0.4222;
    chart.ChartData[1, 4].NumberValue = 0.2587;

    chart.ChartData[2, 1].NumberValue = 0.4136;
    chart.ChartData[2, 2].NumberValue = 0.4402;
    chart.ChartData[2, 3].NumberValue = 0.3883;
    chart.ChartData[2, 4].NumberValue = 0.4905;

    chart.ChartData[3, 1].NumberValue = 0.2539;
    chart.ChartData[3, 2].NumberValue = 0.2369;
    chart.ChartData[3, 3].NumberValue = 0.2699;
    chart.ChartData[3, 4].NumberValue = 0.1449;


    var startIndexAddSeries = chart.Series.Count + 1;
    for (int i = startIndexAddSeries; i <= seriesCount; i++)
    {
        chart.Series.Append(chart.ChartData[0, i]);
    }

    chart.Categories.CategoryLabels = chart.ChartData["A2", "A" + (categoryCount + 1)];
    for (int i = 0; i < seriesCount; i++)
    {
        string letter = GetColumnName(i + 1);
        ChartSeriesDataFormat chartSeriesDataFormat = chart.Series[0];
        chart.Series[i].Values = chart.ChartData[letter + "2", letter + (categoryCount + 1)];
       
        chart.Series[i].DataLabels.CategoryNameVisible = false;
        chart.Series[i].DataLabels.PercentValueVisible = false;
        chart.Series[i].DataLabels.NumberFormat = "0%";
        chart.Series[i].DataLabels.LabelValueVisible = true;
        chart.Series[i].DataLabels.HasDataSource = false;
        chart.Series[i].DataLabels.LeaderLinesVisible = true;
        chart.Series[i].DataLabels.TextProperties.Paragraphs[0].DefaultCharacterProperties.FontHeight = 12;
    }

    presentation.Slides.Append(cloneSlide);

    //Save and launch to view the PPTX document.
    presentation.SaveToFile(@"TestChartDataLabels_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;
}


If there is any other questions, please feel free to write back!

Sincerely,
William
E-iceblue support team
User avatar

William.Zhang
 
Posts: 194
Joined: Mon Dec 27, 2021 2:23 am

Wed Mar 09, 2022 12:11 pm

Thank you William.
It works for this specific case but I don't want to assign all settings one by one. There are a lot of other settings for this chart and also for different chart types.
I am expecting from chart.Series.Append method to inherit visual settings from existing chart which I worked on.

It will be much helpful.

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

Thu Mar 10, 2022 10:03 am

Hello,

Thanks for your reply!
Since our Spire.Presentation is based on the MS PowerPoint. Sorry at present we don't have plan to adjust this part. Please use yesterday provided code to set the data labels format manually.
If there is anything else we can do for you, just feel free to let us know.

Sincerely,
William
E-iceblue support team
User avatar

William.Zhang
 
Posts: 194
Joined: Mon Dec 27, 2021 2:23 am

Return to Spire.Presentation