【问题标题】:Real time chart update实时图表更新
【发布时间】:2016-09-04 14:08:22
【问题描述】:

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

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 funktion,为什么它只在所有步骤完成后才显示?

【问题讨论】:

  • 也许刷新图表会有所帮助。
  • 我没有名为 refresh() 的方法,我知道存在这样的文档,但即使添加了必需的命名空间,它仍然说图表不包含刷新的定义。
  • 好吧c#是区分大小写的,所以方法是cHshares.Refresh();!您确实意识到您正在删除 drawchart 方法中的所有系列,对,没有任何延迟..
  • 哦,谢谢。我不习惯区分大小写,因为我不经常使用 c#。绘图功能不仅由模拟调用,因此在添加我要显示的图形之前它会自行清除。无论如何,现在它可以工作了,非常感谢你。

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


【解决方案1】:

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

【讨论】:

  • 是的,但只有添加新值的代码(以及与此相关的条目)。如果我删除 while 循环,我会得到一个只有 1 个 x-achis 值的图表。 while 循环产生 10 个 x-achsis 值,在每个 x-achsis 值之后它应该更新图表,但它没有。
  • 这不是答案,而是评论!
【解决方案2】:

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

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

我希望这会有用。

【讨论】:

    猜你喜欢
    • 2013-01-30
    • 1970-01-01
    • 1970-01-01
    • 2023-02-13
    • 2015-10-20
    • 1970-01-01
    • 2014-02-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多