Chart Interface
Represents a chart in a workbook. The chart can be either an embedded chart (contained in a ChartObject) or a separate chart sheet.
Assembly: Microsoft.Office.Interop.Excel (in Microsoft.Office.Interop.Excel.dll)
The following properties for returning a Chart object are described in this section:
-
Chart property
-
Charts property
-
ActiveChart property
-
ActiveSheet property
Chart Property
Use the Chart property to return a Chart object that represents the chart contained in a ChartObject object.
Charts Property
The Charts collection contains a Chart object for each chart sheet in a workbook. Use Charts(index), where index is the chart-sheet index number or name, to return a single Chart object.
The chart index number represents the position of the chart sheet on the workbook tab bar. All chart sheets are included in the index count, even if they’re hidden. The chart sheet name is shown on the workbook tab for the chart. You can use the Name property to set or return the chart name.
The Chart object is also a member of the Sheets collection. The Sheets collection contains all the sheets in the workbook (both chart sheets and worksheets). Use Sheets(index), where index is the sheet index number or name, to return a single sheet.
ActiveChart Property
When a chart is the active object, you can use the ActiveChart property to refer to it. A chart sheet is active if the user has selected it or it’s been activated with the Activate method.
An embedded chart is active if the user has selected it or the ChartObject object that it’s contained in has been activated with the Activate method.
ActiveSheet Property
When a chart sheet is the active sheet, you can use the ActiveSheet property to refer to it.
Option Infer Off
Option Explicit On
Imports Microsoft.Office.Interop.Excel
Public Class Form1
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'VB 2010 Office 2010
'This uses shape to institude chart and adds a lot of customization
'In Project>References Add "Microsoft Excel 12.0 Object Library" Then put check in "Imported namespaces" > "Microsoft.Office.Interop
'
'This uses chart wizard to instantiate and format chart
'On Form1 place a Button2 control
'Open a new Excel Workbook - make Sheet1 ActiveSheet
'Add "Imports Microsoft.Office.Interop.Excel" to initialization
Dim Source As Object
Dim misValue As Object = System.Reflection.Missing.Value
Dim xlAPP As Object
Dim WBook As Microsoft.Office.Interop.Excel.Workbook
Dim wsheet As Microsoft.Office.Interop.Excel.Worksheet
Dim QChart As Microsoft.Office.Interop.Excel.Chart
xlAPP = GetObject(, "Excel.Application")
WBook = xlAPP.activeworkbook
wsheet = xlAPP.activesheet
'populate Excel
wsheet.Cells("1", "A") = "Flow"
wsheet.Cells("2", "A") = 124
wsheet.Cells("3", "A") = 180
wsheet.Cells("4", "A") = 235
wsheet.Cells("5", "A") = 278
wsheet.Cells("1", "B") = "Flow 2"
wsheet.Cells("2", "B") = 20
wsheet.Cells("3", "B") = 20
wsheet.Cells("4", "B") = 20
wsheet.Cells("5", "B") = 20
wsheet.Cells("1", "C") = "Q"
wsheet.Cells("2", "C") = 5.5
wsheet.Cells("3", "C") = 7
wsheet.Cells("4", "C") = 8
wsheet.Cells("5", "C") = 8.8
wsheet.Cells("1", "D") = "D"
wsheet.Cells("2", "D") = 19
wsheet.Cells("3", "D") = 36
wsheet.Cells("4", "D") = 56
wsheet.Cells("5", "D") = 74
xlAPP = GetObject(, "Excel.Application")
WBook = xlAPP.activeworkbook
wsheet = xlAPP.activesheet
Dim shape1 As Microsoft.Office.Interop.Excel.Shape = wsheet.Shapes.AddChart()
QChart = shape1.Chart
QChart.ChartType = Microsoft.Office.Interop.Excel.XlChartType.xlXYScatterSmooth
Source = wsheet.Range("A1:A5") 'dummy data to establish chart
QChart.SetSourceData(Source)
QChart.PlotBy = Microsoft.Office.Interop.Excel.XlRowCol.xlColumns
QChart.SeriesCollection(1).xvalues = "Sheet1!$A$2:$A$5"
QChart.SeriesCollection(1).values = "Sheet1!$C$2:$C$7"
'add a new curve to chart
wsheet.Range("D2:D5").Copy()
QChart.Paste()
QChart.SeriesCollection(1).name = "Q kW"
QChart.SeriesCollection(2).name = "Air DP"
QChart.SeriesCollection(2).AxisGroup = 2
QChart.HasTitle = True
QChart.ChartTitle.Caption = "Heat Transfer vs. Air Flow"
QChart.Axes(Excel.XlAxisType.xlCategory, Excel.XlAxisGroup.xlPrimary).HasMajorGridlines = True
QChart.Axes(Excel.XlAxisType.xlCategory, Excel.XlAxisGroup.xlPrimary).HasMinorGridlines = True
QChart.Axes(Excel.XlAxisType.xlValue, Excel.XlAxisGroup.xlPrimary).HasMajorGridlines = True
QChart.Axes(Excel.XlAxisType.xlValue, Excel.XlAxisGroup.xlPrimary).HasMinorGridlines = True
QChart.Axes(Excel.XlAxisType.xlValue, Excel.XlAxisGroup.xlSecondary).MaximumScale = 100
QChart.Axes(Excel.XlAxisType.xlCategory, Excel.XlAxisGroup.xlPrimary).HasTitle = True
QChart.Axes(Excel.XlAxisType.xlCategory, Excel.XlAxisGroup.xlPrimary).AxisTitle.Text = "Air Flow - L/sec"
QChart.Axes(Excel.XlAxisType.xlValue, Excel.XlAxisGroup.xlPrimary).HasTitle = True
QChart.Axes(Excel.XlAxisType.xlValue, Excel.XlAxisGroup.xlPrimary).AxisTitle.Text = "Heat Transfer - kW"
QChart.Axes(Excel.XlAxisType.xlValue, Excel.XlAxisGroup.xlSecondary).HasTitle = True
QChart.Axes(Excel.XlAxisType.xlValue, Excel.XlAxisGroup.xlSecondary).AxisTitle.Text = "Air Pressure Drop - Pa"
QChart.Location(Where:=XlChartLocation.xlLocationAsNewSheet)
xlAPP = Nothing
WBook = Nothing
wsheet = Nothing
QChart = Nothing
End Sub
End Class
- 9/12/2011
- RadiatorBob
- 11/22/2011
- mahios