更改轴刻度和标签以在 QWT 图中在毫米和英寸之间切换 [英] Change axis ticks and label to switch between millimeter and inch in QWT Plot

查看:103
本文介绍了更改轴刻度和标签以在 QWT 图中在毫米和英寸之间切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要的是切换轴标签和刻度以匹配最适合显示英寸和毫米而不改变曲线点的情况.我创建了一个自定义 QwtScaleDraw 并覆盖了 label() 方法,但这不会改变刻度.

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?

推荐答案

在 qwt 库作者的帮助下,我找到了解决方案.答案是从 QwtLinearScaleEngine 类派生并重载 autoScaledivideScale 方法.

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.

在我的实现中,可以选择任何转换因子(默认为 1 = 不转换).这是我的代码:

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

这篇关于更改轴刻度和标签以在 QWT 图中在毫米和英寸之间切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