Link to home
Start Free TrialLog in
Avatar of ocaccy
ocaccyFlag for Japan

asked on

C# Zedgraph Plotting Line Chart

I need the ZedGraph plot a line Chart.
I am getting the data from serial port and sending to the ZedGraph.
To see the plot, must use the Set Scale to Default.
How to display the graph?
private void GRAPHICS2mm()
        {
            zed2mm.GraphPane.CurveList.Clear();
            {
                
                #region 2 mm zed2mm
                // GraphPane SETTINGS
                GraphPane myPane02m = zed2mm.GraphPane;

                //myPane02m.AddCurve("test curve", new ZedGraph.PointPairList(), Color.Red);
                myPane02m.Legend.IsVisible = true;
                myPane02m.Legend.Position = LegendPos.Float;
                myPane02m.Legend.Location = new Location(0.54, 0.02, CoordType.PaneFraction);
                myPane02m.Legend.FontSpec.Size = 18;
                myPane02m.Legend.Border.IsVisible = false;

                myPane02m.Fill.Color = System.Drawing.Color.Transparent;        // Setta a Cor no Grafico.
                myPane02m.Title.Text = "2µm - 7µm";                         // Setta o Titulo do Grafico.
                myPane02m.Title.FontSpec.Size = 29;


                // Y AXIS SETTINGS
                myPane02m.YAxis.Title.IsVisible = false;
                myPane02m.YAxis.MajorGrid.IsVisible = true;                     // Setta Linhas no Eixo Y ou seja na Horizontal.
                myPane02m.YAxis.MajorGrid.DashOff = 1;                          // Seta a Intensidade da Linha no Eixo Y.

                // X AXIS SETTINGS
                myPane02m.XAxis.Title.IsVisible = false;
                myPane02m.XAxis.MajorGrid.DashOff = 1;                          // Seta a Intensidade da Linha no Eixo X.
                myPane02m.XAxis.MajorGrid.IsVisible = true;                     // Setta Linhas no Eixo X ou seja na Vertical.

                myPane02m.XAxis.Scale.FontSpec.Angle = 0;                      // Setta o Angulo do Scale do Eixo X.
                myPane02m.XAxis.Scale.FontSpec.Family = "Arial, Narrow";        // Setta a Fonte da Scale no Eixo X.
                myPane02m.XAxis.Scale.FontSpec.FontColor = Color.Black;       // Setta a Cor da Legenda do Dado que Entrara no Eixo X.
                myPane02m.XAxis.Scale.FontSpec.IsBold = true;                   // Setta Negrito na Scale no Eixo X.
                myPane02m.XAxis.Scale.FontSpec.Size = 30;                       // Setta o Tamanho da Fonte da Scale no Eixo X.

                // Set the initial viewed range
                myPane02m.XAxis.Scale.Min = new XDate(DateTime.Now);            // We want to use time from now
                myPane02m.XAxis.Scale.Max = new XDate(DateTime.Now.AddMinutes(24));        // to 5 min per default
                myPane02m.XAxis.Scale.Min = 0;//new XDate(DateTime.Now);            // We want to use time from now

                int Soma3Tempos = Convert.ToInt32(nUD_KyuSokuX2.Value / 60) + Convert.ToInt32(nUD_SeitiX2.Value / 60) + Convert.ToInt32(nUD_RyuSokuX2.Value / 60);


                myPane02m.XAxis.Scale.Max = Soma3Tempos;//new XDate(DateTime.Now.AddMinutes(24));        // to 5 min per default
                myPane02m.XAxis.Scale.MinorStep = 1;
                myPane02m.XAxis.Scale.MajorStep = 1.0;


                myPane02m.XAxis.Title.FontSpec.FontColor = Color.DarkViolet;    // Setta a Cor do Titulo no Eixo X.


                // Horizontal pan and zoom allowed
                zed2mm.IsEnableHPan = false;
                zed2mm.IsEnableHZoom = false;

                // Vertical pan and zoom not allowed
                zed2mm.IsEnableVPan = false;
                zed2mm.IsEnableVZoom = false;

                //show the cursor Value
                zed2mm.IsShowCursorValues = false;

                PointPairList lista02m_1 = new PointPairList();
                PointPairList lista02m_2 = new PointPairList();
                PointPairList lista02m_3 = new PointPairList();
                PointPairList lista02m_4 = new PointPairList();
                PointPairList lista02m_5 = new PointPairList();
                PointPairList lista02m_6 = new PointPairList();


                // Initially, a curve is added with no data points (list is empty)

                LineItem curve02m_1 = myPane02m.AddCurve("ID 01", lista02m_1, Color.Red, SymbolType.None);
                curve02m_1.Line.IsSmooth = true;
                curve02m_1.Line.SmoothTension = 0.3F;
                curve02m_1.Symbol.Size = 2;

                LineItem curve02m_2 = myPane02m.AddCurve("ID_02", lista02m_2, Color.Blue, SymbolType.None);
                curve02m_2.Line.IsSmooth = true;
                curve02m_2.Line.SmoothTension = 0.3F;

                LineItem curve02m_3 = myPane02m.AddCurve("ID_03", lista02m_3, Color.Black, SymbolType.None);
                curve02m_3.Line.IsSmooth = true;
                curve02m_3.Line.SmoothTension = 0.3F;
                curve02m_3.Symbol.Size = 3;

                LineItem curve02m_4 = myPane02m.AddCurve("ID_04", lista02m_4, Color.BurlyWood, SymbolType.None);
                curve02m_4.Line.IsSmooth = true;
                curve02m_4.Line.SmoothTension = 0.3F;

                LineItem curve02m_5 = myPane02m.AddCurve("ID_05", lista02m_5, Color.Chartreuse, SymbolType.None);
                curve02m_5.Line.IsSmooth = true;
                curve02m_5.Line.SmoothTension = 0.3F;

                LineItem curve02m_6 = myPane02m.AddCurve("ID_06", lista02m_6, Color.Coral, SymbolType.None);
                curve02m_6.Line.IsSmooth = true;
                curve02m_6.Line.SmoothTension = 0.3F;

                // Scale the axes
                zed2mm.AxisChange();

                // Redraw the axes
                zed2mm.Invalidate();

                // Save the beginning time for reference
                //TickStart=Environment.TickCount;

                #endregion
            }
        }

        private void Draw2(string id01_02, string id02_02, string id03_02, string id04_02, string id05_02, string id06_02)
        {
            // Make sure that the curvelist has at least one curve


            id01_02 = lbl_id01_02mm.Text;
            id02_02 = lbl_id02_02mm.Text;
            id03_02 = lbl_id03_02mm.Text;
            id04_02 = lbl_id04_02mm.Text;
            id05_02 = lbl_id05_02mm.Text;
            id06_02 = lbl_id06_02mm.Text;

            double d_id01_02mm;
            double d_id02_02mm;
            double d_id03_02mm;
            double d_id04_02mm;
            double d_id05_02mm;
            double d_id06_02mm;

            double.TryParse(id01_02, out d_id01_02mm);
            double.TryParse(id02_02, out d_id02_02mm);
            double.TryParse(id03_02, out d_id03_02mm);
            double.TryParse(id04_02, out d_id04_02mm);
            double.TryParse(id05_02, out d_id05_02mm);
            double.TryParse(id06_02, out d_id06_02mm);

            if (zed2mm.GraphPane.CurveList.Count <= 0)
                return;

            // Get the first CurveItem in the graph
            LineItem curve02m_1 = zed2mm.GraphPane.CurveList[0] as LineItem;
            LineItem curve02m_2 = zed2mm.GraphPane.CurveList[1] as LineItem;
            LineItem curve02m_3 = zed2mm.GraphPane.CurveList[2] as LineItem;
            LineItem curve02m_4 = zed2mm.GraphPane.CurveList[3] as LineItem;
            LineItem curve02m_5 = zed2mm.GraphPane.CurveList[4] as LineItem;
            LineItem curve02m_6 = zed2mm.GraphPane.CurveList[5] as LineItem;
            if (curve02m_1 == null)
                return;
            if (curve02m_2 == null)
                return;
            if (curve02m_3 == null)
                return;
            if (curve02m_4 == null)
                return;
            if (curve02m_5 == null)
                return;
            if (curve02m_6 == null)
                return;

            // Get the PointPairList
            IPointListEdit lista02m_1 = curve02m_1.Points as IPointListEdit;
            IPointListEdit lista02m_2 = curve02m_2.Points as IPointListEdit;
            IPointListEdit lista02m_3 = curve02m_3.Points as IPointListEdit;
            IPointListEdit lista02m_4 = curve02m_4.Points as IPointListEdit;
            IPointListEdit lista02m_5 = curve02m_5.Points as IPointListEdit;
            IPointListEdit lista02m_6 = curve02m_6.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 (lista02m_1 == null)
                return;
            if (lista02m_2 == null)
                return;
            if (lista02m_3 == null)
                return;
            if (lista02m_4 == null)
                return;
            if (lista02m_5 == null)
                return;
            if (lista02m_6 == null)
                return;

            //double time = (Environment.TickCount-TickStart)/1000.0;

            double dyme;
            dyme = ((double)new XDate(DateTime.Now));
            lista02m_1.Add(dyme, d_id01_02mm);
            lista02m_2.Add(dyme, d_id02_02mm);
            lista02m_3.Add(dyme, d_id03_02mm);
            lista02m_4.Add(dyme, d_id04_02mm);
            lista02m_5.Add(dyme, d_id05_02mm);
            lista02m_6.Add(dyme, d_id06_02mm);

            // Make sure the Y axis is rescaled to accommodate actual data
            zed2mm.AxisChange();
            // Force a redraw
            zed2mm.Invalidate();
        }

