C# 实时折线图,波形图
此Demo是采用VS自带的Chart图表控件,制作实时动态显示的折线图,和波形图。本文仅供学习分享使用,如有不足之处,还请指正。 涉及知识点:
Chart控件的相关概念:
主要有两个方法:
----------------------------------------------------------------------------------------------------------- 效果图 如下【先点击初始化按钮,再点击开始按钮】: 折线图【折线图,是取[0,100]之间的随即数进行填充】: 波形图【波形图,是取正玄值,并放大50倍,然后上移50】 核心代码 代码如下: 1 using System; 2 System.Collections.Generic; 3 System.ComponentModel; 4 System.Data; 5 System.Drawing; 6 System.Linq; 7 System.Text; 8 System.Windows.Forms; 9 System.Windows.Forms.DataVisualization.Charting; 10 11 namespace WindowsFormsApplication1 12 { 13 public partial class RealChart : Form 14 { 15 private Queue<double> dataQueue = new Queue<double>(100); 16 17 private int curValue = 0; 18 19 int num = 5;//每次删除增加几个点 20 21 public RealChart() 22 { 23 InitializeComponent(); 24 } 25 26 /// <summary> 27 /// 初始化事件 28 </summary> 29 <param name="sender"></param> 30 <param name="e"></param> 31 void btnInit_Click(object sender,EventArgs e) 32 33 InitChart(); 34 35 36 37 开始事件 38 39 40 41 void btnStart_Click( 42 43 this.timer1.Start(); 44 } 45 46 47 停止事件 48 49 50 51 void btnStop_Click( 52 53 .timer1.Stop(); 54 55 56 57 定时器事件 58 59 60 61 void timer1_Tick( 62 63 UpdateQueueValue(); 64 this.chart1.Series[].Points.Clear(); 65 for(int i=0;i<dataQueue.Count;i++){ 66 0].Points.AddXY((i+1),dataQueue.ElementAt(i)); 67 } 68 69 70 71 初始化图表 72 73 void InitChart() { 74 定义图表区域 75 .chart1.ChartAreas.Clear(); 76 ChartArea chartArea1 = new ChartArea("C1" 77 .chart1.ChartAreas.Add(chartArea1); 78 定义存储和显示点的容器 79 .chart1.Series.Clear(); 80 Series series1 = new Series(S1 81 series1.ChartArea = 82 .chart1.Series.Add(series1); 83 设置图表显示样式 84 this.chart1.ChartAreas[0].AxisY.Minimum = 85 0].AxisY.Maximum = 86 0].AxisX.Interval = 5 87 0].AxisX.MajorGrid.LineColor = System.Drawing.Color.Silver; 88 0].AxisY.MajorGrid.LineColor = 89 设置标题 90 .chart1.Titles.Clear(); 91 this.chart1.Titles.Add(S01 92 this.chart1.Titles[0].Text = XXX显示 93 0].ForeColor = Color.RoyalBlue; 94 0].Font = new System.Drawing.Font(Microsoft Sans Serif,12F); 95 96 0].Color = Color.Red; 97 if (rb1.Checked) 98 { 99 0].Text =string.Format( XXX {0} 显示100 0].ChartType = SeriesChartType.Line; 101 102 (rb2.Checked) { 103 0].Text = string.Format(104 SeriesChartType.Spline; 105 106 107 108 109 更新队列中的值 110 UpdateQueueValue() { 111 112 if (dataQueue.Count > ) { 113 先出列 114 for (int i = 0; i < num; i++) 115 { 116 dataQueue.Dequeue(); 117 } 118 119 120 121 Random r = new Random(); 122 123 124 dataQueue.Enqueue(r.Next(0,)); 125 126 127 128 129 130 对curValue只取[0,360]之间的值 131 curValue = curValue % 360132 对得到的正玄值,放大50倍,并上移50 133 dataQueue.Enqueue((50*Math.Sin(curValue*Math.PI / 180))+50134 curValue=curValue+10135 136 137 138 } 139 } ?备注 关于定时器Timer【微软自带的控件】: 说明:表示在相同的时间间隔,引发用户自定义的事情?。实现用户需要的功能。本例中是用来定时更新队列中的数据,并刷新图表。 常用说明:
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ 由于需要源码的比较多,故将源码下载链接放于此处,请自行下载,谢谢 源码下载 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |