我想要实时显示图表。

我的方法是在TrackBar滚动时添加DataPoint。
private void trackBar1_Scroll(object sender, EventArgs e)
{
if (trackBar1.Value < valueBefore) //scrolling to the left removes previous DataPoint
{
try
{
chart1.Series["Correlation"].Points.RemoveAt(valueBefore);
}
catch
{
MessageBox.Show(valueBefore.ToString()); //Try catch for debugging purposes
}
}
else //scrolling to the right adds DataPoint based on trackBar's current value
{
chart1.Series["Correlation"].Points.AddXY(Convert.ToDouble(age[trackBar1.Value]), salary[trackBar1.Value]);
}
valueBefore = trackBar1.Value; //in order to know which
label1.Text = "No. of records : " + (trackBar1.Value + 1);
}如果我使用箭头键或鼠标单击(SmallChange和LargeChange定义为1),代码就可以工作。
向右滚动不会导致任何问题,但是向左滚动会导致异常错误,我无法确定。我的DataPoint索引的valueBefore似乎超出了范围:
System.ArgumentOutOfRangeException was unhandled
HResult=-2146233086
Message=Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
ParamName=index
Source=mscorlib我该如何解决这个问题?
发布于 2016-10-28 14:26:05
DataPointCollection.RemoveAt(int)方法是从集合继承的,它将根据引用DataPointCollection的当前状态而不是您的数据的索引移除一个点。
尝试在引发异常的行处放置断点并检查chart1.Series["Correlation"].Points集合。这应该可以让您看到发生了什么。
https://stackoverflow.com/questions/40297291
复制相似问题