irpas技术客

C# chart实时曲线_浪店河畔_c# 实时曲线

未知 763

主要介绍使用C#的winform如何进行实时数据的动态加载的实例,并通过设置参考线来显示数据曲线的分布

文章目录 前言一、运行效果图二、代码


前言

通过C#的winForm开发的一款实时曲线,动态显示数据,能够设置参考线

?

一、运行效果

二、代码

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Windows.Forms.DataVisualization.Charting;

namespace ChartControl { ? ? public partial class Form1 : Form ? ? { ? ? ? ? public Form1() ? ? ? ? { ? ? ? ? ? ? InitializeComponent(); ? ? ? ? }

? ? ? ? private void Form1_Load(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? InitChart(); ? ? ? ? ? ? timer1.Interval = 1000; ? ? ? ? }

? ? ? ? ///?

? ? ? ? /// 图表对象 ? ? ? ? ///?

? ? ? ? Series series; ? ? ? ? ///?

? ? ? ? /// x轴最大点数 ? ? ? ? ///?

? ? ? ? int xValue = 60; ? ? ? ? ///?

? ? ? ? /// 开始时间 ? ? ? ? ///?

? ? ? ? DateTime sDate;

? ? ? ? Random rand = new Random();

? ? ? ? /*增加队列*/ ? ? ? ? Queue<double> thinkness = new Queue<double>(30) ; ? ? ? ? Queue<double> recordTime = new Queue<double>(30);

? ? ? ? private void InitChart() ? ? ? ? { ? ? ? ? ? ? series = chart1.Series[0]; ? ? ? ? ? ? //清空原来数据缓存 ? ? ? ? ? ? series.Points.Clear();

? ? ? ? ? ? //定义图表大小尺寸 ? ? ? ? ? ? chart1.Width = Width - 90; ? ? ? ? ? ? chart1.Height = Height - 110;

? ? ? ? ? ? //以下按照先绘制chartArea、然后再绘制Series的步骤画图 ? ? ? ? ? ? //chartArea背景颜色 ? ? ? ? ? ? chart1.BackColor = Color.Azure;

? ? ? ? ? ? //X轴设置 ? ? ? ? ? ? //chart1.ChartAreas[0].AxisX.Title = "时间"; ? ? ? ? ? ? chart1.ChartAreas[0].AxisX.TitleAlignment = StringAlignment.Near; ? ? ? ? ? ? chart1.ChartAreas[0].AxisX.MajorGrid.Enabled = false;//不显示竖着的分割线

? ? ? ? ?? ? ? ? ? ? ? chart1.ChartAreas[0].AxisX.LabelStyle.Format = "HH:mm:ss"; //X轴显示的时间格式,HH为大写时是24小时制,hh小写时是12小时制 ? ? ? ? ? ? chart1.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Seconds;//如果是时间类型的数据,间隔方式可以是秒、分、时 ? ? ? ? ? ? //chart1.ChartAreas[0].AxisX.Interval = DateTime.Parse("00:05:00").Millisecond;//间隔为5分钟 ? ? ? ? ? ? chart1.ChartAreas[0].AxisX.Interval = DateTime.Parse("00:00:02").Second;//TODO 测试--间隔为5秒 ? ? ? ? ? ? //SetAxisX(2); ? ? ? ? ? ? //SetAxisXReal(); ? ? ? ? ? ? //Y轴设置 ? ? ? ? ? ? //chart1.ChartAreas[0].AxisY.Title = "数据点"; ? ? ? ? ? ? chart1.ChartAreas[0].AxisY.TitleAlignment = StringAlignment.Center; ? ? ? ? ? ? chart1.ChartAreas[0].AxisY.MajorGrid.Enabled = true;//显示横着的分割线 ? ? ? ? ? ? chart1.ChartAreas[0].AxisY.Minimum = 0; ? ? ? ? ? ? chart1.ChartAreas[0].AxisY.Maximum = 90; ? ? ? ? ? ? chart1.ChartAreas[0].AxisY.Interval = 20;

? ? ? ? ? ? //Series绘制 ? ? ? ? ? ? //chart1.Series[0].LegendText = "瞬时速度"; ? ? ? ? ? ? series.ChartType = SeriesChartType.Spline; ? ? ? ? ? ?? ? ? ? ? ? ? series.XValueType = ChartValueType.DateTime; ? ? ? ? ? ? series.IsValueShownAsLabel = true;//显示数据点的值 ? ? ? ? ? ? series.MarkerStyle = MarkerStyle.Diamond;//显示标记,菱形 ? ? ? ? }

?private void button1_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? ?timer1.Start(); ? ? ? ? }

? ? ? ? private void timer1_Tick(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ??//获取实时数据最近的60个数据 ? ? ? ? ? ? UpdateChartData(); ? ? ? ? ? ? //重新绘制曲折线图 ? ? ? ? ? ? this.chart1.Series[0].Points.Clear(); ? ? ? ? ? ? this.chart1.Series[0].Points.DataBindXY(recordTime, thinkness); ? ? ? ? ? ? //for (int i = 0; i < thinkness.Count; i++) ? ? ? ? ? ? //{ ? ? ? ? ? ? // ? ?this.chart1.Series[0].Points.AddXY(recordTime.ElementAt(i), thinkness.ElementAt(i));

? ? ? ? ? ? //}

? ? ? ? }

? ? ? ? private void button2_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? timer1.Stop(); ? ? ? ? }

? ? ? ? private void UpdateChartData() ? ? ? ? { ? ? ? ? ? ? if (thinkness.Count > 60) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? for (int i = 0; i < 2; i++) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? thinkness.Dequeue(); ? ? ? ? ? ? ? ? ? ? recordTime.Dequeue(); ? ? ? ? ? ? ? ? }

? ? ? ? ? ? } ? ? ? ? ? ? else ? ? ? ? ? ? { ? ? ? ? ? ? ? ? thinkness.Enqueue(rand.Next(20, 70)); ? ? ? ? ? ? ? ? recordTime.Enqueue(DateTime.Now.ToOADate()); ? ? ? ? ? ? }

