c# - C# 图表中 x 轴上的时间

标签 c# winforms graph charts

我的 Windows 窗体 中有以下图形:

enter image description here

我设置X轴显示时间,如下图:

enter image description here

以下是我调用来绘制图表的方法:

        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 提供图表与值(value)观。这是一个用计时器绘制曲线的简单程序。定时器设置为 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 轴值的增量。希望这有帮助

关于c# - C# 图表中 x 轴上的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41936346/

相关文章:

c# - 实现线程安全单例模式的最安全/正确方法是什么?

vb.net - 取消关闭时如何重置关闭原因

python - matplotlib 缺少样式模块?

c# - C#中继承类的问题

c# - 生成程序平铺背景

c# - 意外的格式异常

c# - 如何: *. csv -->行-->someArray-->修改

c# - 检查数据库是否已更新

java - 利润最大化,作业调度

php - 从记录的文本文件中在网页中显示数据