qchart绘制曲线
- 效果图
- 代码
#ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include <QLineSeries> #include <QChartView> #include <QChart> #include <QTimer> #include <QValueAxis> #include <QDateTimeAxis> namespace Ui { class Widget; } class Widget : public QWidget { Q_OBJECT public: explicit Widget(QWidget *parent = 0); ~Widget(); private slots: void on_pushButtonFlush_clicked(); void slotTimeOut(); private: Ui::Widget *ui; QT_CHARTS_NAMESPACE::QChart* chart; QT_CHARTS_NAMESPACE::QChartView* view; QT_CHARTS_NAMESPACE::QLineSeries* series1; QT_CHARTS_NAMESPACE::QLineSeries* series2; QT_CHARTS_NAMESPACE::QLineSeries* series3; QT_CHARTS_NAMESPACE::QValueAxis* axisY1; QT_CHARTS_NAMESPACE::QValueAxis* axisY2; QT_CHARTS_NAMESPACE::QValueAxis* axisY3; QT_CHARTS_NAMESPACE::QDateTimeAxis* axisTime; QTimer timerFlush; }; #endif // WIDGET_H
#include "widget.h" #include "ui_widget.h" #include <QDateTime> QT_CHARTS_USE_NAMESPACE Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); connect(&timerFlush, SIGNAL(timeout()), this, SLOT(slotTimeOut())); qsrand(QDateTime::currentDateTime().toTime_t()); chart = new QChart; view = new QChartView(chart); view->setRenderHint(QPainter::Antialiasing); ui->stackedWidget->addWidget(view); chart->setTitle("chart test"); axisTime = new QDateTimeAxis; axisTime->setTitleText("Time"); axisTime->setFormat("hh:mm:ss"); axisTime->setMax(QDateTime::currentDateTime()); axisTime->setMin(QDateTime::currentDateTime().addSecs(-60)); chart->addAxis(axisTime, Qt::AlignBottom); chart->setTheme(QChart::ChartThemeBlueCerulean); //Y1 series1 = new QLineSeries; series1->setName("Y1"); chart->addSeries(series1); axisY1 = new QValueAxis; axisY1->setTitleText("Y1"); chart->addAxis(axisY1, Qt::AlignLeft); series1->attachAxis(axisTime); series1->attachAxis(axisY1); series1->setColor(QColor(Qt::blue)); axisY1->setLinePenColor(QColor(Qt::blue)); axisY1->setLabelsColor(QColor(Qt::blue)); axisY1->setRange(0, 100); series1->setPointsVisible(true); series1->setPointLabelsVisible(true); //Y2 series2 = new QLineSeries; series2->setName("Y2"); chart->addSeries(series2); axisY2 = new QValueAxis; axisY2->setTitleText("Y2"); chart->addAxis(axisY2, Qt::AlignLeft); axisY2->setLabelsColor(QColor(Qt::red)); axisY2->setLinePenColor(QColor(Qt::red)); series2->setColor(QColor(Qt::red)); series2->attachAxis(axisTime); series2->attachAxis(axisY2); axisY2->setRange(0, 200); //Y3 series3 = new QLineSeries; series3->setName("Y3"); chart->addSeries(series3); axisY3 = new QValueAxis; axisY3->setTitleText("Y3"); axisY3->setLabelsColor(QColor(Qt::green)); axisY3->setLinePenColor(QColor(Qt::green)); axisY3->setRange(0, 300); chart->addAxis(axisY3, Qt::AlignLeft); series3->setColor(QColor(Qt::green)); series3->attachAxis(axisTime); series3->attachAxis(axisY3); } Widget::~Widget() { delete ui; } void Widget::on_pushButtonFlush_clicked() { if(timerFlush.isActive()){ timerFlush.stop(); }else{ series1->clear(); series2->clear(); series3->clear(); timerFlush.start(1000); } } void Widget::slotTimeOut() { QDateTime dtcur = QDateTime::currentDateTime(); QDateTime dtmin = dtcur.addSecs(ui->spinBox->value()*(-60)); axisTime->setRange(dtmin, dtcur); series1->append(dtcur.toMSecsSinceEpoch(), rand()%100); series2->append(dtcur.toMSecsSinceEpoch(), rand()%200); series3->append(dtcur.toMSecsSinceEpoch(), rand()%300); }