次の方法で共有


カスタマイズとイベント (グラフ コントロール)

グラフ コントロールでは、カスタムの描画を実行する方法など、グラフの動作をカスタマイズするイベントを使用できます。ASP.NET および Windows フォームのグラフ コントロールは、異なるイベントを公開しています。各コントロールのイベントの一覧については、「System.Web.UI.DataVisualization.Charting.Chart」および「System.Windows.Forms.DataVisualization.Charting.Chart」を参照してください。

PrePaint と PostPaint

PrePaint イベントと PostPaint イベントは、最も一般的に使用されるイベントです。これらのイベントは、それぞれ各グラフ要素の描画前および描画後にトリガーされます。

注意

グラフ領域で 3D を有効にしている場合、Series オブジェクトに対してこれら 2 つのイベントはトリガーされません。

現在のイベントがトリガーされるグラフ要素を検出するには、ChartPaintEventArgs.ChartElement プロパティを使用します。

次のイベント ハンドラーは、Series 要素の描画後に 6 個の同心円を描画し、"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);
      }
   }
}

関連項目

その他の技術情報

高度なトピック