How to display two plots in a chart?? RRS feed

  • Question

  • hello friends 

    i have two variable data that received through serials port , now i want to plotting real time  this two variables in a chart  . 

    I did it ,but have problem when the points of the two variables are plotted the chart disappears after a few seconds. 

    like this picture :

    what is wrong??


    this is part of my code : 

            private void button1_Click(object sender, EventArgs e)
                if (comboBox2.Text == "")
                    MessageBox.Show("!!!pleas select BaudRate");
                    serialPort1.BaudRate = int.Parse(comboBox2.Text);
                    serialPort1.PortName = portName_comobox.Text; 
                    serialPort1.Parity = Parity.None;
                    serialPort1.DataBits = 8;
                    serialPort1.StopBits = StopBits.One;
                    serialPort1.Handshake = Handshake.None;
                    serialPort1.DataReceived += sensport_DataReceived;
                        textBox1.Text = "";
                    catch (Exception ex)
                        MessageBox.Show(ex.Message, "Error");
            void sensport_DataReceived(object sender, SerialDataReceivedEventArgs e)
                data = serialPort1.ReadLine().ToString(); // 
                int tt = data.IndexOf('|');
                long result1;
                long result2;
                if (tt > -1)
                    string  command = data.Substring(0, tt);
                    string  command2 = data.Substring(tt + 1, 2 );
                    result1 = Convert.ToInt64(command); //convert string to long 
                    result2 = Convert.ToInt64(command2);//convert string to long 
                    this.chart1.Series["data1"].Points.AddXY(rt, result2); 
                    this.chart2.Series["data2"].Points.AddXY(rt, result1 );

    I also say that with plotting one data this problem does not occur
    • Edited by ashkan4818 Saturday, April 14, 2018 5:26 PM
    Saturday, April 14, 2018 5:19 PM

All replies

  • Chart series points are type double, not type long. Although narrowing may not need conversion from Long to Double which I doubt.

    Place Option Strict On at the top of your code window to see if errors popup.

    And I don't know what value rt is as I don't see it anywhere in your code except where you AddXY(rt, result2).

    Two separate plots require two series. Also see Getting Started with Chart Controls.

    La vida loca

    Saturday, April 14, 2018 5:31 PM
  •         private void timer2_Tick(object sender, EventArgs e)
                rt += 0.1;

    I have set the x-axis step with rt 
    Saturday, April 14, 2018 5:39 PM
  • result1 and result2 are type Long and need to be converted to Double if Double can handle their values as the charts points are type Double. rt needs to be converted too if it is not type Double.

    Type long is "–2,147,483,648 to 2,147,483,647" and type double "contains 64 bits: 1 for sign, 11 for the exponent, and 52 for the mantissa. Its range is +/–1.7E308 with at least 15 digits of precision".

    La vida loca

    Saturday, April 14, 2018 5:41 PM
  • i do this but the problem is still there. 

    i think i have to do that the Previous data should be deleted, for example after every 3 seconds and the chart screen Just draw  the late data received from the serial port.

    how can i do this ????

    Now this chart plotting the data from the zero to infinity and just compressed.



    • Edited by ashkan4818 Saturday, April 14, 2018 6:41 PM
    Saturday, April 14, 2018 6:39 PM
  • This "this.chart1.Series["data1"].Points" is a collection of points. To empty it I believe use "this.chart1.Series["data1"].Points.Clear".

    Or if you want it to contain some amount of points you could loop with a For/Next. I don't know C# but this is how I would do the If-For/Next in VB.Net. Telerik couldn't convert it to C#.

    If this.chart1.Series["data1"].Points.Count > 3 Then
        For i = this.chart1.Series["data1"].Points.Count - 1 To 3 Step - 1
    End If

    La vida loca

    Saturday, April 14, 2018 6:54 PM