Search code examples
c#.netwinformschartsreal-time

Display Chart (Histogram) Bars with real time data


I am building a simple WinForms (C#) application to display some data distribution in the chart. I am using .net chart control in my application. I only have a chart and button controls on my form and I want to populate the chart onclick event. I am using something like this in the onclick event:

chartPractice.Series["Distribution"].Points.AddXY("1", 150);
chartPractice.Series["Distribution"].Points.AddXY("2", 11);
chartPractice.Series["Distribution"].Points.AddXY("3", 250);

It works fine to populate the values in the graph.

I want to implement the functionality that allows the user of the application to see how the graph is populated in real time.

For example, if I put series in the for loop only the final output is displayed in the chart control:

for (int i = 0; i < 900; i++)
{
    chartPractice.Series["Distribution"].Points.AddXY("1", i);
}

I would like the user to see the histogram bar changing with each loop iteration. Right now when I use the approach above the application simply goes through the loop and displays the the final result in the graph.

Thank you,


Solution

  • Actually the solution was pretty easy and I am a little embarrassed that I did not figure it out sooner. All I had to do was add chartPractice.Refresh(); in the loop.

    It forces the application to redraw the graph after each iteration.

    I hope this answer will help someone else who might be working on the application that requires this functionality.