Results 1 to 8 of 8

Thread: Drawing plots from sql data using datetime

  1. #1
    Join Date
    Dec 2011
    Posts
    18
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Drawing plots from sql data using datetime

    Hello everybody!

    I've this sql table where I have two columns to draw a plot with their data, the first one is easy, could be converted to double , and placed on the Y axis but the second column is of type "datetime" so it's a bit difficult. I've seen the cpuplot example, it would perfectly suit my needs if I could find a way to display datetime on the X axis, all the examples that I ve checked so far, use an enum for this purpose or a QTimer.

    The only hint I could find so far is on this thread but it's a bit vague: http://qt-project.org/forums/viewthread/23752

    Any hints would be great and well appreciated

    Thanks in advance!

  2. #2
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,312
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Drawing plots from sql data using datetime

    1) What version of Qwt are you using
    2) How are the datetime values stored ( time elapsed since some start point - or valid UTC dates/time as string - or ... )
    3) What do you want to display as scale ( similar question to 2 )

    Uwe

  3. #3
    Join Date
    Dec 2011
    Posts
    18
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Drawing plots from sql data using datetime

    Hello Uwe and thank you for replying

    1- I'm using qwt-6.0.1
    2- Data is exclusively UTC dates/time as string as you've stated, the user will have to choose two dates, then data will be retrieved and the plot will be populated
    3- A plot is worth a thousand words ; here's what I'd like to achieve with qwt, I'm doing it so far with jpgraph (PHP)

    graph.png

  4. #4
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,312
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Drawing plots from sql data using datetime

    In Qwt 6.1 you find a new scale engine for datetime values, that aligns the scale and finds tick positions according to years, months, weeks, days, hours, minutes, seconds, milliseconds. When you always have ticks for every second this is maybe not necessary, but as soon as you want to display scales with more than a couple of seconds only ( IMO the plot of your screen shot looks a bit ugly and needs too much horizontal space because of showing too many tick labels - every 5 or 10 ticks should be enough ) you want to have it aligned properly.

    QwtDateScaleEngine and QwtDateScaleDraw use QwtDate, that offers the translations between QDateTime and double. Even if you want to stay with Qwt 6.0 you should have a look at its implementation doing something similar in your code.

    Uwe

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

    Pluvius (17th January 2013)

  6. #5
    Join Date
    Dec 2011
    Posts
    18
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Drawing plots from sql data using datetime

    You are right about the the plot in the screenshot, that's one of the reasons, I want to move to qwt, furthermore, I'll add proper alignement.

    QwtDateScaleEngine and QwtDateScaleDraw use QwtDate, that offers the translations between QDateTime and double.
    This should get me started, thank you !

  7. #6
    Join Date
    Jan 2013
    Posts
    4
    Thanks
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Drawing plots from sql data using datetime

    Good afternoon,

    I am facing a similar situation.
    A database of power consumption readings.
    Basically it consists of two tables (one with different meters and one with readings).

    I have just installed qwt 6.1rc2 because it seems to have some useful features for this.

    Actually the code (not elegant but ok for a beginner, I hope) works and gives me a graph.
    Unfortunately the x-axis is totally cryptic. I am still struggling with QwtDateScaleEngine and QwtDateScaleDraw.

    However, maybe the first part helps and maybe somebody gives me a hint about the usage of the scale engine and scale draw:

    Qt Code:
    1. model->setTable("tbl_zaehlerstaende"); // table with meter readings
    2. model->setRelation(2, QSqlRelation("tbl_zaehler", "zaehler_ID", "name")); //QSqlRelationalTableModel to link the two tables
    3. model->setFilter("name = 'my_meter'"); //show only my own consumption
    4. model->sort(1, Qt::AscendingOrder); //sort in chronological order
    5. model->select();
    6.  
    7. ui->tv_overview->setModel(model);
    8. ui->tv_overview->setItemDelegate(new QSqlRelationalDelegate(ui->tv_overview));
    9. ui->tv_overview->show();
    10.  
    11.  
    12.  
    13.  
    14. int k = model->rowCount(); //number of datapoints, ie readings
    15. QVector<QPointF> datapoints;
    16.  
    17.  
    18.  
    19.  
    20. for (int i = 0; i < k; i++)
    21. {
    22.  
    23. QDateTime date; //the format in MySql is actually just DATE, but it does not seem to matter
    24. date = (model->data(model->index(i,1))).toDateTime();
    25.  
    26. qreal x;
    27.  
    28. x = QwtDate::toDouble(date); //qwt needs a double to work
    29.  
    30.  
    31.  
    32. datapoints.append(QPointF(x, qreal(model->data(model->index(i,3)).toInt())));
    33.  
    34. }
    35.  
    36. QwtPlotCurve *curve = new QwtPlotCurve();
    37.  
    38. curve->setSamples(datapoints);
    39. curve->attach(ui->qwt_plot);
    40.  
    41. ui->qwt_plot->replot();
    To copy to clipboard, switch view to plain text mode 

    Really I used google, I read the documentation. But I find it hard to get started.

    Many greetings,

    RogerWilco

  8. #7
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,312
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Drawing plots from sql data using datetime

    The code translates the dates into milliseconds since Epoch what is correct, when you want to use QwtDateScaleEngine and QwtDateScaleDraw.

    All what is missing is:

    Qt Code:
    1. ui->qwt_plot->setAxisScaleDraw( QwtPlot::xBottom, new DateScaleDraw() );
    2. ui->qwt_plot->setAxisScaleEngine( QwtPlot::xBottom, new QwtDateScaleEngine() );
    To copy to clipboard, switch view to plain text mode 
    Uwe

  9. The following user says thank you to Uwe for this useful post:

    RogerWilco77 (23rd January 2013)

  10. #8
    Join Date
    Jan 2013
    Posts
    4
    Thanks
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Drawing plots from sql data using datetime

    Thanks a lot. It was really simple.
    I tried not to just copy but to understand.
    Well, obviously I would have had to look under QwtPlot and fight my way via setAxisScaleDraw to to the usage of DateScaleDraw...
    Now that I know it, it appears so logial....

Similar Threads

  1. datetime field
    By valgaba in forum Qt Programming
    Replies: 5
    Last Post: 8th August 2012, 10:43
  2. QT Change Handset DateTime
    By javed_alam786 in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 4th May 2011, 17:59
  3. Drawing of graph(data structure) !
    By Abeer in forum Newbie
    Replies: 1
    Last Post: 23rd May 2010, 21:47
  4. Replies: 5
    Last Post: 21st March 2009, 09:10
  5. How to get seconds diferences between two datetime
    By gustavedgardo in forum Newbie
    Replies: 1
    Last Post: 4th March 2009, 22:05

Tags for this Thread

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.