数据筛选可基于一组筛选条件来移除序列中的数据点,或将数据点标记为空。使用数据时请记住,筛选操作可能会修改原始序列的数据,或将输出存储到输出序列。
DataManipulator 类中的下列属性和方法可用于筛选:
当数据点的 X 值是日期时(序列的 Series.XValueType 属性设置为 DateTime),可使用 Filter 方法按日期或时间进行筛选。序列数据拆分成若干个范围,在其中对范围的定义元素进行筛选。若要定义日期范围,请指定两个参数:
-
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”)。
下面的代码演示如何通过与一个常量进行比较来筛选点。
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; } } }
可通过在输入序列字符串中指定逗号分隔的序列名称列表来筛选多个序列。每个序列中的所有点都将基于列表中的第一个序列来筛选。换言之,如果移除第一个序列中带有某个索引的数据点,则将从所有序列移除带有相同索引的数据点。
原文地址:http://technet.microsoft.com/zh-cn/magazine/dd456751(VS.100).aspx