Qwt x-axis should indicate hh:mm:ss
Hi,
I have started to use Qwt 6.0.1. At the moment I have a similar problem as described in this thread [developer.qt.nokia.com] . My value are measured in time. The x-axis should show hh:mm:ss. The QwtScaleDraw and a solution with reimplementation of QwtScaleDraw::label is certainly an option. Currently, I am still looking to find how define the values for different ticks.
Can anyoneelse point me towards to look at in more detail?
Thanks in advance.
10 replies
Thanks for pointing me to the example. This is certainly a good start.
However, it shows also what I want to avoid. The labels drawn are:
- 02:38:46
- 02:38:56
- 02:39:06
- ...
It would be more logical to plot instead:
- 02:38:40
- 02:38:50
- 02:39:00
- ...
or for larger intervals:
- 02:30:00
- 02:40:00
- 02:50:00
- ...
This must be handled somewhere in qwt_scale_engine and qwt_interval, but without detailed background on the implementation it is a bit of a pain.
hmmm ??
I came to this statement here:
- class TimeScaleDraw: public QwtScaleDraw
- {
- public:
- baseTime(base)
- {
- }
- virtual QwtText label(double v) const
- {
- return upTime.toString();
- }
- private:
- QTime baseTime;
- };
The step size of v is in tens.
I have added also the stepsize in CpuPlot constructor to this statement:
- setAxisScale(QwtPlot::xBottom, 0, HISTORY, 10);
But still the same.
[edit] Update because some information was deleted prior to posting :-(
I am simply using the cpuplot example and apply some changes in order to find out how to do it.
However, before we talking about different implementations. I have compiled the examples downloaded together with Qwt 6.0.1. In another thread I have read that there are significant differences to previous versions.
This is the section I have changed with no success:
- QwtPlot(parent),
- dataCount(0)
- {
- setAutoReplot(false);
- canvas()->setBorderRadius( 10 );
- plotLayout()->setAlignCanvasToScales(true);
- QwtLegend *legend = new QwtLegend;
- legend->setItemMode(QwtLegend::CheckableItem);
- insertLegend(legend, QwtPlot::RightLegend);
- setAxisTitle(QwtPlot::xBottom, " System Uptime [h:m:s]");
- setAxisScaleDraw(QwtPlot::xBottom,
- new TimeScaleDraw(cpuStat.upTime()/*.addSecs ( -6 )*/ ) );
- setAxisScale(QwtPlot::xBottom, 0, HISTORY, 10);
- setAxisMaxMajor ( QwtPlot::xBottom, 10 );
- setAxisMaxMinor ( QwtPlot::xBottom, 10 );
- setAxisLabelRotation(QwtPlot::xBottom, -50.0);
- /*
- In situations, when there is a label at the most right position of the
- scale, additional space is needed to display the overlapping part
- of the label would be taken by reducing the width of scale and canvas.
- To avoid this "jumping canvas" effect, we add a permanent margin.
- We don't need to do the same for the left border, because there
- is enough space for the overlapping label below the left scale.
- */
- QwtScaleWidget *scaleWidget = axisWidget(QwtPlot::xBottom);
- scaleWidget->setMinBorderDist(0, fmh / 2);
- setAxisTitle(QwtPlot::yLeft, "Cpu Usage [%]");
- setAxisScale(QwtPlot::yLeft, 0, 100, 10);
setAxisMaxMinor and setAxisMaxMajor has been added now.
In
- setAxisScaleDraw(QwtPlot::xBottom,
- new TimeScaleDraw(cpuStat.upTime()/*.addSecs ( -6 )*/ ) );
you see commented out addSecs. This certainly makes the labels look nicely rounded to every 10 seconds. Unfortunately it is changing basically the x-values now shifted by 6 seconds. So the graph is actually wrong.
I have found a solution for finding better fitting tick labels.
I have introduced following class:
- class SecondsLinearScaleEngine : public QwtLinearScaleEngine
- {
- public:
- virtual void autoScale( int maxSteps,
- double &x1, double &x2, double &stepSize ) const;
- protected:
- double divideInterval( double interval, int numSteps ) const;
- double ceil60 ( double v ) const;
- };
You need to copy autoScale from its base class, since divideInterval is not virtual.
- double SecondsLinearScaleEngine::divideInterval(
- double intervalSize, int numSteps ) const
- {
- if ( numSteps <= 0 )
- return 0.0;
- double v = QwtScaleArithmetic::divideEps( intervalSize, numSteps );
- if ( v < 1.0 )
- return QwtScaleArithmetic::ceil125( v );
- return ceil60( v );
- }
This is basically the same as of the base class. Only the return line has been substituted with the three last lines.
ceil60 does return multiples of 1,2,3,5 up to 30 seconds, minutes.
The equivalent of cpuplot requires two additional statements.
- setAxisScaleEngine ( QwtPlot::xBottom, new SecondsLinearScaleEngine );
Thanks to Scylla for playing the sparing partner.
Thanks for feedback.
at Scylla: Yes, it is a very nice lib. Even so, I grumbled a bit to myself about the documentation in the beginning, it was not so hard to extend. ;-)
at Andre: No. This is a good suggestion. However, at the time being it is a bit too much “hand-knitten”. After some clean-up I will contact them.
You must log in to post a reply. Not a member yet? Register here!



