1.拖一个脚本进去
2.拖一个柱状图进去
3.双击脚本写代码
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Ventuz.Kernel;
using Npgsql;
using System.Threading;
using System.Threading.Tasks;public class Script : ScriptBase, System.IDisposable
{// 这个变量使用在 Validate() 方法中,通过改变 true 还是 false 可以决定 Generate() 方法在下一次执行期间是否进入到 if(changed) 语句,进而通知与此脚本绑定的所有节点。private bool changed;string connStr = "66666666666666666666666666666";// 在打开场景,复制并粘贴此组件时,调用此方法。public Script(){// 注意:构造方法不能访问该脚本节点的输入或输出属性,因为此时它们还没有被分配。}// 在关闭场景,删除或释放组件时,调用此方法public virtual void Dispose(){}// 如果输入属性的值已更改,则调用此方法public override void Validate(){// 切记:如果更改了输出属性,需要设置成员 changed 为 true,请参阅 Generate()方法。}Task p;// 每次渲染帧之前都会调用此方法。// 返回值:如果为true,RayData 将通知与此脚本绑定的所有节点,代表脚本中一个输出值发生了变化,因此其他节点需要重新验证。因为性能原因请谨慎使用此方法,仅在输出值真的发生改变时返回值为true。public override bool Generate(){if (p==null) { p=new Task(() => { GetAllData(); });p.Start();VLog.Info("Task"); } //VLog.Info("299999");if (changed){changed = false;return true;}return false;}private void GetAllData(){while (true){try{VLog.Info("199999");GetSingleData();System.Threading.Thread.Sleep(4000); // 等待5秒}catch (Exception ex){VLog.Info(ex.Message);Console.WriteLine(ex.Message);}System.Threading.Thread.Sleep(5000); // 等待5秒}}private void GetSingleData(){List<int> list1 = new List<int>();List<string> list2 = new List<string>();// 创建连接对象 using (var connection = new NpgsqlConnection(connStr)){// 打开连接 connection.Open();// 创建查询命令 using (var command = new NpgsqlCommand("select callsign,height from ads_b_data where callsign <>'' order by create_time desc limit 10", connection)){// 执行查询并获取结果 using (var reader = command.ExecuteReader()){// 读取数据,假设表中有两列:Id 和 Name while (reader.Read()){string hangban = reader["callsign"].ToString(); // 读取第一列(Id) int height =Convert.ToInt32(reader["height"]); // 读取第二列(Name) list1.Add(height);list2.Add(hangban);//添加航班名称title="航班信息";VLog.Info("xxxx", $"hangban: {hangban}, height: {height}", VPopup.Never);//VLog.Info(System.Threading.Thread.CurrentThread.ManagedThreadId.ToString());//Console.WriteLine($"Id: {id}, Name: {name}");}}}}//赋值给外部变量yheight=String.Join(",", list1); //高度xhangban=String.Join(",", list2); //名称ct = list1.Count;changed = true;}
}
然后编译保存
将三个值都绑定到脚本上。注意数组值也是字符串,逗号隔开。
最后完事儿