基于QWidget绘制蛛网图

本文介绍如何在Qt环境中利用QWidget绘制蛛网图(雷达图),基于已有的资源进行修改,实现了所需效果。附带实现效果图及源码,调用方式简单。
摘要由CSDN通过智能技术生成

最近因项目需要绘制蛛网图,雷达图,于是就参考了https://blog.csdn.net/chyuanrufeng/article/details/76222533的文章,在此基础上做以修改,达到了自己想要的效果。在此感谢这位博主的分享,话不多说上代码。

实现的效果图:



源码如下 

RadarChart.h

#ifndef RADARCHART_H
#define RADARCHART_H
 
#include <QWidget>
#include <QPainter>
#include <QPainterPath>
#include "qmath.h"
 
enum RadarType
{
 
    Radar,  //雷达图
    Spider    //蛛网图
};
 
enum PointTpye
{
 
    ScatterPoints, //散点图
    LinePoints       //连线图
};
 
struct RadarData
{
 
    RadarData(const QString& _name,const qreal &_value){
  name = _name;value=_value;}
    QString name;
    qreal value;
};
 
#define  PI M_PI
 
class RadarChart : public QWidget
{
 
    Q_OBJECT
public:
    explicit RadarChart(QWidget *parent = 0);
    void setRadarType(RadarType radartype);
    void setPointTpye(PointTpye pointstype);
    void setRadialAxisLabelVisible(bool visible){
  m_showRadialAxisLabel = visible;this->update();}
    void setAngularAxisLabelVisible(bool visible){
  m_showAngularAxisLabel = visible;this->update();}
    void setTitle(const QString &title){
  m_title = title;this->update(); }
    void setTitleFont(const QFont &titleFont){
  m_titleFont = titleFont; this->update();}
    void setTitleColor(const QColor &titleColor){
  m_titleColor = titleColor; this->update();}
    void setDatas(const QList<RadarData> &datas);
    void setTickCount(int tickCount){
  m_tickCount = tickCount; this->update();}
    void setAxisRange(qreal min,qreal max);
    void setAngularLineColor(const QColor &color){
  m_AngularLineColor = color;this->update();}
    void setRadialLineColor(const QColor &color){
  m_RadialLineColor = color;this->update();}
    void setAngularLabelColor(const QColor &color){
  m_AngularLabelColor = color;this->update();}
    void setRadialLabelColor(const QColor &color){
  m_RadialLabelColor = color;this->update();}
    void setSeriesColor(const QColor &color){
  m_seriesColor = color;this->update();
    void setbackgroundColor(const QColor &color){
  m_backgroundColor = color;this->update();}
    void setLabelFont(const QFont &labelFont){
  m_labelFont = labelFont; this->update();}
protected:
    void paintEvent(QPaintEvent *event);
 
private:
    void init();
    void drawTitle(QPainter *painter);
    void drawRadarType(QPainter *painter);
    void drawRadar(QPainter *painter);
    void drawSpider(QPainter *painter);
    void drawRadialLabel(QPainter *painter);
    void drawAngularLabel(QPainter *painter);
    void drawData(QPainter *painter);
    void drawScatterPoints(QPainter *painter);
    void drawLinePoints(QPainter *painter);
 
private:
    RadarType m_radartype;
    PointTpye m_pointstype;
    bool m_showRadialAxisLabel;
    bool m_showAngularAxisLabel;
 
    QString m_title;
    QFont m_titleFont
  • 1
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值