0

MSChart 2008 を使用して折れ線グラフを作成しました。x 軸に日付があり、y 軸に倍精度値があります。今日の日付の Y 値を見つける必要があります (これが x 値になります)。これ。

ありがとう

4

1 に答える 1

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();
于 2013-10-08T22:36:19.637 に答える