結果圖
代碼過程:
Form界面布局,控件:2個RadioButton, 3個button,1個chart,1個timer控件
代碼區:Form1.cs
-
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
boxinghuifang
-
{
-
public
partial
class
Form1 :
Form
-
{
-
public Form1()
-
{
-
InitializeComponent();
-
}
-
private Queue<
double> dataQueue =
new Queue<
double>(
100);
-
private Queue<
double> dataQueue1 =
new Queue<
double>(
100);
-
-
private
int curValue =
0;
-
private
int num =
5;
//每次刪除怎加幾個點
-
private void button1_Click(object sender, EventArgs e)
-
{
-
InitChart();
-
}
-
private void InitChart()
-
{
-
//定義圖表區域
-
this.chart1.ChartAreas.Clear();
-
ChartArea chartArea1 =
new ChartArea(
"C1");
-
this.chart1.ChartAreas.Add(chartArea1);
-
//定義存儲和顯示點的容器
-
this.chart1.Series.Clear();
-
Series series1 =
new Series(
"S1");
-
series1.ChartArea =
"C1";
-
this.chart1.Series.Add(series1);
-
-
Series series2 =
new Series(
"WW1");
/////////////////////////
-
series2.ChartArea =
"C1";
///////////////////////////////
-
this.chart1.Series.Add(series2);
/////////////////////////////////
-
-
-
//設置圖表顯示樣式
-
this.chart1.ChartAreas[
0].AxisY.Minimum =
0;
-
this.chart1.ChartAreas[
0].AxisY.Maximum =
100;
-
this.chart1.ChartAreas[
0].AxisX.Interval =
5;
-
this.chart1.ChartAreas[
0].AxisX.MajorGrid.LineColor = System.Drawing.Color.Silver;
-
this.chart1.ChartAreas[
0].AxisY.MajorGrid.LineColor = System.Drawing.Color.Silver;
-
//設置標題
-
this.chart1.Titles.Clear();
-
this.chart1.Titles.Add(
"S01");
-
this.chart1.Titles[
0].Text =
"波形回放顯示";
-
this.chart1.Titles[
0].ForeColor = Color.RoyalBlue;
-
this.chart1.Titles[
0].Font =
new System.Drawing.Font(
"Microsoft Sans Serif",
12F);
-
-
-
//設置標題
-
-
-
// this.chart1.Titles[1].ForeColor = Color.RosyBrown;
-
this.chart1.Titles[
0].Font =
new System.Drawing.Font(
"Microsoft Sans Serif",
12F);
-
//設置圖表顯示樣式
-
this.chart1.Series[
0].Color = Color.Red;
-
this.chart1.Series[
1].Color = Color.Green;
-
-
if (rb1.Checked)
-
{
-
this.chart1.Titles[
0].Text =
string.Format(
"XXX {0} 顯示", rb1.Text);
-
this.chart1.Series[
0].ChartType = SeriesChartType.Line;
-
this.chart1.Series[
1].ChartType = SeriesChartType.Line;
-
}
-
if (rb2.Checked)
-
{
-
this.chart1.Titles[
0].Text =
string.Format(
"XXX {0} 顯示", rb2.Text);
-
this.chart1.Series[
0].ChartType = SeriesChartType.Spline;
-
this.chart1.Series[
1].ChartType = SeriesChartType.Spline;
-
}
-
this.chart1.Series[
0].Points.Clear();
-
this.chart1.Series[
1].Points.Clear();
-
}
-
-
//更新隊列中的值
-
private void UpdateQueueValue()
-
{
-
-
if (dataQueue.Count >
100)
-
{
-
//先出列
-
for (
int i =
0; i < num; i++)
-
{
-
dataQueue.Dequeue();
-
}
-
}
-
if (dataQueue1.Count >
100)
-
{
-
//先出列
-
for (
int i =
0; i < num; i++)
-
{
-
dataQueue1.Dequeue();
-
}
-
}
-
if (rb1.Checked)
-
{
-
Random r =
new Random();
-
for (
int i =
0; i < num;i++ )
-
{
-
dataQueue.Enqueue(r.Next(
0,
100));
-
int[] A =
new
int[
100];
-
A[i] = i + r.Next(
0,
100);
-
dataQueue1.Enqueue(A[i]);
-
}
-
}
-
if (rb2.Checked)
-
{
-
for (
int i =
0; i < num; i++)
-
{
-
//對curValue只取[0,360]之間的值
-
curValue = curValue %
360;
-
//對得到的正玄值,放大50倍,並上移50
-
dataQueue.Enqueue((
50 * Math.Sin(curValue * Math.PI /
180)) +
50);
-
dataQueue1.Enqueue((
50 * Math.Sin(curValue * Math.PI /
180)) +
30);
-
curValue = curValue +
10;
-
}
-
}
-
}
-
-
private void button2_Click(object sender, EventArgs e)
-
{
-
this.timer1.Start();
-
}
-
public
static
bool S =
false;
-
private void button3_Click(object sender, EventArgs e)
-
{
-
if(S==
false)
-
{
-
button3.Text =
"繼續";
-
this.timer1.Stop();
-
S =
true;
-
}
-
else
-
{
-
button3.Text =
"暫停";
-
this.timer1.Start();
-
S =
false;
-
}
-
}
-
-
private void timer1_Tick(object sender, EventArgs e)
-
{
-
UpdateQueueValue();
-
this.chart1.Series[
0].Points.Clear();
-
this.chart1.Series[
1].Points.Clear();
-
for (
int i =
0; i < dataQueue.Count; i++)
-
{
-
this.chart1.Series[
0].Points.AddXY((i +
1), dataQueue.ElementAt(i));
-
}
-
for (
int i =
0; i < dataQueue1.Count; i++)
-
{
-
this.chart1.Series[
1].Points.AddXY((i +
1), dataQueue1.ElementAt(i));
-
}
-
}
-
}
-
}
完整代碼下載地址: