We help IT Professionals succeed at work.
Get Started

C# Zedgraph Plotting Line Chart

ocaccy
ocaccy asked
on
4,805 Views
Last Modified: 2013-11-13
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
Comment
Watch Question
Awarded 2011
Awarded 2011
Commented:
This problem has been solved!
Unlock 1 Answer and 8 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE