ControlCollection.AddChart Method

Definition

Overloads

AddChart(Range, String)

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

AddChart(Double, Double, Double, Double, String)

Adds a new Chart control to the worksheet in the specified size and location.

AddChart(Range, String)

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

public:
 Microsoft::Office::Tools::Excel::Chart ^ AddChart(Microsoft::Office::Interop::Excel::Range ^ range, System::String ^ name);
public Microsoft.Office.Tools.Excel.Chart AddChart (Microsoft.Office.Interop.Excel.Range range, string name);
abstract member AddChart : Microsoft.Office.Interop.Excel.Range * string -> Microsoft.Office.Tools.Excel.Chart
Public Function AddChart (range As Range, name As String) As Chart

Parameters

range
Range

A Range that provides the bounds for the control.

name
String

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

Returns

The Chart control that was added to the ControlCollection instance.

Exceptions

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

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

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.

Examples

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;
}
Private Sub ExcelAddRangeChart()
    Me.Range("E1", "E3").Value2 = 16
    Me.Range("F1", "F3").Value2 = 24

    Dim Chart1 As Microsoft.Office.Tools.Excel.Chart _
        = Me.Controls.AddChart(Me.Range("A1", "C8"), _
        "Chart1")

    Chart1.SetSourceData(Me.Range("E1", "F5"), _
        Excel.XlRowCol.xlColumns)
    Chart1.ChartType = Excel.XlChartType. _
        xlColumnClustered

End Sub 

Remarks

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.

Applies to

AddChart(Double, Double, Double, Double, String)

Adds a new Chart control to the worksheet in the specified size and location.

public:
 Microsoft::Office::Tools::Excel::Chart ^ AddChart(double left, double top, double width, double height, System::String ^ name);
public Microsoft.Office.Tools.Excel.Chart AddChart (double left, double top, double width, double height, string name);
abstract member AddChart : double * double * double * double * string -> Microsoft.Office.Tools.Excel.Chart
Public Function AddChart (left As Double, top As Double, width As Double, height As Double, name As String) As Chart

Parameters

left
Double

The distance in points between the left edge of the control and the left edge of the worksheet.

top
Double

The distance in points between the top edge of the control and the top edge of the worksheet.

width
Double

The width of the control in points.

height
Double

The height of the control in points.

name
String

The name of the control.

Returns

The Chart control that was added to the ControlCollection instance.

Exceptions

The name argument is null or has zero length.

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

Examples

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 the top of the worksheet and passes cells E1 through F3 to the SetSourceData method to populate the chart with data.

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

    Microsoft.Office.Tools.Excel.Chart chart1 =
        this.Controls.AddChart(0, 0, 130, 130, "chart1");

    chart1.SetSourceData(this.Range["E1", "F3"], Excel.XlRowCol.xlColumns);
    chart1.ChartType = Excel.XlChartType.xlColumnClustered;
}
Private Sub ExcelAddChart()
    Me.Range("E1", "E3").Value2 = 16
    Me.Range("F1", "F3").Value2 = 24

    Dim Chart1 As Microsoft.Office.Tools.Excel. _
        Chart = Me.Controls.AddChart(0, 0, 130, _
        130, "Chart1")

    Chart1.SetSourceData(Me.Range("E1", "F3"), _
        Excel.XlRowCol.xlColumns)
    Chart1.ChartType = Excel.XlChartType. _
        xlColumnClustered

End Sub

Remarks

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.

Applies to