【发布时间】:2013-05-22 14:05:20
【问题描述】:
我使用 MSChart 2008 创建了一个折线图。我在 x 轴上有日期,在 y 轴上有双值。现在我需要在今天的日期找到 Y 值(这将是我的 x 值)..谁能帮忙我来实现这一点。
谢谢
【问题讨论】:
我使用 MSChart 2008 创建了一个折线图。我在 x 轴上有日期,在 y 轴上有双值。现在我需要在今天的日期找到 Y 值(这将是我的 x 值)..谁能帮忙我来实现这一点。
谢谢
【问题讨论】:
我对光标使用了类似的方法:我想找到与添加垂直光标的位置最近的记录并显示其值(而不是光标坐标的值):
如果您将 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();
【讨论】: