QCustomPlot Discussion and Comments

Zooming in multiple plots simultaneously Return to overview

Hello there,

I'm trying to implement zooming in my code but I have some issues.
I activated zoom and drag via ui->customPlot->setInteractions(QCP::iRangeZoom|QCP::iRangeDrag)

But I have more than 1 plot (possibly more than 5) and I want their ranges to be sinchronous when manipulated.

And as I'm asking for help better go all the way, would anyone know how to have 1 common xAxis at the bottom of all my plots that would give the range of all the plots above it

If anyone has an idea of how to make this, I will be glad


(Sorry if weird english, baguette speaker here)

Sounds like connecting the rangeChanged signal to according setRange slots of the other axes would do.

To have only one xAxis, set the others to invisible but nevertheless connect their ranges as above.

Thanks for the tip Isso ;)
Sadly this is not over

As I want to manage dynamically made plots, I cannot connect them by hand
I tried like this, but it doesn't seem to work

for(int i =0; i ui->customPlot->graphCount()-1;i++)
     connect(ui->customPlot->axisRect(ui->customPlot->graphCount()-1)->axis(QCPAxis::atBottom, SIGNAL(rangeChanged(QCPRange), ui->customPlot->axisRect(i)->axis(QCPAxis::atBottom), SLOT(setRange(QCPRange));

Yet, this doesn't send any error (would be easier), just not working

I managed my issue

So to explain what I did, I created a function that creates plots (1 each time it is called) which are added in my QCustomPlot (customPlot). In order to do so, the function creates a QCPAxisRect (axisRect) in ui->customPlot
There I set drag and zoom enabled for axisRect (in my case I only want on the xAxis)

and I add axisRect to ui->customPlot->plotLayout()->addElement(axisRect);

Then, I create the QCPGraph(graph) (just followed the basic plotting guide for this)

and now here is the part you waited for
I connect axisRect's xAxis to the previous xAxis created and also connect these last ones back to axisRect's xAxis

It looks like this:

for (int i = 0; i<ui->customplot->graphCount();i++){
     connect(axisRect->axis(QCPAxis::atBottom), SIGNAL(rangeChanged(QCPRange)), ui->customPlot->axisRect(i)->axis(QCPAxis::atBottom), SLOT(setRange(QCPRange)));
     connect(ui->customPlot->axisRect(i)->axis(QCPAxis::atBottom), SIGNAL(rangeChanged(QCPRange)), axisRect->axis(QCPAxis::atBottom), SLOT(setRange(QCPRange)));

And with this, it worked just as I wanted, the zooming and dragging are made on all the plots I made.

Hope it helps


Watch out Prim, your code contains a flaw: You're calling plot->axisRect(i) with the index of a graph, not with the index of the axis rects. You already had that misconception in your previous post.

plot->graphCount() goes together with plot->graph(i)
plot->axisRectCount() goes together with plot->axisRect(i)

In any case, I don't see why you have to iterate over all axis rects. To my understanding it should be enough for you to connect the signals for the new axis rect always to the original axis rect with index 0, right?

Thanks DerManu,
Did not thought to only connect to axisRect(0) but this works perfectly
Also in my code, putting plot->graphCount() or plot->axisRectCount() doesn't change anything (maybe because I only have 1 graph per axisRect? Is it even possible to have more than 1?)

But now my code is lighter so thanks =)