如何更新图表?

时间:2015-05-29 13:35:33

标签: c# forms graph updating

目前我正在开发一个项目,它将您的defaultLayout widget = do master <- getYesod mmsg <- getMessage -- We break up the default layout into two components: -- default-layout is the contents of the body tag, and -- default-layout-wrapper is the entire page. Since the final -- value passed to hamletToRepHtml cannot be a widget, this allows -- you to use normal widget features in default-layout. pc <- widgetToPageContent $ do addStylesheet $ StaticR css_bootstrap_css $(widgetFile "default-layout") withUrlRenderer $(hamletFile "templates/default-layout-wrapper.hamlet") 带到一个很酷的图表中。但它只需要给出第一个值然后停止。它没有不断更新。我尝试过while循环,但它们似乎不起作用。这是我目前的代码:

CPU temperature

这是结果的图像:JavaDoc page

如何添加现有代码以继续更新表单上的值和值?谢谢

1 个答案:

答案 0 :(得分:0)

这是相当直接的。从Value属性中删除setter中的逻辑并将其放入自己的函数中,将逻辑放入getter和setter中除了获取和设置私有成员值之外通常是不好的做法。所以像这样:

Timer pollTimer = new System.Timers.Timer(1000); // fires every second

public void Initialize()
{
   // subscribe the PollTemperature function to the Elapsed event.
   pollTimer.Elapsed += PollTemperature;
   // Enable the periodic polling
   pollTimer.Enabled = true;
}

private float _Value;
public float Value
{
    get { return _Value; }
    set
    { 
       if(_value != value)
       {
          _value = value
       }
    }
 }

 public decimal GetTemperature()
 {
    // get the temperature of the cpu sensor here
    float yourTemperature = resultsOfYourLogic;
    return yourTemperature;
 }

 void PollTemperature(Object source, ElapsedEventArgs e)
 {
    Value = GetTemperature();
    this.Invalidate();
    this.Update();
 } 

这会设置一个计时器,每秒轮询一次温度并更新您的Value属性。它还使此控件无效,然后更新它以重绘它。