PDA

View Full Version : large data files in UNIX time. Possible to plot them in human-readable time??



kja
9th November 2010, 01:03
My computer:
Visual Studio 2008, qt 4.7, qwt 6.0

Hi,

I am making a qwt plot from a very large file that has time and prices. The times in the file are all in Unix time as doubles. The plot comes out great with time on the x-axis and prices on the y-axis. The only problem is that the times are all in Unix and not very useful to look at. Does anyone know if there is a way that I can convert the times on the plot into human-readable dates? The kinds of files that I want to plot come from online databases and are all in Unix time.

The only thing I can think of is loading up all the time data from the files and using mktime to make them all into readable dates, but I don't really know how I'd do that. It seems like a lot of work (especially because the files have between 2-4 million time values) Also, I'm not sure if changing these files would make my code break (like the numOfPts when I load the data)...

here is some of my code:



newPlot::newPlot()
{
QwtScaleEngine::Floating;
QwtPlotCurve *Curve = new QwtPlotCurve("plotty");
Curve->setPen(QPen(Qt::red));
Curve->setStyle(QwtPlotCurve::Steps);
Curve->attach(this);

////chose which file to load and load the data
char * FileName = "C:/data/....price_and_time";
FILE * fp = fopen ( FileName, "rb");
if(fp == NULL) EXCEPTION( "no file" );

fseek(fp, 0, SEEK_END);
int numOfPts = ftell( fp )/ (2 * sizeof (double));

double *time = new double[numOfPts];
double *value = new double[numOfPts];
double *tempTime = time;
double *tempValue = value;

fseek(fp, 0, 0);
for( int i=0; i<numOfPts; i++){
int j = fread(tempTime, sizeof(double), 1, fp);
int k = fread(tempValue, sizeof(double), 1, fp);
tempTime++;
tempValue++;
}
Curve->setRawSamples(time, value, numOfPts);
}

if (fp!=NULL)
fclose(fp);
}

I am putting the plot into a gui so that I can add features later:



class MyWidget : public QWidget{
public:
MyWidget(QWidget *parent = 0);
};

MyWidget::MyWidget(QWidget *parent):QWidget(parent)
{
setFixedSize(1500,750);
newPlot *myPlot = new newPlot;
QVBoxLayout *layout = new QVBoxLayout;

layout->addWidget(myPlot);
setLayout(layout);
}


int main(int argc, char * argv[])
{
QApplication app(argc, argv);
MyWidget widget;
widget.show();
return app.exec();

}



Any ideas?

Thanks for your help!

ChrisW67
9th November 2010, 01:45
QDateTime::fromTime_t() and QDateTime::toString()

Assuming your double time values are seconds and fractions of seconds from 1 Jan 1970 you will lose the fractional seconds.

You probably don't want to convert 4 million entries when you only need a few axis labels to cover the period.

kja
9th November 2010, 05:49
Thanks for the reply, I will check out QDateTime.


You probably don't want to convert 4 million entries when you only need a few axis labels to cover the period.

Actually that was another question I was thinking about. I am new to qt and c++ and I don't know how I would go about writing a program to only plot some of the data in a way that makes a useful plot. Could I make it plot only every certain number of pixels? I would also want more data points to be printed when I zoomed in, like having it replot with more data points, but I don't know where to start. Any Idea?

Thanks a lot!

ChrisW67
9th November 2010, 21:49
If you already have Qwt plotting your entire data set then just tell it to plot a smaller time or price range.

kja
18th November 2010, 22:51
Ok so now that I can convert the doubles to strings how can I get qwtplot to plot it? Or if I don't need to plot it how can I get the x-axis to be labeled with my dates as strings?

I was plotting before with SetRawSamples that takes double *'s

Any ideas?

ChrisW67
19th November 2010, 02:25
I guess you would use QwtScaleDraw and QwtScaleDiv classes. You should probably post this question in the Qwt sub-forum.