1. 添加新窗口
在Form1中新建button控件,名称为buttonStartWaveDisp,这样单击就能调出Form2窗口
在Form1中增加如下:
private void buttonStartWaveDisp_Click(object sender, EventArgs e)
{
if((form2 == null) || (form2.IsDisposed == true))
{
form2 = new Form2();
form2.Show();
}
else
{
form2.Activate();
}
}
2. 在Form2上添加chart1、“画图”button
修改属性
轴参数
Form.Designer.cs自动生成的部分代码如下
//
// chart1
//
chartArea2.AxisX.Maximum = 60D;
chartArea2.AxisX.Minimum = -10D;
chartArea2.AxisY.Maximum = 300D;
chartArea2.Name = "ChartArea1";
this.chart1.ChartAreas.Add(chartArea2);
legend2.Name = "Legend1";
this.chart1.Legends.Add(legend2);
this.chart1.Location = new System.Drawing.Point(16, 18);
this.chart1.Name = "chart1";
series2.ChartArea = "ChartArea1";
series2.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Spline;
series2.Legend = "Legend1";
series2.Name = "AD";
this.chart1.Series.Add(series2);
this.chart1.Size = new System.Drawing.Size(685, 388);
this.chart1.TabIndex = 0;
this.chart1.Text = "chart1";
//
// timer1
//
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
private System.Windows.Forms.DataVisualization.Charting.Chart chart1;
private System.Windows.Forms.Button buttonPointDisp;
private System.Windows.Forms.Timer timer1;
3. 代码如下:
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;
namespace WaveTest
{
public partial class Form2 : Form
{
private int counter = 0;
private int index = 0;
private int fullFlag = 0;
private const int counterMax = 50;
private byte[] dataBuf = new byte[counterMax];
public Form2()
{
InitializeComponent();
}
public void PointDisp(byte dat8)
{
chart1.Series[0].Points.Clear();
dataBuf[index] = dat8;
int j;
if (fullFlag == 0)
{
if (++counter == counterMax)
{
fullFlag = 1;
}
j = 0;
}
else
{
j = index + 1;
}
for (int i = 0; i < counter; i++,j++)
{
if(j == counterMax)
{
j = 0;
}
chart1.Series[0].Points.AddXY(i, dataBuf[j]);
}
if (++index == counterMax)
{
index = 0;
}
}
private void buttonPointDisp_Click(object sender, EventArgs e)
{
Random ra = new Random();
byte dat8 = (byte)(ra.Next() % 256);
PointDisp(dat8);
}
private void timer1_Tick(object sender, EventArgs e)
{
Random ra = new Random();
byte dat8 = (byte)(ra.Next() % 256);
PointDisp(dat8);
}
}
}
4. 实际效果
按一次画图,生成一个随机数,0~49,移动显示