QCustomPlot Discussion and Comments

How to align Y-axis text label with axes of multi Y-axis in vertical layout?Return to overview


Following codes make axes aligning only and can not make text label aligning when tick number of Y-axis scaling.
Anyone have a good idea? Thanks.

msGroup = new QCPMarginGroup(ui->customplot);
axisRect = new QCPAxisRect(ui->customplot, true);
common_axisRect->setAutoMargins(QCP::MarginSide::msLeft | QCP::MarginSide::msBottom | QCP::MarginSide::msRight);
common_axisRect->setMarginGroup(QCP::MarginSide::msLeft | QCP::MarginSide::msBottom | QCP::MarginSide::msRight, msGroup);

ui->customplot->plotLayout()->addElement(0, 0, common_axisRect);

QCPAxisRect *axisRect = new QCPAxisRect(ui->customplot, true);
axisRect->setAutoMargins(QCP::MarginSide::msLeft | QCP::MarginSide::msRight);
axisRect->setMarginGroup(QCP::msLeft | QCP::MarginSide::msRight, msGroup);
ui->customplot->plotLayout()->addElement(1, 0, axisRect);

You can insert the labels as QCPItemText and align its y-axis with the y-axis of the associated AxisRect (see: QCPItemPosition::PositionType).

This worked for me:

// create aligned labels for left axis
mCustomPlot->plotLayout()->insertColumn(0);
mCustomPlot->plotLayout()->setColumnSpacing(15);
mCustomPlot->plotLayout()->setColumnStretchFactor(0, 0.001);

QCPItemText* yAxisLabelTop = new QCPItemText(ui->customplot);
yAxisLabelTop->setText("abc");
yAxisLabelTop->setRotation(90);
yAxisLabelTop->position->setAxisRect(topAxisRect);
yAxisLabelTop->setClipToAxisRect(false);
//x is absolut position
yAxisLabelTop->position->setTypeX(QCPItemPosition::ptAbsolute);
//y is relativ to AxesRect 
yAxisLabelTop->position->setTypeY(QCPItemPosition::ptAxisRectRatio);   
yAxisLabelTop->position->setAxes(0, topAxisRect->axis(QCPAxis::atLeft));
yAxisLabelTop->position->setCoords(10, 0.5); //label is 10 pixels from left side of widget und y=0.5 means in the middle of the y-axis

QCPItemText* yAxisLabelBottom = new QCPItemText(ui->customplot);
yAxisLabelBottom->setText("defg");
yAxisLabelBottom->setRotation(90);
yAxisLabelBottom->position->setAxisRect(bottomAxisRect);
yAxisLabelBottom->setClipToAxisRect(false);
yAxisLabelBottom->position->setTypeX(QCPItemPosition::ptAbsolute);
yAxisLabelBottom->position->setTypeY(QCPItemPosition::ptAxisRectRatio);  
yAxisLabelBottom->position->setAxes(0, bottomAxisRect->axis(QCPAxis::atLeft));
yAxisLabelBottom->position->setCoords(10, 0.5);

This is also meet my needs.
thank you Carina :)