Chart Update after created and linked datasource

jacques labuschagne 1 Reputation point
2020-09-30T21:19:53.553+00:00

Hallo

I'm trying to find a way to update the chart after the a list of Objects was binded with the chart. (Simple explanation below).

Chart is created once the form is loaded, and got a tick event that should update the data every couple of seconds.

public frmMainView()
{
InitializeComponent();
timer1.Start();
timer2.Start();
timer2.Interval = 2000;
UpdateUserScanRatesHourlyDaily();
CreateCharts();
UpdateCharts();
}

private void timer2_Tick(object sender, EventArgs e)
{
UpdateUserScanRatesHourlyDaily();
UpdateCharts();
}

 private void CreateCharts()  
        {  
            charHourlyPacks.DataSource = _scannerPerformanceModel;  
            Series S1 = new Series();  
            S1.XValueMember = "UserName";  
            S1.YValueMembers = "HuPackedDay";  
            charHourlyPacks.Series.Add(S1);  
        }  
        private void UpdateCharts()  
        {  
            charHourlyPacks.Series.ResumeUpdates(); //This is not updating the chart.  
            //charHourlyPacks.Update();  
        }      

29465-chartupdating.jpg

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,898 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Daniel Zhang-MSFT 9,626 Reputation points
    2020-10-01T04:44:28.903+00:00

    Hi jacques labuschagne,
    I have provided a solution in another thread with same problem, and you can update the chart via DataBind method.
    Here is a code example:

    public Form1()  
    {  
        InitializeComponent();  
        timer1.Start();  
        CreateCharts();  
        timer2.Start();  
        timer2.Interval = 2000;  
    }  
    private void timer2_Tick(object sender, EventArgs e)  
    {  
        UpdateUserScanRatesHourlyDaily();  
        UpdateCharts();  
    }  
    List<Scanner> MyList = new List<Scanner>();  
    private void CreateCharts()  
    {  
        Series s = new Series();  
        s.XValueMember = "UserName";  
        s.YValueMembers = "HuPackedDay";  
        MyList.Add(new Scanner("A",1));  
        MyList.Add(new Scanner("D", 2));  
        MyList.Add(new Scanner("F", 3));  
        chart1.DataSource = MyList;  
        chart1.Series.Add(s);  
        chart1.DataBind();  
    }  
    int i = 1;  
    private void UpdateUserScanRatesHourlyDaily()  
    {  
        var found = MyList.FirstOrDefault(c => c.HuPackedDay == i);  
        found.HuPackedDay =i+1;   
        i++;  
    }  
    private void UpdateCharts()  
    {  
        chart1.DataBind();  
    }  
      
      
    public class Scanner  
    {  
        String username;  
        int huPackedDay=0;  
      
        public string UserName  
        {  
            get { return username; }  
            set { username = value; }  
        }  
      
        public int HuPackedDay  
        {  
            get { return huPackedDay; }  
            set { huPackedDay = value;  }  
        }  
        public Scanner(string un, int h)  
        { UserName = un; HuPackedDay = h; }  
      
    }  
    

    Best Regards,
    Daniel Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.