共用方式為


搜尋資料點 (Chart 控制項)

您可以在數列資料點的 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 參數提供搜尋的起始點索引。

  • 在迴圈中呼叫方法,並隨著每個後續方法呼叫遞增索引。

下列程式碼示範如何搜尋值為 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

其他資源

資料繫結和操作

加入資料

使用空資料點