PDA

View Full Version : Qwt Scale Draw



maxpayne
19th October 2008, 12:54
Hi,

I am new to both qt and qwt and I’m facing hell. I need some help in order to understand how to draw and set up a scale in qwt.

My first question concerns the use of Qwt scale draw:

1. Assume I have a set of time strings like 12:33:05 ,12:33:08, 12:33:11, 12:33:14, 12:33:17, 12:33:20. A total of 6. I want to use these as labels for major ticks.

2 .Next, I have a x axis scale set up as : setAxisScale(QwtPlot::xBottom, 0, 60);

3. Somehow qwt automatically divides the scale into 6 major divisions , each of 10 divisions each. This is acceptable for now but how do I change this default behavior in case?

4. Next how do I assign a label to each major tick? I want the time string 12:33:05 to be the label of the first major tick and so on.


my second question relates to the Cpu plot example:


class TimeScaleDraw: public QwtScaleDraw
{
public:
TimeScaleDraw(const QTime &base):
baseTime(base)
{
}
virtual QwtText label(double v) const
{
QTime upTime = baseTime.addSecs((int)v);
return upTime.toString();
}
private:
QTime baseTime;
};

I would like to know which method actually calls the label and passes the parameter v.what exactly is the value of v? and who calls this method? in the example i understand that every second the setRawData is called for each curve.However i would like to know who calls the virtual method label?


Hope you can help me answer these questions.

Thanks,
max.

Uwe
19th October 2008, 14:44
Interval and all tick positions of a scale are stored in a QwtScaleDiv object.

You can manually create a QwtScaleDiv and assign it to a plot axis (QwtPlot::setAxisScaleDiv()) , or you can use a QwtScaleEngine ( QwtPlot has one for each axis), that calculates it for you. When autoscaling ( QwtPlot::setAxisAutoScale() ) is enabled, the interval for the calculation of the scale is taken from the bounding rectangle of the plot items ( f.e. curves ), otherwise interval/step size are passed with QwtPlot::setAxisScale().

The default scale engines try to calculate ticks for linear decimal scales, what doesn't need to be the right thing for date/time scales. ( QwtDateTimeScaleEngine is on my TODO list ). To improve your scale divisions you can derive and assign ( QwtPlot::setAxisScaleEngine()) your own scale engine or simply use QwtPlot::setAxisScaleDiv instead.

QwtScaleDraw is responsible for painting a QwtScaleDiv. QwtScaleDraw::label() maps a tick value into a string.
In the cpuplot example the values on the x-axis are seconds elapsed since the system is up. TimeScaleDraw::label() maps them into time strings - that's all.

Uwe

pradeepreddyg95
6th June 2012, 14:01
use this class
class TimeScaleDraw: public QwtScaleDraw
{
public:
TimeScaleDraw(QString fmt) : format(fmt) { }

virtual QwtText label(double v) const
{
QDateTime t = QDateTime::fromTime_t((int)v);
return t.toString(format);
}
private:
const QString format;
};

and set x-ais as below how many you want

code snipet for filling samples

void Graph::SetSamples(Curve *pCurve, QVector<double> *x , QVector<double> *y ,int size)
{
Xaxis = *x;
Yaxis = *y;


QDateTime pTimeEdit;
pTimeEdit.setTime_t((unsigned int)x->at(0));
unsigned int basetime = (unsigned int)x->at(0);
unsigned int EndTime = (unsigned int)x->at(size-1);

setAxisScaleDraw(QwtPlot::xBottom, new TimeScaleDraw("h:mm"));


pCurve->setSamples(&Xaxis[0],&Yaxis[0] ,Xaxis.size());

setAxisScale(QwtPlot::xBottom, basetime , EndTime ,0);

replot();


}