共用方式為


排序資料 (Chart 控制項)

排序會根據資料點值來變更數列的資料點順序。您可以使用遞增、遞減或自訂排序順序。

執行遞增或遞減排序

若要排序資料,請使用 SeriesDataManipulator 物件中的 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

其他資源

資料繫結和操作

篩選資料

對齊資料

群組資料

複製、分割及合併資料