问题
I have need to create an oscilloscope simulator using the ms-chart control. I store my data in an array. But I don't know how to create the "moving effect" - continuous update of the control. (adding / removing points from the chart control) and having a vertical line drawn every second on the control.
My code is:
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]);
}
}
回答1:
Try this:
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;
}
}
来源:https://stackoverflow.com/questions/42891742/c-sharp-oscilloscope-simulator-with-ms-chart-control