Some modifications with the realtime qcp example seem to do the job, but not sure if it's the best way to do what you want, and in this case, you may lose samples...
void MainWindow::setupRealtimeDataDemo(QCustomPlot *customPlot)
{
demoName = "Real Time Data Demo";
customPlot->addGraph(customPlot->xAxis, customPlot->yAxis); // blue line
customPlot->graph(0)->setPen(QPen(Qt::blue));
customPlot->addGraph(customPlot->xAxis2, customPlot->yAxis); // red line
customPlot->graph(1)->setPen(QPen(Qt::red));
customPlot->xAxis->setRange(0, 5);
customPlot->xAxis2->setRange(5, 10);
// 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 MainWindow::realtimeDataSlot()
{
static QTime time(QTime::currentTime());
// calculate two new data points:
double keyTime = time.elapsed()/1000.0; // time elapsed since start of demo, in seconds
double key = std::fmod(keyTime,10);
static bool plottingBlue = true;
// add data to lines:
double val = qSin(keyTime)+qrand()/(double)RAND_MAX*1*qSin(keyTime/0.3843);
ui->customPlot->graph(0)->addData(key, val);
ui->customPlot->graph(1)->addData(key, val);
if(key < 5.0)
{
if(!plottingBlue)
{
plottingBlue = true;
ui->customPlot->graph(0)->data()->clear();
}
ui->customPlot->graph(1)->data()->removeBefore(5+key);
}
else
{
if(plottingBlue)
{
plottingBlue = false;
ui->customPlot->graph(1)->data()->clear();
}
ui->customPlot->graph(0)->data()->removeBefore(key-5);
}
// make key axis range scroll with the data (at a constant range size of 8):
ui->customPlot->yAxis->rescale();
ui->customPlot->replot();
}