Document Solutions for Excel, .NET Edition | Document Solutions
Features / Chart / Create and Delete Chart
In This Topic
    Create and Delete Chart
    In This Topic

    DsExcel .NET allows users to create and delete chart in spreadsheets as per their requirements.

    You can create and delete chart using the properties and methods of the IShapes Interface and the IChart interface

    Create Chart

    You can create chart in a worksheet by using AddChart method of the IShapes interface. Using this method, you can add a chart at a particular position by providing position coordinates of the target range. The method has another overload that lets you add a chart directly to the target range. You can use Add method of the ISeriesCollection class which lets you reflect data over the chart.

    Refer to the following example code to create a chart.

    C#
    Copy Code
    // Add Data
    worksheet.Range["A1:D6"].Value = new object[,]
    
    {
    {null, "Revenue", "Profit", "Sales"},
    {"North", 10, 25, 25},
    {"East", 51, 36, 27},
    {"South", 52, 85, 30},
    {"West", 22, 65, 65}
    };
    
    // Add Chart at a specified position
    IShape shape = worksheet.Shapes.AddChart(ChartType.ColumnClustered, 200, 75, 300, 300);
    shape.Chart.SeriesCollection.Add(worksheet.Range["A1:D6"], RowCol.Columns, true, true);
    
    // Add Chart at a particular range
    // IShape shapeRange = worksheet.Shapes.AddChart(ChartType.ColumnClustered, worksheet.Range["A8:E20"]);
    // shapeRange.Chart.SeriesCollection.Add(worksheet.Range["A1:D6"], RowCol.Columns, true, true);

    Note: The targetRange and the chart to be added must exist in the same worksheet. Otherwise, it results into an InvalidOperationException.

    Delete Chart

    You can delete an existing chart by using Delete method of the IChart interface.

    Refer to the following example code to delete a chart from your spreadsheet.

    C#
    Copy Code
    // Delete Chart
    shape.Chart.Delete();