C# 示波器模拟器(带 MS Chart 控件)
C# Oscilloscope Simulator (with MS Chart control)
我需要使用 ms-chart 控件创建一个示波器模拟器。我将数据存储在一个数组中。但我不知道如何创建 "moving effect" - 控件的持续更新。 (从图表控件中添加/删除点)并在控件上每秒绘制一条垂直线。
我的代码是:
private void Form1_Load(object sender, EventArgs e)
{
chart1.Series["Series1"].ChartType = SeriesChartType.Line;
chart1.Series["Series1"].BorderWidth = 3;
// NO grids
chart1.ChartAreas[0].AxisX.MajorGrid.LineWidth = 0;
chart1.ChartAreas[0].AxisY.MajorGrid.LineWidth = 0;
chart1.PostPaint += new EventHandler<ChartPaintEventArgs>(chart1_PostPaint);
}
void chart1_PostPaint(object sender, ChartPaintEventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
string fileContent = File.ReadAllText("e:\in.txt");
string[] integerStrings = fileContent.Split(new char[] { ' ', '\t', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
int[] integers = new int[integerStrings.Length];
for (int n = 0; n < integerStrings.Length; n++)
{
integers[n] = int.Parse(integerStrings[n]);
chart1.Series["Series1"].Points.Add(integers[n]);
}
}
试试这个:
public partial class Form1 : Form
{
Timer timer;
double x;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
timer = new Timer();
timer.Tick += Timer_Tick;
timer.Interval = 50;
}
private void button1_Click(object sender, EventArgs e)
{
if (timer.Enabled)
timer.Stop();
else timer.Start();
}
private void Timer_Tick(object sender, EventArgs e)
{
chart1.Series[0].Points.AddXY(x, 3 * Math.Sin(5 * x) + 5 * Math.Cos(3 * x));
if (chart1.Series[0].Points.Count > 100)
chart1.Series[0].Points.RemoveAt(0);
chart1.ChartAreas[0].AxisX.Minimum = chart1.Series[0].Points[0].XValue;
chart1.ChartAreas[0].AxisX.Maximum = x;
x += 0.1;
}
}
我需要使用 ms-chart 控件创建一个示波器模拟器。我将数据存储在一个数组中。但我不知道如何创建 "moving effect" - 控件的持续更新。 (从图表控件中添加/删除点)并在控件上每秒绘制一条垂直线。
我的代码是:
private void Form1_Load(object sender, EventArgs e)
{
chart1.Series["Series1"].ChartType = SeriesChartType.Line;
chart1.Series["Series1"].BorderWidth = 3;
// NO grids
chart1.ChartAreas[0].AxisX.MajorGrid.LineWidth = 0;
chart1.ChartAreas[0].AxisY.MajorGrid.LineWidth = 0;
chart1.PostPaint += new EventHandler<ChartPaintEventArgs>(chart1_PostPaint);
}
void chart1_PostPaint(object sender, ChartPaintEventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
string fileContent = File.ReadAllText("e:\in.txt");
string[] integerStrings = fileContent.Split(new char[] { ' ', '\t', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
int[] integers = new int[integerStrings.Length];
for (int n = 0; n < integerStrings.Length; n++)
{
integers[n] = int.Parse(integerStrings[n]);
chart1.Series["Series1"].Points.Add(integers[n]);
}
}
试试这个:
public partial class Form1 : Form
{
Timer timer;
double x;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
timer = new Timer();
timer.Tick += Timer_Tick;
timer.Interval = 50;
}
private void button1_Click(object sender, EventArgs e)
{
if (timer.Enabled)
timer.Stop();
else timer.Start();
}
private void Timer_Tick(object sender, EventArgs e)
{
chart1.Series[0].Points.AddXY(x, 3 * Math.Sin(5 * x) + 5 * Math.Cos(3 * x));
if (chart1.Series[0].Points.Count > 100)
chart1.Series[0].Points.RemoveAt(0);
chart1.ChartAreas[0].AxisX.Minimum = chart1.Series[0].Points[0].XValue;
chart1.ChartAreas[0].AxisX.Maximum = x;
x += 0.1;
}
}