Results 1 to 13 of 13

Thread: Time in x - axis

  1. #1
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Time in x - axis

    Hello together,

    i use qwt and its a nice tool but i am a newbie (work on it

    i have problems to implement a time in my x-axis.

    I want to display datas on the x-axis (from 08:00 clock to 20:00 clock)
    The y-axis are simple values from 0-100...

    i got the data for the clock values in a simple string for example: 160025 (hhmmss) (x-axis value) and another value y-axis = 10.

    i dont know how to implement the x-axis to draw a correct time/clock distance...

    Sorry for my bad english

    Thank in advance for any help!
    Faramia

  2. #2
    Join Date
    Jan 2012
    Posts
    17
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Time in x - axis

    Hi Faramia,

    you have create a new class deriving from QwtScaleDraw and re-implement the QwtText label (double) const; method

    When you have your new class, you have to attach it in your plot, like this

    QwtScaleDrawTime *scaleTime = new QwtScaleDrawTime;
    plot->setAxisScaleDraw(QwtPlot::xBottom,scaleTime);

    Sorry for my bad english too.

  3. The following user says thank you to desantossierra for this useful post:

    Faramia (3rd February 2012)

  4. #3
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Time in x - axis

    Hi,

    that is really quite easy. desantosseria already tolod you how to do that.

    Additionally, have a look at the examples delivered with qwt. there is at least one example using a time axis (don't ask me which one, might be "CPUPlot" or something thelike).

    Felix

  5. The following user says thank you to FelixB for this useful post:

    Faramia (3rd February 2012)

  6. #4
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Smile Re: Time in x - axis

    Hi Thanks for your replys desantossierra and FelixB.

    My new QwtScaleDrawClass looks like: i dont think this is the right way
    "i" is a static var which holds the counter for the hours...

    Qt Code:
    1. class QwtScaleDrawTime: public QwtScaleDraw
    2. {
    3. public:
    4. QwtScaleDrawTime(const QTime &base):
    5. baseTime(base)
    6. {
    7. }
    8. virtual QwtText label(double v) const
    9. {
    10. i++;
    11. qDebug() << "basetime to string" << baseTime.toString() << "v:" << v << "i:" << i;
    12. qDebug() << baseTime.addSecs(3600*i).toString();
    13. return baseTime.addSecs(3600*i).toString();
    14. }
    15. private:
    16. QTime baseTime;
    17. static int i;
    18. };
    To copy to clipboard, switch view to plain text mode 

    implements to my plot:
    i set the default time to 7:00 to achieve a x-axis from 8-to20. but it doesnt work really well
    it draws on the x-axis 8:00, 10:00 ,11:00....20:00, 09:00... Why is the 09:00 at the end of the x-axis?

    Qt Code:
    1. setAxisScale(xBottom, 08.00, 20.00, 1);
    2. QTime myTime(7,00);
    3. setAxisScaleDraw(QwtPlot::xBottom,
    4. new QwtScaleDrawTime(myTime));
    To copy to clipboard, switch view to plain text mode 



    i THink i dont get the point, how to implement it -.-

    Sorry for that.

    Faramia

  7. #5
    Join Date
    Jan 2012
    Posts
    17
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Time in x - axis

    Hi Faramia,

    the main idea is that you have the normal x-axis in the plot, I mean
    x from 1 to ...
    and you have to convert to hours, in the label method
    for example, when x = 1 then hour = 7:00

    virtual QwtText label(double v) const v is the x-value in the plot
    {
    // check if v is an integer value

    // if yes do this
    qDebug() << "basetime to string" << baseTime.toString() << "v:" << v << "i:" << i;
    qDebug() << baseTime.addSecs(3600*v).toString();
    return baseTime.addSecs(3600*v).toString();
    // if not
    return EMPTY_STRING;
    }

    then, you have to change your code to

    setAxisScale(xBottom, 1,1+numOfHours, 1); // numOfHours = number of hours you want to show
    QTime myTime(7,00);
    setAxisScaleDraw(QwtPlot::xBottom,
    new QwtScaleDrawTime(myTime));

  8. The following user says thank you to desantossierra for this useful post:

    Faramia (3rd February 2012)

  9. #6
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Time in x - axis

    Thank you very much desantossierra!,

    this helps really a lot!

    for my understanding i have to map all the x-values, right?

    that means for example:
    value1: y = 5, x=08:00 clock,
    value2: y = 10, x = 08:25...
    value3: y= 3, x = 9:45...

    so the first value for x is 0800, -> x = 1 ( if 8:00 is the startvalue for the x-axis.)
    2nd value x 08:25 -> x = 1,41 - for correct display ?
    3rd value x 9:45, -> x = 2,75 - for correct display ?

    TIA
    Faramia

  10. #7
    Join Date
    Jan 2012
    Posts
    17
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Time in x - axis

    mmm...

    why x=1,41?

    for example, if x is the double value of the label method then you could use something like this

    int hour = (floor(x) + initHour ) % 24 ;
    int minute = 60*(x - floor(x));
    return QwtText(QString('%1:%2').arg(hour).arg(minute));

  11. The following user says thank you to desantossierra for this useful post:

    Faramia (6th February 2012)

  12. #8
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Time in x - axis

    Thank you again desantossierra,

    i will try ur solution.

    i thought and tested it, that the x-axis values starts every time with 1. (not the label, the values in the plot.... - for example, if i start wiht 8:00 to 15:00 (x axis labels...) and if i want to display a x value with y = 5 and x = 8:00, i have to map x to 1 to set the correct display in the plot...
    But perhaps i am wrong so i will try your solution.

    Thank again and again!

    Sorry for my bad english

    Faramia

  13. #9
    Join Date
    Jan 2012
    Posts
    17
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Time in x - axis

    don't worry for your english, my english is worse

    tell me if works or not

  14. #10
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Time in x - axis

    it works ty!

  15. #11
    Join Date
    Jan 2012
    Posts
    23
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Time in x - axis

    Hello, I also have a question about the time axis X.
    Value for the X axis, I get in the loop from the database in the format of Unix time like 1328612875.
    From the example cpuplot, write the following:
    Qt Code:
    1. class TimeScaleDraw: public QwtScaleDraw
    2. {
    3. public:
    4. TimeScaleDraw(const QTime &base):
    5. baseTime(base)
    6. {
    7. }
    8. virtual QwtText label(double v) const
    9. {
    10.  
    11. QTime upTime = baseTime.addSecs((int)v);
    12. return upTime.toString();
    13. }
    14. private:
    15. QTime baseTime;
    16. };
    17.  
    18.  
    19. QTime TrendTop::upTime() const
    20. {
    21. QTime t;
    22. t=QTime::currentTime();
    23. return t;
    24. }
    To copy to clipboard, switch view to plain text mode 

    But time is displayed on the X axis does not correspond to reality.
    Can you please tell what I need to fix?

  16. #12
    Join Date
    Jan 2012
    Posts
    17
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Time in x - axis

    The time correspond with the creation time plus a X value.

    what do you want to do exactly?

    Maybe you need a QList<QTime> to save the times you want to represent in x-axis,
    but I don't know what you are doing

  17. #13
    Join Date
    Jan 2012
    Posts
    23
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Time in x - axis

    desantossierra, I get the value of X and Y axes from the database:
    Qt Code:
    1. query.exec("SELECT tm, val FROM currstamp where id = 1136");
    2.  
    3. if (query.next())
    4. {
    5. osX = query.value(0).toUInt();
    6. osY = query.value(1).toDouble();
    7. }
    8. vectX.append(osX);
    9. vectY.append(osY);
    10. curv1->setSamples(vectX,vectY);
    To copy to clipboard, switch view to plain text mode 

    Data for the X axis, I get in the Unix time format ( 1328676218, 1328676230,etc...).
    And I need to value time, obtained from the database, on the plot looked like this: 08/02/2012 12:32:05



    Problem was solved. Added

    Qt Code:
    1. class TimeScaleDraw: public QwtScaleDraw
    2. {
    3. public:
    4. TimeScaleDraw(/*const QTime &base*/)/* : baseTime(base)*/
    5. {
    6. setTickLength(QwtScaleDiv::MajorTick, 6);
    7. setTickLength(QwtScaleDiv::MinorTick, 0);
    8. setTickLength(QwtScaleDiv::MediumTick, 0);
    9. setLabelRotation(0);
    10. setLabelAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    11. setSpacing(20);
    12. }
    13.  
    14. virtual QwtText label(double v) const
    15. {
    16. QDateTime time;
    17. time = time.fromTime_t(v);
    18. return time.toString("yyyy.MM.dd.\nhh:mm:ss");
    19. }
    20.  
    21. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. if (query.next())
    2. {
    3.  
    4. osX = query.value(0).toUInt();
    5. qDebug() << osX;
    6. osY = query.value(1).toDouble();
    7. }
    8.  
    9. qDebug() << time1.fromTime_t(osX).toTime_t();
    10. vectX.append(time1.fromTime_t(osX).toTime_t());
    11. vectY.append(osY);
    12. curv1->setSamples(vectX,vectY);
    To copy to clipboard, switch view to plain text mode 
    Attached Images Attached Images
    Last edited by oddytz1989; 8th February 2012 at 06:34.

Similar Threads

  1. Replies: 1
    Last Post: 2nd August 2011, 18:01
  2. Replies: 9
    Last Post: 3rd May 2011, 21:21
  3. Time/Date Axis
    By sigger in forum Qwt
    Replies: 12
    Last Post: 1st May 2011, 09:55
  4. Replies: 2
    Last Post: 8th February 2011, 14:53
  5. Replies: 0
    Last Post: 9th August 2010, 10:46

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.