简体   繁体   中英

C# - Updating a Chart in Real-Time

Bear with me, I'm new to Stack Overflow, but have used it as a resource for a long time when researching methods of programming that I'm not fond with.

I read up a tutorial on how to create a graph in a C# Windows Forms Application , and was attempting to find out how to make it update itself in real-time if I ever need to use the graph to plot the total amount of data in a sensor. To test it out, I'm using a timer, ticking at every second (1000ms). And before I can use this with a sensor, I'm having two values automatically increment by 1.

The current problem I'm facing is that the chart itself won't update, and only stays the way it was drawn when the form loaded. I thought it was because I have to redraw the chart with chart1.Update(); , and I tried using that before/after recreating the chart every second. However, the result is the same regardless. I just wondered if there's something I haven't done or needs to be changed in order to update the chart in real-time.

This is where the code is at currently:

public partial class Form1 : Form
{
    int a = 1;
    int b = 2;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        // Data arrays.
        string[] seriesArray = { "Cats", "Dogs" };
        int[] pointsArray = { a, b };

        // Set palette.
        this.chart1.Palette = ChartColorPalette.SeaGreen;

        // Set title.
        this.chart1.Titles.Add("Pets");

        // Add series.
        for (int i = 0; i < seriesArray.Length; i++)
        {
            // Add series.
            Series series = this.chart1.Series.Add(seriesArray[i]);

            // Add point.
            series.Points.Add(pointsArray[i]);
        }
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        a++;
        b++;

        // Data arrays.
        string[] seriesArray = { "Cats", "Dogs" };
        int[] pointsArray = { a, b };

        // Set palette.
        this.chart1.Palette = ChartColorPalette.SeaGreen;

        // Set title.
        this.chart1.Titles.Add("Pets");

        // Add series.
        for (int i = 0; i < seriesArray.Length; i++)
        {
            // Add series.
            Series series = this.chart1.Series.Add(seriesArray[i]);

            // Add point.
            series.Points.Add(pointsArray[i]);
        }

        chart1.Update();
    }
}

Your code has several problems:

  • The timer click event is not hooked up. I know that it isn't because otherwise you'd get an exception telling you that..
  • ..you can add a series only once . You were doing it on each timer.Tick . This and all other setup commands should go into an initial method like the form load.

I have corrected the errors in the code below, but, obviously, the data don't make any sense yet. Also: While I have added code to hook up the timer.Tick , the button.Click is not hooked up. Usually you let the designer do this by double-clicking the control to hook up the standard event of a control or by double-clicking the event in the control's event tab in the property page.

int a = 1;
int b = 2;
string[] seriesArray = { "Cats", "Dogs" };

private void Form1_Load(object sender, EventArgs e)
{
    // Set palette.
    this.chart1.Palette = ChartColorPalette.SeaGreen;

    // Set title.
    this.chart1.Titles.Add("Pets");

    // Add series
    this.chart1.Series.Clear();
    for (int i = 0; i < seriesArray.Length; i++)
    {
        chart1.Series.Add(seriesArray[i]);
    }
    // hook up timer event
    timer1.Tick += timer1_Tick;
}




private void timer1_Tick(object sender, EventArgs e)
{
    a++;
    b++;

    // Data array
    int[] pointsArray = { a, b };

    for (int i = 0; i < seriesArray.Length; i++)
    {
        // Add point.
        chart1.Series[i].Points.Add(pointsArray[i]);
    }

}

private void button1_Click(object sender, EventArgs e)
{
    timer1.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