3
        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.

enter image description here

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;

enter image description here

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.

Blast
  • 955
  • 1
  • 17
  • 40

1 Answers1

2

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.

discomurray
  • 14,411
  • 1
  • 21
  • 11
  • Yea you assumed right it is the start time of data buffer. I tried your code. There is something wrong here. I copied Scale xScale = zgc.GraphPane.XAxis.Scale; and pasted it out of the if's scopes. However it draws something different. I uploaded some pictures related with your code. – Blast Sep 12 '13 at 07:36
  • I fixed the location of xScale initialization. how is data being filled? is it new data every call or the same buffer with more points in it? is the tickStart a new value call? are you using threading? – discomurray Sep 12 '13 at 16:43
  • `int tickStart;` int Form_Load i use `tickStart = Environment.TickCount;` Then i run the method with timer tick (20ms). Maybe we should add your code this if statement. `if (time > xScale.Max - xScale.MajorStep) { xScale.Max = time + xScale.MajorStep; xScale.Min = xScale.Max - 30.0; }` – Blast Sep 13 '13 at 09:41