HOW TO: Handle Events for the Office XP Chart Component by Using Visual Basic .NET (319557)



The information in this article applies to:

  • Microsoft Office XP Web Components
  • Microsoft Visual Basic .NET (2002)
  • Microsoft Visual Basic .NET (2003)

This article was previously published under Q319557
For a Microsoft Visual C# version of this article, see 319559.
For a Microsoft Visual Basic 6.0 version of this article, see 235885.

IN THIS TASK

SUMMARY

This step-by-step guide demonstrates how you can use Visual Basic .NET to handle events for an Office XP Chart component on a Windows Form. The code that is used in this step-by-step guide shows how you can use events to add custom layout and drawing to an Office XP chart.

back to the top

Step-by-Step Guide

Before you start the following steps, you must modify the class wrappers that Visual Studio .NET generates for the Office XP Web Components (OWC). Modification of the class wrappers is required for Visual Basic .NET to properly handle OWC events. For additional information, click the article number below to view the article in the Microsoft Knowledge Base:

328275 HOW TO: Handle Events for the Office Web Components in Visual Studio .NET

  1. Create a new Visual Basic Windows Application project, and then name the project ChartEvents.

    By default, Form1 creates, and then appears in Design view.
  2. On the View menu, click Toolbox.
  3. Drag the Chart component to Form1.
  4. Double-click Form1 to display the code window for Form1.
  5. Add the following code at the top of Form1.vb:
    Imports OWC10 = Microsoft.Office.Interop.OWC
    					
  6. Add the following private member variables to the Form1 class:
    Private m_Chart As OWC10.ChChart
    
    ' Chart dimensions.
    Private m_nPlotTop As Long
    Private m_nPlotBottom As Long
    Private m_nPlotRight As Long
    Private m_nPlotLeft As Long
    					
  7. Add the following code to the "Form1_Load" section:
    ' Create one chart in the Chartspace by using literal data.
    m_Chart = AxChartSpace1.Charts.Add
    Dim oSer As OWC10.ChSeries = m_Chart.SeriesCollection.Add
    Dim aCats As Object() = New Object() {"A", "B", "C"}
    Dim aVals As Object() = New Object() {100, 120, 128}
    oSer.SetData(OWC10.ChartDimensionsEnum.chDimSeriesNames, -1, "MySeries")
    oSer.SetData(OWC10.ChartDimensionsEnum.chDimCategories, -1, aCats)
    oSer.SetData(OWC10.ChartDimensionsEnum.chDimValues, -1, aVals)
    oSer.Interior.SetTextured(OWC10.ChartPresetTextureEnum.chTextureDenim)
    m_Chart.PlotArea.Interior.Color = OWC10.ChartColorIndexEnum.chColorNone
    m_Chart.PlotArea.Border.Color = OWC10.ChartColorIndexEnum.chColorNone
    
    ' Turn on render and layout events.
    AxChartSpace1.AllowLayoutEvents = True
    AxChartSpace1.AllowRenderEvents = True
  8. Add the following code to the AfterFinalRender, AfterLayout, AfterRender, and BeforeRender event handlers in Form1.vb:
    Private Sub AxChartSpace1_AfterFinalRender(ByVal sender As Object, ByVal _
      e As AxMicrosoft.Office.Interop.OWC.IChartEvents_AfterFinalRenderEvent) _
      Handles AxChartSpace1.AfterFinalRender
    
        Debug.WriteLine("AfterFinalRender Event")
    
        ' Draw two separate lines of custom text below the plot area and 
        ' center the text with the chart space.
        Dim sText As String = "Custom Chart Text"
        e.drawObject.Font.Size = 16
        e.drawObject.Font.Bold = True
        e.drawObject.DrawText(sText, _
            (m_Chart.Right - m_Chart.Left) / 2 - e.drawObject.TextWidth( _
            sText) / 2, m_nPlotBottom + 40)
        e.drawObject.Font.Size = 9
        e.drawObject.Font.Bold = False
        e.drawObject.Font.Color = "Navy"
        sText = "Additional custom chart text that is a subtitle"
        e.drawObject.DrawText(sText, _
            (m_Chart.Right - m_Chart.Left) / 2 - e.drawObject.TextWidth( _
            sText) / 2, m_nPlotBottom + 70)
    
    End Sub
    
    Private Sub AxChartSpace1_BeforeRender(ByVal sender As Object, ByVal _
       e As AxMicrosoft.Office.Interop.OWC.IChartEvents_BeforeRenderEvent) _
       Handles AxChartSpace1.BeforeRender
    
        ' Draw a textured rectangle for the backdrop of the chart before 
        ' you render the plot area.
        If TypeName(e.chartObject) = "ChPlotArea" Then
            Debug.WriteLine("BeforeRender Event - PlotArea")
            e.drawObject.Interior.SetTextured( _
               OWC10.ChartPresetTextureEnum.chTextureBlueTissuePaper)
            e.drawObject.Border.Weight = 2
            e.drawObject.Border.Color = "Navy"
            e.drawObject.DrawRectangle(10, 10, _
               m_Chart.Right - 10, m_Chart.Axes( _
               OWC10.ChartAxisPositionEnum.chAxisPositionBottom).Bottom)
        End If
    
    End Sub
    
    Private Sub AxChartSpace1_AfterRender(ByVal sender As Object, ByVal _
       e As AxMicrosoft.Office.Interop.OWC.IChartEvents_AfterRenderEvent) _
       Handles AxChartSpace1.AfterRender
    
        ' After the gridlines are rendered, draw a line in the plot area that 
        ' represents the average of the data points. NOTE: The line is drawn 
        ' after the gridlines but before the data points are drawn so that the 
        ' line appears behind the data points.
    
        If TypeName(e.chartObject) = "ChGridlines" Then
    
            Debug.WriteLine("AfterRender Event - Gridlines")
    
            ' Compute the average value for the first series.
            Dim nAvg As Double, i As Integer
            Dim oSer As OWC10.ChSeries = m_Chart.SeriesCollection(0)
            For i = 0 To oSer.Points.Count - 1
                nAvg = nAvg + oSer.Points(i).GetValue( _
                   OWC10.ChartDimensionsEnum.chDimValues)
            Next
            nAvg = nAvg / oSer.Points.Count
    
            Dim nIncrement As Double, nAvgLineY As Double
            nIncrement = (m_nPlotBottom - m_nPlotTop) / ( _
             m_Chart.Scalings(OWC10.ChartDimensionsEnum.chDimValues).Maximum _
             - m_Chart.Scalings(OWC10.ChartDimensionsEnum.chDimValues).Minimum)
            nAvgLineY = m_nPlotBottom - (nIncrement * nAvg)
            e.drawObject.Line.DashStyle = _
               OWC10.ChartLineDashStyleEnum.chLineDashDot
            e.drawObject.Line.Color = "Navy"
            e.drawObject.Line.Weight = 4
            e.drawObject.DrawLine(m_nPlotLeft + 1, nAvgLineY, _
               m_nPlotRight - 1, nAvgLineY)
    
            ' Add text at the right of the drawn line to display the value it 
            ' represents.
            e.drawObject.Font.Color = "Navy"
            Dim sText
            sText = "Avg = " & CLng(nAvg)
            e.drawObject.DrawText(sText, m_nPlotRight + 10, _
               nAvgLineY - e.drawObject.TextHeight(sText) / 2)
    
        End If
    
    End Sub
    
    Private Sub AxChartSpace1_AfterLayout(ByVal sender As Object, ByVal _
       e As AxMicrosoft.Office.Interop.OWC.IChartEvents_AfterLayoutEvent) _
       Handles AxChartSpace1.AfterLayout
    
        Debug.WriteLine("AfterLayout Event")
    
        ' Resize the chart's plot area to provide room at the top, bottom, and
        ' right sides of the chart for custom-drawn shapes and text. Store 
        ' those new dimensions in member variables.
        m_nPlotTop = m_Chart.PlotArea.Top + 50
        m_Chart.PlotArea.Top = m_nPlotTop
        m_nPlotBottom = m_Chart.PlotArea.Bottom - 80
        m_Chart.PlotArea.Bottom = m_nPlotBottom
        m_nPlotRight = m_Chart.PlotArea.Right - 100
        m_Chart.PlotArea.Right = m_nPlotRight
        m_nPlotLeft = m_Chart.PlotArea.Left + 20
        m_Chart.PlotArea.Left = m_nPlotLeft
    
    End Sub 
    					
  9. Press F5 to build and to run the sample.

    When Form1 appears, you see that the chart layout has been customized. Lines, shapes, and text are drawn on the chart. This customization occurs during render and layout events for the chart.
back to the top

REFERENCES

For additional information, visit the following Microsoft Web site:

Microsoft Office Development with Visual Studio
http://msdn.microsoft.com/library/en-us/dnoxpta/html/vsofficedev.asp

For additional information about how to use custom layout and drawing with an Office XP Chart Component on a Web page (including a code sample), click the article number below to view the article in the Microsoft Knowledge Base:

290348 HOWTO: Use Custom Layout and Drawing with the Office XP Chart Component

back to the top

Modification Type:MajorLast Reviewed:1/19/2006
Keywords:kbHOWTOmaster kbOfficeWebChart KB319557 kbAudDeveloper