Microsoft KB Archive/319557

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

Article ID: 319557

Article Last Modified on 6/29/2007

-

APPLIES TO


 * Microsoft Office Web Components
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual Basic .NET 2003 Standard Edition

-



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
 * Step-by-Step Guide

REFERENCES



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

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

By default, Form1 creates, and then appears in Design view. On the View menu, click Toolbox. Drag the Chart component to Form1. Double-click Form1 to display the code window for Form1.  Add the following code at the top of Form1.vb: Imports OWC10 = Microsoft.Office.Interop.OWC   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 </li>  Add the following code to the &quot;Form1_Load&quot; 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 {&quot;A&quot;, &quot;B&quot;, &quot;C&quot;} Dim aVals As Object = New Object {100, 120, 128} oSer.SetData(OWC10.ChartDimensionsEnum.chDimSeriesNames, -1, &quot;MySeries&quot;) 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 </li>  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(&quot;AfterFinalRender Event&quot;)

' Draw two separate lines of custom text below the plot area and ' center the text with the chart space. Dim sText As String = &quot;Custom Chart Text&quot; 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 = &quot;Navy&quot; sText = &quot;Additional custom chart text that is a subtitle&quot; 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) = &quot;ChPlotArea&quot; Then Debug.WriteLine(&quot;BeforeRender Event - PlotArea&quot;) e.drawObject.Interior.SetTextured( _          OWC10.ChartPresetTextureEnum.chTextureBlueTissuePaper) e.drawObject.Border.Weight = 2 e.drawObject.Border.Color = &quot;Navy&quot; 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) = &quot;ChGridlines&quot; Then

Debug.WriteLine(&quot;AfterRender Event - Gridlines&quot;)

' 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 = &quot;Navy&quot; 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 = &quot;Navy&quot; Dim sText sText = &quot;Avg = &quot; & 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(&quot;AfterLayout Event&quot;)

' 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 </li> 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.</li></ol>

back to the top

<div class="references_section">