共用方式為


自訂和事件

在圖表控制項中,您可以使用事件自訂圖表行為,例如執行自訂繪圖。適用於 ASP.NET 和 Windows Form 的圖表控制項會公開不同的一組事件。如需各控制項的完整事件清單,請參閱 System.Web.UI.DataVisualization.Charting.ChartSystem.Windows.Forms.DataVisualization.Charting.Chart

PrePaint 和 PostPaint

PrePaintPostPaint 事件是最常使用的事件。這些事件會在繪製每個圖表項目之前和之後觸發。

[!附註]

當圖表區域啟用 3D 時,則不會觸發 Series 物件的這兩個事件。

若要找出觸發目前事件的圖表項目,請使用 ChartPaintEventArgs.ChartElement 屬性。

下列事件處理常式會在繪製 Series 項目後繪製六個同心圓,並強調名為 "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);
      }
   }
}

請參閱

其他資源

進階主題