2013-05-22 91 views
0

我使用MSChart 2008創建了一個折線圖。我在x軸上有日期,在y軸上有雙值。現在我需要在今天的日期上找到Y值(這將是我的x值)..任何人都可以幫助我實現這一目標。從MSChart系列的X值中查找Y值

感謝

回答

1

我使用類似的方法將它與光標:我想找到最接近的記錄加入垂直光標在哪裏,並顯示該值(而不是光標座標值):

如果您將e.NewPosition替換爲您的查詢值(例如今天的日期),則代碼應該按照您的要求進行操作。

// Cursor Position Changing Event 
private void chart1_CursorPositionChanged(object sender, CursorEventArgs e) 
{ 
    //Get the nearest record and use its X value as the position 
    double coercedPosition = chart1.Series[0].Points.Aggregate((x, y) => Math.Abs(x.XValue - e.NewPosition) < Math.Abs(y.XValue - e.NewPosition) ? x : y).XValue; 

    //Set cursor to the coerced position 
    chart1.ChartAreas[0].CursorX.Position = coercedPosition; 

    SetPosition(e.Axis, coercedPosition); 
} 

// Update the labels with the values at the indicated position 
private void SetPosition(Axis axis, double position) 
{ 
    if (double.IsNaN(position)) 
     return; 

    if (axis.AxisName == AxisName.X) 
    { 
     //Get the value at the position 
     DateTime dt = DateTime.FromOADate(position); 

     //set the timestamp label 
     lbl_CursorTimestampValue.Text = dt.ToString(); 

     //get the point at that timestamp (x value) 
     var point = chart1.Series[0].Points.FindByValue(position, "X"); 

     //set the y value label 
     lbl_CursorYValue.Text = point.IsEmpty == true ? "--" : point.YValues[0].ToString(); 
    } 
} 

另外請注意,如果你想將日期時間轉換爲OA值:

double yourOADate = yourDateTime.ToOADate();