#include "mywidget.h"
#include "ui_mywidget.h"
class TimeScaleDraw: public QwtScaleDraw
{
public:
TimeScaleDraw()
{
}
virtual QwtText label( double v ) const
{
QDateTime dt;
dt.setTime_t((int)v);
return dt.toString("yyyy-MM-dd\n hh:mm:ss");
}
private:
QDateTime baseTime;
};
myWidget::myWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::myWidget)
{
ui->setupUi(this);
ui->qwtPlot->insertLegend(new QwtLegend, QwtPlot::RightLegend);
ui->qwtPlot->setAxisTitle(QwtPlot::xBottom, " System Uptime[yyyy-MM-dd\n hh:mm:ss]");
ui->qwtPlot->setAxisScaleDraw(QwtPlot::xBottom,new TimeScaleDraw());
ui->qwtPlot->setAxisScale(QwtPlot::xBottom, QDateTime::currentDateTime().toTime_t()-5, xMaxScale = QDateTime::currentDateTime().toTime_t());
ui->qwtPlot->setAxisLabelAlignment(QwtPlot::xBottom, Qt::AlignHCenter | Qt::AlignBottom);
ui->qwtPlot->canvas()->setFrameStyle( QFrame::NoFrame | QFrame::Plain );
ui->qwtPlot->setCanvasBackground(Qt::blue);
ui->qwtPlot->setTitle ("按秒采集数据波形图");
QwtScaleWidget *scaleWidget = ui->qwtPlot->axisWidget(QwtPlot::xBottom);//设置坐标边距
const int fmh = QFontMetrics(scaleWidget->font()).height();
scaleWidget->setMinBorderDist(0, fmh / 2);
ui->qwtPlot->setAxisTitle(QwtPlot::yLeft, "V");
ui->qwtPlot->setAxisScale(QwtPlot::yLeft, 0, 3);
p_adplot = new QwtPlotCurve("AD模拟采集");
QColor c = Qt::red;
p_adplot->setPen(c);
p_adplot->attach(ui->qwtPlot);
QwtPlotZoomer* zoomer = new QwtPlotZoomer( ui->qwtPlot->canvas() );
zoomer->setRubberBandPen( QColor( Qt::black ) );
zoomer->setTrackerPen( QColor( Qt::black ) );
zoomer->setMousePattern(QwtEventPattern::MouseSelect2,Qt::RightButton, Qt::ControlModifier );
zoomer->setMousePattern(QwtEventPattern::MouseSelect3,Qt::RightButton );
//qDebug()<<QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")+"fir";
PlotTimer = new QTimer();
connect(PlotTimer, SIGNAL(timeout()),this, SLOT(plotCurve()));
PlotTimer->start(1000);
}
void myWidget::plotCurve()
{
ui->qwtPlot->setAxisScale(QwtPlot::xBottom, QDateTime::currentDateTime().toTime_t()-5, xMaxScale = QDateTime::currentDateTime().toTime_t());
//qDebug()<<QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")+"sec";
points<<QPointF(QDateTime::currentDateTime().toTime_t(),rand()%3);
p_adplot->setSamples(points);
p_adplot->attach(ui->qwtPlot);
ui->qwtPlot->replot();
}
myWidget::~myWidget()
{
delete ui;
}