项目中需要用到QCustomPlot的自适应图像接口,但是发现没有特别好用,比如当Y轴最大值有多个相同值时,即顶端图像为一段直线时,调用此接口后,y轴顶端曲线会紧贴边缘,导致可能看不到最顶部的直线,如下图:
于是,便对源码进行了修改,修改后,调用此接口后,上下会预留十分之一的空间,效果如下图:
上修改后的源码,对源码的rescale接口做了修改,增加了是否为y轴的判断。如果是y轴自适应时,预留十分之一
下面展示一些 内联代码片
。
``
void QCPAxis::rescale(bool onlyVisiblePlottables)
{
QCPRange newRange;
bool haveRange = false;
bool bIsyAxis = false;
foreach (QCPAbstractPlottable *plottable, plottables())
{
if (!plottable->realVisibility() && onlyVisiblePlottables)
continue;
QCPRange plottableRange;
bool currentFoundRange;
QCP::SignDomain signDomain = QCP::sdBoth;
if (mScaleType == stLogarithmic)
signDomain = (mRange.upper < 0 ? QCP::sdNegative : QCP::sdPositive);
if (plottable->keyAxis() == this)
plottableRange = plottable->getKeyRange(currentFoundRange, signDomain);
else
{
bIsyAxis = true;
plottableRange = plottable->getValueRange(currentFoundRange, signDomain);
}
if (currentFoundRange)
{
if (!haveRange)
newRange = plottableRange;
else
newRange.expand(plottableRange);
haveRange = true;
}
}
if (haveRange)
{
if (!QCPRange::validRange(newRange)) // likely due to range being zero (plottable has only constant data in this axis dimension), shift current range to at least center the plottable
{
double center = (newRange.lower+newRange.upper)*0.5; // upper and lower should be equal anyway, but just to make sure, incase validRange returned false for other reason
if (mScaleType == stLinear)
{
newRange.lower = center-mRange.size()/2.0;
newRange.upper = center+mRange.size()/2.0;
} else // mScaleType == stLogarithmic
{
newRange.lower = center/qSqrt(mRange.upper/mRange.lower);
newRange.upper = center*qSqrt(mRange.upper/mRange.lower);
}
setRange(newRange);
return;
}
//自适应时,Y轴上下预留10分之一
double dSize = 0;
if (bIsyAxis)
dSize = (newRange.upper - newRange.lower) / 10;
setRange(newRange.lower - dSize, newRange.upper + dSize);
}
}
下面为未修改的源码:
下面展示一些 内联代码片
。
void QCPAxis::rescale(bool onlyVisiblePlottables)
{
QCPRange newRange;
bool haveRange = false;
foreach (QCPAbstractPlottable *plottable, plottables())
{
if (!plottable->realVisibility() && onlyVisiblePlottables)
continue;
QCPRange plottableRange;
bool currentFoundRange;
QCP::SignDomain signDomain = QCP::sdBoth;
if (mScaleType == stLogarithmic)
signDomain = (mRange.upper < 0 ? QCP::sdNegative : QCP::sdPositive);
if (plottable->keyAxis() == this)
plottableRange = plottable->getKeyRange(currentFoundRange, signDomain);
else
{
plottableRange = plottable->getValueRange(currentFoundRange, signDomain);
}
if (currentFoundRange)
{
if (!haveRange)
newRange = plottableRange;
else
newRange.expand(plottableRange);
haveRange = true;
}
}
if (haveRange)
{
if (!QCPRange::validRange(newRange)) // likely due to range being zero (plottable has only constant data in this axis dimension), shift current range to at least center the plottable
{
double center = (newRange.lower+newRange.upper)*0.5; // upper and lower should be equal anyway, but just to make sure, incase validRange returned false for other reason
if (mScaleType == stLinear)
{
newRange.lower = center-mRange.size()/2.0;
newRange.upper = center+mRange.size()/2.0;
} else // mScaleType == stLogarithmic
{
newRange.lower = center/qSqrt(mRange.upper/mRange.lower);
newRange.upper = center*qSqrt(mRange.upper/mRange.lower);
}
}
setRange(newRange);
}
}