Hi All,
I need my ColorMap to keep the aspect ratio of the background image.
This should also hold during resizing and user interactions with the plot.
The axis rect can ignore the aspect ratio and fit the max possible size of the widget, but the inner plot should keep the ratio.
So instead of this:
https://drive.google.com/file/d/1FZIZ6ZWftTSxb-6lueaG0C8_bMqzlHvO/view?usp=sharing
I want this:
https://drive.google.com/file/d/1Sgke1_HiMhZU_CysjHBH7kgmpwVl48Pb/view?usp=sharing
In the second image, I aligned the plot and the image by manually manipulating the axes. It is not perfect, but close to what I need.
I need it to work in the code.
What I tried is manipulating axes:
// Get the image size from the view model. QSize imageSize = mViewModel->plotPixelSize(); // Get pointers to the axes. (xAxis is at the top, yAxis is on the left.) QCPAxis *xAxis = axisRect()->axis(QCPAxis::atTop); QCPAxis *yAxis = axisRect()->axis(QCPAxis::atLeft); // Optionally reverse the y-axis if required (as in your code). yAxis->setRangeReversed(true); // Calculate the image's aspect ratio. double aspectRatio = imageSize.width() / static_cast<double>(imageSize.height()); // Enforce the aspect ratio of the image on the plot. // This ensures that one unit in x corresponds to the correct number of units in y. xAxis->setScaleRatio(yAxis, aspectRatio); xAxis->setRange(0, aspectRatio); yAxis->setRange(0, 1);
then creating a color map:
// Set the data size uint32_t columns = mViewModel->plotGridColumns(); uint32_t rows = mViewModel->plotGridRows(); mColorMap->data()->setSize(columns, rows); // Set the data range QSize imageSize = mViewModel->plotPixelSize(); double ratio = imageSize.width() / static_cast<double>(imageSize.height()); mColorMap->data()->setRange(QCPRange(0, ratio), QCPRange(0, 1.0));
and later overlaying pixmap:
mPixmap->topLeft->setAxes(axisRect()->axis(QCPAxis::atTop), axisRect()->axis(QCPAxis::atLeft)); mPixmap->bottomRight->setAxes(axisRect()->axis(QCPAxis::atTop), axisRect()->axis(QCPAxis::atLeft)); mPixmap->topLeft->setType(QCPItemPosition::PositionType::ptPlotCoords); mPixmap->bottomRight->setType(QCPItemPosition::PositionType::ptPlotCoords); QSize imageSize = mViewModel->plotPixelSize(); double ratio = imageSize.width() / static_cast<double>(imageSize.height());; mPixmap->topLeft->setCoords(0, 0); mPixmap->bottomRight->setCoords(ratio, 1.0);
and lastly, in the resize event:
// Update the axes range to match the background image QSize imageSize = mViewModel->plotPixelSize(); double ratio = imageSize.width() / static_cast<double>(imageSize.height());; QCPAxis *xA = axisRect()->axis(QCPAxis::atBottom); QCPAxis *yA = axisRect()->axis(QCPAxis::atLeft); xA->setRange(0, ratio); yA->setRange(0, 1.0); // Ensure the axis scaling preserves the image's aspect ratio: xA->setScaleRatio(yA, ratio); replot();
Does anyone know if this is even feasible with QCustomPlot?
I would really appreciate this feature to work in my application.
Thanks in advance for your answer.
Cheers,
Rafal