PDA

View Full Version : QwtScaleDraw align label to xaxis



kja
20th November 2010, 02:36
Hi,

I'm making a qwtplot and I am setting the xaxis with some dates using QwtScaleDraw, I have a few questions about the implementation.

First, when I load the data I want the first Xaxis date to be the date of the first y value, but it always ends up being a few days before it, I think this is because the plot background is bigger than the data(which I want to keep). How can I get the first QwtScaleDraw label to correspond with my first y value?

Second, and this is more a general qt/c++ question, is there a better way to get my values in my QwtScaleDraw into human-readable format? Right now I am doing some funky stuff: I take the double value (which is date in unix GMT) then i use ctime to convert it to a char*. Ctime converts my unix GMT to local time, which I don't want (I want it to stay GMT). So I add the seconds that my time is differnt from GMT to the value and then call ctime. There has got to be a better way!




class xAxisDates: public QwtScaleDraw
{
public:
xAxisDates()
{
setSpacing(15);
}
virtual QwtText label(double value) const
{
double diff = 7*60*60 //7hrs*60min*60sec - how far my timezone is from GMT

time_t valueInGmt = value;
time_t gmtPlusDiffForLocal = value+diff;

char * humanTime = ctime(&(gmtPlusDiffForLocal)); //bad form: I add offset to gmt time becuase i use ctime to convert to char*

return QString(humanTime);
}
};
/////
setAxisScaleDraw(QwtPlot::xBottom, new xAxisDates);
////


this is where I load up the data and where QwtScaleDraw gets its value:


void plotFile( double st, double end)
{
newNumbPts = //num of pts in the file
int newNumbPts = endIDX - stIDX;
cout << "newnumb pts: "<< newNumbPts << endl;
t = new double[newNumbPts];
v = new double[newNumbPts];
double *tempT = t;
double *tempV = v;

fseek(fp, stIDX*2*sizeof(double), SEEK_SET);
for( int i=0; i<newNumbPts; i++){
int j = fread(tempT, sizeof(double), 1, fp);
int k = fread(tempV, sizeof(double), 1, fp);
tempT++;
tempV++;
}
Curve->setRawSamples(t, v, newNumbPts);

};


Any ideas?
Thanks

Uwe
21st November 2010, 19:55
First, when I load the data I want the first Xaxis date to be the date of the first y value, but it always ends up being a few days before it, I think this is because the plot background is bigger than the data(which I want to keep). How can I get the first QwtScaleDraw label to correspond with my first y value?

Calculating the scale from min/max values is the job of the scale engine. You can use QwtScaleEngine::setAttribute() to modify the algorithm.

If you don't like any of the results you can also set your scale ( with all its ticks ) manually using QwtPlot::setAxisScaleDiv().

Uwe