如何使用C#在ZedGraph的XAxis上绘制系统时间?

时间:2013-10-26 07:56:37

标签: c# zedgraph

我搜索了一些ZedGraph的例子,但是我无法执行我想要的。我每20毫秒绘制一次实时数据,我想在x轴上显示系统时间(使用ZedGraph类XAxis)。但是,当我尝试在x轴上绘制毫秒时,我看不到任何数据。这是我的代码:

//X-Axis Settings
pane.XAxis.Scale.MinorStep = 1;
pane.XAxis.Scale.MajorStep = 5;
pane.XAxis.Type = AxisType.Date;
pane.XAxis.Scale.Format = "HH:mm:ss.fff";
pane.XAxis.Scale.Min = new XDate(DateTime.Now);
pane.XAxis.Scale.Max = new XDate(DateTime.Now.AddSeconds(10));
pane.XAxis.Scale.MinorUnit = DateUnit.Second;
pane.XAxis.Scale.MajorUnit = DateUnit.Second;

XDate time = new XDate(DateTime.Now.ToOADate());
for (int i = 1; i < 16; i++)
{
    listAuido.Add(time, (double)Read_Data1[i]);
}

Scale xScale1 = zgcMasterPane.MasterPane.PaneList[0].XAxis.Scale;
if (time.XLDate > xScale1.Max)
{
   xScale1.Max = (XDate)(DateTime.Now.AddSeconds(1));
   xScale1.Min = (XDate)(DateTime.Now.AddSeconds(-20));
}

编辑:这个代码结构解决了我的问题。

1 个答案:

答案 0 :(得分:3)

以下代码绘制了相同x点的所有数据!

for (int i = 1; i < 16; i++)
{
    listAuido.Add((XDate)(DateTime.Now.Millisecond), (double)Read_Data1[i]);
}

如果您不希望XAxis显示为日期格式,为什么将其设置为日期格式?

好的,试试这个:

//Declare the x coordinate (time) variable
double xValue = 0;

//Setting the axis
pane.XAxis.Scale.MinorStep = 1;
pane.XAxis.Scale.MajorStep = 5;
pane.XAxis.Scale.Max = 0;
pane.XAxis.Scale.Min = -10;

//drawing the data
private void draw(double dataValue)
{
    LineItem curve1 = zedGraphControl1.GraphPane.CurveList[0] as LineItem;
    IPointListEdit list1 = curve1.Points as IPointListEdit;

    list1.Add(xValue*(20/1000), dataValue); //

    //Scroll
    Scale XScale = zedGraphControl1.GraphPane.XAxis.Scale as Scale;
    XScale.Max = xValue*(20/1000);
    XScale.Min = XScale.Max - 10;

    xValue++;
    zedGraphControl1.AxisChange();
    zedGraphControl1.Invalidate();
}

//Now you call the function draw every 20 ms using a [Timer][1] for example
private void timer1_Tick(object sender, EventArgs e)
{
    draw(data[xValue]);
}

顺便说一下,我没有在这里使用MasterPane。