Freigeben über


Filtering Data in Chart Controls

Data filtering removes data points from a series or marks the data point as empty based on a set of filtering criteria. When you are working with your data, keep in mind that a filtering operation can modify the original series' data or store the output to an output series.

Warning

When filtering multiple series, make sure that all series are aligned. Otherwise, the filtering method throws an exception. For more information, see Aligning Data.

Filtering Data

The following properties and methods in the DataManipulator class are used for filtering:

  • FilterSetEmptyPoints property
    Specifies whether to remove data points from the series or mark them as empty points.

  • FilterMatchedPoints property
    Specifies whether to remove data points that match the criteria. If set to false, points that do not match the filtering criteria are filtered.

    This property only applies to the Filter method.

  • Filter method
    Filters a series' data points using a date or time range, or a comparison of a data point value to a number value, or some custom user-defined criteria.

  • FilterTopN method
    Filters a series' data points except for those with either the largest or smallest values in the series.

Filtering by Date or Time Ranges

Use the Filter method to filter by date or time ranges when the X values of the data points are dates (the series' Series.XValueType property is set to DateTime). Series data is split into ranges where the defined elements of the range are filtered. To define a date range, specify two parameters:

  • A range type in the DateRangeType class.

  • A string with range elements. This string can include commas, and dashes. For example, "1-10, 20, 25".

The following code filters all weekend data points from a series called "MySeries", and then removes all data points except for the first of the month.

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");

Filtering All Data except Extremes

Use the FilterTopN method to filter all points from a series except for a specified number of points with the largest or smallest point values. To use this method, specify the following criteria:

  • Number of total points to retain.

  • Y values to filter. For example, "Y2". The default value is the first Y value ("Y").

  • Whether to get the top values. If set to False, FilterTopN retains the smallest values. The default value is True.

The code below demonstrates how to use the FilterTopN method.

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); 

Filtering by Values

Use the Filter method to filter by value comparison. Specify the following parameters:

  • A compare method in the CompareMethod class.

  • A constant value for comparison.

  • Y values to filter. For example, "Y2". The default is the first Y value ("Y").

The following code demonstrates how to filter points by comparing them with a constant.

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");

Filtering with Custom Criteria

Use the IDataPointFilter interface to define custom criteria. This interface exposes the FilterDataPoint method, which determines the data points to be removed. The following are the parameters of the FilterDataPoint method and its return value:

  • The data point object to be filtered.

  • The series that the point belongs to.

  • The data point's index in the Series.Points collection object.

  • Return True if the point should be filtered; otherwise, return False.

The following code demonstrates how to filter points with user-defined criteria.

' 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;
        }
    }
}

Filtering Multiple Series

Filter multiple series by specifying a comma-separated list of series names in input series string. All points in each series will be filtered based on the first series in the list. In other words, data points with the same index is removed from all series if the data point with the data point with that index in the first series is removed.

See Also

Reference

System.Windows.Forms.DataVisualization.Charting

System.Web.UI.DataVisualization.Charting

Other Resources

Data Binding and Manipulation