Share via


使用图表控件搜索数据点

可以对于某个值范围或特定值搜索序列中数据点的 X 和 Y 值。当您要执行以下操作时,搜索具有特定值的数据点很有用:

  • 检查值范围。

  • 更改具有特定值的点的可视外观。

  • 设置点标签。

  • 使用点的位置进行自定义绘图。

搜索数据点

Series.Points 集合属性公开了用于搜索点的几个方法。

  • FindValue
    返回序列中具有指定值的第一个点。

  • FindMaxValue
    返回序列中具有最大值的第一个点。

  • FindMinValue
    返回序列中具有最小值的第一个点。

备注

如果没有符合搜索条件的点,则这些方法返回 Null 值。

循环使用其中的每个方法,以找到符合搜索条件的所有点。若要从预定义的起始索引中查找所有点,请使用带 startFromIndex 参数的其中某个方法。如果提供此参数,该方法还将使用它来指示返回的数据点的索引。

下面的代码演示如何按第一个 Y 值搜索数据点。

' 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);

若要搜索诸如 X 或 Y2 的值,请提供该值的名称。下面的代码演示如何按 X 值搜索数据点。

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

搜索多个点

若要查找符合搜索条件的所有数据点:

  • 使用 startFromIndex 参数提供该搜索的起始点索引。

  • 循环调用该方法,每次调用方法后使索引加 1。

下面的代码演示如何搜索值 10 的第二个 Y 值并重置结果数据点的颜色。

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);
    }
}

请参见

参考

System.Windows.Forms.DataVisualization.Charting

System.Web.UI.DataVisualization.Charting

概念

添加数据

使用空的数据点

其他资源

数据绑定和操纵