Export (0) Print
Expand All

How to: Add Chart Controls to Worksheets

You can add Chart controls to a Microsoft Office Excel worksheet at design time and at run time in document-level customizations. You can also add Chart controls at run time in application-level add-ins.

Applies to: The information in this topic applies to document-level projects and application-level projects for Excel 2007 and Excel 2010. For more information, see Features Available by Office Application and Project Type.

This topic describes the following tasks:

For more information about Chart controls, see Chart Control.

You can add the Chart control to your worksheet in the same manner you would add a chart from within the application.

NoteNote

The Chart control is not available from the Toolbox or the Data Sources window.

To add a Chart host control to a worksheet in Excel

  1. On the Insert tab, in the Charts group, click Column, click a category of charts, and then click the type of chart you want.

  2. In the Insert Chart dialog box, click OK.

  3. On the Design tab, in the Data group, click Select Data.

  4. In the Select Data Source dialog box, click in the Chart data range box and clear any default selection.

  5. In the Data for Chart sheet, select the range of cells that contains the data for the chart (cells A5 through D8).

  6. In the Select Data Source dialog box, click OK.

You can add the Chart control dynamically at run time. Dynamically created charts are not persisted in the document as host controls when the document is closed. For more information, see Adding Controls to Office Documents at Run Time.

To add a Chart control to a worksheet programmatically

  • In the Startup event handler of Sheet1, insert the following code to add the Chart control.

    
    Microsoft.Office.Tools.Excel.Chart employeeData;
    employeeData = this.Controls.AddChart(25, 110, 200, 150, "employees");
    employeeData.ChartType = Excel.XlChartType.xl3DPie;
    
    // Gets the cells that define the data to be charted.
    Excel.Range chartRange = this.get_Range("A5", "D8");
    employeeData.SetSourceData(chartRange, missing);
    
    
    

You can add a Chart control programmatically to any open worksheet in an application-level add-in project. For more information, see Extending Word Documents and Excel Workbooks in Application-Level Add-ins at Run Time.

Dynamically created chart controls are not persisted in the worksheet as host controls when the worksheet is closed. For more information, see Adding Controls to Office Documents at Run Time.

To add a Chart control to a worksheet programmatically

  • The following code generates a worksheet host item that is based on the open worksheet, and then adds a Chart control.

    
    private void AddChart()
    {
        // Use the following line of code in projects that target the .NET Framework 4.
        Worksheet worksheet = Globals.Factory.GetVstoObject(
            Globals.ThisAddIn.Application.ActiveWorkbook.ActiveSheet);
    
        // In projects that target the .NET Framework 3.5, use the following line of code.
        // Worksheet worksheet = 
        //     ((Excel.Worksheet)Globals.ThisAddIn.Application.ActiveWorkbook.ActiveSheet).GetVstoObject();
    
        Excel.Range cells = worksheet.Range["A5", "D8"];
        Chart chart = worksheet.Controls.AddChart(cells, "employees");
        chart.ChartType = Microsoft.Office.Interop.Excel.XlChartType.xl3DPie;
        chart.SetSourceData(cells, missing);
    }       
    
    
    

This example has the following requirements:

  • Data to be charted, stored in the range from A5 to D8 in the worksheet.

Community Additions

ADD
Show:
© 2014 Microsoft