Menu

My zedGraph Program do not show x axis

Help
chenggf2
2006-10-08
2012-09-19
  • chenggf2

    chenggf2 - 2006-10-08
    My zedGraph Program that  show  the dynamic Data do not show x axis.
    

    My Program want to show the dynamic Data. y axis show the double data . x axis show the datetime data (for example:12:23:31 12:23:51 12:24:11 12:24:31 12:23:51). but the x axis of my Program show nothing . I think that the Form1_Load function has the problem .

    thinks for you

    my code as follows:

    public partial class Form1 : Form
    {

       Random rd;
       double gMinTime,gMaxTime ;
       double PerSecd;
       public Form1()
       {
           InitializeComponent();
       }
    

    private void Form1_Load(object sender, EventArgs e)

       {
           GraphPane myPane=zedGraphControl1.GraphPane   ;
            myPane.XAxis.Title.Text = "date time";
           myPane.YAxis.Title.Text = "Rate";
            rd = new Random(1); 
           RollingPointPairList list = new RollingPointPairList(1200);
    
           LineItem myCurve = myPane.AddCurve("Rate", list, Color.Green  , SymbolType.None);
    
           myPane.Legend.IsVisible = false;  
           myPane.Title.IsVisible = false;
    
    
    
           myPane.XAxis.MajorGrid.IsVisible = true;
           myPane.XAxis.MajorGrid.Color = Color.Green;
           myPane.XAxis.MajorGrid.PenWidth =1;
           myPane.XAxis.Type = AxisType.Date;
           myPane.XAxis.Scale.MajorUnit = DateUnit.Second ;
    
           myPane.XAxis.Scale.Format = "T";
           myPane.XAxis.Scale.FontSpec.Angle = 65;
    
    
    
    
           myPane.YAxis.MajorGrid.IsVisible = true;
           myPane.YAxis.MajorGrid.Color = Color.Green;
           myPane.YAxis.MajorGrid.PenWidth = 1;
    
    
    
           myPane.YAxis.Scale.FontSpec.FontColor = Color.Red;
           myPane.YAxis.Title.FontSpec.FontColor = Color.Red;
           myPane.YAxis.MajorTic.IsOpposite = false;
           myPane.YAxis.MinorTic.IsOpposite = false;
           myPane.YAxis.MajorGrid.IsZeroLine = false;
           myPane.YAxis.Scale.Align = AlignP.Inside;
           myPane.YAxis.MajorGrid.IsVisible = true;
           myPane.XAxis.MajorGrid.IsVisible = true;
    
    
    
           myPane.Chart.Fill = new Fill( Color.Black );
           timer1.Interval = 500;
           timer1.Enabled = true;
           timer1.Start();
           DateTime mynow = DateTime.Now;
    
           gMinTime = new XDate(mynow)  ;
           gMaxTime = new XDate(mynow.AddSeconds(30))  ;
           PerSecd = gMaxTime - gMinTime;
    
    
    
           myPane.XAxis.Scale.Min = gMinTime;
           myPane.XAxis.Scale.Max = gMaxTime;
           myPane.XAxis.Scale.MinorStep = PerSecd / 30;
           myPane.XAxis.Scale.MajorStep = PerSecd / 6;
    
           zedGraphControl1.AxisChange();
    
    
    
    
       }
    
    
    
    
    
    
       private void Form1_Resize(object sender, EventArgs e)
       {
           SetSize();
       }
       private void SetSize()
       {
    
           Rectangle formRect = this.ClientRectangle;
           formRect.Inflate(-1, -1);
           if (zedGraphControl1.Size != formRect.Size)
           {
               zedGraphControl1.Location = formRect.Location;
               zedGraphControl1.Size = formRect.Size;
           } 
       }
    
       private void timer1_Tick(object sender, EventArgs e)
       {
           // Make sure that the curvelist has at least one curve
           if (zedGraphControl1.GraphPane.CurveList.Count <= 0)
               return;
           // Get the first CurveItem in the graph
           LineItem curve = zedGraphControl1.GraphPane.CurveList[0] as LineItem;
           if (curve == null)
               return;
           // Get the PointPairList
           IPointListEdit list = curve.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 (list == null)
               return;
    
    
    
           double time = new XDate(DateTime.Now)  ;
           tickStart++;
    
    
    
          list.Add(time, 20*Math.Sin(2.0 * Math.PI * tickStart / 3.0) * rd.NextDouble());
    
    
           Scale xScale = zedGraphControl1.GraphPane.XAxis.Scale;
           if (time > xScale.Max - xScale.MajorStep)
           {
               xScale.Max = time + xScale.MajorStep;
               xScale.Min = xScale.Max - PerSecd;
           }
    
    
    
           // Make sure the Y axis is rescaled to accommodate actual data
           zedGraphControl1.AxisChange();
           // Force a redraw
          zedGraphControl1.Invalidate();
       }
    

    }

     
    • John Champion

      John Champion - 2006-10-08

      The problem is because of the relationship between Scale.BaseTic and the MajorUnit/MinorUnit. If your scale is in "auto" mode, then ZedGraph makes all these settings for you. If you manually set your scale, then you must manually set the MajorUnit and MinorUnit.

      Scale.MajorUnit determines the units of the major tics. So setting Scale.MajorUnit = DateUnit.Second means that your Scale.MajorStep value is in units of seconds. Also, the Scale.BaseTic value is automatically determined by ZedGraph unless you set it manually. The BaseTic calculation depends on the MajorUnit setting. So if the MajorUnit is not "consistent" with the scale range (e.g., don't use a majorunit of "days" if the scale range is in seconds), then the BaseTic might be set unreasonably. For example, if your scale range is 30 seconds and your MajorUnit is "days", and your MajorStep is 5/86400 (5 seconds equivalent in units of days), then the basetic will be selected at an even "day" unit, which will probably be way outside your scale range. That is, the range of the scale might cover 08:37 AM, while the nearest even "day" basetic is 00:00.

      In your case, you have used MajorUnit = Seconds, but you specified your MajorStep in days. So, you can either specify your MajorStep in seconds, OR, you can specify your MajorStep in days and manually set the Scale.BaseTic property.

      Try this:

      DateTime mynow = DateTime.Now;
      myPane.XAxis.Type = AxisType.Date;
      myPane.XAxis.Scale.Format = "T";
      myPane.XAxis.Scale.MajorUnit = DateUnit.Second;
      myPane.XAxis.Scale.MinorUnit = DateUnit.Second;
      myPane.XAxis.Scale.Min = mynow.ToOADate();
      myPane.XAxis.Scale.Max = mynow.AddSeconds(30).ToOADate();
      myPane.XAxis.Scale.MinorStep = 1; // 1 second
      myPane.XAxis.Scale.MajorStep = 5; // 5 seconds

      This should work for you.

      PS: Please don't double post.
      John

       

Log in to post a comment.

MongoDB Logo MongoDB