当前位置:   article > 正文

Qt 实现雷达图_qt 雷达航迹图

qt 雷达航迹图

利用 QPainter实现雷达图(玫瑰图)。效果如下:
这里写图片描述这里写图片描述这里写图片描述

源码如下
.h文件

#ifndef CRADARDLG_H
#define CRADARDLG_H

#include <QWidget>
#include <QPainter>
#include <QPainterPath>
#include <QMap>
#include "qmath.h"

enum RadarType
{
    Radar=0,  //雷达图
    Spider    //蜘蛛网图
};

enum PointTpye
{
    ScatterPoints=0, //散点图
    LinePoints       //连线图
};

typedef struct RadarData
{
    QColor color;
    QMap<qreal,qreal> datamap;           //key : 角度 value:值 
    QMap<qreal,qreal> transitiondatmap;  //key : 角度 value:转换的值 


}RadarData;

#define  PI 3.14159


class CRadarDlg : public QWidget
{
    Q_OBJECT

public:
    CRadarDlg(QWidget *parent = 0);
    ~CRadarDlg();

    void setRadarType(RadarType radartype);
    void setPointTpye(PointTpye pointstype);
protected:
    void paintEvent(QPaintEvent *e);

private:
    void init();
    void drawRadarType(QPainter *painter);
    void drawRadar(QPainter *painter);
    void drawSpider(QPainter *painter);
    void drawAxisLabel(QPainter *painter);
    void drawLabel(QPainter *painter);
    void drawData(QPainter *painter);
    void drawScatterPoints(QPainter *painter);
    void drawLinePoints(QPainter *painter);


private:
    RadarType m_radartype;
    PointTpye m_pointstype;
    int m_cylindernum;  //圈数
    int m_lineangle;    //角度,决定多少。默认是30
    int m_Axis_max,m_Axis_min; //最大最小
    QStringList m_AxisLabelList; //标签 刻度标签
    QStringList m_labelList;     //外围标签
    bool m_showAxisLabel;
    bool m_showLabel;   

    QMap<int,RadarData> m_datamap;  //key 表示组,value表示对于的值
    QList<QColor> m_colorlist;



};

#endif // CRADARDLG_H
  • 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

cpp文件

#include "cradardlg.h"

CRadarDlg::CRadarDlg(QWidget *parent)
    : QWidget(parent)
{
    init();
}

CRadarDlg::~CRadarDlg()
{

}

void CRadarDlg::setRadarType( RadarType radartype )
{
    m_radartype = radartype;
}

void CRadarDlg::setPointTpye( PointTpye pointstype )
{
    m_pointstype = pointstype;
}


void CRadarDlg::init()
{
    setRadarType(Radar);
    setPointTpye(LinePoints);
    m_cylindernum = 5;  //默认6圈
    m_lineangle = 30; //默认30度
    m_showAxisLabel = true;
    m_Axis_min = 0;
    m_Axis_max = 100;
    m_AxisLabelList << "0" << "20" << "40" << "60" << "80" << "100";

    m_colorlist << QColor(136,68,255);
    m_colorlist << QColor(102,170,238);
    m_colorlist << QColor(146,39,143);
    m_colorlist << QColor(238,187,34);
    m_colorlist << QColor(238,187,34);
    m_colorlist << QColor(238,187,34);
    m_colorlist << QColor(141,198,63);
    m_colorlist << QColor(255,242,0);

    for (int i = 0 ; i < 360; i+=m_lineangle)
    {
        m_labelList << QString("%1").arg(i);
    }
    RadarData tlist1,tlist2;
    QMap<qreal,qreal> datamap,datamap2;
    for (int i = 0; i < 10;++i)
    {
        datamap2 .insert (i*20,i*10);
        datamap .insert(i*30,i*5);
    }
    tlist2.color = QColor(136,68,255);;
    tlist1.color = QColor(146,39,143);;

    tlist1.transitiondatmap = datamap;
    tlist2.transitiondatmap = datamap2;

    m_datamap.insert(0,tlist2);
    m_datamap.insert(1,tlist1);

}


