Microsoft KB Archive/319559

From BetaArchive Wiki
Knowledge Base


HOW TO: Handle Events for the Office XP Chart Component on a Windows Form in Visual C# .NET

Article ID: 319559

Article Last Modified on 6/29/2007



APPLIES TO

  • Microsoft Office Web Components
  • Microsoft Visual C# .NET 2002 Standard Edition



This article was previously published under Q319559

For a Microsoft Visual Basic .NET version of this article, see 319557.

IN THIS TASK

SUMMARY

REFERENCES

SUMMARY

This step-by-step guide shows you how to use Visual C# .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 demonstrates 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 C# .NET Windows Application project. Name the project ChartEvents.

    By default, Form1 creates, and then opens in Design view.
  2. On the View menu, click Toolbox.
  3. Drag the Chart component from the Toolbox to Form1.
  4. Double-click Form1 to view the code module and to set up the Load event handler for the form.
  5. Add the following lines of code before the NAMESPACE statement in the Form1.cs file:

    using OWC10 = Microsoft.Office.Interop.OWC;
    using System.Diagnostics;
                        
  6. Add the following private member variables to the Form1 class:

    private OWC10.ChChart m_Chart;
    private int m_nPlotTop;
    private int m_nPlotBottom;
    private int m_nPlotRight;
    private int m_nPlotLeft;
    private bool m_bCustomLineDone = false;
                        
  7. Add the following code to the Load event for the form:

    //===== Build the Chart. ======
    m_Chart = axChartSpace1.Charts.Add(0);
    OWC10.ChSeries oSer  = m_Chart.SeriesCollection.Add(0);
    object aCats = new object[] {"A", "B", "C"};
    object aVals = 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,
       OWC10.ChartTextureFormatEnum.chTile, 0, 
       OWC10.ChartTexturePlacementEnum.chAllFaces);
    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, the AfterLayout, the AfterRender, and the BeforeRender events:

    private void AfterFinalRender(object sender, 
    AxMicrosoft.Office.Interop.OWC.IChartEvents_AfterFinalRenderEvent e)
    {
     Debug.WriteLine("AfterFinalRender Event");
     
     //Draw two separate lines of custom text below the plot area and 
     //center the text with the chart space.
     string sText = "Custom Chart Text";
     e.drawObject.Font.Size = 16;
     e.drawObject.Font.Bold = true;
     e.drawObject.DrawText(sText, (m_Chart.Right - m_Chart.Left) / 
               2 - (int)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 - (int)e.drawObject.TextWidth(sText) / 2, m_nPlotBottom + 70);
    }
     
    private void AfterLayout(object sender, 
    AxMicrosoft.Office.Interop.OWC.IChartEvents_AfterLayoutEvent e)
    {
     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;
     
    }
     
    private void AfterRender(object sender, 
    AxMicrosoft.Office.Interop.OWC.IChartEvents_AfterRenderEvent e)
    {
     //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(m_bCustomLineDone) return;
     
     if((e.chartObject as OWC10.ChGridlines)!=null)
     {
      OWC10.ChGridlines g = (OWC10.ChGridlines) e.chartObject;
     
      Debug.WriteLine("AfterRender Event - Gridlines");
     
      // Compute the average value for the first series.
      double nAvg=0;
      double i=0;
      OWC10.ChSeries oSer = m_Chart.SeriesCollection[0];
      for (i=0;i<=oSer.Points.Count - 1;i++)
      {
       double d = (double)oSer.Points[i].GetValue(
                               OWC10.ChartDimensionsEnum.chDimValues, false);
       nAvg = nAvg + (int)d;
      }
      nAvg = nAvg / oSer.Points.Count;
     
      double nIncrement=0;
      double nAvgLineY=0;
      nIncrement = (double)(m_nPlotBottom - m_nPlotTop) / 
                       (m_Chart.get_Scalings(
                       OWC10.ChartDimensionsEnum.chDimValues).Maximum - 
                        m_Chart.get_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.set_Weight(
                       OWC10.LineWeightEnum.owcLineWeightThick);
      e.drawObject.DrawLine(m_nPlotLeft + 1, (int)nAvgLineY, 
                       m_nPlotRight - 1, (int)nAvgLineY);
     
      // Add text at the right of the drawn line to display the 
      // value it represents.
      e.drawObject.Font.Color = "Navy";
      string sText;
      sText = "Avg = " + nAvg.ToString();
      e.drawObject.DrawText(sText, m_nPlotRight + 10, 
                       (int)nAvgLineY - (int)(e.drawObject.TextHeight(
                       sText)) / 2);
     
      m_bCustomLineDone=true;
     }
    }
     
    private void BeforeRender(object sender, 
    AxMicrosoft.Office.Interop.OWC.IChartEvents_BeforeRenderEvent e)
    {
     // Draw a textured rectangle for the backdrop of the chart before 
     // rendering the plot area.
     if ((e.chartObject as OWC10.ChPlotArea)!=null)
     {
               Debug.WriteLine("BeforeRender Event - PlotArea");
     
               e.drawObject.Interior.SetTextured(
               OWC10.ChartPresetTextureEnum.chTextureBlueTissuePaper,
        OWC10.ChartTextureFormatEnum.chTile, 0, 
               OWC10.ChartTexturePlacementEnum.chAllFaces);
        e.drawObject.Border.set_Weight(
                   OWC10.LineWeightEnum.owcLineWeightThin);
        e.drawObject.Border.Color = "Navy";
        e.drawObject.DrawRectangle(10, 10, m_Chart.Right - 10, 
                  m_Chart.Axes[
                  OWC10.ChartAxisPositionEnum.chAxisPositionBottom].Bottom);
     }
    }
                        
  9. Press F5 to build and to run the sample.
  10. When Form1 appears, notice that the chart layout has been customized. Lines, shapes, and text are drawn on the chart. This customization occurs during the render and the layout events for the chart.

back to the top

REFERENCES

For additional information about the Office Web Components, visit the following Microsoft Web site:

Microsoft Office Development with Visual Studio
http://msdn2.microsoft.com/en-us/library/aa188489(office.10).aspx


For additional information about handling Office XP Spreadsheet component events with Visual C#, click the article number below to view the article in the Microsoft Knowledge Base:

319341 HOW TO: Handle Events for the Office XP Spreadsheet Component on a Windows Form in Visual C# .NET


back to the top


Additional query words: advise unadvise connectionpoint connection point trigger fired fires handler webchart owc owc10

Keywords: kbhowto kbhowtomaster kbofficewebchart KB319559