实时图表更新

本文关键字:更新 实时 | 更新日期: 2023-09-27 18:05:30

在我当前的项目中,我有一个生成数据的算法,该数据将显示在线形图中,但是图表只有在算法结束而不是在每个"步骤"之后才更新。

private void simulate(share[] shares)
{
    int k = 0;
    Random r = new Random(); //random values just for testing 
    while (k < 10)//10 steps (10 values for each share)
    {
        for (int i = 0; i < shares.Length; i++)
        {
            shares[i].value = r.Next(0, 10000);//random a new value
            shares[i].history.Add(shares[i].value);//add the value to the value history
        }
        //now update the chart so that it first shows only one x point than two and so on 
        drawchart(shares);
        k++;
    }
}
private void drawchart(share[] shares)
{
    cHshares.Series.Clear();//clear the chart
    for (int i = 0; i < shares.Length; i++)//for each share
    {
        cHshares.Series.Add(shares[i].name);//add a new share to the chart
        cHshares.Series[shares[i].name].ChartType = SeriesChartType.FastLine; 
        int j = 0;
        //draw the lines with each value that exists for each share
        foreach (double value in shares[i].history)
        {
            cHshares.Series[shares[i].name].Points.AddXY(j, value);
            j++;
        }
    }
}

既然我调用drawchart函数每一步,为什么它只显示所有步骤完成后?

实时图表更新

你已经把你的代码放在一个循环中,就像for循环

每次绘制图表后,更新您的图表。例如

drawchart(shares);
cHshares.Update();
k++;