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.
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
(http://support.microsoft.com/kb/328275/EN-US/
)
HOW TO: Handle Events for the Office Web Components in Visual Studio .NET
Create a new Visual C# .NET Windows Application project. Name the project ChartEvents.
By default, Form1 creates, and then opens in Design view.
On the View menu, click Toolbox.
Drag the Chart component from the Toolbox to Form1.
Double-click Form1 to view the code module and to set up the Load event handler for the form.
Add the following lines of code before the NAMESPACE statement in the Form1.cs file:
using OWC10 = Microsoft.Office.Interop.OWC;
using System.Diagnostics;
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;
Add the following code to the Load event for the form:
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);
}
}
Press F5 to build and to run the sample.
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.
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
(http://support.microsoft.com/kb/319341/EN-US/
)
HOW TO: Handle Events for the Office XP Spreadsheet Component on a Windows Form in Visual C# .NET