• Qt绘图控件qwt绘制等比例坐标图


    需要用到QwtPlotRescaler类,用法如下:

    QwtPlotRescaler *plotRescaler = new QwtPlotRescaler(canvas, yLeft, QwtPlotRescaler::Fitting ); 
    
    plotRescaler->setExpandingDirection(QwtPlotRescaler::ExpandBoth);

    其构造函数可以设置参考坐标轴,及其缩放动作,默认其他轴与参考轴的比例为1.0。setExpandingDirection函数可以指定在固定好参考轴后,另一轴拉伸时的模式,QwtPlotRescaler::ExpandBoth参数是指向两边扩展。

    在找到这个类前,我有一个手动写法,原理是将QwtPlotZoomer的zoomed(const QRectF &)信号与在它所在的QwtPlot类中编写的槽相连,并重写QwtPlot类中的resizeEvent事件:

    void TrackPlot::zoomedSlot(const QRectF &rectF)
    {
        QPointF centerPoint = rectF.center();
    
        double Ratio = rectF.width() / rectF.height();
        double hvRatio =  double(canvas()->width()) / double(canvas()->height());    
    
        if(Ratio > 1)
        {
            double scaleFactor = hvRatio / Ratio > 1 ? hvRatio / Ratio : 1;
    
            setAxisScale(xBottom, centerPoint.x() - scaleFactor * rectF.width() / 2.0, 
                centerPoint.x() + scaleFactor * rectF.width() / 2.0);
            double step = axisStepSize(xBottom);
            setAxisScale(yLeft, centerPoint.y() - scaleFactor * rectF.width() / (2.0 * hvRatio), 
                scaleFactor * centerPoint.y() + scaleFactor * rectF.width() / (2.0 * hvRatio), step);    
        }
        else
        {
            double scaleFactor = Ratio / hvRatio > 1 ? Ratio / hvRatio : 1;
    
            setAxisScale(yLeft, centerPoint.y() - scaleFactor * rectF.height() / 2.0, 
                centerPoint.y() + scaleFactor * rectF.height() / 2.0);
            double step = axisStepSize(yLeft);
            setAxisScale(xBottom, centerPoint.x() - scaleFactor * rectF.height() * hvRatio / 2.0, 
                centerPoint.x() + scaleFactor * rectF.height() * hvRatio / 2.0, step);
        }
    
        replot();
    }
    
    void TrackPlot::resizeEvent(QResizeEvent *ev)
    {
        QwtPlot::resizeEvent(ev);
    
        if(ev->oldSize().width() > 5)
        {
            double hRatio = double(ev->size().width()) / double(ev->oldSize().width());
            double vRatio = double(ev->size().height()) / double(ev->oldSize().height());
    
            if(hRatio != 1)
                printf("");
    
            double xLowBound = axisScaleDiv(xBottom).lowerBound();
            double xUpperBound = axisScaleDiv(xBottom).upperBound();
            double yLowBound = axisScaleDiv(yLeft).lowerBound();
            double yUpperBound = axisScaleDiv(yLeft).upperBound();
            
            setAxisScale(xBottom, hRatio * xLowBound, hRatio * xUpperBound);
            double ystep = axisStepSize(xBottom);
            setAxisScale(yLeft, vRatio * yLowBound, vRatio * yUpperBound, ystep);
    
            replot();
        }
        else
        {
            double minX = axisScaleDiv(xBottom).lowerBound();
            double maxX = axisScaleDiv(xBottom).upperBound();
            double minY = axisScaleDiv(yLeft).lowerBound();
            double maxY = axisScaleDiv(yLeft).upperBound();
    
            QRectF rectF = QRectF(minX, minY, maxX - minX, maxY - minY); 
    
            QPointF centerPoint = rectF.center();
    
            double Ratio = rectF.width() / rectF.height();
            double hvRatio = double(canvas()->width()) / double(canvas()->height());    
    
            if(Ratio > 1)
            {
                double scaleFactor = hvRatio / Ratio > 1 ? hvRatio / Ratio : 1;
    
                setAxisScale(xBottom, centerPoint.x() - scaleFactor * rectF.width() / 2.0, 
                    centerPoint.x() + scaleFactor * rectF.width() / 2.0);
                double step = axisStepSize(xBottom);
                setAxisScale(yLeft, centerPoint.y() - scaleFactor * rectF.width() / (2.0 * hvRatio), 
                    scaleFactor * centerPoint.y() + scaleFactor * rectF.width() / (2.0 * hvRatio), step);    
            }
            else
            {
                double scaleFactor = Ratio / hvRatio > 1 ? Ratio / hvRatio : 1;
    
                setAxisScale(yLeft, centerPoint.y() - scaleFactor * rectF.height() / 2.0, 
                    centerPoint.y() + scaleFactor * rectF.height() / 2.0);
                double step = axisStepSize(yLeft);
                setAxisScale(xBottom, centerPoint.x() - scaleFactor * rectF.height() * hvRatio / 2.0, 
                    centerPoint.x() + scaleFactor * rectF.height() * hvRatio / 2.0, step);
            }
    
            replot();
        }
    }
  • 相关阅读:
    textarea组件
    switch组件
    slider组件
    radio组件
    picker-view组件
    picker组件 label组件讲解
    weui 框架
    微信小程序 input 组件
    在php中获取 数据库的内容,返回到页面
    微信小程序 form 组件
  • 原文地址:https://www.cnblogs.com/lcxu2/p/3374119.html
Copyright © 2020-2023  润新知