void CRadarDlg::paintEvent( QPaintEvent *e )
{
    QPainter *painter = new QPainter(this);
    painter->setRenderHint(QPainter::Antialiasing);
    painter->setRenderHint(QPainter::SmoothPixmapTransform);
    painter->setRenderHint(QPainter::TextAntialiasing);

    //添加背景颜色
//  painter->save();
//  painter->setBrush(Qt::blue);
//  painter->drawRect(0,0,width(),height());
//  painter->restore();
//////////////////////////////////////////////////////////////////////////

    drawRadarType(painter);
    drawAxisLabel(painter);
    drawLabel(painter);
    drawData(painter);

}


void CRadarDlg::drawRadarType( QPainter *painter )
{   
    switch (m_radartype)
    {
        case Radar:drawRadar(painter);break;
        case Spider:drawSpider(painter);break;
        default:drawRadar(painter);break;
    }
}


void CRadarDlg::drawRadar( QPainter *painter )
{
    painter->save();
    QPen pen;
    QColor color(Qt::lightGray);
    pen.setWidthF(0.4);
    //pen.setColor(color/*.lighter(100)*/); //添加线的颜色
    painter->setPen(pen);

    QPainterPath looppath;
    int w = width();
    int h = height();

    int count = m_cylindernum; 
    int radius = qMin(w,h);
    int step = radius/(count+1);  //加1是为了四周留出空间,写标签
    int x = w*0.2;
    int y = h*0.2;

    QRectF outrect;
    for (int i = 1; i < count+1; ++i)
    {
        radius = step*i;
        x = w/2-radius/2;
        y = h/2-radius/2;
        QRectF rect(x,y,radius,radius);
        looppath.addEllipse(rect);
        outrect = rect;
    }
    painter->drawPath(looppath);
//////////////////////////////////////////////////////////////////////////

    if (m_lineangle == 0)
    {
        m_lineangle =90;
    }
    int linecount = 360/m_lineangle; 
    int x0 = w/2 ;
    int y0 = h/2;
    int newradius = outrect.height()/2;
    for (int i = 0 ; i < linecount; ++i)
    {
        int x1 = x0 + newradius*qSin(PI*2/linecount*i);
        int y1 = y0 + newradius*qCos(PI*2/linecount*i);

        painter->drawLine(x0,y0,x1,y1);
    }

    painter->restore();

}

void CRadarDlg::drawSpider( QPainter *painter )
{
    painter->save();
    QPen pen;
    QColor color(Qt::lightGray);
    pen.setWidthF(0.4);
    //pen.setColor(color/*.lighter(100)*/); //添加线的颜色
    painter->setPen(pen);

    QPainterPath looppath;
    int w = width();
    int h = height();
    if (m_lineangle == 0)
    {
        m_lineangle =90;
    }

    int  laycount = m_cylindernum;  //默认是几层 

    int count = 360/m_lineangle; 
    int radius = qMin(w,h)/2;
    int x0 = w/2 ;
    int y0 = h/2;

    for (int j = 1 ; j < laycount+1; ++j)
    {
        int newradius = radius*j/(laycount+1);
        for (int i = 0 ; i < count; ++i)
        {
            int x1 = x0 + newradius*qSin(PI*2/count*i);
            int y1 = y0 + newradius*qCos(PI*2/count*i);
            painter->drawLine(x0,y0,x1,y1);

            int x2;
            int y2; 
            if (i == count*2-1)
            {
                x2 = x0 + newradius*qSin(0);
                y2 = y0 + newradius*qCos(0);
            }
            else
            {
                x2 = x0 + newradius*qSin(PI*2/count*(i+1));
                y2 = y0 + newradius*qCos(PI*2/count*(i+1));
            }
            painter->drawLine(x1,y1,x2,y2);
        }
    }
    painter->drawPath(looppath);

    painter->restore();

}

void CRadarDlg::drawAxisLabel( QPainter *painter )
{
    if (!m_showAxisLabel) return;

    painter->save();

    //QPen pen;
    //QColor color(Qt::lightGray);
    //pen.setColor(color/*.lighter(100)*/); //添加线的颜色
    //painter->setPen(pen);


    int  laycount = m_cylindernum;  //默认是几层 
    int w = width();
    int h = height();
    int count = 360/m_lineangle; 
    int radius = qMin(w,h)/2;
    int x0 = w/2 ;
    int y0 = h/2;

    QFont font;
    int fontsize = 10;
    if (h > 500)
        fontsize = 15;
    font.setPointSize(fontsize);
    painter->setFont(font);

    for (int j = 0 ; j < laycount+1; ++j)
    {
        int newradius = radius*j/(laycount+1);
        int x1 = x0 + newradius*qSin(PI);
        int y1 = y0 + newradius*qCos(PI)+20;
        if (j < m_AxisLabelList.count())
        {
            painter->drawText(x1,y1,m_AxisLabelList.at(j));
        }
    }

    painter->restore();
}

