简体   繁体   中英

ZedGraph RealTime Data Plotting C#

        public void DrawingPulseData(byte[] data)
        {

            // Make sure that the curvelist has at least one curve
            if (PulseControl.GraphPane.CurveList.Count <= 0)
                return;

            // Get the first CurveItem in the graph
            LineItem curve = PulseControl.GraphPane.CurveList[0] as LineItem;
            if (curve == null)
                return;

            // Get the PointPairList
            IPointListEdit list = curve.Points as IPointListEdit;
            // If this is null, it means the reference at curve.Points does not
            // support IPointListEdit, so we won't be able to modify it
            if (list == null)
                return;

            double time = (Environment.TickCount - tickStart) / 1000.0;

            for (int i = 0; i < count; i++)
            {
                list.Add(time, (double)data[i]);
            }

            Scale xScale = PulseControl.GraphPane.XAxis.Scale;

            if (time > xScale.Max - xScale.MajorStep)
            {
                xScale.Max = time + xScale.MajorStep;
                xScale.Min = xScale.Max - 30.0;
            }

            // Make sure the Y axis is rescaled to accommodate actual data
            PulseControl.AxisChange();
            // Force a redraw
            PulseControl.Invalidate();

            count = 0;
        }

Hi. I am using this method to draw real time data in zedgraph. count is length of incoming serial port data. This code works fine in timer(20ms) and draws data at each tick. However if i move this method into a class it does not work correctly. It draws too fast and wrong data.

public static void DrawingPulseData(byte[] data,ZedGraphControl zgc,int count, int TickStart)

I changed parameters like this after moving it into class. I changed PulseControl as zgc, tickstart as TickStart. I could not understand why it is not work same as the first code.

在此输入图像描述

At the first picture,using code provided by @discomurray, i wrote this code statement out of the if's scopes. It gives me data like this.

   Scale xScale = zgc.GraphPane.XAxis.Scale;
   xScale.Max = now;
   xScale.Min = now - 30.0;

在此输入图像描述

If i write the same code statement into if's scopes data looks like picture above. It is a 10 seconds record. I don't have such a data with my method.

I assume that tickCount is the start time of the data buffer.

When adding the data to the list you need to change the x value (time) for each point in the list.

public static void DrawPulseData(byte[] data, ZedGraphControl zgc, int count, int tickStart)
{
    double now = Environment.TickCount / 1000.0;

    if (count != 0)
    {
        double span = (now - tickStart);

        double inverseRate = span / count;

        for (int i = 0; i < count; i++)
        {
            list.add(tickStart + ((i+1) * inverseRate), data[i]);
        }
    }

    Scale xScale = zgc.GraphPane.XAxis.Scale;
    xScale.Max = now;
    xScale.Min = now - 30.0;

    PulseControl.AxisChange();
    PulseControl.Invalidate();
}

as for drawing to fast, it will only go as fast as you tell it go.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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