我的 Windows 窗体
中有以下图形:
我设置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
提供图表与值(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/