2

I have a line chart in ZedGraph. The data is coming from serial port every second. The chart is running. But do not roll (scroll) on the X axis and is compressed in the left corner. I need the chart scroll from right to left and the date and time appear in the X axis

 #region 2 mm zed2mm
            GraphPane myPane02m=zed2mm.GraphPane;
            myPane02m.Fill.Color=System.Drawing.Color.GhostWhite;
            myPane02m.Title.Text="Particle Counter: 2μm";
            myPane02m.YAxis.Title.Text="Particles";

            // X AXIS SETTINGS

            myPane02m.XAxis.Title.Text="Date & Time";
            myPane02m.XAxis.Scale.FontSpec.Family="Arial, Narrow";
            myPane02m.XAxis.Scale.FontSpec.Size=14;
            myPane02m.XAxis.Scale.FontSpec.IsBold = false;
            myPane02m.XAxis.MajorGrid.IsVisible = true;
            myPane02m.YAxis.MajorGrid.IsVisible = true;
            myPane02m.XAxis.MajorGrid.DashOff = 15;
            myPane02m.YAxis.MajorGrid.DashOff = 15;
            myPane02m.XAxis.Scale.IsPreventLabelOverlap = false;
            myPane02m.XAxis.Type=AxisType.Date;
            myPane02m.XAxis.Scale.Format = "HH:mm:ss\nyy/MM/dd";

            myPane02m.XAxis.Scale.Min = new XDate(DateTime.Now);       // We want to use time from now
            myPane02m.XAxis.Scale.Max = new XDate(DateTime.Now.AddMinutes(5));        // to 5 min per default
            myPane02m.XAxis.Scale.MinorUnit = DateUnit.Second;         // set the minimum x unit to time/seconds
            myPane02m.XAxis.Scale.MajorUnit = DateUnit.Minute;         // set the maximum x unit to time/minutes

            myPane02m.XAxis.Scale.MinorStep = 1;
            myPane02m.XAxis.Scale.MajorStep = 50;

            myPane02m.XAxis.MinorTic.IsAllTics = true;
            myPane02m.XAxis.MajorTic.IsOpposite = true;
            myPane02m.XAxis.Scale.IsSkipCrossLabel = false;

            //myPane02m.XAxis.Scale.MinGrace = 0;
            //myPane02m.XAxis.Scale.MaxGrace = 0;

            //myPane02m.XAxis.Scale.MinorUnit = DateUnit.Second;
            //myPane02m.XAxis.Scale.MajorUnit = DateUnit.Minute;

            //myPane02m.XAxis.Scale.Min = DateTime.Now.AddSeconds(-60).ToOADate();
            //myPane02m.XAxis.Scale.Max = DateTime.Now.ToOADate();


            // Save 6000 points. At 50 ms sample rate, this is one minute
            // The RollingPointPairList is an efficient storage class that always
            // keeps a rolling set of point data without needing to shift any data values
            RollingPointPairList lista02m_1=new RollingPointPairList(6000);
            RollingPointPairList lista02m_2=new RollingPointPairList(6000);
            RollingPointPairList lista02m_3=new RollingPointPairList(6000);
            RollingPointPairList lista02m_4=new RollingPointPairList(6000);
            RollingPointPairList lista02m_5=new RollingPointPairList(6000);
            RollingPointPairList lista02m_6=new RollingPointPairList(6000);

            // Initially, a curve is added with no data points (list is empty)
            // Color is blue, and there will be no symbols
            LineItem curve02m_1=myPane02m.AddCurve("ID 01",lista02m_1,Color.Red,SymbolType.Circle);
            curve02m_1.Line.IsSmooth=true;
            curve02m_1.Line.SmoothTension=0.3F;

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

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

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

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

            LineItem curve02m_6=myPane02m.AddCurve("ID_06",lista02m_6,Color.Coral,SymbolType.Square);
            curve02m_6.Line.IsSmooth=true;
            curve02m_6.Line.SmoothTension=0.3F;
            // Sample at 50ms intervals

            // Scale the axes
            zed2mm.AxisChange();

            // Redraw the axes
            zed2mm.Invalidate();

            // Save the beginning time for reference
            TickStart=Environment.TickCount;
            #endregion
