次の方法で共有


グラフ コントロールでのデータのフィルター処理

データのフィルター処理を実行すると、フィルター基準に基づいて、系列からデータ ポイントが削除されるか、データ ポイントが空としてマークされます。データを操作するときは、フィルター操作によって元の系列データが変更される可能性や、出力が出力系列に格納される可能性があることに留意してください。

注意

複数の系列をフィルター処理するときは、すべての系列が整列されていることを確認してください。整列されていない場合、フィルター処理メソッドによって例外がスローされます。詳細については、「データの整列」を参照してください。

データのフィルター処理

フィルター処理には、DataManipulator クラスの次のプロパティとメソッドを使用します。

  • FilterSetEmptyPoints プロパティ
    系列からデータ ポイントを削除するか、空のポイントとしてマークするかを指定します。

  • FilterMatchedPoints プロパティ
    基準に一致するデータ ポイントを削除するかどうかを指定します。false に設定すると、フィルター基準に一致しないポイントがフィルター処理されます。

    このプロパティは、Filter メソッドにのみ適用されます。

  • Filter メソッド
    日付の範囲、時刻の範囲、数値に対するデータ ポイント値の比較、またはカスタムのユーザー定義基準を使用して、系列のデータ ポイントをフィルター処理します。

  • FilterTopN メソッド
    系列内で最大値または最小値を持つデータ ポイントを除き、系列のデータ ポイントをフィルター処理します。

日付の範囲または時刻の範囲によるフィルター処理

データ ポイントの X 値が日付の場合 (系列の Series.XValueType プロパティが DateTime に設定されている場合)、日付または時刻の範囲を指定してフィルター処理するには、Filter メソッドを使用します。系列データは、範囲の定義済み要素をフィルター処理する範囲に分割されます。日付の範囲を定義するには、次の 2 つのパラメーターを指定します。

  • DateRangeType クラスの範囲の種類。

  • 範囲の要素を含む文字列。この文字列には、コンマとダッシュを使用できます。たとえば、"1-10, 20, 25" などです。

次のコードでは、"MySeries" という系列から、すべての週末のデータ ポイントをフィルター処理し、月の最初の日を除きすべてのデータ ポイントを削除します。

With Chart1.DataManipulator
        ' Remove weekends.
          .Filter(DateRangeType.DayOfWeek, "0,6", "MySeries")
        
        ' Remove all days of month except of the first. Our 
        ' criteria is the first of each month, and we are 
        ' filtering points that DO NOT match the criteria.
          .FilterMatchedPoints = False
          .Filter(DateRangeType.DayOfMonth, "1", "MySeries")
End With
DataManipulator myDataManip = Chart1.DataManipulator;
// Remove weekends.
  myDataManip.Filter(DateRangeType.DayOfWeek, "0,6", "MySeries");

// Remove all days of month except of the first. Our 
// criteria is the first of each month, and we are 
// filtering points that DO NOT match the criteria.
  myDataManip.FilterMatchedPoints = false;
  myDataManip.Filter(DateRangeType.DayOfMonth, "1", "MySeries");

両極の値を除くすべてのデータのフィルター処理

最大値または最小値を持つ、指定された数のポイントを除き、系列からすべてのポイントをフィルター処理するには、FilterTopN メソッドを使用します。このメソッドを使用するには、次の基準を指定します。

  • 保持する合計ポイント数。

  • フィルターする Y 値。たとえば、"Y2" などです。既定値は最初の Y 値 ("Y") です。

  • 最高値を取得するかどうか。False に設定すると、FilterTopN で最小値が保持されます。既定値は、True です。

次のコードは、FilterTopN メソッドの使用例です。

With Chart1.DataManipulator
        ' Get the top 10 sales persons, and overwrite the
        ' original series data with the new data.
        ' We assume the first Y value of the points stores
        ' the sales values.
        .FilterTopN(10, "MySeries")
        
        ' Get the 5 points with the smallest X values. 
        ' The filtered data is stored in an output series.
        .FilterTopN(5, "MySeries", "ResultSeries", "X", False)
End With
DataManipulator myDataManip = Chart1.DataManipulator;
// Get the top 10 sales persons, and overwrite the
// original series data with the new data.
// We assume the first Y value of the points stores
// the sales values.
myDataManip.FilterTopN(10, "MySeries"); 

