当前位置:   article > 正文

Qt绘制雷达图(效果图)_qt雷达图

qt雷达图
  • 效果图如下:

在这里插入图片描述

  • 原理:

自定义控件,并使用QPainter等绘制。
雷达图主要包括的元素有: 同心圆、十字架、文本标签、不同颜色、不同样式的圆圈、余晖等;可以设置目标显示的方位、距离等,经过我的测试,显示100个目标,界面不会卡顿,满足正常环境下的使用。

  • paintEvent()的主要代码:
void SkyplotWidget::paintEvent(QPaintEvent *)
{
    QSize          widgetSize(this->size());
    QPalette       p = palette();
    QPainter      *painter = new QPainter(this);
    float          topMargin ;
    float          leftMargin;
    float          size;
    float          satelliteSize;
    float          fontSize;
    float          availableWidth    = widgetSize.width ();
    float          availableHeight   = widgetSize.height();

    if( availableHeight > availableWidth )
    {
        size = widgetSize.width() * p_marginScale;
        topMargin   = ( widgetSize.width() - widgetSize.width() * p_marginScale + widgetSize.height() - widgetSize.width() ) / 2.0;
        leftMargin  = ( widgetSize.width() - widgetSize.width() * p_marginScale ) / 2.0;
    }
    else
    {
        size = widgetSize.height() * p_marginScale;
        leftMargin = ( widgetSize.height() - widgetSize.height() * p_marginScale + widgetSize.width() - widgetSize.height()) / 2.0;
        topMargin  = ( widgetSize.height() - widgetSize.height() * p_marginScale ) / 2.0;
    }

    satelliteSize = size * p_satScale;
    painter->setRenderHint(QPainter::Antialiasing, p_antialiased);
    painter->translate( leftMargin, topMargin );  //重新设置坐标原点
    fontSize = size * p_fontScale;
    painter->setFont(QFont( "Arial", static_cast< int >(fontSize)));

    //圆圈
    for(int i=0; i <= p_ellipses; i++)
    {
        float radius = size / 2.0 - i * ( size / ( 2.0  * p_ellipses ));
        painter->setPen( QPen( p_gridColor, p_gridWidth ) );
        painter->drawEllipse(QPoint( size/2.0, size/2.0 ), static_cast< int >( radius ), static_cast< int >(radius));
        if(p_withGridLabels)
        {
            painter->setPen( QPen( p_gridTextColor, p_gridWidth -2.5 ) );
            double tmp = ((p_ellipses - i) * (mMonitorScope / p_ellipses));
            QString distance = QString::number(((p_ellipses - i) * (mMonitorScope / p_ellipses)) ,10 , 2);
            painter->drawText(QPoint(size/2.0 + p_textMargin, size/2.0 - (radius + p_textMargin)), QString("%1 km").arg(distance)); //距离刻度
        }
    }

    //十字架
    for(int i=0; i < p_crosses; i++)
    {
        QLineF line1,line2;
        float angle = (static_cast<float>(i) * 90.0) / (static_cast<float>(p_crosses));
        line1.setP1( QPoint( size/2.0, size/2.0 ) );
        line1.setLength( size/2.0 );

        QRectF textRect(0, 0, 4.0 * fontSize, fontSize + 2.0);

        for(int c = 0; c < 4; c++)
        {
            line1.setAngle(angle + c*90.0 + 90.0);
            painter->setPen(QPen(p_gridColor, p_gridWidth));
            painter->drawLine(line1);
            if( p_withGridLabels )
            {
                painter->setPen(QPen( p_gridTextColor, p_gridWidth));
                line2 = QLineF(line1);
                line2.setLength(size/2.0 + 2.0 * fontSize);
                textRect.moveCenter(line2.p2());
                if(i > 0)
                    painter->drawText( textRect, Qt::AlignCenter, QString("%1").arg((int)(360 - (c*90 ) - angle) , 3 , 10 , QChar('0')));
                else
                {
                    if(c == 0)
                    {
                        painter->drawText( textRect, Qt::AlignCenter,QString("000"));
                    }
                    else if(c == 1)
                    {
                        painter->drawText( textRect, Qt::AlignCenter, QString("270"));
                    }
                    else if(c == 2)
                    {
                        painter->drawText( textRect, Qt::AlignCenter, QString("180"));
                    }
                    else if(c == 3)
                    {
                        painter->drawText( textRect, Qt::AlignCenter, QString("090"));
                    }
                }
            }
        }
    }

    QBrush innerBrush = QBrush( Qt::SolidPattern );
    QBrush outerBrush = QBrush( );
    //卫星
    foreach( auto s, satellites )
    {
        //------w---设置每一个卫星的大小
        satelliteSize = size * s.rsize;
        // skip invisible satellites
        if(     s.state & SatelliteState::Invisible       ||  // the invisible flag is set

                ( !( s.state & SatelliteState::Visible ) &         // neither visible nor half-visible
                  !( s.state & SatelliteState::HalfVisible ) ) ||

                (  s.state & SatelliteState::Flashing && flash )    // flashing flag is set and flash is active
                )
            continue;

        QRectF labelRect( 0, 0, s.label.length() * satelliteSize, satelliteSize+2 );

        double nmdX = 0,nmdY = 0;  //卫星的X轴坐标,卫星的Y轴坐标
        calculatePos(s.az , s.distance ,nmdX, nmdY);
        QPoint satPos(size/2.0 + size*(nmdX/mMonitorScope) * 0.5,size/2.0 +  size*(nmdY/mMonitorScope) * 0.5);

        // define the color's alpha value (0.3 or 1.0)
        QColor innerColor = s.innerColor;
        QColor outerColor = s.outerColor;
        QColor fontColor  = s.fontColor;
        if( s.state & SatelliteState::Visible )
        {
            innerColor.setAlphaF( 1.0 );
            outerColor.setAlphaF( 1.0 );
            fontColor.setAlphaF( 1.0 );
        }
        else
        {
            innerColor.setAlphaF( 0.3 );
            outerColor.setAlphaF( 0.3 );
            fontColor.setAlphaF( 0.3 );
        }

        // paint the inner circle
        innerBrush.setColor( innerColor );
        painter->setBrush( innerBrush );
        if( s.state & SatelliteState::Marked )
            painter->setPen( QPen( outerColor, satelliteSize/4 ) );
        else
            painter->setPen( QPen( p_gridColor, 0 ) );
        painter->drawEllipse( satPos, static_cast< int >( satelliteSize ), static_cast< int >( satelliteSize ) );


        // write the text
        painter->setPen( QPen( fontColor, 2 ) );
        painter->setFont( QFont( "Arial", static_cast< int >( satelliteSize ),  QFont::Bold ) );
        labelRect.moveCenter( satPos );
        painter->drawText( labelRect, Qt::AlignCenter, s.label );

    }

    foreach (auto l, indicatrix)
    {
        if(  l.lineState & SatelliteState::Invisible       ||  // the invisible flag is set
             ( !( l.lineState & SatelliteState::Visible ) &         // neither visible nor half-visible
               !( l.lineState & SatelliteState::HalfVisible ) ) ||
             (  l.lineState & SatelliteState::Flashing && flash )    // flashing flag is set and flash is active
             )
            continue;

        QPoint spoint ( l.point1.rx() * size / 100 + size/2, l.point1.ry() * size/100 + size/2);
        QPoint epoint ( l.point2.rx() * size / 100 + size/2, l.point2.ry() * size/100 + size/2);

        painter->setPen(QPen(l.lineColor,1.5,Qt::DashDotLine,Qt::RoundCap));
        painter->drawLine(spoint,epoint);
    }

    //余晖扫描
    //   int len = m_drawArea.width();
    qreal x = size/2.0 + (qreal)size/2 * cos(-m_pieRotate*3.14159/180);
    qreal y = size/2.0 + (qreal)size/2 * sin(-m_pieRotate*3.14159/180);
    painter->setPen(QPen(Qt::green));
    painter->drawLine(QPoint( size/2.0, size/2.0 ),QPointF(x,y));

    //扇形
    QConicalGradient gradient;
    gradient.setCenter(QPoint( size/2.0, size/2.0 ));
    gradient.setAngle(m_pieRotate + 180); //渐变与旋转方向恰好相反,以扇形相反的边作为渐变角度。
    gradient.setColorAt(0.4,QColor(169,253,51,100)); //从渐变角度开始0.5 - 0.75为扇形区域,由于Int类型计算不精确,将范围扩大到0.4-0.8
    gradient.setColorAt(0.8,QColor(169,253,51,0));
    painter->setBrush(QBrush(gradient));
    painter->setPen(Qt::NoPen);
    painter->drawPie(0,0,size,size,m_pieRotate*16,90*16);
    //--------------------------------------------------
    delete painter;
}在这里插入代码片
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 下载地址:

代码下载地址稍后贴在下方评论区。

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读