Change axis ticks and label to switch between millimeter and inch in QWT Plot

ⅰ亾dé卋堺 提交于 2019-12-11 12:17:37

问题


What I want is to switch axis labels and ticks to match as best for displaying inch and millimeters without changing curve points. I created a custom QwtScaleDraw and overwrote label() method, but this will not change the ticks.

class QwtMyScaleDraw : public QwtScaleDraw {
public:
    QwtMyScaleDraw() : QwtScaleDraw() {
    }
protected:
    QwtText label(double value) const {
           // global conversion routine that creates a display string either as millimeter or inch
           return ConvertToString(value, ConversionFromMillimeter);
    }
};

You can see this behaviour on following picture:

Scale on left is the original scale, scale in the middle is with my custom label and right is the desired scale.

What is the best approach to get my desired results without adding entire curves again with manually converted values?


回答1:


With the help of the author of the qwt library I found the solution. The answer is to derive from QwtLinearScaleEngine class and overload autoScale and divideScale methods.

In my implementation any conversion factor can be choosen (default is 1 = no conversion). This is my code:

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;
};


来源:https://stackoverflow.com/questions/18587571/change-axis-ticks-and-label-to-switch-between-millimeter-and-inch-in-qwt-plot

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!