Open in new window

image-1.PNG
image-2.PNG
Avatar of for_yan
for_yan
Flag of United States of America image

I was trying to compile your code.

   But I guess, I'm missing all of these:

          id01_02 = lbl_id01_02mm.Text;
            id02_02 = lbl_id02_02mm.Text;
            id03_02 = lbl_id03_02mm.Text;
            id04_02 = lbl_id04_02mm.Text;
            id05_02 = lbl_id05_02mm.Text;
            id06_02 = lbl_id06_02mm.Text;


Avatar of ocaccy

ASKER

lbl_id01_02mm.Text;
lbl_id02_02mm.Text;
lbl_id03_02mm.Text;
lbl_id04_02mm.Text;
lbl_id05_02mm.Text;
lbl_id06_02mm.Text;

private String PrepareData(String StringIn)
        {
            // The names of the first 32 characters
            string[] charNames = {
                                 // "NUL", "SOH", "STX", "ETX", "EOT",
                //"ENQ", "ACK", "BEL", "BS", "TAB", "LF", "VT", "FF", "CR", "SO", "SI",
                //"DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB", "CAN", "EM", "SUB",
                //"ESC", "FS", "GS", "RS", "US", "Space"
                                 };

            string StringOut = "";

            foreach (char c in StringIn)
            {
                if (Settings.Option.HexOutput)
                {
                    StringOut = StringOut + String.Format("{0:X2} ", (int)c);
                }
                else if (c < 32 && c != 9)
                {
                    StringOut = StringOut + "";// +"<"+charNames[c]+">";

                 
                    //StringOut = StringOut + String.Format("[{0:X2}]", (int)c);
                }
                else
                {
                    StringOut = StringOut + c;
                }
            }
            return StringOut;
        }

        /// <summary>
        /// Partial line for AddData().
        /// </summary>
        private Line partialLine = null;

        /// <summary>
        /// Add data to the output.
        /// </summary>
        /// <param name="StringIn"></param>
        /// <returns></returns>
        private Line AddData(String StringIn)
        {
            String StringOut = PrepareData(StringIn);

            // if we have a partial line, add to it.
            if (partialLine != null)
            {
                // tack it on
                partialLine.Str = partialLine.Str + StringOut;
                outputList_Update(partialLine);
                return partialLine;
            }

            return outputList_Add(StringOut, receivedColor);
        }

        // delegate used for Invoke
        internal delegate void StringDelegate(string data);
        bool dataReceived = false;

        public void OnDataReceived(string dataIn)
        {
            //Handle multi-threading
            if (InvokeRequired)
            {
                Invoke(new StringDelegate(OnDataReceived), new object[] { dataIn });
                return;
            }

            try
            {
                // pause scrolling to speed up output of multiple lines
                bool saveScrolling = scrolling;
                scrolling = false;

                // if we detect a line terminator, add line to output
                int index;
                while (dataIn.Length > 0 &&
                    ((index = dataIn.IndexOf("\r")) != -1 ||
                    (index = dataIn.IndexOf("\n")) != -1))
                {
                    String StringIn = dataIn.Substring(0, index);
                    dataIn = dataIn.Remove(0, index + 1);

                    logFile_writeLine(AddData(StringIn).Str);
                    logFile_writeLine1(AddData(StringIn).Str);
                    //listBox3.Items.Add(AddData(StringIn).Str);
                    partialLine = null;     // terminate partial line
                }

                // if we have data remaining, add a partial line
                if (dataIn.Length > 0)
                {
                    partialLine = AddData(dataIn);
                }

                // restore scrolling
                scrolling = saveScrolling;
                outputList_Scroll();
                listBox1_Scroll();
            }
            finally
            {
                // Set your flag
                this.dataReceived = true;
            }
        }


lbl_id01_02mm.Text = stringOut.Trim().Substring(4, 5);
                        lbl_id01_07mm.Text = stringOut.Trim().Substring(10, 5);
                        lbl_id01_10mm.Text = stringOut.Trim().Substring(16, 5);

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ocaccy

ASKER

Thank you for_yan.

I send to you, one code.
Thanks a lot, I saw the code in another question, I'll certainly look at it, when I have a little bit of time, and if I'm lucky
might even be of some help. I'll let you know in any case.
Avatar of ocaccy

ASKER

Thanks a lot for_yan.
Avatar of ocaccy

ASKER

Thank you, for help me to solve my needs.

Best Regards,
ocaccy
Thanks to you. It was good to learn about this library thorough your question.