Realtime and Auto Updating Plot in C#

Admin
0

 Hi there, now I am going to show you how to create an auto updating plot in c# based on timer. The complete video guide is available on my Youtube Channel.



I have put the full code at the end of this page.

To begin, create a simple template consisted of "Chart" and "Button" like this :


Don't forget to change series type to "line'. To do that, click chart, see properties and find "Series", from the collection, change the "ChartType" to "Line".

Create a new timer :

System.Windows.Forms.Timer PlotTimer = new System.Windows.Forms.Timer();

Create a void function that can be called each by the timer. This function is used to generated data and plot them to the chart. This is the code :

 private void RealTimePlot(Object myObject, EventArgs myEventArgs)
        {
            int length = 400;
            double[] x = new double[length];
            double[] y = new double[length];
            Random rd = new Random();
            double amplitude = rd.Next(0, length);
            chart1.Series[0].Points.Clear();
            for (int i = 0; i < length; i++)
            {
                x[i] = i;  
                y[i] = amplitude * (Math.Sin(i));
                chart1.Series[0].Points.AddXY(x[i], y[i]);
            }
            chart1.ChartAreas[0].AxisY.Minimum = 0;
            chart1.ChartAreas[0].AxisY.Maximum = length;
        }

Now, connect the Timer and the function you have created :

            PlotTimer.Tick += new EventHandler(RealTimePlot);

and set the speed of plot in miliseconds :

      PlotTimer.Interval = 400;

Finally, under the push button, put a code to start and stop the plot. It is just like this :

 if (PlotTimer.Enabled == true)

            {
                PlotTimer.Enabled = false;
            }

            else

            {
                PlotTimer.Enabled = true;
            }

Compile and Run. You should have a program like this :



Here is the complete code in visual C# :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        System.Windows.Forms.Timer PlotTimer = new System.Windows.Forms.Timer();
        private void RealTimePlot(Object myObject, EventArgs myEventArgs)
        {
            int length = 400;
            double[] x = new double[length];
            double[] y = new double[length];
            Random rd = new Random();
            double amplitude = rd.Next(0, length);
            chart1.Series[0].Points.Clear();
            for (int i = 0; i < length; i++)
            {
                x[i] = i;
                
                y[i] = amplitude * (Math.Sin(i));
                chart1.Series[0].Points.AddXY(x[i], y[i]);
            }
            chart1.ChartAreas[0].AxisY.Minimum = 0;
            chart1.ChartAreas[0].AxisY.Maximum = length;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
 
            PlotTimer.Interval = 400;
            PlotTimer.Tick += new EventHandler(RealTimePlot);
            
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (PlotTimer.Enabled == true)
            {
                PlotTimer.Enabled = false;
            }
            else
            {
                PlotTimer.Enabled = true;
            }
        }
        private void chart1_Click(object sender, EventArgs e)
        {
        }
    }
}





Post a Comment

0Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment (0)

Disclaimer : Content provided on this page is for general informational purposes only. We make no representation or warranty of any kind, express or implied, regarding the accuracy, adequacy, validity, reliability, availability or completeness of any information.

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !
To Top