Coder Social home page Coder Social logo

how-to-create-a-real-time-chart-in-winforms's Introduction

How to create a real-time chart in WinForms?

This example illustrates 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.

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

how-to-create-a-real-time-chart-in-winforms's People

Contributors

devakumard avatar mrhemalatha avatar muneeshkumarg avatar sarubala20 avatar vinothkumar-ganesan avatar yuvarajpalanisamy avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.