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:
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:
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"];
```
When we assign serials labels and data.
Result File: https://app.zenhub.com/files/349071455/ ... 0/download
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);
}
}
}
```