다음을 통해 공유


차트 컨트롤에서 데이터 정렬

정렬을 수행하면 데이터 요소 값을 기준으로 계열의 데이터 요소 순서가 변경됩니다.오름차순, 내림차순 또는 사용자 지정 정렬 순서를 사용할 수 있습니다.

오름차순 또는 내림차순 정렬 수행

데이터를 정렬하려면 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를 사용하여 여러 계열을 정렬할 경우 모든 계열의 데이터를 정렬해야 합니다.그렇지 않으면 메서드에서 예외가 throw됩니다.데이터 정렬에 대한 자세한 내용은 데이터 맞춤을 참조하십시오.

다음 코드에서는 여러 계열을 내림차순으로 정렬하는 방법을 보여 줍니다.

Chart1.DataManipulator.Sort(PointsSortOrder.Descending, "ProductSales,ProductPrice")
Chart1.DataManipulator.Sort(PointsSortOrder.Descending, "ProductSales,ProductPrice");

사용자 지정 정렬 수행

사용자 지정 정렬을 정의하려면 IComparer 인터페이스를 사용합니다.이 인터페이스의 Compare 메서드는 두 DataPoint 개체를 인수로 받습니다.첫 번째 개체가 두 번째 개체보다 작으면 0보다 작은 값을 반환하고, 두 개체가 같으면 0을 반환하며, 첫 번째 개체가 두 번째 개체보다 크면 0보다 큰 값을 반환합니다.

이 예에서는 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

개념

데이터 필터링

데이터 맞춤

데이터 그룹화

데이터 복사, 분할 및 병합

기타 리소스

데이터 바인딩 및 조작