在图表控件中对数据进行排序
排序基于数据点值更改序列的数据点顺序。可以使用升序、降序或自定义排序顺序。
执行升序或降序排序
若要对数据进行排序,请使用 Series 或 DataManipulator 对象中的 Sort 方法。可以使用升序或降序排序。
下面的代码演示如何使用默认的第一个 Y 值对序列数据进行排序。
' Sort series in ascending order.
Chart1.Series("MySeries").Sort(PointsSortOrder.Ascending)
' Sort series in descending order.
Chart1.DataManipulator.Sort(PointsSortOrder.Descending, "MySeries")
// Sort series in ascending order.
Chart1.Series["MySeries"].Sort(PointsSortOrder.Ascending);
// Sort series in descending order.
Chart1.DataManipulator.Sort(PointsSortOrder.Descending, "MySeries");
默认情况下,数据点的第一个 Y 值用于排序。但是,您可以使用其 X 值或任何其他 Y 值(如 Y2)对数据点进行排序。也可以按每个值的 AxisLabel 属性进行排序。
下面的代码演示如何使用非默认点值对序列数据进行排序。
' Sort series in ascending order by X value.
Chart1.Series("MySeries").Sort(PointsSortOrder.Ascending, "X")
' Sort series in descending order by axis label.
Chart1.DataManipulator.Sort(PointsSortOrder.Descending, "AxisLabel","MySeries")
// Sort series in ascending order by X value.
Chart1.Series["MySeries"].Sort(PointsSortOrder.Ascending, "X");
// Sort series in descending order by axis label.
Chart1.DataManipulator.Sort(PointsSortOrder.Descending, "AxisLabel", "MySeries");
对多个序列进行排序
通过指定以逗号分隔的序列名称列表,可以使用 DataManipulator.Sort 对多个对齐的序列进行排序。此方法基于列出的第一个序列中的对应值,对所有序列中的所有点进行排序。换言之,此方法将第一个序列中数据点顺序的更改复制到列表中的所有序列。
备注
如果使用 DataManipulator.Sort 对多个序列进行排序,则必须将所有序列的数据对齐。否则,该方法将引发异常。有关对齐数据的更多信息,请参见对齐数据。
下面的代码演示如何按降序对多个序列进行排序。
Chart1.DataManipulator.Sort(PointsSortOrder.Descending, "ProductSales,ProductPrice")
Chart1.DataManipulator.Sort(PointsSortOrder.Descending, "ProductSales,ProductPrice");
指定自定义排序
若要定义自定义排序,请使用 IComparer 接口。此接口的 Compare 方法接收两个 DataPoint 对象作为参数。如果第一个对象小于第二个对象,则该方法应返回小于零的值,如果两个对象相等,则应返回零,如果第一个对象大于第二个对象,则应返回大于零的值。
此示例演示如何使用 IComparer 接口来提供自定义排序逻辑。
' Sort series.
Chart1.DataManipulator.Sort(New CustomComparer(), "MySeries")
' Custom sorting comparing class.
Public Class CustomComparer Implements IComparer
' Compares two data points by their Labels and returns a value indicating
' if the first data point is less than, equal to, or greater than the second
' data point.
Public Function Compare(ByVal a As Object, ByVal b As Object) As Integer Implements IComparer.Compare
Dim pointA As DataPoint = a
Dim pointB As DataPoint = b
' Compares data points' Label property
If pointA.Label < pointB.Label Then
Compare = -1
ElseIf pointA.label > pointB.Label Then
Compare = 1
Else
Compare = 0
End If
End Function
End Class
// Sort series.
Chart1.DataManipulator.Sort(new CustomComparer(), "MySeries");
// Custom sorting comparing class.
public class CustomComparer : IComparer {
// Compares two data points by their Labels and returns a value indicating
// if the first data point is less than, equal to, or greater than the second
// data point.
public int Compare(object a, object b) {
DataPoint pointA = a;
DataPoint pointB = b;
// Compares data points' Label property
if ((pointA.Label < pointB.Label)) {
Compare = -1;
}
else if ((pointA.label > pointB.Label)) {
Compare = 1;
}
else {
Compare = 0;
}
}
}
请参见
参考
System.Windows.Forms.DataVisualization.Charting
System.Web.UI.DataVisualization.Charting