none
Time(mm:ss) in X-axis winforms RRS feed

  • Question

  • Is it possible to get time format in X-axis in windows forms? Yes, I can able to get the system time in X-axis. My objective is to make that time start from (mm:ss) 00:00 on clicking start event so that it starts like a stopwatch, which helps for me to plot the dynamic live values vs the time that starts from 00:00.

    • Moved by CoolDadTx Tuesday, September 17, 2019 2:09 PM Winforms related
    Tuesday, September 17, 2019 4:24 AM

Answers

  • Hi Magesh85,

    Thank you for posting here.

    According to your description, you want to get time format in X-sxis in winforms.

    You could try the following code to get it.

       public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
          
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                this.chart1.ChartAreas[0].AxisX.LabelStyle.Format = "mm:ss";
                chart1.Series[0].XValueType = ChartValueType.DateTime;
            }
            int i = 0;
            private DateTime _start;
            private void timer1_Tick(object sender, EventArgs e)
            {
                DateTime now = DateTime.Parse("00:00");
                TimeSpan t = DateTime.Now - _start;
                //TimeList.Add();
                DateTime dt = now + t;
    
                chart1.Series[0].Points.AddXY(dt, Math.Sin(i / 60.0));
                i += 2;
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                _start = DateTime.Now;
                timer1.Start();
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                timer1.Stop();
            }
        }

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Magesh85 Wednesday, September 18, 2019 9:02 AM
    Tuesday, September 17, 2019 6:34 AM

All replies

  • Hi Magesh85,

    Thank you for posting here.

    According to your description, you want to get time format in X-sxis in winforms.

    You could try the following code to get it.

       public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
          
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                this.chart1.ChartAreas[0].AxisX.LabelStyle.Format = "mm:ss";
                chart1.Series[0].XValueType = ChartValueType.DateTime;
            }
            int i = 0;
            private DateTime _start;
            private void timer1_Tick(object sender, EventArgs e)
            {
                DateTime now = DateTime.Parse("00:00");
                TimeSpan t = DateTime.Now - _start;
                //TimeList.Add();
                DateTime dt = now + t;
    
                chart1.Series[0].Points.AddXY(dt, Math.Sin(i / 60.0));
                i += 2;
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                _start = DateTime.Now;
                timer1.Start();
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                timer1.Stop();
            }
        }

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Magesh85 Wednesday, September 18, 2019 9:02 AM
    Tuesday, September 17, 2019 6:34 AM
  • Thank you so much Jack 

    Could you also help me in setting limits for the x - axis? For the above example, the min x-axis label is 00:00 and max x-axis label is 00:20. And these limits are fixed so that the plotting graph won't change in size for the increasing time. 

    Thanks in advance.

    Tuesday, September 17, 2019 10:54 AM
  • Hi Magesh85,

    Thanks for the feedback.

    I have modified my code, you could have a look.

      private void Form1_Load(object sender, EventArgs e)
            {
                this.chart1.ChartAreas[0].AxisX.LabelStyle.Format = "mm:ss";
                // this sets the type of the X-Axis values
                chart1.Series[0].XValueType = ChartValueType.DateTime;
                timer1.Interval = 1000;
    
            }
            int i = 0;
            List<DateTime> TimeList = new List<DateTime>();
            private DateTime _start;
            private void timer1_Tick(object sender, EventArgs e)
            {
              
                DateTime now = DateTime.Parse("00:00");
                TimeSpan t = DateTime.Now - _start;
                //TimeList.Add();
                DateTime dt = now + t;
    
                chart1.Series[0].Points.AddXY(dt, Math.Sin(i / 60.0));
    
    
                i += 1;
                if (i == 20)
                {
                    timer1.Stop();
                }
    
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                _start = DateTime.Now;
                timer1.Start();
            }

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, September 18, 2019 3:23 AM
  • Hi Jack,

    Thanks for your valuable reply. 

    How to make the X-axis label constant? Still it starts from 00:00, 00:01 and so on to 00:20. I just wanted to show the x-axis min and max at the start of the button click/timer, so that the dynamic y values plot in that. 

    Also shall we store these x and y values in the SQL server? As I'm going to show these data once again in another form with the same X-axis labels.

    Wednesday, September 18, 2019 4:24 AM
  • Hi Magesh85,

    >>How to make the X-axis label constant?  I just wanted to show the x-axis min and max at the start of the button click/timer

    I am not sure what you want. Do you mean that you want to only show x-axis 00:00 and 00:20 label or you want to show x-axis from 00:00 to 00:20 every second?

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Wednesday, September 18, 2019 5:28 AM
  • Hi Jack, 

    Sorry for confusing you. Consider the below image as Chart and X-axis in seconds (mm:ss). Now when I click the start button, the chart should have the X-axis from 0 to 30, after that only it should keep plotting the y values. It's like plotting all the y values in the below chart after clicking Start.

    |

    |_______________________

    0  5  10  15  20  25  30

    Also kindly suggest where can I explore these options or learn more about programming since I'm new to these.

    Wednesday, September 18, 2019 9:02 AM