实时图表更新

时间:2016-09-04 14:08:22

标签: c# visual-studio charts real-time

在我目前的项目中,我有一个生成数据的算法,这些数据将显示在折线图中,但是图表只在algorythm结束时更新,而不是在每个“步骤”之后更新。

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++;
        }
    }
}

因为我在每一步都称之为绘图功能,为什么它只在所有步骤完成后显示?

2 个答案:

答案 0 :(得分:0)

您已将代码置于循环

之类的循环中

答案 1 :(得分:0)

在每个绘图后,更新您的图表。即。

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

我希望这会有用。

相关问题