簡體   English   中英

C#圖表中X軸上的時間

[英]Time on x-axis in C# charts

我的Windows Form具有以下圖形:

在此處輸入圖片說明

我將X軸設置為顯示時間,如下所示:

在此處輸入圖片說明

這是我用來繪制圖形的方法:

        private void funcaoQualquer()
        {
            while (true)
            {
                funcaoArray[funcaoArray.Length - 1] = hScrollBar1.Value;
                Array.Copy(funcaoArray, 1, funcaoArray, 0, funcaoArray.Length - 1);
                if (chart1.IsHandleCreated)
                {
                    this.Invoke((MethodInvoker)delegate { atualizaGraficoFuncaoQualquer(hScrollBar1.Value); });
                }
                else
                {
                    //...
                }
                Thread.Sleep(250);
            }
        }

        private void atualizaGraficoFuncaoQualquer(int valor)
        {
            for (int i = 0; i < funcaoArray.Length - 1; ++i)
            {
                chart1.Series["Series1"].Points.Add(valor);
            }
        }

但是,繪制圖形並隨時間推移時,X軸不會更改值。 我認為它顯示為小時。 如何更改為分鍾或秒?

您可以這樣設置格式:

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

如果采樣率為4 Hz,則還可以使用秒和毫秒:

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

編輯:

我建議在一個額外的List<DateTime>捕獲采樣時間,並通過AddXY將值輸入圖表。 這是一個使用計時器繪制曲線的簡單程序。 計時器設置為500毫秒。

// 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;
}

現在,您可以觀看x軸值以您喜歡的格式遞增。 希望這可以幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM