PDA

View Full Version : Continuous x-axis qwt realtime plot



Phataas
2nd February 2015, 23:38
I am trying to make a realtime plot and so far I got it to show a continuous sine wave, but the plot is not moving continuously on the x-axis, but more like every second it jumps one tick to the left. How can I fix this when using the QwtDateScaleDraw and QwtDateScaleEngine? I am sure it is easy, but I can not figure it out...

My code:


MyPlot::MyPlot(QWidget *parent) : QWidget(parent)
{
xData = new QList<double>();
yData = new QList<double>();
plot = new QwtPlot(this);
layout = new QHBoxLayout();

//Add widget to layout.
layout->addWidget(plot);

//Add curves.
curve1 = new QwtPlotCurve("Curve 1");
curve1->setSamples(xData->toVector(), yData->toVector());
curve1->attach(plot);

//Set x-axis scaling.
QwtDateScaleDraw *qwtDateScaleDraw = new QwtDateScaleDraw (Qt::OffsetFromUTC);
QwtDateScaleEngine *qwtDateScaleEngine = new QwtDateScaleEngine(Qt::OffsetFromUTC);
qwtDateScaleDraw->setDateFormat(QwtDate::Second, "hh:mm:ss");
qwtDateScaleDraw->setUtcOffset(QDateTime::currentMSecsSinceEpoch() / 1000);

plot->setAxisScaleDraw ( QwtPlot::xBottom, qwtDateScaleDraw);
plot->setAxisScaleEngine ( QwtPlot::xBottom, qwtDateScaleEngine);
plot->setAxisMaxMajor(QwtPlot::xBottom, 0);
plot->setAxisMaxMinor(QwtPlot::xBottom, 10);

//Replot.
plot->replot();

//Simulated data timer.
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(appendData()));
timer->start(1);
}

void MyPlot::appendData()
{
static unsigned int counter = 0;

//Add new data.
xData->append(counter);
yData->append(sin(M_PI*( (double)counter++ / 180)));

//Remove oldest data.
if (yData->size() > 1000)
{
xData->removeFirst();
yData->removeFirst();
}

//Set data.
curve1->setSamples(xData->toVector(), yData->toVector());

//Replot.
plot->replot();
}

Uwe
3rd February 2015, 06:37
Use QwtPlot::setAxisScale for the X axis instead of using the autoscaler.

Uwe

Phataas
4th February 2015, 21:53
You are right! Thank you very much :) I did try it, but I obviously did something wrong. I still have a somewhat related problem with the timestamp label "hh:mm:ss" adjusting the x-axis every time a new x-axis label appears from the right. I guess the spacing between the major ticks changes and this looks like the x-axis is lagging a little. I would rather that the label is gradually becoming visible as the time (x-axis) increases in value. Like it fades in from the right y-axis border. Any quick tip? I have been looking at margins in the QwtDateScaleDraw and QwtDateScaleEngine, but no luck so far.