ControlCollection.AddChart Method (Range, String)

 

Adds a new Chart control to the worksheet at the range specified.

Namespace:   Microsoft.Office.Tools.Excel
Assembly:  Microsoft.Office.Tools.Excel (in Microsoft.Office.Tools.Excel.dll)

Chart AddChart(
	Range range,
	string name
)

Parameters

range
Type: Microsoft.Office.Interop.Excel.Range

A Range that provides the bounds for the control.

name
Type: System.String

The name of the control that can be used to index the control in the ControlCollection instance.

Return Value

Type: Microsoft.Office.Tools.Excel.Chart

The Chart control that was added to the ControlCollection instance.

Exception Condition
ArgumentNullException

The name or range argument is null, or the name argument has zero length.

ControlNameAlreadyExistsException

A control with the same name is already in the ControlCollection instance.

InvalidRangeException

The range that was specified is not valid. Multi-area ranges cannot be used. The range should be on the same worksheet as the ControlCollection instance.

The AddChart method enables you to add Chart controls to the end of the ControlCollection. To remove a Chart control that was previously added programmatically, use the Remove method.

The following code example inserts the number 16 to cells E1 through E3 and the number 24 to cells F1 through F3. The code then creates a Chart control to cells A1 through C8 and passes cells E1 through F3 to the SetSourceData method to populate the chart with data.

private void ExcelAddRangeChart()
{
    this.Range["E1", "E3"].Value2 = 16;
    this.Range["F1", "F3"].Value2 = 24;

    Microsoft.Office.Tools.Excel.Chart chart1 =
        this.Controls.AddChart(this.Range["A1", "C8"], "Chart1");

    chart1.SetSourceData(this.Range["E1", "F5"], Excel.XlRowCol.xlColumns);
    chart1.ChartType = Excel.XlChartType.xlColumnClustered;
}
Return to top
Show: