Doug
Doug

Reputation: 5276

C# Chart: How to draw a plot over time

So far I have been successful in plotting a chart with the following code, but I want it to draw and connect the data points over time, not just all at once. For example, I might want it to take a total of 60 seconds to plot all of the points. How can I do this?

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));
}

Upvotes: 0

Views: 812

Answers (1)

Mike Strobel
Mike Strobel

Reputation: 25623

You can create a DispatcherTimer and set its Interval to the amount of time you want to wait between points plotted. Give it a Tick event handler that adds the next point to the chart, and disable the timer when you're done.

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();

Upvotes: 1

Related Questions