简体   繁体   English

C#图表中X轴上的时间

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

I have the following graphic in my Windows Form : 我的Windows Form具有以下图形:

在此处输入图片说明

I set the X axis to display Time, as shown below: 我将X轴设置为显示时间,如下所示:

在此处输入图片说明

Here are the methods I call to draw the graph: 这是我用来绘制图形的方法:

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

However, when drawing the graph and over time, the X axis does not change the values. 但是,绘制图形并随时间推移时,X轴不会更改值。 I think it's being displayed as hours. 我认为它显示为小时。 How do I change to minutes or seconds? 如何更改为分钟或秒?

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: 如果采样率为4 Hz,则还可以使用秒和毫秒:

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. 我建议在一个额外的List<DateTime>捕获采样时间,并通过AddXY将值输入图表。 Here is a simple programm that draws a curve with a timer. 这是一个使用计时器绘制曲线的简单程序。 The timer is set to 500 msec. 计时器设置为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;
}

Now you can watch the x-axis values increment in the format that you like. 现在,您可以观看x轴值以您喜欢的格式递增。 Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM