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.

Wed Oct 27, 2021 5:14 pm

I want to update the 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.

During our testing, we have found that if the number of series behind the existing chart is the same as the number of series in the data you want to add then the visual formatting of the chart is not affected. However, if you have 1 or more series than the number of series behind the existing chart then the chart formatting is lost.

Below is our test case and results. Please you let us know how we can add a new series to the existing chart without having to copy visual properties from one series to another using spire.

-------------------

Test: replacing data which **has more series** but same number of categories

pptx template: pptx:
https://github.com/Indico-Labs/pptx_gen ... .test.pptx

here is my template chart, it has multiple series and categories:
Image

Here is the data I was to replace the chart with. Notice it has 1 additional series compared to the number of series behind the existing chart.

  | brand 1 | brand 2 | brand 3| brand 4
-- | -- | -- | -- | --
car 1 | 50 | 55 | 20| 20
car 2 | 60 | 45 | 30| 20
car 3 | 40 | 35 | 40| 20
car 4 | 60 | 25 | 10| 20
car 5 | 50 | 14 | 20| 20


when I send this data to my template chart, this is what the expected output should be:
Image

Test 3 Result:

It throws a runtime error when we don't assign serials labels as below.

Code: Select all
```
//Set series labels
chart.Series.SeriesLabel = chart.ChartData["B1", "E1"];
```


Image

When we assign serials labels and data.

Result File: https://app.zenhub.com/files/349071455/ ... 0/download

Image

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

