简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM