ControlCollection.AddChart Method (Double, Double, Double, Double, String) (2007 System)

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

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

Syntax

'Declaration
Public Function AddChart ( _
    left As Double, _
    top As Double, _
    width As Double, _
    height As Double, _
    name As String _
) As Chart
'Usage
Dim instance As ControlCollection 
Dim left As Double 
Dim top As Double 
Dim width As Double 
Dim height As Double 
Dim name As String 
Dim returnValue As Chart 

returnValue = instance.AddChart(left, _
    top, width, height, name)
public Chart AddChart(
    double left,
    double top,
    double width,
    double height,
    string name
)
public:
Chart^ AddChart(
    double left, 
    double top, 
    double width, 
    double height, 
    String^ name
)
public function AddChart(
    left : double, 
    top : double, 
    width : double, 
    height : double, 
    name : String
) : Chart

Parameters

  • left
    Type: System.Double

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

  • top
    Type: System.Double

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

  • height
    Type: System.Double

    The height of the control in points.

Return Value

Type: Microsoft.Office.Tools.Excel.Chart
The Chart control that was added to the ControlCollection instance.

Exceptions

Exception Condition
ArgumentNullException

The name argument is nulla null reference (Nothing in Visual Basic) or has zero length.

ControlNameAlreadyExistsException

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

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.

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 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
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;
}

.NET Framework Security

See Also

Reference

ControlCollection Class

ControlCollection Members

AddChart Overload

Microsoft.Office.Tools.Excel Namespace