namespace PptxTester
{
    class Program
    {
        static void Main(string[] args)
        {
            //Load template presentation
            Presentation templatePresentation = new Presentation();
            templatePresentation.LoadFromFile("multi.series.test.pptx");


            //Get first slide from template presentation
            var slide = templatePresentation.Slides[0];

            //Get Chart                       
            IChart chart = slide.Shapes.ToArray().FirstOrDefault(x => x.Name == "Chart 5") as IChart;


            chart.ChartData[1, 0].Value = "car 1";
            chart.ChartData[2, 0].Value = "car 2";
            chart.ChartData[3, 0].Value = "car 3";
            chart.ChartData[4, 0].Value = "car 4";
            chart.ChartData[5, 0].Value = "car 5";


            chart.ChartData[0, 1].Value = "brand 1";
            chart.ChartData[0, 2].Value = "brand 2";
            chart.ChartData[0, 3].Value = "brand 3";
            chart.ChartData[0, 4].Value = "brand 4";

            chart.ChartData[1, 1].Value = 50;
            chart.ChartData[2, 1].Value = 60;
            chart.ChartData[3, 1].Value = 40;
            chart.ChartData[4, 1].Value = 60;
            chart.ChartData[5, 1].Value = 50;

            chart.ChartData[1, 2].Value = 55;
            chart.ChartData[2, 2].Value = 45;
            chart.ChartData[3, 2].Value = 35;
            chart.ChartData[4, 2].Value = 25;
            chart.ChartData[5, 2].Value = 14;

            chart.ChartData[1, 3].Value = 20;
            chart.ChartData[2, 3].Value = 30;
            chart.ChartData[3, 3].Value = 40;
            chart.ChartData[4, 3].Value = 10;
            chart.ChartData[5, 3].Value = 20;

            chart.ChartData[1, 4].Value = 20;
            chart.ChartData[2, 4].Value = 20;
            chart.ChartData[3, 4].Value = 20;
            chart.ChartData[4, 4].Value = 20;
            chart.ChartData[5, 4].Value = 20;

            //Set series labels
            chart.Series.SeriesLabel = chart.ChartData["B1", "E1"];

            //Select data               
            chart.Series[0].Values = chart.ChartData["B2", "B6"];
            chart.Series[1].Values = chart.ChartData["C2", "C6"];
            chart.Series[2].Values = chart.ChartData["D2", "D6"];
            chart.Series[3].Values = chart.ChartData["F2", "F6"];

            //Save and launch to view the PPTX document.
            templatePresentation.SaveToFile("multi.series.test.result.case3.pptx", Spire.Presentation.FileFormat.Pptx2013);
        }
    }
}
```
Last edited by majeed_s on Thu Oct 28, 2021 5:30 pm, edited 1 time in total.

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

Thu Oct 28, 2021 4:06 am

Hello Majeed,

Thanks for your inquiry.
Unfortunately, I am unable to download your pptx files from your links. But based on your screenshots and code, I recreated your issue.
It has been logged in our issue tracking system under the ticket SPIREPPT-1721. We will fix it as per your request. I will keep you posted on its progress.

Sincerely,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Wed Nov 03, 2021 2:26 am

Hello Majeed,

Hope you are doing well.
I have some good news for you. Now you can use this line : chart.Series.Append(chart.ChartData[0, 4]); to replace the line: chart.Series.SeriesLabel = chart.ChartData["B1", "E1"]; to implement what you request.
Here is all the code:
Code: Select all
 //Load template presentation
            Presentation templatePresentation = new Presentation();
            templatePresentation.LoadFromFile("C:/multi.series.test.pptx");


            //Get first slide from template presentation
            var slide = templatePresentation.Slides[0];

            //Get Chart                       
            IChart chart = slide.Shapes.ToArray().FirstOrDefault(x => x.Name == "Chart 5") as IChart;


            chart.ChartData[1, 0].Value = "car 1";
            chart.ChartData[2, 0].Value = "car 2";
            chart.ChartData[3, 0].Value = "car 3";
            chart.ChartData[4, 0].Value = "car 4";
            chart.ChartData[5, 0].Value = "car 5";


            chart.ChartData[0, 1].Value = "brand 1";
            chart.ChartData[0, 2].Value = "brand 2";
            chart.ChartData[0, 3].Value = "brand 3";
            chart.ChartData[0, 4].Value = "brand 4";

            chart.ChartData[1, 1].Value = 50;
            chart.ChartData[2, 1].Value = 60;
            chart.ChartData[3, 1].Value = 40;
            chart.ChartData[4, 1].Value = 60;
            chart.ChartData[5, 1].Value = 50;

            chart.ChartData[1, 2].Value = 55;
            chart.ChartData[2, 2].Value = 45;
            chart.ChartData[3, 2].Value = 35;
            chart.ChartData[4, 2].Value = 25;
            chart.ChartData[5, 2].Value = 14;

            chart.ChartData[1, 3].Value = 20;
            chart.ChartData[2, 3].Value = 30;
            chart.ChartData[3, 3].Value = 40;
            chart.ChartData[4, 3].Value = 10;
            chart.ChartData[5, 3].Value = 20;

            chart.ChartData[1, 4].Value = 20;
            chart.ChartData[2, 4].Value = 20;
            chart.ChartData[3, 4].Value = 20;
            chart.ChartData[4, 4].Value = 20;
            chart.ChartData[5, 4].Value = 20;

            ////Set series labels
            //chart.Series.SeriesLabel = chart.ChartData["B1", "E1"];
            chart.Series.Append(chart.ChartData[0, 4]);
            //Select data               
            chart.Series[0].Values = chart.ChartData["B2", "B6"];
            chart.Series[1].Values = chart.ChartData["C2", "C6"];
            chart.Series[2].Values = chart.ChartData["D2", "D6"];
            chart.Series[3].Values = chart.ChartData["E2", "E6"];

            //Save and launch to view the PPTX document.
            templatePresentation.SaveToFile("C:/multi.series.test.result.case3.pptx", Spire.Presentation.FileFormat.Pptx2013);


In addition, our developer has modified SET method of chart.Series.SeriesLabel. When new version is released, I will inform you.

Sincerely,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Wed Nov 03, 2021 9:10 am

Fantastic! Thank for resolving this. We will test and get back to you.

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

Wed Nov 03, 2021 9:30 am

Dear Majeed,

Thanks for your response.
I'm looking forward to your feedback.

Sincerely,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Fri Nov 05, 2021 10:16 pm

It works fine now. Thank you.

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

Mon Nov 08, 2021 2:29 am

Thanks for your feedback.

Have a good day.

Sincerely,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Tue Sep 13, 2022 4:55 am

Hello,

Thanks for your patient waiting.
I'm glad to inform you that SPIREPPT-1721 has been resolved.
Welcome to download and test Spire.Presentation Pack Version:7.8.
https://www.e-iceblue.com/Download/download-presentation-for-net-now.html
Nuget link: https://www.nuget.org/packages/Spire.Presentation/

Please set chart.Series.KeepSeriesFormat = true.

Full Code:
Code: Select all
//Load template presentation
            Presentation templatePresentation = new Presentation();
            templatePresentation.LoadFromFile("C:/multi.series.test.pptx");


            //Get first slide from template presentation
            var slide = templatePresentation.Slides[0];

            //Get Chart                       
            IChart chart = slide.Shapes.ToArray().FirstOrDefault(x => x.Name == "Chart 5") as IChart;


            chart.ChartData[1, 0].Value = "car 1";
            chart.ChartData[2, 0].Value = "car 2";
            chart.ChartData[3, 0].Value = "car 3";
            chart.ChartData[4, 0].Value = "car 4";
            chart.ChartData[5, 0].Value = "car 5";


            chart.ChartData[0, 1].Value = "brand 1";
            chart.ChartData[0, 2].Value = "brand 2";
            chart.ChartData[0, 3].Value = "brand 3";
            chart.ChartData[0, 4].Value = "brand 4";

            chart.ChartData[1, 1].Value = 50;
            chart.ChartData[2, 1].Value = 60;
            chart.ChartData[3, 1].Value = 40;
            chart.ChartData[4, 1].Value = 60;
            chart.ChartData[5, 1].Value = 50;

            chart.ChartData[1, 2].Value = 55;
            chart.ChartData[2, 2].Value = 45;
            chart.ChartData[3, 2].Value = 35;
            chart.ChartData[4, 2].Value = 25;
            chart.ChartData[5, 2].Value = 14;

            chart.ChartData[1, 3].Value = 20;
            chart.ChartData[2, 3].Value = 30;
            chart.ChartData[3, 3].Value = 40;
            chart.ChartData[4, 3].Value = 10;
            chart.ChartData[5, 3].Value = 20;

            chart.ChartData[1, 4].Value = 20;
            chart.ChartData[2, 4].Value = 20;
            chart.ChartData[3, 4].Value = 20;
            chart.ChartData[4, 4].Value = 20;
            chart.ChartData[5, 4].Value = 20;

            chart.Series.KeepSeriesFormat = true;
            //Set series labels
            chart.Series.SeriesLabel = chart.ChartData["B1", "E1"];

            //Select data               
            chart.Series[0].Values = chart.ChartData["B2", "B6"];
            chart.Series[1].Values = chart.ChartData["C2", "C6"];
            chart.Series[2].Values = chart.ChartData["D2", "D6"];
            chart.Series[3].Values = chart.ChartData["E2", "E6"];

            //Save and launch to view the PPTX document.
            templatePresentation.SaveToFile("C:/multi.series.test.result.case3.pptx", Spire.Presentation.FileFormat.Pptx2013);


Sincerely,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Return to Spire.Presentation

cron