Partilhar via


Chart.SetDefaultChart Method

Specifies the name of the chart template that Microsoft Office Excel uses when it creates new charts.

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

Syntax

'Declaration
Sub SetDefaultChart ( _
    Name As Object _
)
void SetDefaultChart(
    Object Name
)

Parameters

  • Name
    Type: System.Object

    A string that indicates the name of the default chart template that will be used to create new charts. This name can identify a chart in the gallery for a user-defined template, or it can be one of the Microsoft.Office.Interop.Excel.XlChartType values that specifies a built-in chart template.

Examples

The following code example sets the default chart template to the line chart type. Next the example adds a new chart to the active worksheet and populates its source data from a specified range on the worksheet. To run this code example, your workbook must contain a worksheet named Sheet1 with a chart named Chart_1.

Private Sub SetDefaultLineChartTemplate()
    ' Set default chart template 
    Dim myChart As Microsoft.Office.Tools.Excel.Chart = _
        Globals.Sheet1.Chart_1
    myChart.SetDefaultChart(Excel.XlChartType.xlLine)

    ' Add a new chart and populate source data 
    Dim myNewChart As Microsoft.Office.Tools.Excel.Chart = _
        Globals.Sheet1.Controls.AddChart( _
        Globals.Sheet1.Range("D5", "J16"), "myNewChart")
    Globals.Sheet1.Range("A1").Value2 = "Product"
    Globals.Sheet1.Range("B1").Value2 = "Units Sold" 
    Dim i As Integer 
    For i = 1 To 3
        Globals.Sheet1.Range("A" + (i + 1).ToString()).Value2 = "Product" + i.ToString()
        Globals.Sheet1.Range("B" + (i + 1).ToString()).Value2 = i * 10
    Next 
    Dim data As Excel.Range = Globals.Sheet1.Range.Item("A1", "B4")
    myNewChart.SetSourceData(data)
End Sub
private void SetDefaultLineChartTemplate()
{
    // Set default chart template
    Microsoft.Office.Tools.Excel.Chart myChart =
        Globals.Sheet1.Chart_1;
    myChart.SetDefaultChart(Excel.XlChartType.xlLine);

    // Add a new chart and populate source data
    Microsoft.Office.Tools.Excel.Chart myNewChart = 
        Globals.Sheet1.Controls.AddChart(
            Globals.Sheet1.Range["D5","J16"],"myNewChart");                      
    Globals.Sheet1.Range["A1","A1"].Value2 = "Product";
    Globals.Sheet1.Range["B1","B1"].Value2 = "Units Sold";            
    for (int i = 1; i<4; i++)
    {
        Globals.Sheet1.Range["A" + (i + 1).ToString()].Value2 = "Product" + i.ToString();
        Globals.Sheet1.Range["B" + (i + 1).ToString()].Value2 = i * 10;
    }
    Excel.Range data = Globals.Sheet1.Range["A1", "B4"];
    myNewChart.SetSourceData(data);
}

.NET Framework Security

See Also

Reference

Chart Interface

Microsoft.Office.Tools.Excel Namespace