PDA

View Full Version : QCustomPlot - overlapping graphs, fixed origin and y-axis rescaling



Thibaut
27th July 2020, 13:13
Hello everyone!

I use QCustomPlot to draw my graphs and I tried different methods and nothing seems to give me the result I search.

I want to draw multiple graphs, one below the other. The separation between each graph should be the same for all of them.
I also want to be able to change the scale of both the x- and y-axis, keeping the origin of each graph the same (their "0" shouldn't move when rescaling the y-axis). Which also mean that each graph could overlap with the others if needed (this also mean that it can happen that data can go out of the screen, but that's ok).
For the x-axis, I don't have a problem, its quite easy. Its the rescaling of the y-axis that gives me problems...

So here's what I've got for now:

In this first code, I'm able to fix the position of each plot, but I don't see how to change the y-axis scale during execution.


plot = new QCustomPlot;

xAxis.resize(timeScale);
for (int i = 0; i < timeScale; i++)
{
xAxis[i] = i;
}

// Offset the data so they're not on top of each other
//--------------------
QCPRange defaultRange(-100., 100.);
for (int i = 0; i < nbPlot; i++)
{
QCPAxis *axis = plot->axisRect()->addAxis(QCPAxis::atLeft);
axis->setRange(defaultRange.lower - (nbPlot - 1 - i) * defaultRange.size(),
defaultRange.upper + i * defaultRange.size());
axis->setVisible(false);

QCPGraph *graph = new QCPGraph(plot->xAxis, axis);
}

// Set the data to the plot
//--------------------
for (int i = 0; i < plot->graphCount(); i++)
{
plot->graph(i)->setData(xAxis, plotMatrix[i], true);
}



In this second code, I offset the data, and I'm able to dynamically change the y-axis during execution. But the graphs's origins aren't fixed, and move when rescaling.


plot = new QCustomPlot;

xAxis.resize(timeScale);
for (int i = 0; i < timeScale; i++)
{
xAxis[i] = i;
}

// Set the data to the plot
//--------------------
for (int i=0; i<nbPlot; ++i)
{
plot->addGraph();
// Offset data:
for (int j = 0; j < plotMatrix[i].length(); j++)
{
plotMatrix[i][j] -= (i + 1) * 200;
}
plot->graph()->setData(xAxis, plotMatrix[i]);
plot->graph()->rescaleAxes(true);
}


In both cases, the graphs can overlap with the other if the data ask for it. But none of those codes satisfy my needs...

If you have any advice on how to make it or even code example, I'll take it ^^

Have a good day!

Thibaut
28th July 2020, 13:43
Hi again!

So, after some testing, I found something that's working! (don't know why I didn't thought of it earlier...)

Here's the solution for anyone having the same problem (I kept the first code I gave to create the graphs):


QCPRange newRange(-50., 50.); // Choose your range according to your data
for (int i = 0; i < nbPlot; i++)
{
QCPAxis *newAxis = new QCPAxis(plot->axisRect(), QCPAxis::atLeft);

newAxis->setRange(newRange.lower - (nbPlot - 1 - i) * newRange.size(),
newRange.upper + i * newRange.size());
newAxis->setVisible(false);

plot->graph(i)->setValueAxis(newAxis);
}


Fast and easy.