Click here to Skip to main content
15,911,715 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I'm reading from textboxes and writing to a log.txt file first. Reading from that log.txt file respective to time and date format and displaying in richtextbox and drawing a graph plot for those values. Now in the plot the curve is returning back to zero at last. Please help so dat the plot at last must not come back to zero value
C#
private void button1_Click(object sender, EventArgs e)
       {
           OpenFileDialog ofd = new OpenFileDialog();
           if (ofd.ShowDialog() == DialogResult.OK)
           {
               StreamWriter sw = new StreamWriter(File.OpenWrite(ofd.FileName));
               sw.WriteLine(DateTime.Now);
               sw.WriteLine(textBox1.Text);
               sw.WriteLine(textBox2.Text);
               sw.WriteLine(textBox3.Text);
               sw.WriteLine(textBox4.Text);
               sw.WriteLine(textBox5.Text);
               sw.Dispose();
           }

        }
       List<double> lines = new List<double>();
       private void button2_Click(object sender, EventArgs e)
       {

           string input = textBox6.Text;
           DateTime dt = Convert.ToDateTime(input);
           string str;
           StreamReader s = File.OpenText("c:\\log.txt");
           richTextBox1.Clear();
           zedGraphControl1.GraphPane.CurveList.Clear();

           while ((str = s.ReadLine()) != null)
           {
               if (str.Contains(input))
               {
                   str = s.ReadLine();
                   try
                   {
                       while (!str.Contains('/'))
                       {
                           lines.Add(Convert.ToDouble(str));
                           richTextBox1.Text += str + "\n";
                           str = s.ReadLine();
                       }
                   }
                   catch { }
               }
           }
           s.Dispose();
       }
       private void button3_Click(object sender, EventArgs e)
       {
           double[] z = new double[30];
           double[] x = new double[30];

           int h = 0;
           try
           {
               foreach (double line in lines)
               {
                   MessageBox.Show(line.ToString());

                       z[h] = line;
                       x[h] = (double)h;
                       h++;
                }
            }
            catch
           {

           }
           zedGraphControl1.GraphPane.CurveList.Clear();

           // GraphPane object holds one or more Curve objects (or plots)
           GraphPane myPane = zedGraphControl1.GraphPane;

           // PointPairList holds the data for plotting, X and Y arrays
           PointPairList spl1 = new PointPairList(x, z);


           // Add cruves to myPane object
           LineItem myCurve1 = myPane.AddCurve("Sine Wave", spl1, Color.Blue, SymbolType.None);


           myCurve1.Line.Width = 3.0F;

           myPane.Title.Text = "My First Plot";

           // I add all three functions just to be sure it refeshes the plot.
           zedGraphControl1.AxisChange();
           zedGraphControl1.Invalidate();
           zedGraphControl1.Refresh();
           }
Posted
Updated 7-May-14 10:24am
v2
Comments
CHill60 9-Feb-14 14:14pm    
What is a "d curve" ?
OriginalGriff 7-May-14 16:36pm    
It's moron for "the curve"...
CHill60 7-May-14 17:07pm    
Love it!
Manas Bhardwaj 7-May-14 17:17pm    
:)

1 solution

I'd guess that you don't have 30 real points, so when you are loading the z and x arrays you have leftover entries that contain 0.
Make the arrays exactly as large as required:
C#
private void button3_Click(object sender, EventArgs e)
{
  double[] z = new double[lines.Count];
  double[] x = new double[lines.Count];

  int h = 0;
  try
  {
    foreach (double line in lines)
    {
      MessageBox.Show(line.ToString());

      z[h] = line;
      x[h] = (double)h;
      h++;
    }
  }
  catch  // An ignoring catch is NEVER the right way to handle errors!
  {
  }
  zedGraphControl1.GraphPane.CurveList.Clear();

  // GraphPane object holds one or more Curve objects (or plots)
  GraphPane myPane = zedGraphControl1.GraphPane;

  // PointPairList holds the data for plotting, X and Y arrays
  PointPairList spl1 = new PointPairList(x, z);
  // remainder of button3_Click here...

Using Linq, this could be further simplified:
C#
private void button3_Click(object sender, EventArgs e)
{
  double[] z = lines.ToArray();
  double[] x = Enumerable.Range(0, lines.Count).ToArray();

  zedGraphControl1.GraphPane.CurveList.Clear();

  // GraphPane object holds one or more Curve objects (or plots)
  GraphPane myPane = zedGraphControl1.GraphPane;

  // PointPairList holds the data for plotting, X and Y arrays
  PointPairList spl1 = new PointPairList(x, z);
  // remainder of button3_Click here...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900