QCustomPlot Discussion and Comments

Overlapping graphs, fixed origin and y-axis rescalingReturn to overview

Hello everyone!

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...
I can re-explain or give another example if its not clear enough.
If you have any advice on how to make it or even code example, I'll take it ^^

Have a good day!

I'm not sure I fully understand your goal, but as a start: Have you tried connecting a slot to the rangeChanged signal of your yAxis, and there enforcing a lower bound of 0? (just call yAxis->setRangeLower(0);) This way the 0 never moves as you zoom or drag the yAxis.

The thing is that my data can be be in the negative, so the lower range must be in the negative too.

But I find a simple solution (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);
        }