Create an array with chart points

Stéphani Pinheiro Ferreira 1 Reputation point
2021-02-09T14:51:00.007+00:00

Hello, I'm creating a graph that records temperature values recorded over time, so that the X axis records the hours and the Y axis records the temperature values.
What I need is to save the temperature values registered in an array or a list, that is, create a list of temperatures.

How do I do this?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,648 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Timon Yang-MSFT 9,586 Reputation points
    2021-02-10T02:19:46.74+00:00

    You can use Datatable as the data source of the chart, and then use Timer to call the method to get the temperature.

    A simple example:

            private Timer Timer;  
            private DataTable Tems;  
            private void Form1_Load(object sender, EventArgs e)  
            {  
                chart1.Series[0].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;  
                chart1.Series[0].Color = Color.Red;  
                Tems = new DataTable();  
                Tems.Columns.Add("Time",typeof(int));  
                Tems.Columns.Add("temperature",typeof(double));  
                chart1.Series[0].XValueMember = "Time";  
                chart1.Series[0].YValueMembers = "temperature";  
                chart1.DataSource = Tems;  
                chart1.DataBind();  
      
                Timer = new Timer();  
                Timer.Interval = 5 * 1000;  
                Timer.Tick += Timer_Tick;  
                Timer.Start();  
            }  
      
            private void Timer_Tick(object sender, EventArgs e)  
            {  
                Tems.Rows.Add(h++, 36.5 + n++);  
                chart1.DataBind();  
            }  
           
            double n = 1.2;  
            int h = 1;  
            private void button1_Click(object sender, EventArgs e)  
            {  
                Timer.Stop();  
            }  
    

    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