在 qwt 库作者的帮助下,我找到了解决方案。答案是派生自QwtLinearScaleEngine
类和重载autoScale
和divideScale
方法。
在我的实现中,可以选择任何转换因子(默认为 1 = 无转换)。这是我的代码:
class QwtUnitScaleDraw : public QwtScaleDraw {
public:
QwtUnitScaleDraw(double ConversionFactor = 1.0) : QwtScaleDraw(), _ConversionFactor(ConversionFactor) {
}
protected:
QwtText label(double value) const {
return QwtScaleDraw::label(value * _ConversionFactor);
}
protected:
double _ConversionFactor;
};
class QwtUnitScaleEngine: public QwtLinearScaleEngine {
public:
QwtUnitScaleEngine(double ConversionFactor = 1.0) : QwtLinearScaleEngine(10), _ConversionFactor(ConversionFactor) {
}
void autoScale(int maxSteps, double &x1, double &x2, double &stepSize) const {
x1 *= _ConversionFactor;
x2 *= _ConversionFactor;
stepSize *= _ConversionFactor;
QwtLinearScaleEngine::autoScale(maxSteps, x1, x2, stepSize);
x1 /= _ConversionFactor;
x2 /= _ConversionFactor;
stepSize /= _ConversionFactor;
}
QwtScaleDiv divideScale(double x1, double x2, int numMajorSteps, int numMinorSteps, double stepSize = 0.0) const {
x1 *= _ConversionFactor;
x2 *= _ConversionFactor;
stepSize *= _ConversionFactor;
QwtScaleDiv Div = QwtLinearScaleEngine::divideScale(x1, x2, numMajorSteps, numMinorSteps, stepSize);
QList<double> Ticks[QwtScaleDiv::NTickTypes];
Ticks[QwtScaleDiv::MajorTick] = Div.ticks(QwtScaleDiv::MajorTick);
for (unsigned int i = 0; i < Ticks[QwtScaleDiv::MajorTick].count(); i++) {
Ticks[QwtScaleDiv::MajorTick][i] /= _ConversionFactor;
}
Ticks[QwtScaleDiv::MediumTick] = Div.ticks(QwtScaleDiv::MediumTick);
for (unsigned int i = 0; i < Ticks[QwtScaleDiv::MediumTick].count(); i++) {
Ticks[QwtScaleDiv::MediumTick][i] /= _ConversionFactor;
}
Ticks[QwtScaleDiv::MinorTick] = Div.ticks(QwtScaleDiv::MinorTick);
for (unsigned int i = 0; i < Ticks[QwtScaleDiv::MinorTick].count(); i++) {
Ticks[QwtScaleDiv::MinorTick][i] /= _ConversionFactor;
}
return QwtScaleDiv(QwtInterval(x1 / _ConversionFactor, x2 / _ConversionFactor), Ticks);
}
protected:
double _ConversionFactor;
};