winform chart画折线,波形图,多条数据

结果图

 

代码过程:

Form界面布局,控件:2个RadioButton, 3个button,1个chart,1个timer控件

代码区:Form1.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Windows.Forms.DataVisualization.Charting;
  11. namespace boxinghuifang
  12. {
  13. public partial class Form1 : Form
  14. {
  15. public Form1()
  16. {
  17. InitializeComponent();
  18. }
  19. private Queue<double> dataQueue = new Queue<double>(100);
  20. private Queue<double> dataQueue1 = new Queue<double>(100);
  21. private int curValue = 0;
  22. private int num = 5;//每次删除怎加几个点
  23. private void button1_Click(object sender, EventArgs e)
  24. {
  25. InitChart();
  26. }
  27. private void InitChart()
  28. {
  29. //定义图表区域
  30. this.chart1.ChartAreas.Clear();
  31. ChartArea chartArea1 = new ChartArea("C1");
  32. this.chart1.ChartAreas.Add(chartArea1);
  33. //定义存储和显示点的容器
  34. this.chart1.Series.Clear();
  35. Series series1 = new Series("S1");
  36. series1.ChartArea = "C1";
  37. this.chart1.Series.Add(series1);
  38. Series series2 = new Series("WW1"); /////////////////////////
  39. series2.ChartArea = "C1"; ///////////////////////////////
  40. this.chart1.Series.Add(series2); /////////////////////////////////
  41. //设置图表显示样式
  42. this.chart1.ChartAreas[0].AxisY.Minimum = 0;
  43. this.chart1.ChartAreas[0].AxisY.Maximum = 100;
  44. this.chart1.ChartAreas[0].AxisX.Interval = 5;
  45. this.chart1.ChartAreas[0].AxisX.MajorGrid.LineColor = System.Drawing.Color.Silver;
  46. this.chart1.ChartAreas[0].AxisY.MajorGrid.LineColor = System.Drawing.Color.Silver;
  47. //设置标题
  48. this.chart1.Titles.Clear();
  49. this.chart1.Titles.Add("S01");
  50. this.chart1.Titles[0].Text = "波形回放显示";
  51. this.chart1.Titles[0].ForeColor = Color.RoyalBlue;
  52. this.chart1.Titles[0].Font = new System.Drawing.Font("Microsoft Sans Serif", 12F);
  53. //设置标题
  54. // this.chart1.Titles[1].ForeColor = Color.RosyBrown;
  55. this.chart1.Titles[0].Font = new System.Drawing.Font("Microsoft Sans Serif", 12F);
  56. //设置图表显示样式
  57. this.chart1.Series[0].Color = Color.Red;
  58. this.chart1.Series[1].Color = Color.Green;
  59. if (rb1.Checked)
  60. {
  61. this.chart1.Titles[0].Text = string.Format("XXX {0} 显示", rb1.Text);
  62. this.chart1.Series[0].ChartType = SeriesChartType.Line;
  63. this.chart1.Series[1].ChartType = SeriesChartType.Line;
  64. }
  65. if (rb2.Checked)
  66. {
  67. this.chart1.Titles[0].Text = string.Format("XXX {0} 显示", rb2.Text);
  68. this.chart1.Series[0].ChartType = SeriesChartType.Spline;
  69. this.chart1.Series[1].ChartType = SeriesChartType.Spline;
  70. }
  71. this.chart1.Series[0].Points.Clear();
  72. this.chart1.Series[1].Points.Clear();
  73. }
  74. //更新队列中的值
  75. private void UpdateQueueValue()
  76. {
  77. if (dataQueue.Count > 100)
  78. {
  79. //先出列
  80. for (int i = 0; i < num; i++)
  81. {
  82. dataQueue.Dequeue();
  83. }
  84. }
  85. if (dataQueue1.Count > 100)
  86. {
  87. //先出列
  88. for (int i = 0; i < num; i++)
  89. {
  90. dataQueue1.Dequeue();
  91. }
  92. }
  93. if (rb1.Checked)
  94. {
  95. Random r = new Random();
  96. for (int i = 0; i < num;i++ )
  97. {
  98. dataQueue.Enqueue(r.Next(0, 100));
  99. int[] A = new int[100];
  100. A[i] = i + r.Next(0, 100);
  101. dataQueue1.Enqueue(A[i]);
  102. }
  103. }
  104. if (rb2.Checked)
  105. {
  106. for (int i = 0; i < num; i++)
  107. {
  108. //对curValue只取[0,360]之间的值
  109. curValue = curValue % 360;
  110. //对得到的正玄值,放大50倍,并上移50
  111. dataQueue.Enqueue((50 * Math.Sin(curValue * Math.PI / 180)) + 50);
  112. dataQueue1.Enqueue((50 * Math.Sin(curValue * Math.PI / 180)) + 30);
  113. curValue = curValue + 10;
  114. }
  115. }
  116. }
  117. private void button2_Click(object sender, EventArgs e)
  118. {
  119. this.timer1.Start();
  120. }
  121. public static bool S = false;
  122. private void button3_Click(object sender, EventArgs e)
  123. {
  124. if(S==false)
  125. {
  126. button3.Text = "继续";
  127. this.timer1.Stop();
  128. S = true;
  129. }
  130. else
  131. {
  132. button3.Text = "暂停";
  133. this.timer1.Start();
  134. S = false;
  135. }
  136. }
  137. private void timer1_Tick(object sender, EventArgs e)
  138. {
  139. UpdateQueueValue();
  140. this.chart1.Series[0].Points.Clear();
  141. this.chart1.Series[1].Points.Clear();
  142. for (int i = 0; i < dataQueue.Count; i++)
  143. {
  144. this.chart1.Series[0].Points.AddXY((i + 1), dataQueue.ElementAt(i));
  145. }
  146. for (int i = 0; i < dataQueue1.Count; i++)
  147. {
  148. this.chart1.Series[1].Points.AddXY((i + 1), dataQueue1.ElementAt(i));
  149. }
  150. }
  151. }
  152. }

 

完整代码下载地址:

 

https://download.csdn.net/my/uploads

posted @ 2020-07-14 09:52  奋斗的中年人哈哈哈  阅读(1405)  评论(0编辑  收藏  举报