一、效果
使用随机数模拟产生实时温度数据,数据使用label1、textbox1、char1曲线三种方式实时展示,数据大于设定值label2显示红灯,小于设定值label2显示蓝灯,介于二者之间显示绿灯。以下是运行效果:
二、软件设置
2.1如下图拖入需要的控件
2.2按下面截图设置控件属性
butten1
butten2
label1
label2
label3
textbox1
char1
默认拖进来的表是条形统计图,按如下设置就可以调整为折现图
三、代码
using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
//using static WindowsFormsApp2.Form1;
namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
Random r = new Random();
//全局变量
public class CommonData
{
public static int i = 100;
public static int Max = 100;
public static int Min = 0;
public static int rdData ;
}
System.Timers.Timer timer = new System.Timers.Timer(1000);//Form1刷新线程,刷新时间1000ms
public Form1()
{
InitializeComponent();
Chart_design();
}
public void TextBox()
{
CommonData.rdData = r.Next(0,100);
textBox1.Text += CommonData.rdData + "\r\n";
Console.WriteLine(textBox1.Text);
this.textBox1.Select(this.textBox1.Text.Length, 0);
this.textBox1.ScrollToCaret();
}
private void button1_Click(object sender, EventArgs e)
{
timer.Start();
}
public void Chart_design()
{
this.chart1.Titles.Add("温度曲线");
this.chart1.Series[0].Name = "温度";
this.chart1.ChartAreas[0].AxisX.Minimum = CommonData.Min;
this.chart1.ChartAreas[0].AxisY.Maximum = CommonData.Max;
this.chart1.Series[0].Color = Color.Brown;
Add_Topline(80);//添加上限直线
Add_Botline(20);//添加下限直线
chart1.ChartAreas[0].AxisX.ScaleView.Size = 5;
chart1.Series[0].ToolTip = "#VALX,#VALY";
}
private void Add_Topline(int Off_value)
{
StripLine stripline = new StripLine();
stripline.Interval = 0;// 设置多长间隔继续绘制,如为0则只绘制一条
stripline.IntervalOffset = Off_value;// 线的起始位置
stripline.StripWidth = 0.5;// 线的宽度
stripline.BackColor = Color.Red;// 线的颜色
stripline.BorderDashStyle = ChartDashStyle.Dash;// 线的形状
stripline.Text = string.Format("上限:{0:F}", Off_value);// 展示文本
stripline.TextAlignment = StringAlignment.Far;//文本右对齐
stripline.TextLineAlignment = StringAlignment.Far;
stripline.TextOrientation = TextOrientation.Auto;
chart1.ChartAreas[0].AxisY.StripLines.Add(stripline);//直线添加到表
}
private void Add_Botline(int Off_value)
{
StripLine stripline = new StripLine();
stripline.Interval = 0;
stripline.IntervalOffset = Off_value;
stripline.StripWidth = 0.5;
stripline.BackColor = Color.Blue;
stripline.BorderDashStyle = ChartDashStyle.DashDot;
stripline.Text = string.Format("下限:{0:F}", Off_value);
stripline.TextAlignment = StringAlignment.Far;
chart1.ChartAreas[0].AxisY.StripLines.Add(stripline);
}
private void Form1_Load(object sender, EventArgs e)
{
timer.Elapsed += new System.Timers.ElapsedEventHandler(ShowData);
timer.AutoReset = true;
timer.Enabled = false;
label1.Visible = true;//温度00°
label2.Visible = true;//指示灯
label3.Visible = true;//正常,低温,高温
}
void ShowData(object o, System.Timers.ElapsedEventArgs e)
{
//子线程操作主线程
this.Invoke(new Action(() =>
{
TextBox(); //刷新textbox
this.chart1.Series[0].Points.AddXY(CommonData.i, CommonData.rdData);
CommonData.i++;
chart1.ChartAreas[0].AxisX.ScaleView.Scroll(ScrollType.Last);//刷新曲线
label1.Text =CommonData.rdData.ToString()+"°";//刷新温度标签
if (CommonData.rdData>80) //刷新指示灯标签
{
label2.ForeColor= Color.Red;
label3.Text="高温";
}else if (CommonData.rdData < 20)
{
label2.ForeColor = Color.Blue;
label3.Text = "低温";
}
else
{
label2.ForeColor = Color.Green;
label3.Text = "正常";
}
}));
}
private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
throw new NotImplementedException();
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
System.Environment.Exit(0);//窗口关闭,释放所有线程
}
private void button2_Click(object sender, EventArgs e)
{
timer.Stop();
}
}
}