Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

realtime graph with hh:mm on x

Help
2010-11-24
2012-09-19
  • MajorMadness
    MajorMadness
    2010-11-24

    Hi, the hole day i was searching the forum and wiki, but i think I'm to stupit
    to find my answer... sry.

    So, I have a graph wich gets realtime values from a website. I followed the
    "Display Dynamic or Real-Time Data" example and everything works well. But I
    can't figure out how to get the current time on x. At the moment it says
    "00:00".

    Here is some code:

    GraphPane myPane = ListenGraph.GraphPane;

    myPane.XAxis.Type = AxisType.Date;

    myPane.XAxis.Scale.Format = "HH:mm";

    myPane.Title.Text = "Radio Statistic";

    myPane.XAxis.Title.Text = "Time";

    myPane.YAxis.Title.Text = "Listener";

    RollingPointPairList list = new RollingPointPairList(12000);

    RollingPointPairList list1 = new RollingPointPairList(12000);

    RollingPointPairList list2 = new RollingPointPairList(12000);

    LineItem curve = myPane.AddCurve("All Listener", list, Color.Blue,
    SymbolType.None);

    LineItem curve1 = myPane.AddCurve("Mount1", list1, Color.Black,
    SymbolType.None);

    LineItem curve2 = myPane.AddCurve("Mount2", list2, Color.Red,
    SymbolType.None);

    // myPane.XAxis.Scale.Min = 0; <-- testing

    //myPane.XAxis.Scale.Max = 3000;

    myPane.XAxis.Scale.MinorUnit = DateUnit.Hour;

    myPane.XAxis.Scale.MajorUnit = DateUnit.Hour;

    LineItem curve = ListenGraph.GraphPane.CurveList as LineItem;

    LineItem curve1 = ListenGraph.GraphPane.CurveList as LineItem;

    LineItem curve2 = ListenGraph.GraphPane.CurveList as LineItem;

    IPointListEdit list = curve.Points as IPointListEdit;

    IPointListEdit list1 = curve1.Points as IPointListEdit;

    IPointListEdit list2 = curve2.Points as IPointListEdit;

    // 3 seconds per cycle

    double ges_list = 0;

    for (int n = 1; n < this.mounts; n++) {

    ges_list = ges_list + Convert.ToDouble(this.RadioXML);

    }

    double timestamp = ?????????????????;

    list.Add(timestamp, ges_list);

    list1.Add(timestamp, Convert.ToDouble(this.RadioXML));

    list2.Add(timestamp, Convert.ToDouble(this.RadioXML));

    problem is "timestamp". How do I get a time like 18:00, 18:15 in it?

    Thanks for the help. And if anyone needs an icecast stats program: you can
    have it when i'm done ;-)

    Best Regarts MM

     
  • Jeff McKim
    Jeff McKim
    2010-11-24

    I'm a bit pressed for time right now...good though because it's a customer
    doing the pressing, I won't have time for detail.

    However, it looks like you're trying to display time on the X axis in a
    format. I'm going to guess that's C# from the .Convert. Might be a c++ library
    as well. That will drastically affect how you convert for time.

    If C#, check the overloads for Add. There might be one in a time stamp format.

    Another idea: Don't know how "real time" your requirements are but you can
    rewrite the axis legend either every refresh or every n refreshes. This then
    just becomes a matter of formatting, loading and calling Refresh.

    I do track time in a graph I wrote but I save it to the DB and don't display
    it. Subsequent to run time, the user can use a slide control to move along the
    duration of the run to direct into it in order to examine data at a given time
    slice. Zed will also support something like Yahoo does for historical
    financial data. That's a bit more complex but completely doable.

     
  • MajorMadness
    MajorMadness
    2010-11-24

    You are right, it is C#. I'm useing VS2008, .Net2.0, WindowsForms...

    Normaly I can call DateTime.Now to get current time or
    DateTime.Now.ToString("HH:mm") to get something like 18:00 as string.
    Convert.To... works fine I'm casting a given string with a number to double
    with it. My problem is just to get it into AxisType.Date.

    list.Add(double x, double y) requiers something I can't figure out to display
    the time as HH:mm like set @ myPane.XAxis.Scale.Format = "HH:mm";

     
  • MajorMadness
    MajorMadness
    2010-12-01

    Hi, last couple days I was working on Other stuff, but I'm still trying to
    figure out how to put the time to a realtime graph. Some vars are comming from
    an sql like "01.12.2010 15:11:56" (as string), others should be the current
    time.

    I looked at the overloads for add and even how to display a string @ x, but
    nothing really works. it's still "00:00" on my x.

    Can anyone please give me a hint how to do this...

    THX

     
  • Jeff McKim
    Jeff McKim
    2010-12-01

    Did you look at XDate?

     
  • MajorMadness
    MajorMadness
    2010-12-01

    yes I did and tried to figure out how it works, but I didn't find a way to get
    the current time into this format and displayed in Graph.

     
  • MajorMadness
    MajorMadness
    2010-12-02

    Finally I got it. Thanks so much for pointing me into the right direction. In
    the end it was the scale.min and max. So here is the code to help someone else
    with the same problem.

    Realtime Graph with flexible numbers of lines and current time: (tried to
    comment it, sry for misspelling)

            private void radio_stats_Load(object sender, EventArgs e) {
                Thread.Sleep(3000);     // We need this to get the Data from XML
                GraphPane myPane = ListenGraph.GraphPane;
                myPane.XAxis.Type = AxisType.Date;
                myPane.XAxis.Scale.Format = "HH:mm:ss";     // set label to something like 00:00:00
                myPane.Title.Text = "Radio Statistic";      // Name it
                myPane.XAxis.Title.Text = "Time";           // label it
                myPane.YAxis.Title.Text = "Listener";
    
                RollingPointPairList list = new RollingPointPairList(12000);    // First we need a Graph wich holts a summary or default
                LineItem curve = myPane.AddCurve("All Listener", list, Color.Blue, SymbolType.Circle);            // and we need to style this curve.
    
                // and now we need 10 more lists for data from XML
                RollingPointPairList list1 = null, list2 = null, list3 = null, list4 = null, list5 = null, list6 = null, list7 = null, list8 = null, list9 = null, list10 = null;
                LineItem curve1 = null, curve2 = null, curve3 = null, curve4 = null, curve5 = null, curve6 = null, curve7 = null, curve8 = null, curve9 = null, curve10 = null;
                switch (this.mounts) { // lets see wich one we realy need
                    case 10:
                        list10 = new RollingPointPairList(12000);
                        curve10 = myPane.AddCurve(this.RadioXML[10, 0], list10, Color.YellowGreen, SymbolType.Plus);
                        goto case 9;
                    .......
                    .......
                    .......
                    .......
                    .......
                    .......
                    case 4:
                        list4 = new RollingPointPairList(12000);
                        curve4 = myPane.AddCurve(this.RadioXML[4, 0], list4, Color.Violet, SymbolType.Plus);
                        goto case 3;
                    case 3:
                        list3 = new RollingPointPairList(12000);
                        curve3 = myPane.AddCurve(this.RadioXML[3, 0], list3, Color.Green, SymbolType.Star);
                        goto case 2;
                    case 2:
                        list2 = new RollingPointPairList(12000);
                        curve2 = myPane.AddCurve(this.RadioXML[2, 0], list2, Color.Red, SymbolType.Square);
                        goto case 1;
                    case 1:
                        list1 = new RollingPointPairList(12000);
                        curve1 = myPane.AddCurve(this.RadioXML[1, 0], list1, Color.Black, SymbolType.Diamond);
                        break;
                }
    
                timer1.Interval = 1000;     // update graph every second
                timer1.Enabled = true;
                timer1.Start();
    
                myPane.XAxis.Scale.Min = new XDate(DateTime.Now);       // We want to use time from now
                myPane.XAxis.Scale.Max = new XDate(DateTime.Now.AddMinutes(5));        // to 5 min per default
                myPane.XAxis.Scale.MinorUnit = DateUnit.Second;         // set the minimum x unit to time/seconds
                myPane.XAxis.Scale.MajorUnit = DateUnit.Minute;         // set the maximum x unit to time/minutes
                myPane.YAxis.Scale.Min = 0;     // don't need negativ by default
    
                ListenGraph.AxisChange();            // Scale the axes
    
                tickStart = Environment.TickCount;            // Save the beginning time for reference
                SetSize();
    
            }
    
            private void timer1_Tick(object sender, EventArgs e) {
                if (ListenGraph.GraphPane.CurveList.Count <= 0) { return; }           // Make sure that the curvelist has at least one curve
    
                LineItem curve = ListenGraph.GraphPane.CurveList[0] as LineItem;            // Get the first CurveItem in the graph
                IPointListEdit list = curve.Points as IPointListEdit;            // Get the PointPairList
                // And all the things we maybe need
                LineItem curve1 = null, curve2 = null, curve3 = null, curve4 = null, curve5 = null, curve6 = null, curve7 = null, curve8 = null, curve9 = null, curve10 = null;
                IPointListEdit list1 = null, list2 = null, list3 = null, list4 = null, list5 = null, list6 = null, list7 = null, list8 = null, list9 = null, list10 = null;
                int d = 1;      // somehow we need to count backwarts, so we need a number to help identify the wright curve
                switch (this.mounts){
                    case 10:
                        curve10 = ListenGraph.GraphPane.CurveList[d] as LineItem;
                        list10 = curve10.Points as IPointListEdit;
                        d++;
                        goto case 9;
                    .......
                    .......
                    .......
                    case 4:
                        curve4 = ListenGraph.GraphPane.CurveList[d] as LineItem;
                        list4 = curve4.Points as IPointListEdit;
                        d++;
                        goto case 3;
                    case 3:
                        curve3 = ListenGraph.GraphPane.CurveList[d] as LineItem;
                        list3 = curve3.Points as IPointListEdit;
                        d++;
                        goto case 2;
                    case 2:
                        curve2 = ListenGraph.GraphPane.CurveList[d] as LineItem;
                        list2 = curve2.Points as IPointListEdit;
                        d++;
                        goto case 1;
                    case 1:
                        curve1 = ListenGraph.GraphPane.CurveList[d] as LineItem;
                        list1 = curve1.Points as IPointListEdit;
                        d++;
                        break;
                }
                if (curve == null) { return; } // do we have data?
                if (list1 == null) { return; } // If this is null, it means the reference at curve.Points does not support IPointListEdit, so we won't be able to modify it
    
                double time = (Environment.TickCount - tickStart) / 1000.0; // Time is measured in seconds
    
                double ges_list = 0;        // calculate the value from the default curve
                for (int n = 1; n < this.mounts; n++) {
                    ges_list = ges_list + Convert.ToDouble(this.RadioXML[n, 5]);
                }
    
                DateTime now=new DateTime();        // get current time
                now=DateTime.Now;
                double timestamp = (double)new XDate(now);      // convert it to a double xData
    
                list.Add(timestamp, ges_list);            // first add our default list
    
                switch (this.mounts) {      // and now all other data
                    case 10:
                        list10.Add(timestamp, Convert.ToDouble(this.RadioXML[10, 5]));
                        goto case 9;
                    .......
                    .......
                    .......
                    case 4:
                        list4.Add(timestamp, Convert.ToDouble(this.RadioXML[4, 5]));
                        goto case 3;
                    case 3:
                        list3.Add(timestamp, Convert.ToDouble(this.RadioXML[3, 5]));
                        goto case 2;
                    case 2:
                        list2.Add(timestamp, Convert.ToDouble(this.RadioXML[2, 5]));
                        goto case 1;
                    case 1:
                        list1.Add(timestamp, Convert.ToDouble(this.RadioXML[1, 5]));
                        break;
                }
    
                Scale xScale = ListenGraph.GraphPane.XAxis.Scale;       // lets get the current displayed graph (x we need for rolling)
                Scale yScale = ListenGraph.GraphPane.YAxis.Scale; //Just if we need it for something
                if (timestamp > xScale.Max) {       // do we need an update?
                    double range = xScale.Max - xScale.Min;     // get current displayed range
                    xScale.Max = (double)new XDate(DateTime.Now.AddMilliseconds(timer1.Interval));  // increase max
                    xScale.Min = xScale.Max - range;        // and set the min to same range as befor
                }
    
                ListenGraph.AxisChange(); // Make sure the Y axis is rescaled to accommodate actual data
                ListenGraph.Invalidate(); // Force a redraw
    
            }
    
     
  • Jeff McKim
    Jeff McKim
    2010-12-02

    Great! Glad you got it.

     
  • brian d
    brian d
    2011-05-12

    eh

    Looks kind of complicated.

    I keep a timestamp in the tag.

    Then I have a String Array that I manage separately.

    Then I use the X-axis label as text labels.

    This is good if the points are all evenly spaced.

    My points only update every five seconds, and I refresh my label array every
    time I add a point.

     
  • marini luka
    marini luka
    2011-06-25

    Hi zedleppelin,

    Can you post an exaple of your code.....

    thanks

     
  • Ocaccy
    Ocaccy
    2012-09-18

    My Desktop: W7, VS2010, C# and ZedGraph.

    In the my Desktop App, has a numericUpDown ranging from 1 to 3300.

    Each 1 of the numericUpDown is equal 1 second.

    On ZedGraph, how to put this numericUpDown in Xaxis in format 00:00~55:00,
    with an interval of 5 minutes in Pane.XAxis.MajorStep and 1 minute in
    Pane.XAxis.MajorStep?

    Thanks in advance, pontes