Articles in this section
Category / Section

How to create a real-time chart in WF

2 mins read

This article explains How to create a real time chart in WinForms. Real-time charts can be created by continuously adding new points in existing series in chart. Timer is used to continuously add new points in the chart.

 

Step1: Create a dataset with table, which should contain two columns. The following code example shows how to create a dataset with table.

 

string tableName = "Products";
prodDs1 = new DataSet();
prodDs1.Tables.Add(tableName);
DataColumn ExpiresDate = prodDs1.Tables[tableName].Columns.Add("Date", typeof(System.DateTime));
DataColumn Quantity = prodDs1.Tables[tableName].Columns.Add("Load", typeof(double));

 

Step2: Bind the dataset as data source for the series using SeriesModel, and then map the columns in the table for x and y-values using the XName and YNames properties.

 

ChartSeries series = new ChartSeries();
series.Name = "Products";
series.Text = series.Name;
model = new ChartDataBindModel(this.prodDs1, "Products");
model.XName = "Date";
model.YNames = new String[] { "Load" };
series.SeriesModel = model;
series.Type = ChartSeriesType.Spline;
series.Style.DisplayShadow = false;
this.chartControl1.Series.Add(series);

 

Step3: Add new rows in table, which is bound as data source for the chart, using timer tick event.

 

private void timer1_Tick(object sender, System.EventArgs e)
{
    try
    {
        string tableName = "Products";
        Random rand = new Random();
        if (prodDs1 != null && prodDs1.Tables.Count > 0)
        {
            DataRow drEmpty = prodDs1.Tables[tableName].NewRow();
            prodDs1.Tables[tableName].Rows.Add(drEmpty);
            int count = Math.Max(0, prodDs1.Tables[tableName].Rows.Count - 1);
            prodDs1.Tables[tableName].Rows[count]["Load"] = (double)rand.Next(0, 60);
            prodDs1.Tables[tableName].Rows[count]["Date"] = lastTime.AddMinutes(30);
        }
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.ToString());
     }
}

 

The above codes will automatically update the chart with new values present in the data table.

 

View the Sample in GitHub

 

See Also

How to make the added points as empty at runtime

How to bind the data source via chart wizard

How to create a chart in vb.net WinForms.

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied