Update Chart data in PowerPoint

427 Views Asked by At

I am creating PowerPoint by loading template and updating the data and saving as a new copy. I am using GemBox.Presentation and GemBox.SpreadSheet for that. I can access the chart data using spreadsheet however I am not sure how to update it.
Here is how i access chart from the template slide.

var chart = ((GraphicFrame)currentSlide.Content.Drawings[0]).Chart;
var stackedColumn = (GemBox.Spreadsheet.Charts.ColumnChart)chart.ExcelChart;

I have tried to update the data by converting to list but it still did not update.

foreach (var item in data)
{
     var seriesItem = data;
     var valueList = item.Values.Cast<int>().Select(x => x).ToList();
     
    // values to update in chart
     List<int> myValues = new List<int>(new int[] { 1, 2});

     for (int v = 0; v < valueList.Count(); v++)
     {
        valueList[v] = myValues[v];
     }
}

I also saw this example https://www.gemboxsoftware.com/presentation/examples/powerpoint-charts/412 to update chart however this adds the series but not updates the current data. If you can please share if there is any support available for this component to perform to update chart data that would be appreciated. Thanks

2

There are 2 best solutions below

0
On BEST ANSWER

One way is to use one of the ChartSeries.SetValues methods, as mentioned in the previous answer.

Another way is to update the underlining Excel cell.
For instance, in that example you linked to, you could do this:

var lineChart = (LineChart)chart.ExcelChart;
var lineSeries = lineChart.Series[0];

// Get series source cells.
string[] cellsReference = lineSeries.ValuesReference.Split('!');
var cells = lineChart.Worksheet.Parent.Worksheets
    .First(w => w.Name == cellsReference[0].Trim('\''))
    .Cells.GetSubrange(cellsReference[1]);

// Update cells with doubled values.
foreach (var cell in cells)
    cell.SetValue(cell.DoubleValue * 2);
0
On

I have managed to update the data on the spreadsheet that is associated with powerpoint charts.

Class named ChartSeries contains method named SetValues() that will set the chart series values. There are two ways you can use this method as it takes IEnumerable and Object [] as parameters as shown below.

public void SetValues(IEnumerable values)

public void SetValues(params object[] values)

And here is how i have access that value and updated it.

foreach (var item in data)
 {
  var seriesItem = data;
  foreach (var value in seriesItem)
    {
      if (value.DisplayName == "[Open]")
       {
         value.SetValues(50,25);
       }
    }
}