Share via


Chart.Name Property

Definition

Gets the name of the Chart control.

public:
 property System::String ^ Name { System::String ^ get(); };
public string Name { get; }
member this.Name : string
Public ReadOnly Property Name As String

Property Value

The name of the Chart control.

Remarks

When you create a Chart control, Excel sets the Name property to the string "sheetname Chart n", where n is the number of embedded charts on the worksheet. For example, if you add a Chart to Sheet1 and it is the first embedded chart on the worksheet, the Name property will have the value Sheet1 Chart 1.

Although the Name property is read-only, you can modify a portion of the name by using the Name property of the parent Microsoft.Office.Interop.Excel.ChartObject. The new name you specify replaces the "Chart n" substring in the string returned by the Name property. For example, the following code changes the Name property value of a Chart control from Sheet1 Chart 1 to Sheet1 SalesChart.

private void RenameChart()
{
    // Set some test data and create a new Chart.
    Globals.Sheet1.Range["A1", "A5"].Value2 = 22;
    Globals.Sheet1.Range["B1", "B5"].Value2 = 55;

    Microsoft.Office.Tools.Excel.Chart chart1 = Globals.Sheet1.Controls.AddChart(
        Globals.Sheet1.Range["D2", "H12"], "renameChartExample");
    chart1.SetSourceData(Globals.Sheet1.Range["A1", "B5"], Excel.XlRowCol.xlColumns);
    chart1.ChartType = Excel.XlChartType.xl3DColumn;

    // This message box displays "Sheet1 Chart 1".
    MessageBox.Show("The default chart name is: " + chart1.Name);

    // Set the name of the parent ChartObject of this embedded Chart.
    Excel.ChartObject chartObjectParent = chart1.Parent as Excel.ChartObject;
    chartObjectParent.Name = "SalesChart";

    // This message box displays "Sheet1 SalesChart".
    MessageBox.Show("The new chart name is: " + chart1.Name);
}
Private Sub RenameChart()
    ' Set some test data and create a new Chart.
    Globals.Sheet1.Range("A1", "A5").Value2 = 22
    Globals.Sheet1.Range("B1", "B5").Value2 = 55

    Dim Chart1 As Microsoft.Office.Tools.Excel.Chart = _
        Globals.Sheet1.Controls.AddChart(Globals.Sheet1.Range("D2", "H12"), _
        "renameChartExample")
    Chart1.SetSourceData(Me.Range("A1", "B5"), Excel.XlRowCol.xlColumns)
    Chart1.ChartType = Excel.XlChartType.xl3DColumn

    ' Displays "Sheet1 Chart 1".
    MessageBox.Show("The default chart name is: " & Chart1.Name)

    ' Set the name of the parent ChartObject of this embedded Chart.
    Dim ChartObjectParent As Excel.ChartObject = TryCast(Chart1.Parent, Excel.ChartObject)
    ChartObjectParent.Name = "SalesChart"

    ' Displays "Sheet1 SalesChart".
    MessageBox.Show("The default chart name is: " & Chart1.Name)
End Sub

Applies to