共用方式為


教學課程:使用事件來自訂圖表

此教學課程示範如何使用事件來自訂圖表。您要在最大資料點加入自訂標記來反白顯示今年銷售量最高的銷售人員。

若要完成此教學課程,您必須先完成教學課程:將圖表資料繫結至資料庫中的教學課程

加入 Chart 命名空間

開始前,請在 ASP.NET 應用程式中開啟程式碼後置檔案,或在 Windows Form 應用程式中切換到程式碼檢視。

在程式碼檔案頂端加入 Chart 命名空間,如下所示。

' For Windows Forms
Imports System.Windows.Forms.DataVisualization.Charting
' For ASP.NET
Imports System.Web.UI.DataVisualization.Charting
// For Windows Forms
using System.Windows.Forms.DataVisualization.Charting;
// For ASP.NET
using System.Web.UI.DataVisualization.Charting;

加入 PostPaint 事件

加入 Chart.PostPaint 事件的事件處理常式。

在 Windows Form 中,您是在表單建構函式中執行此動作,就在 InitializeComponent 方法之後。在 ASP.NET 中,則是在自動建立的 Page_Load 事件處理常式方法中執行此動作。

AddHandler Me.Chart1.PostPaint, AddressOf Chart1_PostPaint
this.Chart1.PostPaint += new EventHandler<ChartPaintEventArgs>(Chart1_PostPaint);

接下來,為您剛登錄的事件處理常式加入方法定義,在類別定義中加入下列程式碼即可完成這個動作。

Private Sub Chart1_PostPaint(ByVal sender As Object, ByVal e As ChartPaintEventArgs) 
End Sub 
void Chart1_PostPaint(object sender, ChartPaintEventArgs e)
{
}

執行自訂繪製

現在要加入可以反白顯示今年銷售量最高之銷售人員的自訂程式碼。若要執行這個步驟,請將下列程式碼加入到您剛建立的 Chart1_PostPaint 事件處理常式,儲存檔案,然後執行應用程式。

' Make sure all series have been drawn before proceeding 
If TypeOf e.ChartElement Is Series AndAlso DirectCast(e.ChartElement, Series).Name = "Series2" Then 
   Dim s As Series = e.Chart.Series(0) 
   Dim cg As ChartGraphics = e.ChartGraphics 
   Dim max As Double = s.Points.FindMaxByValue().YValues(0) 
   
   ' Highlight the maximum sales this year 
   For i As Integer = 0 To s.Points.Count - 1 
      If s.Points(i).YValues(0) = max Then 
         ' Get relative coordinates of the data point 
         Dim pos As System.Drawing.PointF = System.Drawing.PointF.Empty 
         pos.X = CSng(cg.GetPositionFromAxis("ChartArea1", AxisName.X, i+1)) 
         pos.Y = CSng(cg.GetPositionFromAxis("ChartArea1", AxisName.Y, max)) 
         
         ' Convert relative coordinates to absolute coordinates. 
         pos = cg.GetAbsolutePoint(pos) 
         
         ' Draw concentric circles at the data point 
         For radius As Integer = 10 To 39 Step 10 
            cg.Graphics.DrawEllipse(System.Drawing.Pens.Red, _
               CSng(pos.X - radius / 2), _
               CSng(pos.Y - radius / 2), radius, radius) 
         Next 
      End If 
   Next 
End If 
// Make sure all series have been drawn before proceeding
if (e.ChartElement is Series && ((Series)e.ChartElement).Name == "Series2" )
{
    Series s = e.Chart.Series[0];
    ChartGraphics cg = e.ChartGraphics;
    double max = s.Points.FindMaxByValue().YValues[0];

   // Highlight the maximum sales this year
   for(int i = 0; i < s.Points.Count; i++)
   {
      if(s.Points[i].YValues[0] == max)
      {
         // Get relative coordinates of the data point
         System.Drawing.PointF pos = System.Drawing.PointF.Empty;
         pos.X = (float)cg.GetPositionFromAxis("ChartArea1", AxisName.X, i);
         pos.Y = (float)cg.GetPositionFromAxis("ChartArea1", AxisName.Y, max);

         // Convert relative coordinates to absolute coordinates.
         pos = cg.GetAbsolutePoint(pos);

         // Draw concentric circles at the data point
         for (int radius = 10; radius < 40; radius += 10)
         {
            cg.Graphics.DrawEllipse(
               System.Drawing.Pens.Red, 
               pos.X - radius / 2, 
               pos.Y - radius / 2, 
               radius, radius);
         }
      }
   }
}

請參閱

參考

System.Windows.Forms.DataVisualization.Charting

System.Web.UI.DataVisualization.Charting

概念

自訂和事件

其他資源

使用者入門