Customization and Events

In the Chart control, you can use events to customize chart behavior, such as perform custom drawing. The Chart controls for ASP.NET and Windows Forms expose different sets of events. For a complete list of events for each control, see System.Web.UI.DataVisualization.Charting.Chart and System.Windows.Forms.DataVisualization.Charting.Chart.

PrePaint and PostPaint

The PrePaint and PostPaint events are the most commonly used events. They are triggered before and after each chart element is drawn.

Note

These two events are not triggered for Series objects when 3D is enabled for a chart area.

To find out the chart element for which the current event is trigger, use the ChartPaintEventArgs.ChartElement property.

The following event handler draws six concentric circles after the Series element is drawn and emphasizes a data point named "Product F".

Private Sub chart1_PostPaint(ByVal sender As Object, ByVal e As ChartPaintEventArgs) 

   If TypeOf e.ChartElement Is Series Then 
      Dim series As Series = DirectCast(e.ChartElement, Series) 
      Dim position As System.Drawing.PointF = System.Drawing.PointF.Empty 

      ' Find data point with label "Product F". 
      For Each point As DataPoint In series.Points 
         position.X += 1 
         
         If point.AxisLabel = "Product F" Then 
            position.Y = CSng(point.YValues(0)) 
            Exit For 
         End If 
      Next 

      ' Get relative coordinates of the data point values found. 
      position.X = CSng(e.ChartGraphics.GetPositionFromAxis("Default", AxisName.X, position.X)) 
      position.Y = CSng(e.ChartGraphics.GetPositionFromAxis("Default", AxisName.Y, position.Y)) 
      
      ' Convert relative coordinates to absolute coordinates. 
      position = e.ChartGraphics.GetAbsolutePoint(position) 
      
      ' Draw custom object. 
      For radius As Integer = 20 To 79 Step 10 
         e.ChartGraphics.Graphics.DrawEllipse(System.Drawing.Pens.Red, position.X - radius / 2, _
            position.Y - radius / 2, radius, radius) 
      Next 
   End If 
End Sub
private void chart1_PostPaint(object sender, ChartPaintEventArgs e)
{
   if (e.ChartElement is Series)
   {
      Series series = (Series)e.ChartElement;
      System.Drawing.PointF position = System.Drawing.PointF.Empty;
      // Find data point with label "Product F".
      foreach (DataPoint point in series.Points)
      {
         ++position.X;

         if (point.AxisLabel == "Product F")
         {
            position.Y = (float)point.YValues[0];
            break;
         }
      }
      // Get relative coordinates of the data point values found.
      position.X = (float)e.ChartGraphics.GetPositionFromAxis("Default", AxisName.X, position.X);
      position.Y = (float)e.ChartGraphics.GetPositionFromAxis("Default", AxisName.Y, position.Y);

      // Convert relative coordinates to absolute coordinates.
      position = e.ChartGraphics.GetAbsolutePoint(position);

      // Draw custom object.
      for (int radius = 20; radius < 80; radius += 10)
      {
         e.ChartGraphics.Graphics.DrawEllipse(System.Drawing.Pens.Red, position.X - radius / 2, 
                                              position.Y - radius / 2, radius, radius);
      }
   }
}

See Also

Other Resources

Advanced Topics

Build Date:

2012-08-02