Add Chart into worksheet

Hi, I am following into this code.

please refer to https://help.syncfusion.com/windowsforms/spreadsheet/shapes#adding-charts-at-runtime

The code below is a sample on how to create chart. But this example is hard coded(predefined). What I want is to do is to generate or add chart during run time. Please refer to attached.

var chart = spreadsheet.AddChart(spreadsheet.ActiveSheet);

object[] Y_values = new object[] { 200, 100, 100 };
object[] X_values = new object[] { "Total Income", "Expenses", "Profit" };

IChartSerie series = chart.Series.Add(ExcelChartType.Pie);

// Enters the X and Y values directly
series.EnteredDirectlyValues = Y_values;
series.EnteredDirectlyCategoryLabels = X_values;

var shape = chart as ShapeImpl;

// Re-Positioning Chart
shape.Top = 200;
shape.Left = 200;

//Re-sizing a Chart
shape.Height = 300;
shape.Width = 300;

Attachment: ExcelChart_bdd3b946.rar

3 Replies

SS Sampathnarayanan Sankaralingam Syncfusion Team January 25, 2022 01:57 PM UTC

Hi Mark, 

There is no direct support to add chart at the run time. However you can achieve your requirement by the following workaround states that you can generate the chart based on the selected range of data. Please find the code snippet below. 

private void ChartBtn_Click(object sender, EventArgs e) 
{ 
    var chart = spreadsheet.AddChart(spreadsheet.ActiveSheet); 
    GridRangeInfo range = spreadsheet.ActiveGrid.SelectedRanges.ActiveRange; 
    chart.DataRange = spreadsheet.ActiveSheet.Range[range.Left,range.Top,range.Bottom,range.Right]; 
 
    var shape = chart as ShapeImpl; 
    shape.Top = 100; 
    shape.Left = 200; 
 
   // chart.ChartType = ExcelChartType.Column_Clustered; 
    spreadsheet.ActiveGrid.GraphicModel.InvalidateGraphicObjects(); 
    spreadsheet.ActiveGrid.GraphicModel.InvalidateGraphicVisual(); 
} 



Regards, 
Sampath Narayanan.S 




MJ Mark Jayvee January 26, 2022 09:08 AM UTC

thank you for the response. The chart is for the Column,


How about if I want a pie chart?



MA Mohanram Anbukkarasu Syncfusion Team January 27, 2022 11:52 AM UTC

Hi Mark, 

You can add a pie chart by setting ChartType property of the chart as ExcelChartType.Pie as shown in the below given KB document.  


Please let us know if you require further assistance from us.  

Regards, 
Mohanram A. 



Loader.
Up arrow icon