Search code examples
c#checkboxzedgraph

Add/remove curve with checkbox on zedgraph


Hello i am designing a graph that displays a line graph from open file I have got it all working so this isnt the problem, The problem is i want checkboxes to be able to add the line if the check box is checked or to remove the line if the check box is unchecked, i want the graph to refresh on changing the state of the check box to add/remove the line accordingly. I am having trouble with the checkbox and the graph refreshing

if (LCheck.Checked)
{
    LineItem line1 = myPane.AddCurve("LINE1",
        LL, Color.Red, SymbolType.None);
    zgc.AxisChange();
    zgc.Invalidate();
    zgc.Refresh(); 
}

else if (!LCheck.Checked)
{
    LL.Clear();
    zgc.AxisChange();
    zgc.Invalidate();
    zgc.Refresh();
}

Is this the way to do it or is there a better way?

Update:

     public void CreateGraph(ZedGraphControl zgc)
        {
          //HRCheck.Checked = true;
          SpeedCheck.Checked = true;
          AltCheck.Checked = true;
        PowerCheck.Checked = true; 


        Form1 form = new Form1(fileOpen);
        GraphPane myPane = zgc.GraphPane;
double x, y1, y2, y3, y4;
 x = 0; 
   PointPairList LL = new PointPairList();
for (int i = 0; i < form.dataGridView1.Rows.Count; ++i)
        {

        y1 = (Convert.ToDouble(form.dataGridView1.Rows[i].Cells["VALUE1"].Value));


LL.Add(x, y1);


x++;


 }
  LineItem line1 = myPane.AddCurve("LINE1",
        LL, Color.Red, SymbolType.None);

 zgc.AxisChange();
      zgc.Invalidate();
       zgc.Refresh(); 
}

This is my create graph method

I have put all relevant code inside the checkchanged event but it doesnt seem to be working, The fileOpen and datagridview are for another aspect of my program but they work fine.


Solution

  • If you want to observe changes of Checked property of Checkbox, you need to add your code Checkbox.CheckedChanged to event handler.

    You can refactor your code to

    if (LCheck.Checked)
    {
        LineItem line1 = myPane.AddCurve("LINE1",
            LL, Color.Red, SymbolType.None);
    }
    else
    {
        LL.Clear();
    }
    zgc.AxisChange();
    zgc.Invalidate();
    zgc.Refresh(); 
    

    Update:

    I think, you are recreating graph everytime you call CreateGraph method. In this case, you can call this on LCheck.ChechedChanged event. And check for Checked state of LCheck.