簡體   English   中英

在C#中獲取圖形的Y值

[英]Get Y-value of a graph in C#

在C#中,我如何獲得圖表系列中一個點的Y值,知道它的X值是DateTime類型的? 我不知道該系列的索引,只知道他們的名字。

以下是我的代碼。 事實上,我隨着時間的推移模擬了股票價格。 現在我想添加“服務日期”系列來標記模擬的特定日期點。 我現在需要的是將colpos設置為其名稱由colinfo.colptf給出的系列的Y值

或者你能告訴我如何獲得chart1.series[colinfo.colptf]的索引?

private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
        if (radioButton1.Checked == true)
        {
            radioButton2.Checked = false;
            radioButton3.Checked = false;
            radioButton4.Checked = false;
            clearseries(chart1, "Service dates");
            chart1.Series.Add("Service dates");
            chart1.Series["Service dates"].Color = Color.Red;
            chart1.Series["Service dates"].ChartType = SeriesChartType.Point;
            foreach (var simul in simulations)
                foreach (var colinfo in simul.VPdates)
                {
                    double colpos = chart1.Series[colinfo.colptf].Points.First(x => x.XValue == colinfo.coldate.ToOADate()).YValues[0];
                    addpoint(colinfo.coldate, colpos, colinfo.colstring, chart1.Series["Service dates"]);
                }
        }
}

你可以用

var collection = chart1.Series.Select(series => series.Points.Where(point => point.XValue == 0).ToList()).ToList();

這將為您提供一個系列列表,其中包含指定X坐標處的所有點。

如果您想要1個列表中的所有數據點,您現在可以使用

List<DataPoint> points = new List<DataPoint>();
collection.ForEach(series => series.ForEach(points.Add));

從DataPoint獲取Y值只是使用YValues,它返回Y值的數組。 我假設您只為每個點分配1個Y值,因此您可以采用第一個索引,即[0]。

因此,獲得第1個Y值的第一個點將是:

double yVal = points[0].YValues[0];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM