{
xData = new QList<double>();
yData = new QList<double>();
//Add widget to layout.
layout->addWidget(plot);
//Add curves.
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.
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();
}
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();
}
To copy to clipboard, switch view to plain text mode
Bookmarks