// Get the 5 points with the smallest X values. 
// The filtered data is stored in an output series.
myDataManip.FilterTopN(5, "MySeries", "ResultSeries", "X", false); 

値によるフィルター処理

値の比較によってフィルター処理するには、Filter メソッドを使用します。次のパラメーターを指定します。

  • CompareMethod クラスの比較メソッド。

  • 比較対象の定数値。

  • フィルターする Y 値。たとえば、"Y2" などです。既定値は最初の Y 値 ("Y") です。

次のコードは、Y 値を定数と比較してポイントをフィルター処理する方法の例です。

With Chart1.DataManipulator
    ' Filtered points are only marked as empty.
    .FilterSetEmptyPoints = True
    ' Filters all points where the first Y value is greater than 100. 
    ' The input series is overwritten with the filtered data.    
    .Filter(CompareMethod.More, 100, "MySeries")
    ' Filters all points where the X value is less than, or equal to, a specific date.    
    ' The resulting data is stored in an output series, preserving the original data.    
    .Filter(CompareMethod.LessOrEqual, DateTime.Parse("1/1/2001").ToOADate(), "MySeries", "ResultSeries", "X")
End With
DataManipulator myDataManip = Chart1.DataManipulator;

// Filtered points are only marked as empty.
myDataManip.FilterSetEmptyPoints = true;

// Filters all points where the first Y value is greater than 100. 
// The input series is overwritten with the filtered data.    
myDataManip.Filter(CompareMethod.More, 100, "MySeries");

// Filters all points where the X value is less than, or equal to, a specific date.    
// The resulting data is stored in an output series, preserving the original data.    
myDataManip.Filter(CompareMethod.LessOrEqual, DateTime.Parse("1/1/2001").ToOADate(), "MySeries", "ResultSeries", "X");

カスタム基準によるフィルター処理

カスタム基準を定義するには、IDataPointFilter インターフェイスを使用します。このインターフェイスは FilterDataPoint メソッドを公開しています。このメソッドによって削除対象のデータ ポイントを決定します。次に、FilterDataPoint メソッドのパラメーターと戻り値を示します。

  • フィルター処理されるデータ ポイント オブジェクト。

  • ポイントが属する系列。

  • Series.Points コレクション オブジェクト内のデータ ポイントのインデックス。

  • ポイントがフィルター処理される場合は True を返します。それ以外の場合は False を返します。

次のコードは、ユーザー定義基準でポイントをフィルターする方法の例です。

' Filters points using custom criteria.
Dim filter As New MyPointFilter()
Chart1.DataManipulator.Filter(filter, "MySeries")

' User defined filtering criteria. Filters all points with 
' Y values greater than 100 or less than 10.
Public Class MyPointFilter  Implements IDataPointFilter
    Private Function FilterDataPoints(ByVal point As DataPoint, ByVal series As Series, ByVal pointIndex As Int32) _
      As Boolean Implements IDataPointFilter.FilterDataPoint
      
      If point.YValues(0) > 100.0 Or point.YValues(0) < 10.0 Then
            FilterDataPoints = True
        Else
            FilterDataPoints = False   
        End If          
    End Function
End Class
MyPointFilter filter = new MyPointFilter();
Chart1.DataManipulator.Filter(filter, "MySeries");

// User defined filtering criteria. Filters all points with 
// Y values greater than 100 or less than 10.
public class MyPointFilter : IDataPointFilter 
{    
    private bool FilterDataPoints(DataPoint point, Series series, Int32 pointIndex)
    {
        if((point.YValues(0)>100) || (point.YValues(0)<10))
        {
            FilterDataPoints = true;
        }
        else 
        {
            FilterDataPoints = false;
        }
    }
}

複数の系列のフィルター処理

複数の系列をフィルター処理するには、入力系列の文字列で、系列名をコンマで区切った一覧を指定します。各系列のすべてのポイントは、一覧の最初の系列に基づいてフィルター処理されます。つまり、最初の系列内のインデックスを持つデータ ポイントが削除される場合、同じインデックスを持つデータ ポイントはすべての系列から削除されます。

参照

関連項目

System.Windows.Forms.DataVisualization.Charting

System.Web.UI.DataVisualization.Charting

その他の技術情報

データのバインドと操作