如图所示,实时从云端接受排队人数数据,并在折线图中实时更新,首先新建一个定时器:
private ObservableDataSource<Point> dataSource3 = new ObservableDataSource<Point>();//排队人数
设置刷新时间,chart2为要更新的折线图:
public Window1()
{
InitializeComponent();
timer2.Tick += new EventHandler(timer_Tick2);
timer2.Interval = TimeSpan.FromSeconds(5); //设置刷新的间隔时间5s
chart2.AddLineGraph(dataSource3, Colors.Green, 2, "排队人数");//
chart2.Viewport.Visible = new Rect(0, 0, 20, 40);
}
定时器内容:
void timer_Tick2(object sender, EventArgs e)//更新人数
{
var SDK = new NLECloudAPI("http://api.nlecloud.com");
dynamic qry1 = SDK.GetSensorInfo(deviceId, "people", Token);
if (qry1.IsSuccess())
{
Console.WriteLine("success!!!!");
}
string json = SerializeToJson(qry1);
JObject result = JObject.Parse(json);
string value1 = result["ResultObj"]["Value"].ToString();
CHART(Convert.ToDouble(value1));
this.textbox_number.Dispatcher.Invoke(new outputDelegate(outputAction), value1);//显示人数
data.Dispatcher.Invoke(new outputDelegate(outputAction2), value1);
}
public void outputAction(string msg)
{
textbox_number.Text = msg + "人";
}
public void CHART(double xx)//打印折线图
{
double x = temp1_num;//定义全局变量temp1_num=0,表示x值;
Point point = new Point(x, xx);
dataSource3.AppendAsync(base.Dispatcher, point);
chart2.Viewport.FitToView();
temp1_num += 5;//坐标加5(5秒1跳)
int newX = 0;
double newY = 2 * xx;
//实现折现图自动滚动,x轴最多显示20个数据,num_num表示x最小值
if (num_num > 20)
{
newX = num_num - 20;
}
chart2.Viewport.Visible = new Rect(newX, 0, 20, newY);
num_num+=5;
}
private string SerializeToJson(object qry)
{
return Newtonsoft.Json.JsonConvert.SerializeObject(qry);
}
开始获取数据按钮和暂停按钮:
private void Button_Click_4(object sender, RoutedEventArgs e)//暂停取号
{
var SDK = new NLECloudAPI("http://api.nlecloud.com");
String actuatorApiTag = "key"; //发送命令的执行器标识名
dynamic qry;
qry = SDK.Cmds(deviceId, actuatorApiTag, 0, Token);
timer2.Stop();
}
private void Button_Click_3(object sender, RoutedEventArgs e)//开始取号
{
var SDK = new NLECloudAPI("http://api.nlecloud.com");
String actuatorApiTag = "key"; //发送命令的执行器标识名
dynamic qry;
qry = SDK.Cmds(deviceId, actuatorApiTag, 1, Token);
timer2.Start();
}
至此,一个从云端接受数据的并实时更新的折线图便完成了!!!