QCustomPlot Discussion and Comments

Multi-axis scaling bindingReturn to overview

There are 3 axes: one in X, two in Y on the right and left. When scaling only one axis (when you hover over the line of the axis itself), only this axis changes the scale, but the second axis needs to be zoomed too

    ui->plot->addGraph(ui->plot->xAxis,ui->plot->yAxis);
    ui->plot->graph(0)->setPen(QPen(Qt::green));
    ui->plot->graph(0)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssDot,6));

    ui->plot->addGraph(ui->plot->xAxis,ui->plot->yAxis2);
    ui->plot->graph(1)->setPen(QPen(Qt::red));
    ui->plot->graph(1)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCrossCircle,6));

    ui->plot->addGraph(ui->plot->xAxis,ui->plot->yAxis2);
    ui->plot->graph(2)->setPen(QPen(Qt::yellow));
    ui->plot->graph(2)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssPlus,8));

    ui->plot->yAxis2->setVisible(true);

    ui->plot->xAxis->setRange(-0.5,10.5);
    ui->plot->yAxis->setRange(-0.5,5.5);

    double tempLowRange=15000./11.;
    ui->plot->yAxis2->setRange(-tempLowRange,15000);

    ui->plot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);

    QList <QCPAxis*> draggableAxes={ui->plot->xAxis,ui->plot->yAxis,ui->plot->yAxis2};

    ui->plot->axisRect()->setRangeDragAxes(draggableAxes);
    ui->plot->axisRect()->setRangeZoomAxes(draggableAxes);

What needs to be added or removed?

To put it another way, you need to combine the zero line X

Do you mean when you change Y1 axis ,the other axis(Y2) zooms too?
void QCPAxisRect::setupFullAxesBox ( bool connectRanges = false) would works.
Or simply you can use codes like

connect(xAxis,SIGNAL(rangeChanged(QCPRange)),xAxis2,SLOT(setRange(QCPRange)));
connect(yAxis,SIGNAL(rangeChanged(QCPRange)),yAxis2,SLOT(setRange(QCPRange)));

Thanks for your answer YYZ, but QCPAxisRect::setupFullAxesBox ( bool connectRanges = false) doesn't work, and the value range of yAxis2 becomes the same as that of yAxis (but needs yAxis=[0,15], yAxis2=[0,0.2])

Hi Fedor,
I apologize for the confusion regarding your requirements. The functionality you may want to achieve is as follows: when the mouse hovers over a single axis (such as Y1), and trigger scaling, and the corresponding axis (such as Y2) should also scale, even if their ranges are not necessarily the same.

This functionality seems to deviate from the original design of QCP. Currently, the built-in functionality of QCP is that when the mouse hovers over a single axis, only that axis will scale or drag. When the mouse hovers over the axis rect, the axes that are zoomable within the axis rect will be scaled, and which axes are zoomable is controlled byvoid QCPAxisRect::setRangeZoomAxes . This information is derived from my analysis of the source code in void QCustomPlot::wheelEvent(QWheelEvent* event).

Nevertheless, I believe your desired functionality can be implemented. Currently, my initial thought is that it may be necessary to modify the source code. By altering the source code to emit a signal invoid QCPAxis::wheelEvent(QWheelEvent* event) , the corresponding axis could receive the signal. This is just a preliminary idea, and you may want to give it a try.

I strongly recommend that you study the source code of void QCPAxis::wheelEvent(QWheelEvent* event), as it will provide you with a lot of insights.

Thank you YYZ for your answer, i will study the source code of QCPAxis::wheelEvent(QWheelEvent* event)