void CRadarDlg::drawLabel( QPainter *painter )
{
    if (!m_showLabel) return;

    painter->save();

    //QPen pen;
    //QColor color(Qt::lightGray);
    //pen.setColor(color/*.lighter(100)*/); //添加线的颜色
    //painter->setPen(pen);


    int  laycount = m_cylindernum;  //默认是几层 
    int w = width();
    int h = height();
    int count = 360/m_lineangle; 
    int radius = qMin(w,h)/2;
    int x0 = w/2 ;
    int y0 = h/2;

    QFont font;
    int fontsize = 10;
    if (h > 500)
        fontsize = 15;
    font.setPointSize(fontsize);
    painter->setFont(font);


    for (int i = 0 ; i < count; ++i)
    {
        int newradius = radius*laycount/(laycount+1)+10;
        newradius = newradius+10*qSin(PI*2/count*i);
        int x1 = x0 - newradius*qSin(PI*2/count*i);
        int y1 = y0 - newradius*qCos(PI*2/count*i);

        if (y0 < y1 && x0 < x1)
        {
            y1 -= fontsize*qSin(PI*2/count*i);
        }
        else
        {
            if (x1 < x0)
                x1-=fontsize;
            y1 += fontsize*qSin(PI*2/count*i);
        }

        if (i < m_labelList.count())
        {
            painter->drawText(x1,y1,m_labelList.at(i));
        }

    }

    painter->restore();
}

void CRadarDlg::drawData( QPainter *painter )
{
    switch(m_pointstype)
    {
    case ScatterPoints:drawScatterPoints(painter);break;
    case LinePoints:drawLinePoints(painter);break;
    default:drawLinePoints(painter);break;
    }

}

void CRadarDlg::drawScatterPoints( QPainter *painter )
{
    painter->save();

    int w = width();
    int h = height();
    int count = 360/m_lineangle; 
    int radius = qMin(w,h)/2*m_cylindernum/(m_cylindernum+1);

    QList<int> keyilist = m_datamap.keys();

    qreal step = 1.0*radius/(m_Axis_max-m_Axis_min);
    foreach (qreal key, keyilist)
    {
        RadarData rdata = m_datamap.value(key);
        QColor color = rdata.color;
        painter->setPen(color.darker(200));
        painter->setBrush(color.darker(200));
        foreach (qreal angle ,rdata.transitiondatmap.keys())
        {
            qreal length = step*rdata.transitiondatmap.value(angle);
            angle+=90; //因为转90
            QPointF certp(w/2 + length*qCos(angle*PI/180),h/2 - length*qSin(angle*PI/180));
            painter->drawEllipse(certp,2,2);

        }
    }

    painter->restore();
}

void CRadarDlg::drawLinePoints( QPainter *painter )
{
    painter->save();

    int w = width();
    int h = height();
    int count = 360/m_lineangle; 
    int radius = qMin(w,h)/2*m_cylindernum/(m_cylindernum+1);

    QList<int> keyilist = m_datamap.keys();

    qreal step = 1.0*radius/(m_Axis_max-m_Axis_min);

    foreach (qreal key, keyilist)
    {
        RadarData rdata = m_datamap.value(key);
        QPainterPath path;
        int  i = 0;
        QPointF startp;

        QColor color = rdata.color;
        painter->setPen(color.darker(200));
        painter->setBrush(color.darker(200));
        foreach (qreal angle ,rdata.transitiondatmap.keys())
        {
            qreal length = step*rdata.transitiondatmap.value(angle);
            angle+=90; //因为转90
            QPointF certp(w/2 + length*qCos(angle*PI/180),h/2 - length*qSin(angle*PI/180));
            painter->drawEllipse(certp,2,2);
            if (i == 0)
            {
                path.moveTo(certp);
                startp = certp;
            }
            else
                path.lineTo(certp);
            ++i;
        }
        path.lineTo(startp);
        painter->setBrush(color.lighter(100));
        painter->drawPath(path);
    }


    painter->restore();
}
  • 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
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/170574
推荐阅读
相关标签
  

闽ICP备14008679号