網站首頁 編程語言 正文
概述
這一章節,我們主要實現的功能是為軟件增加實時顯示曲線。
winform在4.x版本的時候,還有charts組建,到了5.0時代,沒有了原生的charts組件
我這邊選擇第三方曲線包。ScottPlot
安裝插件包
比較簡單,已經提了很多次了。工具,nuget包管理。搜索scottplot,之后點擊安裝
注意安裝的是scottplot.winform
修改界面,增加曲線顯示
在表格下方,增加一個曲線。在工具欄中搜索plot
如果沒有搜索到,可以重新啟動一下vs修改完畢的界面如圖
修改代碼
增加曲線初始化
具體的曲線配置我們可以參考scottplot的demo,這里我直接給出code
本次窗口比較小,因此我禁止了x軸的顯示
/// <summary>
/// 初始化曲線
/// </summary>
private void InitPlot()
{
//line
formsPlot1.Plot.Style(Style.Seaborn);
formsPlot1.Plot.Title("數據曲線");
//不顯示x軸
formsPlot1.Plot.XAxis.Ticks(false);
formsPlot1.Plot.XAxis.Line(false);
formsPlot1.Plot.YAxis2.Line(false);
formsPlot1.Plot.XAxis2.Line(false);
}
增加曲線刷新
scottplot的刷新目前沒有找到合適的辦法
使用了比較笨的辦法,就是先clear然后重新賦值
有什么好的辦法,歡迎交流
/// <summary>
/// 刷新曲線
/// </summary>
private void RefreshLine(DataTable ThisData)
{
try
{
if (ThisData.Rows.Count < 1)
return;
//清空
formsPlot1.Plot.Clear();
//溫度
string[] data = ThisData.AsEnumerable().Select(d => d.Field<string>("溫度")).ToArray();
double[] ys = Array.ConvertAll<string, double>(data, s => double.Parse(s));
//濕度
string[] data2 = ThisData.AsEnumerable().Select(d => d.Field<string>("濕度")).ToArray();
double[] ys2 = Array.ConvertAll<string, double>(data2, s => double.Parse(s));
formsPlot1.Plot.AddSignal(ys);
formsPlot1.Plot.AddSignal(ys2);
formsPlot1.Refresh();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + ex.StackTrace);
MyLogger._.Error(ex.Message + "\r\n" + ex.StackTrace);
}
}
修改load函數,增加曲線初始化
/// <summary>
/// 界面加載函數
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_Load(object sender, EventArgs e)
{
MyLogger._.Debug("程序啟動");
//掃描串口
string[] comlist = ComPort.V_ScanPort();
if (comlist.Length < 1)
{
MessageBox.Show("沒有掃描到串口,請檢查硬件連接");
return;
}
else
{
foreach (string name in comlist)
{
this.comboBox1.Items.Add(name);
}
//默認
this.comboBox1.Text = comlist[0];
}
//波特率初始化
this.comboBox2.Items.Add("4800");
this.comboBox2.Items.Add("9600");
this.comboBox2.Items.Add("115200");
//默認
this.comboBox2.Text = "9600";
//默認地址
this.textBox1.Text = "01";
//默認背景色
this.button1.BackColor = Color.LightGreen;
//初始化數據
InitDataTable();
InitDataGrid();
InitPlot();
}
修改定時器函數,增加曲線實時刷新
/// <summary>
/// 定時器回調
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timer1_Tick(object sender, EventArgs e)
{
if (Runport.IsOpen)
{
//獲取地址
byte addr = byte.Parse(this.textBox1.Text);
string ret = THSensor.ReadTHDataFromSensor(Runport, addr);
if (!string.IsNullOrEmpty(ret))
{
string temp = ret.Split('&')[0];
string humi = ret.Split('&')[1];
//入庫操作
DB_SerAPI.SaveTHData(temp, humi);
//刷新列表
string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
if (DataBuffer != null)
{
Index++;
DataRow row = DataBuffer.NewRow();
row["ID"] = Index.ToString();
row["時間"] = time;
row["溫度"] = temp;
row["濕度"] = humi;
DataBuffer.Rows.Add(row);
}
//刷新曲線
RefreshLine(DataBuffer);
}
else
{
MessageBox.Show("無數據,請檢查配置參數");
}
}
}
測試
到目前為止,我們做的功能都是基于一個固定不變的小窗口驅完成。
后續,我們為了給工具增加歷史數據查詢,報警設置等基本的功能。需要對界面進行重新的布局和設計。
下一節,我們將主要修改布局,是窗體可以自適應分辨率。
原文鏈接:https://juejin.cn/post/7107895413297905677
相關推薦
- 2022-12-24 C++中的函數返回值與拷貝用法_C 語言
- 2022-08-15 element的form表單中如何一行顯示多el-form-item標簽
- 2022-07-18 實現?Python?腳本生成命令行_python
- 2022-11-08 Go中init()執行順序詳解_Golang
- 2022-12-03 內網環境下registry搭建步驟詳解_docker
- 2022-11-01 Go語言框架快速集成限流中間件詳解_Golang
- 2022-10-29 編寫字符設備驅動控制樹莓派io口
- 2022-02-05 Numpy中不同維度數組之間的計算
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支