Search code examples
c#chartspowerpointgembox-spreadsheetgembox-presentation

Update Chart data in PowerPoint


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


Solution

  • 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);