Results 1 to 5 of 5

Thread: QCustomPlot using timestamp to set Date along one axis

  1. #1
    Join Date
    Mar 2016
    Posts
    18
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt5

    Default QCustomPlot using timestamp to set Date along one axis

    I am having trouble setting the date properly. Basically I have timestamp, open, close, high, low, volume stored line by line in a text file (downloaded using Yahoo API). My program then reads each line and converts it to a QStringList. It then puts each item in the list into the appropriate QVector<double> (dates[], open[], close[], high[], low[], volume[]) converting each item to a double. Here is where the problem is. It appears that the precision is lost during the conversion. The dates always show as periods back in 1970 when the actually timestamp is in fact a date from a few days ago.

    Qt Code:
    1. #include "dialog.h"
    2. #include "ui_dialog.h"
    3. #include<QFile>
    4. #include<QTextStream>
    5. #include<string>
    6. #include<iostream>
    7. using namespace std;
    8.  
    9. Dialog::Dialog(QWidget *parent) :
    10. QDialog(parent),
    11. ui(new Ui::Dialog)
    12. {
    13. ui->setupUi(this);
    14. QStringList lines;
    15. QString line;
    16.  
    17. QVector<double> dates;
    18. QVector<double> high;
    19. QVector<double> low;
    20. QVector<double> open;
    21. QVector<double> close;
    22. QVector<double> volume;
    23.  
    24. QFile file ("YHOO.cvs");
    25. if(file.open(QIODevice::ReadOnly))
    26. {
    27.  
    28.  
    29. QTextStream in(&file);
    30. while (!in.atEnd())
    31. {
    32.  
    33. line = in.readLine();
    34. lines = line.split(",");
    35. dates.append(lines[0].toDouble());
    36. close.append(lines[1].toDouble());
    37. high.append(lines[2].toDouble());
    38. low.append(lines[3].toDouble());
    39. open.append(lines[4].toDouble());
    40. volume.append(lines[5].toInt());
    41. }
    42. file.close();
    43. }
    44. else{
    45.  
    46. QMessageBox::information(0,"info",file.errorString());
    47. }
    48.  
    49.  
    50. ui->plot->addGraph();
    51.  
    52. ui->plot->graph(0)->setData(dates, high);
    53.  
    54.  
    55. ui->plot->xAxis->setTickLabelType(QCPAxis::ltDateTime);
    56. ui->plot->xAxis->setDateTimeFormat("MM/dd/yyyy");
    57.  
    58. QPen pen;
    59. pen.setColor(QColor(200,200,200));
    60.  
    61. ui->plot->graph(0)->setPen(pen);
    62. ui->plot->graph(0)->setLineStyle(QCPGraph::lsLine);
    63. ui->plot->graph(0)->setBrush(QBrush(QColor(160,50,150)));
    64.  
    65. ui->plot->xAxis->setRange(dates[0], dates[dates.length()-1]);
    66. ui->plot->yAxis->setRange(*std::min_element(high.begin(), high.end()),*std::max_element(high.begin(),high.end()));
    67.  
    68. }
    69.  
    70. Dialog::~Dialog()
    71. {
    72. delete ui;
    73. }
    To copy to clipboard, switch view to plain text mode 

    YHOO.cvs

    Qt Code:
    1. 20140227,30.1000,30.1600,28.4100,29.7000,2351300
    2. 20140228,28.3000,32.0000,27.0000,29.2000,3781000
    3. 20140303,28.1900,28.9100,26.8900,27.3000,1664900
    4. 20140304,30.0400,30.3800,28.6300,28.8500,2341700
    5. 20140305,28.5500,29.5000,28.4900,29.2400,7314100
    6. 20140306,27.1700,29.0100,27.1500,28.7600,3007300
    7. 20140307,27.2000,28.3200,26.7100,27.8400,2961800
    8. 20140310,28.2400,28.5000,27.3500,27.7200,1622100
    9. 20140311,27.5300,28.7400,27.1800,28.4400,1745200
    10. 20140312,28.5400,28.7400,27.3500,27.4700,2206300
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QCustomPlot using timestamp to set Date along one axis

    The first column looks like a date formatted as a YYYYMMDD string.

    Cheers,
    _

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

    bandito (5th March 2016)

  4. #3
    Join Date
    Mar 2016
    Posts
    18
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt5

    Default Re: QCustomPlot using timestamp to set Date along one axis

    Quote Originally Posted by anda_skoa View Post
    The first column looks like a date formatted as a YYYYMMDD string.

    Cheers,
    _
    You are correct. Thanks!

    I came up with this function to convert the QString to a double value timestamp.

    Qt Code:
    1. double timeStamp(QString qs){
    2. char tempDate[10];
    3. memcpy(tempDate, qs.toStdString().c_str(), 10);
    4. char date[] = " ";
    5. date[0] = tempDate[0];
    6. date[1] = tempDate[1];
    7. date[2] = tempDate[2];
    8. date[3] = tempDate[3];
    9. date[4] = '\0';
    10. date[5] = tempDate[4];
    11. date[6] = tempDate[5];
    12. date[7] = '\0';
    13. date[8] = tempDate[6];
    14. date[9] = tempDate[7];
    15.  
    16. struct tm tmdate = {0};
    17. tmdate.tm_year = atoi(&date[0]) - 1900;
    18. tmdate.tm_mon = atoi(&date[5]) - 1;
    19. tmdate.tm_mday = atoi(&date[8]);
    20. time_t t = mktime( &tmdate );
    21.  
    22. double actual_time_sec = difftime(t,0);
    23.  
    24. return actual_time_sec;
    25. }
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,521
    Thanked 278 Times in 273 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QCustomPlot using timestamp to set Date along one axis

    In one line :
    Qt Code:
    1. double timeStamp(QString qs){
    2. return QDateTime(QDate::fromString(qs,"yyyyMMdd"),QTime(0,0,0)).toTime_t();
    3. }
    To copy to clipboard, switch view to plain text mode 

  6. The following user says thank you to Lesiok for this useful post:

    bandito (5th March 2016)

  7. #5
    Join Date
    Mar 2016
    Posts
    18
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt5

    Default Re: QCustomPlot using timestamp to set Date along one axis

    Quote Originally Posted by Lesiok View Post
    In one line :
    Qt Code:
    1. double timeStamp(QString qs){
    2. return QDateTime(QDate::fromString(qs,"yyyyMMdd"),QTime(0,0,0)).toTime_t();
    3. }
    To copy to clipboard, switch view to plain text mode 
    Wow that's sexy. Thanks!

Similar Threads

  1. Replies: 3
    Last Post: 1st February 2016, 09:04
  2. KDChart Date-time Axis howto?
    By galrub in forum Qt Programming
    Replies: 0
    Last Post: 15th October 2012, 12:36
  3. Qwt X-Axis with Date, correct distance
    By Lykanthropie in forum Qwt
    Replies: 5
    Last Post: 5th January 2012, 19:22
  4. Date Scale for X-Axis
    By mishani99 in forum Qwt
    Replies: 3
    Last Post: 6th September 2011, 12:40
  5. Time/Date Axis
    By sigger in forum Qwt
    Replies: 12
    Last Post: 1st May 2011, 10:55

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.