? ? ? ? }

? ? ? ? private void ReferenceLine(double refernceValue,Chart charContor) ? ? ? ? { ? ? ? ? ? ? /* 绘制参考线*/ ? ? ? ? ? ? double max = refernceValue; ? ? ? ? ? ? StripLine stripMax = new StripLine(); ? ? ? ? ? ? stripMax.Text = string.Format("最大:{0:F}", max);//展示文本 ? ? ? ? ? ? stripMax.BackColor = Color.Yellow;//背景色 ? ? ? ? ? ? stripMax.Interval = 0;//间隔 ? ? ? ? ? ? stripMax.IntervalOffset = max;//偏移量 ? ? ? ? ? ? stripMax.StripWidth = 0.001;//线宽 ? ? ? ? ? ? stripMax.ForeColor = Color.White;//前景色 ? ? ? ? ? ? stripMax.TextAlignment = StringAlignment.Near;//文本对齐方式 ? ? ? ? ? ? charContor.ChartAreas[0].AxisY.StripLines.Add(stripMax);//添加到ChartAreas中 ? ? ? ? }

? ? ? ? private void button3_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? ReferenceLine(Convert.ToDouble(textBox1.Text.Trim()), chart1); ? ? ? ? } ? ? } } ?

改项目主要有两个重点,一、是队列的生成。二、队列绑定到chart的X轴和Y轴


1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,会注明原创字样,如未注明都非原创,如有侵权请联系删除!;3.作者投稿可能会经我们编辑修改或补充;4.本站不提供任何储存功能只提供收集或者投稿人的网盘链接。

标签: #C #实时曲线 #Systemusing