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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM