我使用MSChart 2008创建了一个折线图。我在x轴上有Dates,在y轴上有Double值。现在我需要在今天的日期找到Y值(这将是我的x值)..任何人都可以帮忙我要做到这一点。
由于
答案 0 :(得分: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();
}
}
另请注意,如果您想将DateTime转换为OA值:
double yourOADate = yourDateTime.ToOADate();