24 Star 63 Fork 25

风木 / MyYoudaoNoteLike

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
myrealtimeplot.cpp 3.65 KB
一键复制 编辑 原始数据 按行查看 历史
风木 提交于 2015-02-06 18:09 . add
#include "myrealtimeplot.h"
#include <QDebug>
MyRealTimePlot::MyRealTimePlot(QCustomPlot *parent) :
QCustomPlot(parent)
{
QCustomPlot*customPlot=this;
customPlot->addGraph(); // blue line
customPlot->graph(0)->setPen(QPen(Qt::blue));
customPlot->graph(0)->setBrush(QBrush(QColor(240, 255, 200,180)));
customPlot->graph(0)->setAntialiasedFill(false);
customPlot->graph(0)->setChannelFillGraph(0);
customPlot->addGraph(); // red line
customPlot->graph(1)->setPen(QPen(Qt::red));
customPlot->graph(1)->setBrush(QBrush(QColor(204, 232, 207,180)));
customPlot->graph(1)->setChannelFillGraph(0);
customPlot->addGraph(); // blue dot
customPlot->graph(2)->setPen(QPen(Qt::blue));
customPlot->graph(2)->setLineStyle(QCPGraph::lsNone);
customPlot->graph(2)->setScatterStyle(QCPScatterStyle::ssDisc);
customPlot->addGraph(); // red dot
customPlot->graph(3)->setPen(QPen(Qt::red));
customPlot->graph(3)->setLineStyle(QCPGraph::lsNone);
customPlot->graph(3)->setScatterStyle(QCPScatterStyle::ssDisc);
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
}
void MyRealTimePlot::realtimeDataSlot()
{
QCustomPlot*customPlot=this;
// calculate two new data points:
double key = QDateTime::currentDateTime().toMSecsSinceEpoch()/1000.0;
static double lastPointKey = 0;
if (key-lastPointKey > 0.01) // at most add point every 10 ms
{
double value0 = qSin(key); //qSin(key*1.6+qCos(key*1.7)*2)*10 + qSin(key*1.2+0.56)*20 + 26;
double value1 = qCos(key); //qSin(key*1.3+qCos(key*1.2)*1.2)*7 + qSin(key*0.9+0.26)*24 + 26;
// add data to lines:
customPlot->graph(0)->addData(key, value0);
customPlot->graph(1)->addData(key, value1);
// set data of dots:
customPlot->graph(2)->clearData();
customPlot->graph(2)->addData(key, value0);
customPlot->graph(3)->clearData();
customPlot->graph(3)->addData(key, value1);
// remove data of lines that's outside visible range:
customPlot->graph(0)->removeDataBefore(key-8);
customPlot->graph(1)->removeDataBefore(key-8);
// rescale value (vertical) axis to fit the current data:
customPlot->graph(0)->rescaleValueAxis();
customPlot->graph(1)->rescaleValueAxis(true);
lastPointKey = key;
}
// make key axis range scroll with the data (at a constant range size of 8):
customPlot->xAxis->setRange(key+0.25, 8, Qt::AlignRight);
customPlot->replot();
// calculate frames per second:
static double lastFpsKey;
static int frameCount;
++frameCount;
if (key-lastFpsKey > 2) // average fps over 2 seconds
{
QString fps= QString("%1 FPS, Total Data points: %2")
.arg(frameCount/(key-lastFpsKey), 0, 'f', 0)
.arg(customPlot->graph(0)->data()->count()+customPlot->graph(1)->data()->count());
qDebug()<<fps;
lastFpsKey = key;
frameCount = 0;
}
}
C++
1
https://gitee.com/kyyblabla/MyYoudaoNoteLike.git
git@gitee.com:kyyblabla/MyYoudaoNoteLike.git
kyyblabla
MyYoudaoNoteLike
MyYoudaoNoteLike
master

搜索帮助