Searching Data Points (Chart Controls)

You can search the X and Y values of data points in a series for a range of values or a specific value. Searching data points with specific values is useful when you want to :

  • Check a value range.

  • Change the visual appearance of points that have a certain value.

  • Set point labels.

  • Use a point's position for custom drawing.

Searching Data Points

The Series.Points collection property exposes several methods for searching points.

  • FindValue
    Returns the first point in a series with the specified value.
  • FindMaxValue
    Returns the first point in a series with the largest value.
  • FindMinValue
    Returns the first point in a series with the smallest value.

Note

These methods return a null value if no points match the search criteria.

Use each of these methods in a loop to locate all points that match a search criteria. To find all points from a pre-defined starting index, use one of the methods with a startFromIndex parameter. If you provide this parameter, the method also uses it to indicate the index of the returned data point.

The following code demonstrates how to search data points by the first Y value.

' Find the first data point with the maximum Y value.
Dim maxDataPoint As DataPoint = mySeries.Points().FindMaxValue()
' Find the first data point with the minimum Y value.
Dim minDataPoint As DataPoint = mySeries.Points().FindMinValue()
' Find the first data point with a first Y value of 10.
Dim dataPoint As DataPoint = mySeries.Points().FindValue(10.0)
// Find the first data point with the maximum Y value.  
DataPoint maxDataPoint = mySeries.Points().FindMaxValue();
// Find the first data point with the minimum Y value.
DataPoint minDataPoint = mySeries.Points().FindMinValue();
// Find the first data point with a first Y value of 10.
DataPoint dataPoint = mySeries.Points().FindValue(10);

To search a value such as X or Y2, provide the name of the value. The following code demonstrates how to search data points by the X value.

' Find first data point with the maximum X value.
Dim maxDataPoint As DataPoint = mySeries.Points().FindMaxValue("X")
' Find the first data point with the minimum second Y value.
Dim minDataPoint As DataPoint = mySeries.Points().FindMinValue("Y2")
' Find first data point with an X value of "1/1/2001".
Dim dataPoint As DataPoint = mySeries.Points().FindValue(DateTime.Parse("1/1/2001").ToOADate(), "X")
// Find first data point with the maximum X value.
DataPoint maxDataPoint = mySeries.Points().FindMaxValue("X");
// Find the first data point with the minimum second Y value.
DataPoint minDataPoint = mySeries.Points().FindMinValue("Y2");
// Find first data point with an X value of "1/1/2001".
DataPoint dataPoint = mySeries.Points().FindValue(DateTime.Parse("1/1/2001").ToOADate(), "X");

Searching Multiple Points

To find all data points that match a search criterion:

  • Provide the starting point index of the search using the startFromIndex parameter.

  • Call the method in a loop and increment the index with each successive method call.

The following code demonstrates how to search the second Y value for the value 10 and reset the color of the result data points.

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  'Find all points with a second Y value equal to 10, and change their color.
  Dim index As Integer = 0
  'Find first point with a Y2 value of 10.
  Dim dataPoint As DataPoint = Chart1.Series("Series1").Points.FindValue(10, "Y2", index)
  While Not (dataPoint Is Nothing)
        dataPoint.Color = Color.FromArgb(255, 128, 128)
        'Find all other data points with a second Y value 10.
        index += 1
        dataPoint = Chart1.Series("Series1").Points.FindValue(10, "Y2", index)
  End While
End Sub
private void Page_Load(object sender, System.EventArgs e)
{
    // Find all points with a second Y value equal to 10, and change their color.
    int index = 0;
    // Find first point with a Y2 value of 10.
    DataPoint dataPoint = Chart1.Series("Series1").Points.FindValue(10, "Y2", index);
    while(!(dataPoint == null)) 
    {
        dataPoint.Color = Color.FromArgb(255, 128, 128);
        // Find all other data points with a second Y value 10.
        index++;
        dataPoint = Chart1.Series("Series1").Points.FindValue(10, "Y2", index);
    }
}

See Also

Reference

System.Windows.Forms.DataVisualization.Charting
System.Web.UI.DataVisualization.Charting

Concepts

Adding Data
Using Empty Data Points

Other Resources

Data Binding and Manipulation