赞
踩
利用 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
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();
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。