2019-01-13 01:53:16 +08:00
|
|
|
/** \example speedtestplot.cpp
|
2019-01-20 23:15:10 +08:00
|
|
|
* JKQTPlotter: Examples: Simple line-graph with live-data (speed test)
|
2019-01-13 01:53:16 +08:00
|
|
|
*
|
2019-01-20 23:15:10 +08:00
|
|
|
* \ref JKQTPlotterSpeedTest
|
2019-01-13 01:53:16 +08:00
|
|
|
*/
|
|
|
|
|
2018-12-29 00:46:47 +08:00
|
|
|
#include "speedtestplot.h"
|
2019-04-22 19:27:50 +08:00
|
|
|
#include "jkqtplotter/jkqtpgraphsscatter.h"
|
2018-12-29 00:46:47 +08:00
|
|
|
|
|
|
|
|
|
|
|
SpeedTestPlot::SpeedTestPlot():
|
2019-01-20 23:15:10 +08:00
|
|
|
JKQTPlotter(), dx(1.0/double(NDATA)*4.0*M_PI), x0(0)
|
2018-12-29 00:46:47 +08:00
|
|
|
|
|
|
|
{
|
2019-01-20 23:15:10 +08:00
|
|
|
// 1. optimize JKQTPlotter for speed (by switching off anti-aliasing)
|
2019-01-26 03:16:04 +08:00
|
|
|
getPlotter()->setUseAntiAliasingForGraphs(false);
|
|
|
|
getPlotter()->setUseAntiAliasingForSystem(false);
|
|
|
|
getPlotter()->setUseAntiAliasingForText(false);
|
2018-12-29 00:46:47 +08:00
|
|
|
|
|
|
|
|
|
|
|
// 2. now we create data for a simple plot (a sine curve + random[-0.5,0.5])
|
|
|
|
for (size_t i=0; i<NDATA; i++) {
|
|
|
|
const double x=static_cast<double>(i)*dx;
|
|
|
|
X[i]=x0+x;
|
|
|
|
Y[i]=sin(x)+static_cast<double>(std::rand())/static_cast<double>(RAND_MAX + 1u)-0.5;
|
|
|
|
Y2[i]=cos(x)+static_cast<double>(std::rand())/static_cast<double>(RAND_MAX + 1u)-0.5;
|
|
|
|
}
|
|
|
|
|
2019-01-20 23:15:10 +08:00
|
|
|
// 3. make data available to JKQTPlotter by adding it to the internal datastore.
|
2018-12-29 00:46:47 +08:00
|
|
|
// Here the data from the std::array's is not copied, but only the pointer to
|
|
|
|
// the array is added to the datastore. therefore the datastore does not manage
|
|
|
|
// the memory, oly uses the data stored in it!
|
2019-01-20 17:49:29 +08:00
|
|
|
JKQTPDatastore* ds=getDatastore();
|
2018-12-29 00:46:47 +08:00
|
|
|
size_t columnX=ds->addColumn(X.data(), X.size(), "x");
|
|
|
|
size_t columnY=ds->addColumn(Y.data(), Y.size(), "y");
|
|
|
|
size_t columnY2=ds->addColumn(Y2.data(), Y2.size(), "y2");
|
|
|
|
|
|
|
|
// 4. create two graphs in the plot, which plots the dataset X/Y:
|
2019-01-20 17:49:29 +08:00
|
|
|
JKQTPXYLineGraph* graph=new JKQTPXYLineGraph(this);
|
2019-01-26 20:00:40 +08:00
|
|
|
graph->setXColumn(columnX);
|
|
|
|
graph->setYColumn(columnY);
|
|
|
|
graph->setTitle(QObject::tr("live sin() graph"));
|
2019-01-26 03:16:04 +08:00
|
|
|
graph->setLineWidth(1);
|
2018-12-29 00:46:47 +08:00
|
|
|
addGraph(graph);
|
|
|
|
|
2019-01-20 17:49:29 +08:00
|
|
|
JKQTPXYLineGraph* graph2=new JKQTPXYLineGraph(this);
|
2019-01-26 20:00:40 +08:00
|
|
|
graph2->setXColumn(columnX);
|
|
|
|
graph2->setYColumn(columnY2);
|
|
|
|
graph2->setTitle(QObject::tr("live cos() graph"));
|
2019-01-26 03:16:04 +08:00
|
|
|
graph2->setLineWidth(1);
|
2018-12-29 00:46:47 +08:00
|
|
|
addGraph(graph2);
|
|
|
|
|
|
|
|
// 6. scale the plot so the graph is contained
|
|
|
|
setX(X[0], X[NDATA-1]);
|
|
|
|
setY(-2,2);
|
|
|
|
|
|
|
|
// show plotter and make it a decent size
|
|
|
|
show();
|
|
|
|
resize(1000,500);
|
|
|
|
}
|
|
|
|
|
|
|
|
SpeedTestPlot::~SpeedTestPlot()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpeedTestPlot::plotNewData()
|
|
|
|
{
|
|
|
|
// move old data to the left
|
|
|
|
for (size_t i=0; i<NDATA-1; i++) {
|
|
|
|
X[i]=X[i+1];
|
|
|
|
Y[i]=Y[i+1];
|
|
|
|
Y2[i]=Y2[i+1];
|
|
|
|
}
|
|
|
|
// add one new data point
|
|
|
|
X[NDATA-1]=X[NDATA-2]+dx;
|
|
|
|
Y[NDATA-1]=sin(X[NDATA-1])+static_cast<double>(std::rand())/static_cast<double>(RAND_MAX + 1u)-0.5;
|
|
|
|
Y2[NDATA-1]=cos(X[NDATA-1])+static_cast<double>(std::rand())/static_cast<double>(RAND_MAX + 1u)-0.5;
|
|
|
|
|
|
|
|
/*
|
|
|
|
// ALTERNATIVE: MOVE data, but keep x-axis range
|
|
|
|
x0+=dx;
|
|
|
|
for (size_t i=0; i<NDATA-1; i++) {
|
|
|
|
Y[i]=Y[i+1];
|
|
|
|
Y2[i]=Y2[i+1];
|
|
|
|
}
|
|
|
|
// add one new data point
|
|
|
|
Y[NDATA-1]=sin(X[NDATA-1]+x0)+static_cast<double>(std::rand())/static_cast<double>(RAND_MAX + 1u)-0.5;
|
|
|
|
Y2[NDATA-1]=cos(X[NDATA-1]+x0)+static_cast<double>(std::rand())/static_cast<double>(RAND_MAX + 1u)-0.5;
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
// set new x-range and replot
|
|
|
|
setX(X[0], X[NDATA-1]);
|
2019-01-26 19:28:44 +08:00
|
|
|
redrawPlot();
|
2018-12-29 00:46:47 +08:00
|
|
|
|
|
|
|
// calculate and update FPS-rate in window title
|
|
|
|
auto tlastalst=t_lastplot;
|
|
|
|
t_lastplot=std::chrono::system_clock::now();
|
|
|
|
double delta_secs=static_cast<double>(std::chrono::duration_cast<std::chrono::milliseconds>(t_lastplot-tlastalst).count())/1000.0;
|
|
|
|
setWindowTitle(QString("Live Data Speed Test: %2 datapoint, %1 fps").arg(1/delta_secs).arg(NDATA));
|
|
|
|
// enqueue call for next data value
|
|
|
|
QTimer::singleShot(1, this, SLOT(plotNewData()));
|
|
|
|
}
|