Time on x-axis in C# charts

前端 未结 1 690
情话喂你
情话喂你 2021-01-26 04:28

I have the following graphic in my Windows Form:

I set the X axis to display Time, as shown below:

Here are the methods I call to

相关标签:
1条回答
  • 2021-01-26 05:18

    you could set the Format like this:

    this.chart1.ChartAreas[0].AxisX.LabelStyle.Format = "mm:ss";
    

    if you have a sampling rate of 4 Hz you could also use seconds and milliseconds:

    this.chart1.ChartAreas[0].AxisX.LabelStyle.Format = "ss.fff";
    

    EDIT:

    I would suggest to catch the sampling times in an extra List<DateTime> and feed the chart via AddXY with values. Here is a simple programm that draws a curve with a timer. The timer is set to 500 msec.

    // Constructor
    public Form1()
    {
        InitializeComponent();
        // Format
        this.chart1.ChartAreas[0].AxisX.LabelStyle.Format = "ss.fff";
        // this sets the type of the X-Axis values
        chart1.Series[0].XValueType = ChartValueType.DateTime;
        timer1.Start();
    }
    
    int i = 0;
    List<DateTime> TimeList = new List<DateTime>();
    
    private void timer1_Tick(object sender, EventArgs e)
    {
        DateTime now = DateTime.Now;
        TimeList.Add(now);
    
        chart1.Series[0].Points.AddXY(now, Math.Sin(i / 60.0));
        i+=2;
    }
    

    Now you can watch the x-axis values increment in the format that you like. Hope this helps

    0 讨论(0)
提交回复
热议问题