3

I am trying to draw velocity-time graphics using qwtplot.

My X datas are QTime values and Y datas are corresponding speed values. I could not find any example on drawing plots with QTime. Can anyone simply explain how to draw QTime versus Y data ? If possible I would also like to learn how to scale QTime axis.

Muhammet Ali Asan
  • 1,486
  • 22
  • 39
  • 1
    Qt has `QwtDateScaleEngine` class to create time based scales. Check out the "stockchart" example. If you zoom in enough, you will see that it is capable of showing time values as well. – HeyYO Nov 11 '15 at 13:15
  • Thank you for your hint,I come with a solution using that. – Muhammet Ali Asan Nov 11 '15 at 15:15

1 Answers1

5

For future readers I have found a solution thanks to HeyyYO. I am sharing this very simple example :

#include "QApplication"
#include<qwt_plot_layout.h>
#include<qwt_plot_curve.h>
#include<qwt_scale_draw.h>
#include<qwt_scale_widget.h>
#include<qwt_legend.h>
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;
};

int main(int argc,char * argv[])
{
    QApplication a(argc,argv);

    QwtPlot * myPlot = new QwtPlot(NULL);

    myPlot->setAxisScaleDraw(QwtPlot::xBottom,new TimeScaleDraw(QTime::currentTime()));
    myPlot->setAxisTitle(QwtPlot::xBottom,"Time");
    myPlot->setAxisLabelRotation(QwtPlot::xBottom,-50.0);
    myPlot->setAxisLabelAlignment(QwtPlot::xBottom,Qt::AlignLeft|Qt::AlignBottom);

    myPlot->setAxisTitle(QwtPlot::yLeft,"Speed");
    QwtPlotCurve * cur = new QwtPlotCurve("Speed");
    QwtPointSeriesData * data = new QwtPointSeriesData;
    QVector<QPointF>* samples=new QVector<QPointF>;
    for ( int i=0;i<60;i++)
    {
        samples->push_back(QPointF(i,i*i));
    }
    data->setSamples(*samples);
    cur->setData(data);
    cur->attach(myPlot);
    myPlot->show();
    return a.exec();
}
Community
  • 1
  • 1
Muhammet Ali Asan
  • 1,486
  • 22
  • 39