到目前为止,我已成功使用以下代码绘制图表,但我希望它能够随时间绘制和连接数据点,而不是一次性绘制和连接数据点。例如,我可能希望总共花费60秒来绘制所有点。我怎么能这样做?
chart1.Series["test1"].ChartType = SeriesChartType.FastLine;
chart1.Series["test1"].Color = Color.Red;
chart1.Series["test2"].ChartType = SeriesChartType.FastLine;
chart1.Series["test2"].Color = Color.Blue;
Random rdn = new Random();
for (int i = 0; i < 50; i++)
{
chart1.Series["test1"].Points.AddXY(rdn.Next(0,10), rdn.Next(0,10));
chart1.Series["test2"].Points.AddXY(rdn.Next(0,10), rdn.Next(0,10));
}
答案 0 :(得分:1)
您可以创建DispatcherTimer
并将其Interval
设置为您希望在绘制的点之间等待的时间。给它一个Tick
事件处理程序,将下一个点添加到图表中,并在你完成后禁用计时器。
var timer = new DispatcherTimer
{
Interval = TimeSpan.FromSeconds(0.1d),
};
var pointsRemaining = 50;
var r = new Random();
timer.Tick += (sender, args) =>
{
if (--pointsRemaining == 0)
timer.Stop();
chart1.Series["test1"].Points.AddXY(r.Next(0,10), r.Next(0,10));
chart1.Series["test2"].Points.AddXY(r.Next(0,10), r.Next(0,10));
};
timer.Start();