Search code examples
c#graphzedgraph

Remove GraphCurve list on ZedGraph


I am new in C# and trying to create zedgraph of lessor sensor. First I create a global variable and write code for graph. My graph is working but after reached to the point of 100 of x axis it will overlap to old line. z1.GraphPane.CurveList.Clear(); command is not working. I tried listPointsOne.clear(); command also but that clear the line and doesn't show anything on graph. Please help me out with this. My code is below :

        string DatafromCOM;
        double[] x = new double[100];
        double[] y = new double[100];
        int i;
        PointPairList listPointsOne = new PointPairList();
 private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            try
            {
                while (serialPort1.BytesToRead > 0)
                {
                    DatafromCOM = serialPort1.ReadLine();
                    double iData;
                    var ok = double.TryParse(txtKQ.Text, out iData);
                    if (DatafromCOM.Trim() != "" && ok)
                    {
                        i= (i + 1) % 100;
                        x[i] = i;
                        y[i] = iData;
                        listPointsOne.Add(i,iData);

                    }
                }

            }
            catch { }
        }
 private void timer1_Tick(object sender, EventArgs e)
        {
                z1.GraphPane.CurveList.Clear();
                z1.GraphPane.AddCurve(null, listPointsOne, Color.Red, SymbolType.None);
                z1.AxisChange();
                z1.Invalidate();
        }

Solution

  • You should clear the curvlist

    string DatafromCOM;
            double[] x = new double[100];
            double[] y = new double[100];
            int i;
            PointPairList listPointsOne = new PointPairList();
     private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
            {
                try
                {
                    while (serialPort1.BytesToRead > 0)
                    {
                        DatafromCOM = serialPort1.ReadLine();
                        double iData;
                        var ok = double.TryParse(txtKQ.Text, out iData);
                        if (DatafromCOM.Trim() != "" && ok)
                        {
                            i= (i + 1) % 100;
                            x[i] = i;
                            y[i] = iData;
                            listPointsOne.Add(i,iData);
    
                        }
                        z1.GraphPane.CurveList.Clear(); // Change here
                    }
    
                }
                catch { }
            }
    
    
    
    private void timer1_Tick(object sender, EventArgs e)
        {
    
                z1.GraphPane.AddCurve(null, listPointsOne, Color.Red, SymbolType.None);
                z1.AxisChange();
                z1.Invalidate();
        }