Export (0) Print
Expand All

Sorting Data in Chart Controls

Sorting changes the order of a series' data points based on a data point value. You can use ascending, descending, or custom sort orders.

To sort data, use the Sort method in the Series or DataManipulator object. You can use an ascending or descending sort order.

The following code demonstrates how to sort series data using the default first Y value.

// Sort series in ascending order.
Chart1.Series["MySeries"].Sort(PointsSortOrder.Ascending);

// Sort series in descending order.
Chart1.DataManipulator.Sort(PointsSortOrder.Descending, "MySeries");

By default the first Y value of the data points is used for sorting. However, you can sort data points using their X value, or any of their other Y values, such as Y2. You can also sort by each value's AxisLabel property.

The following code demonstrates how to sort series data using non-default point values.

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

You can use DataManipulator.Sort to sort multiple aligned series by specifying a comma-separated list of series names. This method sorts all points in all series based on their corresponding values from the first series listed. In other words, it duplicates the changes in the order of data points in the first series to all series in the list.

Note Note

If you use DataManipulator.Sort to sort more than one series, the data from all series must be aligned. Otherwise, the method throws an exception. For more information on aligning data, see Aligning Data.

The following code demonstrates how to sort multiple series in descending order.

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

To define a custom sort, use the IComparer interface. The Compare method of this interface receives two DataPoint objects as arguments. It should return a value less than zero if the first object is less than the second, zero if they are equal, and a value greater than zero if the first object is greater than the second.

This example demonstrates how to use the IComparer interface to provide custom sorting logic.

// 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;
        }
    }
}
Show:
© 2014 Microsoft