C#图表中X轴上的时间 [英] Time on x-axis in C# charts
问题描述
我的 Windows窗体
中具有以下图形:
我将X轴设置为显示时间,如下所示:
I set the X axis to display Time, as shown below:
以下是我用来绘制图形的方法:
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);
}
}
但是,绘制图形并随着时间推移, X轴不更改值。我认为它显示为小时。如何更改为分钟或秒?
However, when drawing the graph and over time, the X axis does not change the values. I think it's being displayed as hours. How do I change to minutes or seconds?
推荐答案
您可以将格式设置为:
this.chart1.ChartAreas[0].AxisX.LabelStyle.Format = "mm:ss";
如果采样率为4 Hz,则还可以使用秒和毫秒:
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";
编辑:
我建议在额外的 List< DateTime>
中捕获采样时间,并通过 AddXY
填充图表与价值观。这是一个使用计时器绘制曲线的简单程序。计时器设置为500毫秒。
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;
}
现在,您可以按照自己喜欢的格式观看x轴值的增加。希望这会有所帮助
Now you can watch the x-axis values increment in the format that you like. Hope this helps
这篇关于C#图表中X轴上的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!