How do I know the plot pointer?
I have a number of plots > 1 all dynamically created.
Thus I can not write a connect code and a function for each plot. I need this to work automatically.
I solved this using the Qt property function and included your code.
Why is such functionality not within the QCP code itsself?
void MainWindow::setupPlot(QCustomPlot *plot, QString title)
{
...
connect(plot->xAxis, SIGNAL(rangeChanged(QCPRange)), this, SLOT(xAxisChanged(QCPRange)));
connect(plot->yAxis, SIGNAL(rangeChanged(QCPRange)), this, SLOT(yAxisChanged(QCPRange)));
}
void MainWindow::updatePlotData(QCustomPlot *plot, vector<double> & y0, vector<double> & y1 )
{
...
plot->replot();
plot->setProperty("xmin", plot->xAxis->range().minRange());
plot->setProperty("xmax", plot->xAxis->range().maxRange());
plot->setProperty("ymin", plot->yAxis->range().lower);
plot->setProperty("ymax", plot->yAxis->range().upper);
}
void MainWindow::xAxisChanged(const QCPRange & newRange)
{
QCPAxis * axis = qobject_cast<QCPAxis *>(QObject::sender());
QCustomPlot * plot = axis->parentPlot();
QCPRange limitRange(plot->property("xmin").toDouble(), plot->property("xmax").toDouble());
limitAxisRange(axis, newRange, limitRange);
}
void MainWindow::yAxisChanged(const QCPRange & newRange)
{
QCPAxis * axis = qobject_cast<QCPAxis *>(QObject::sender());
QCustomPlot * plot = axis->parentPlot();
QCPRange limitRange(plot->property("ymin").toDouble(), plot->property("ymax").toDouble());
limitAxisRange(axis, newRange, limitRange);
}
void MainWindow::limitAxisRange(QCPAxis * axis, const QCPRange & newRange, const QCPRange & limitRange)
{
auto lowerBound = limitRange.lower;
auto upperBound = limitRange.upper;
// code assumes upperBound > lowerBound
QCPRange fixedRange(newRange);
if (fixedRange.lower < lowerBound)
{
fixedRange.lower = lowerBound;
fixedRange.upper = lowerBound + newRange.size();
if (fixedRange.upper > upperBound || qFuzzyCompare(newRange.size(), upperBound-lowerBound))
fixedRange.upper = upperBound;
axis->setRange(fixedRange); // adapt this line to use your plot/axis
} else if (fixedRange.upper > upperBound)
{
fixedRange.upper = upperBound;
fixedRange.lower = upperBound - newRange.size();
if (fixedRange.lower < lowerBound || qFuzzyCompare(newRange.size(), upperBound-lowerBound))
fixedRange.lower = lowerBound;
axis->setRange(fixedRange); // adapt this line to use your plot/axis
}
}