Otiel
  • 18,404
  • 16
  • 78
  • 126
soushinsha
  • 83
  • 3
  • 11

1 Answers1

5

Try

zed2mm.IsShowHScrollBar = true;
zed2mm.IsAutoScrollRange = true;

For more information see IsAutoScrollRangeProperty and IsShowHScrollBarProperty

EDIT:
For autoscrolling you have to scroll by hand when new data comes in (or your timer elapses):

double xRange = myPane02m.XAxis.Scale.Max - myPane02m.XAxis.Scale.Min;
myPane02m.XAxis.Scale.Max = new XDate(DateTime.Now);
myPane02m.XAxis.Scale.Min = myPane02m.XAxis.Scale.Max - xRange;
SpeziFish
  • 3,262
  • 2
  • 28
  • 27
  • Thanks, this help me. However I need the graph is moving from right to left as in this video.[link](http://www.youtube.com/watch?v=XXjbu2v1AMU&feature=feedlik). – soushinsha Oct 07 '11 at 04:46
  • I believe I have done something that is preventing your code to have effect, because it did not work! I'm posting the code in my blog. [link](http://ocaccy.blogspot.com/) Thanks, ocaccy – soushinsha Oct 07 '11 at 10:56
  • 1
    You have to put the added code into your "timer1_Tick"-function, not in the constructor. Because every time, when new data comes in, you have to update the Min and Max. – SpeziFish Oct 07 '11 at 12:10
  • It worked! However, we have a defect that takes away the beauty of the graphic. The grid is also rising and times is like being repeated but with different values. Posted on [Youtube](http://www.youtube.com/watch?v=xEuhtqlKYwQ) and on my [blog](http://ocaccy.blogspot.com/2011/10/graph-is-plotted-with-zedgraph.html). – soushinsha Oct 08 '11 at 02:30
  • How to ZedGraph plot a Graph for 5 minutes and stop? – soushinsha Oct 08 '11 at 02:58
  • I need save this 5 minutes data in excel file, how? – soushinsha Oct 08 '11 at 03:04
  • 1
    1. Easily stop your timer after 5 minutes. 2. You have to create a csv-file, go through your date and fill it into the columns of your csv-file. – SpeziFish Oct 10 '11 at 11:40
  • I managed to do the timer and I'm saving. Xls with NPOI Library! I need help to move the graph with beauty. For this jumping at times. I am too grateful for your tips and help. ocaccy – soushinsha Oct 10 '11 at 13:24
  • I am facing a big problem with my project. We have 6 particle counters with four channels each. We receive data from each device after sending a command. The data is received in a string like this: @ 01,05858,04569,00189 We're taking the empty spaces with Trim (); use substring (x, y) we take the parts that interest us. – soushinsha Oct 10 '11 at 13:47
  • Using ZedGraph; generate four graphs: one with the first channel 01 which is after the ID, one with the second channel after the ID with the third and another after the channel ID. I need to save these files in XLS format for Excel, then I have to make many calculations to determine the maximum and minimum value and what is average. Would not it be better to store a datagrid before I save the excel spreadsheet? – soushinsha Oct 10 '11 at 13:47
  • 2
    The reason for the ugly jumps is, that zedgraph automatically resets the basetic each time. I had the same problem some time ago, but I don't know the solution anymore... I think it has something to do with [XAxis.Scale.BaseTic](http://zedgraph.sourceforge.net/documentation/html/P_ZedGraph_Scale_BaseTic.htm) and I had to change the sourcecode of zedgraph to achieve this behaviour. It's generally a good idea, to look into the sourcecode, so you better understand, what zedgraph is doing. – SpeziFish Oct 10 '11 at 13:49
  • 1
    Regarding your excel-issue: the comments are not the place to ask completely new questions. Make a new question and more people will notice it. – SpeziFish Oct 10 '11 at 13:51