Qwt x-axis should indicate hh:mm:ss


  • Moderators

    Hi,

    I have started to use Qwt 6.0.1. At the moment I have a similar problem as described in "this thread":http://developer.qt.nokia.com/forums/viewthread/4890 . 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.



  • Take a look at cpuplot example in the qwt source. This should be a good starting point.


  • Moderators

    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.



  • No, it depends on the x-achsis values. Put the the x-values in a 10s step and you will see the right scale.

    @QTime upTime = baseTime.addSecs((int)v);@

    convert your x-values from seconds to a QTime.


  • Moderators

    hmmm ??
    I came to this statement here:
    @
    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;
    };
    @
    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 :-(



  • Can you show how you use the "TimeScaleDraw", show more code.
    Anyway, may be you should take look at setAxisMajor(...) and setAxisMinor(...) methods of the QwtPlot.


  • Moderators

    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:
    @
    CpuPlot::CpuPlot(QWidget *parent):
    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);
    setAxisLabelAlignment(QwtPlot::xBottom, Qt::AlignLeft | Qt::AlignBottom);
    
    /*
     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);
    const int fmh = QFontMetrics(scaleWidget->font()).height();
    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.


  • Moderators

    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.
    @
    setAxisScaleDraw ( QwtPlot::xBottom, new TimeScaleDraw ( QTime() ) );
    setAxisScaleEngine ( QwtPlot::xBottom, new SecondsLinearScaleEngine );
    @

    Thanks to Scylla for playing the sparing partner.



  • Thank you for sharing your solution. I'm sure that I can use this in the next project where I have to use qwt havily. So may be I will/must have a deeper look in the qwt source. By the way, qwt is a very nice lib ;-)



  • Did you send your class to the QWT maintainer? He might be interested in adding this functionality to the library itself?


  • Moderators

    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.



  • Hi Koahing,
    I'm new in qwt and i work in a project similar to what you do, and i can't understund what does mean QwtScaleArithmetic::ceil125 in your example, it give me an error in my example . Can you help me ?
    Ps : sorry for my english


  • Moderators

    Welcome to devnet

    I am not sure right away, but IIRC it is part of the old style in Qwt lib and had something to do with the rounding process.
    You should change to Qwt 6.1.0 which has a date-time engine already as part of the library. Checkout "QwtDateScaleEngine":http://qwt.sourceforge.net/class_qwt_date_scale_engine.html
    With Qt5.1 I had to change to this recent version 6.1.0 of Qwt and I abandoned my solution too.



  • And what is your new solution?


  • Moderators

    My code contains now following part
    @
    #if QWT_VERSION < 0x060100
    setAxisScaleDraw ( QwtPlot::xBottom, new TimeScaleDraw ( QTime() ) );
    setAxisScaleEngine ( QwtPlot::xBottom, new SecondsLinearScaleEngine );
    #endif
    #if QWT_VERSION >= 0x060100
    setAxisScaleDraw ( QwtPlot::xBottom, new QwtDateScaleDraw ( Qt::UTC ) );
    setAxisScaleEngine ( QwtPlot::xBottom, new QwtDateScaleEngine ( Qt::UTC ) );
    #endif
    @

    The first part would be for my old-style as described above. However, it has a couple of deficiencies. For the use of Qt 5.1 I had to change to newer Qwt 6.1 lib. Qwt 6.1 did not work with my solution. I believe I ran into the same problem as you did. However, the second part of code above did solve it for me.



  • Hello Sir
    I want to plot every 24 hours graph using qwt. Please suggest me How to take time scale on x-axis for 24 hours.

    Thanks inadvance



  • Hello Sir
    I want to plot every 24 hours graph using qwt. Please suggest me How to take time scale on x-axis for 24 hours.

    Thanks inadvance


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.