我有一个从串口读取数据的图表,我的数据是无限的。我想扩展我的图表范围,向我显示数据。
我不知道该怎么做。这是我将数据添加到图表的代码:
public void AddData()
{
DateTime now = DateTime.Now;
InputData = ComPort.ReadByte();
object firstByte = InputData;
if (ComPort.IsOpen == true)
{
s = Convert.ToString(Convert.ToChar(firstByte));
temp1 += s;
lock (firstByte)
{
if (Convert.ToInt32(firstByte) == 13)
{
temp = temp1;
temp1 = "";
ComPort.DiscardInBuffer();
_valuelist.Add(Convert.ToInt32(temp));
FileStream fs = new FileStream(filename, FileMode.Append);
var data = System.Text.Encoding.UTF8.GetBytes(String.Format("{0} {1}", temp, DateTime.Now.ToString("hh:mm:ss:ff")) + "\r\n");
fs.Write(data, 0, data.Length);
fs.Close();
chart1.ResetAutoValues();
if (chart1.Series[0].Points.Count > 0)
{
while (chart1.Series[0].Points[0].XValue < now.AddSeconds(-5).ToOADate())
{
chart1.Series[0].Points.RemoveAt(0);
chart1.ChartAreas[0].AxisX.Minimum = chart1.Series[0].Points[0].XValue;
chart1.ChartAreas[0].AxisX.Maximum = now.AddSeconds(5).ToOADate();
}
}
chart1.Series[0].Points.AddXY(now.ToOADate(), _valuelist[_valuelist.Count - 1]);
chart1.Invalidate();
}
}
}
}