轉載自http://blog.chinaunix.net/uid-11829250-id-5750296.html
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QTimer>
#include "qcustomplot.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
//設置qcustomplot畫圖屬性,實時
void setupRealtimeDataDemo(QCustomPlot *customPlot);
private slots:
//添加實時數據槽
void realtimeDataSlot();
private:
Ui::MainWindow *ui;
//定時器,週期調用realtimeDataSlot()槽,實現動態數據添加到曲線
QTimer dataTimer;
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QVector>
#include <QTimer>
#include <QTime>
/*
*瞭解x座標軸的方法,添加數據的方式
*
*/
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setupRealtimeDataDemo(ui->customPlot);
ui->customPlot->replot();
ui->checkBox_temp->setChecked(true);
ui->checkBox_hui->setChecked(true);
}
MainWindow::~MainWindow()
{
delete ui;
}
//畫圖初始化
void MainWindow::setupRealtimeDataDemo(QCustomPlot *customPlot)
{
customPlot->addGraph(); // blue line
customPlot->graph(0)->setPen(QPen(Qt::blue));
customPlot->graph(0)->setName("temp");
//customPlot->graph(0)->setBrush(QBrush(QColor(240, 255, 200)));
//customPlot->graph(0)->setAntialiasedFill(false);
customPlot->addGraph(); // red line
customPlot->graph(1)->setPen(QPen(Qt::red));
customPlot->graph(1)->setName("hui");
//customPlot->graph(0)->setChannelFillGraph(customPlot->graph(1));
customPlot->xAxis->setTickLabelType(QCPAxis::ltDateTime);
customPlot->xAxis->setDateTimeFormat("hh:mm:ss");
customPlot->xAxis->setAutoTickStep(false);
customPlot->xAxis->setTickStep(2);
customPlot->axisRect()->setupFullAxesBox();
// make left and bottom axes transfer their ranges to right and top axes:
//connect(customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->xAxis2, SLOT(setRange(QCPRange)));
//connect(customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->yAxis2, SLOT(setRange(QCPRange)));
// setup a timer that repeatedly calls MainWindow::realtimeDataSlot:
connect(&dataTimer, SIGNAL(timeout()), this, SLOT(realtimeDataSlot()));
dataTimer.start(0); // Interval 0 means to refresh as fast as possible
customPlot->legend->setVisible(true);
}
void MainWindow::realtimeDataSlot()
{
//key的單位是秒
double key = QDateTime::currentDateTime().toMSecsSinceEpoch()/1000.0;
qsrand(QTime::currentTime().msec() + QTime::currentTime().second() * 10000);
//使用隨機數產生兩條曲線
double value0 = qrand() % 100;
double value1 = qrand() % 80;
if (ui->checkBox_temp->isChecked())
ui->customPlot->graph(0)->addData(key, value0);//添加數據1到曲線1
if (ui->checkBox_hui->isChecked())
ui->customPlot->graph(1)->addData(key, value1);//添加數據2到曲線2
//刪除8秒之前的數據。這裏的8要和下面設置橫座標寬度的8配合起來
//才能起到想要的效果,可以調整這兩個值,觀察顯示的效果。
ui->customPlot->graph(0)->removeDataBefore(key-8);
ui->customPlot->graph(1)->removeDataBefore(key-8);
//自動設定graph(1)曲線y軸的範圍,如果不設定,有可能看不到圖像
//也可以用ui->customPlot->yAxis->setRange(up,low)手動設定y軸範圍
ui->customPlot->graph(0)->rescaleValueAxis();
ui->customPlot->graph(1)->rescaleValueAxis(true);
//這裏的8,是指橫座標時間寬度爲8秒,如果想要橫座標顯示更多的時間
//就把8調整爲比較大到值,比如要顯示60秒,那就改成60。
//這時removeDataBefore(key-8)中的8也要改成60,否則曲線顯示不完整。
ui->customPlot->xAxis->setRange(key+0.25, 8, Qt::AlignRight);//設定x軸的範圍
ui->customPlot->replot();
}
已經說的很詳細了,留作筆記