my-QT专栏

QChrat系列图

2021-09-05  本文已影响0人  c之气三段

折线图

.h

#ifndef CHARTWIDGET_H
#define CHARTWIDGET_H

#include <QWidget>
#include <QtCharts>
#include <QChartView>
#include <QSplineSeries>
namespace Ui {
class LinesWidget;
}
struct Line
{
  QString name;
  QList<QPointF> listPoints;
};
class LinesWidget : public QWidget
{
    Q_OBJECT

public:
    explicit LinesWidget(QWidget *parent = 0);
    ~LinesWidget();
    void addLine(const QList<QPointF> &listPoints,const QString &lineName);
    void setTitle(const QString &title);
    void draw();
    void clearLines();
    void test();
private:
    Ui::LinesWidget *ui;
    QChart *m_pChart;    //创建图表对象
    QList<Line>m_listLines;
    QString m_title=QStringLiteral("我的图例");
    QList<QLineSeries*>m_lineSeriess;
    QChartView *m_pChartView;
};

#endif // CHARTWIDGET_H

.cpp

#include "LinesWidget.h"
#include "ui_LinesWidget.h"

LinesWidget::LinesWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::LinesWidget)
{
    ui->setupUi(this);
    m_pChartView = new QChartView();
    m_pChart = new QChart();                          //创建图标对象
    m_pChart->setTheme(QChart::ChartThemeDark);       //设置图标主题
    m_pChartView->setChart(m_pChart);                    //向chartView中添加图表
    m_pChartView->setRenderHints(QPainter::Antialiasing);//消除边缘,看起来平滑一些
    ui->horizontalLayout->addWidget(m_pChartView);
    m_pChart->setBackgroundBrush(QColor(230,230,230));//修改背景色
}

LinesWidget::~LinesWidget()
{
    qDeleteAll(m_lineSeriess);
    m_lineSeriess.clear();
    delete m_pChart;
    delete m_pChartView;
    delete ui;
}

void LinesWidget::addLine(const QList<QPointF> &listPoints, const QString &lineName)
{
    Line line;
    line.name=lineName;
    line.listPoints=listPoints;
    m_listLines.append(line);
}

void LinesWidget::setTitle(const QString &title)
{
    m_title=title;
}

void LinesWidget::draw()
{
    m_pChart->setTitle(m_title);     //设置图标名称
    QLineSeries *pLineSeries =nullptr;        //创建折线系列
    for(int i=0;i<m_listLines.size();i++)
    {
        pLineSeries=new QLineSeries;
        m_lineSeriess.append(pLineSeries);
        for(int j=0;j<m_listLines.at(i).listPoints.size();j++)
        {
            pLineSeries->append(m_listLines.at(i).listPoints.at(j));
        }
        pLineSeries->setPointLabelsColor(QColor(70,70,70));
        pLineSeries->setName(m_listLines.at(i).name);
        m_pChart->addSeries(pLineSeries);
        //移动到线上输出坐标
        connect(pLineSeries,&QLineSeries::hovered,[=](QPointF point,bool state){
            if(state)
            {
               QString strXY;
               strXY=QString("%1").arg(point.x());
               strXY+=",";
               strXY+=QString("%1").arg(point.y());
               QToolTip::showText(QCursor::pos(),strXY);
            }
        });
    }
    m_pChart->createDefaultAxes();//创建默认的坐标,必须在addSeries之后调用
    m_pChart->setTitleBrush(QColor(70,70,70));  //设置标题颜色
    m_pChart->legend()->setLabelColor(QColor(70,70,70));//设置图例字体颜色
    m_pChart->axisX()->setLabelsColor(QColor(70,70,70));//设置x轴的标签颜色,就是显示的文字颜色
    m_pChart->axisY()->setLabelsColor(QColor(70,70,70));
}

void LinesWidget::clearLines()
{
    m_listLines.clear();
    qDeleteAll(m_lineSeriess);
    m_lineSeriess.clear();
}

void LinesWidget::test()
{
    setTitle("examp");
    QList<QPointF> listPoints;
    for(quint8 i=10; i<20; i++)
    {
       listPoints.append(QPointF(i,qrand()%300));
    }
    addLine(listPoints,"numberone");
    draw();
}

柱状图

.h

#ifndef HISTGRAMWIDGET_H
#define HISTGRAMWIDGET_H

#include <QWidget>
#include <QChartView>
#include <QtCharts>
struct Pillars 
{
    QString name;
    QList<float>data;
};
namespace Ui {
class HistgramWidget;
}
class HistgramWidget : public QWidget
{
    Q_OBJECT

public:
    explicit HistgramWidget(QWidget *parent = 0);
    ~HistgramWidget();
     void addPillars(const QList<float> &pillarsData,const QString &name);
     void setTitle(const QString &title);
     void draw();
     void setAxisText(const QStringList &listAxisText);
     void clearPillars();
     void test();
private:
    Ui::HistgramWidget *ui;
    QChartView *m_pChartView;
    QChart *m_pChart;
    QBarSeries *m_pBarSeries = nullptr;
    QList<QBarSet*> m_barSets;
    QList<Pillars>m_kindsPillars;
    QString m_title = "Histgram";
    QBarCategoryAxis *m_pAxis = nullptr;
};

#endif // HISTGRAMWIDGET_H

.cpp

#include "HistgramWidget.h"
#include "ui_HistgramWidget.h"

HistgramWidget::HistgramWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::HistgramWidget)
{
    ui->setupUi(this);
    m_pChartView = new QChartView;
    m_pChart = new QChart;
    m_pChart->setTheme(QChart::ChartThemeDark);
    m_pChartView->setChart(m_pChart);
    m_pChartView->setRenderHints(QPainter::Antialiasing);
    ui->verticalLayout->addWidget(m_pChartView);
    m_pChart->setBackgroundBrush(QColor(230,230,230));
    m_pAxis = new QBarCategoryAxis;//横坐标显示
    //m_pChart->legend()->setAlignment(Qt::AlignBottom);//设置图例的显示位置在底部
}

HistgramWidget::~HistgramWidget()
{
    if(m_barSets.size()!=0)
    {
        qDeleteAll(m_barSets);
        m_barSets.clear();
        delete m_pBarSeries;
    }
    delete m_pAxis;
    delete ui;
}

void HistgramWidget::addPillars(const QList<float> &pillarsData, const QString &name)
{
    Pillars pillars;
    pillars.data = pillarsData;
    pillars.name = name;
    m_kindsPillars.append(pillars);
}

void HistgramWidget::setTitle(const QString &title)
{
    m_title = title;
}

void HistgramWidget::draw()
{
    m_pChart->setTitle(m_title);
    m_pBarSeries = new QBarSeries;
    QBarSet *pBarSet = nullptr;
    for(int i=0;i<m_kindsPillars.size();i++)
    {
        pBarSet=new QBarSet(m_kindsPillars.at(i).name);
        for(int j=0;j<m_kindsPillars.at(i).data.size();j++)
        {
            *pBarSet<<m_kindsPillars.at(i).data.at(j);
        }
        m_barSets.append(pBarSet);
        m_pBarSeries->append(pBarSet);
    }
    connect(m_pBarSeries,&QBarSeries::hovered,[=](bool state,int index,QBarSet *pCurrBarSet){
        if(state)
        {
            QString text;
            text=pCurrBarSet->label();
            text+=":";
            text+=QString("%1").arg(pCurrBarSet->at(index));
            text+=",";
            text+= m_pAxis->at(index);
            QToolTip::showText(QCursor::pos(),text);
        }
    });
    m_pChart->addSeries(m_pBarSeries);
    m_pChart->createDefaultAxes();
    m_pChart->setAxisX(m_pAxis);
    m_pChart->setTitleBrush(QColor(70,70,70));  //设置标题颜色
    m_pChart->legend()->setLabelColor(QColor(70,70,70));//设置图例字体颜色
    m_pChart->axisX()->setLabelsColor(QColor(70,70,70));//设置x轴的标签颜色,就是显示的文字颜色
    m_pChart->axisY()->setLabelsColor(QColor(70,70,70));
}

void HistgramWidget::setAxisText(const QStringList &listAxisText)
{
    m_pAxis->clear();
    m_pAxis->append(listAxisText);
}

void HistgramWidget::clearPillars()
{
    if(m_barSets.size()==0)return;
    m_kindsPillars.clear();
    qDeleteAll(m_barSets);
    m_barSets.clear();
    delete m_pBarSeries;
    m_pBarSeries=nullptr;
}

void HistgramWidget::test()
{
    QList<float> data;
    for(int i=0;i<5;i++)
    {
        data<<i*2+1;
    }
    addPillars(data,"one");
    QList<float> data1;
    for(int i=0;i<5;i++)
    {
        data1<<i+1;
    }
    addPillars(data1,"tow");
    QStringList list;
    list<<"a";
    list<<"b";
    list<<"c";
    list<<"d";
    list<<"e";
    setAxisText(list);
    draw();
}

面积图

.h

#ifndef AREAWIDGET_H
#define AREAWIDGET_H

#include <QWidget>
#include <QtCharts>
#include <QChartView>
struct AreaLine
{
  QString name;
  QList<QPointF> listPoints;
};
namespace Ui {
class AreaWidget;
}
class AreaWidget : public QWidget
{
    Q_OBJECT

public:
    explicit AreaWidget(QWidget *parent = 0);
    ~AreaWidget();
    void addLine(const QList<QPointF> &listPoints,const QString &lineName);
    void setTitle(const QString &title);
    void draw();
    void clearArea();
    void test();
private:
    Ui::AreaWidget *ui;
    QChart *m_pChart;
    QChartView *m_pChartView;
    QList<AreaLine>m_listLines;
    QString m_title="areaChart";
    QList<QLineSeries*>m_listLineSeriess;
    QList<QAreaSeries*>m_listAreaSeriess;
};

#endif // AREAWIDGET_H

.cpp

#include "AreaWidget.h"
#include "ui_AreaWidget.h"

AreaWidget::AreaWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::AreaWidget)
{
    ui->setupUi(this);
    m_pChartView = new QChartView;
    m_pChart = new QChart;
    m_pChart->setTheme(QChart::ChartThemeDark);
    m_pChartView->setChart(m_pChart);
    m_pChartView->setRenderHints(QPainter::Antialiasing);
    m_pChart->setBackgroundBrush(QColor(230,230,230));
    m_pChartView->setChart(m_pChart);
    ui->verticalLayout->addWidget(m_pChartView);
}

AreaWidget::~AreaWidget()
{
    qDeleteAll(m_listLineSeriess);
    m_listLineSeriess.clear();
    qDeleteAll(m_listAreaSeriess);
    m_listAreaSeriess.clear();
    delete m_pChartView;
    delete ui;
}

void AreaWidget::addLine(const QList<QPointF> &listPoints,const QString &lineName)
{
    AreaLine line;
    line.name=lineName;
    line.listPoints=listPoints;
    m_listLines.append(line);
}

void AreaWidget::setTitle(const QString &title)
{
    m_title = title;
}

void AreaWidget::draw()
{
    m_pChart->setTitle(m_title);
    QLineSeries *pLineSeries=nullptr;
    QAreaSeries *pAreaSeries=nullptr;
    QLineSeries *pTempLineSeries=nullptr;
    for(int i=0;i<m_listLines.size();i++)
    {
        pLineSeries=new QLineSeries;
        //pLineSeries->setName(m_listLines.at(i).name);
        for(int j=0;j<m_listLines.at(i).listPoints.size();j++)
        {
            pLineSeries->append(m_listLines.at(i).listPoints.at(j));
        }
        m_listLineSeriess.append(pLineSeries);
        pAreaSeries=new QAreaSeries;
        pAreaSeries->setName(m_listLines.at(i).name);
        pAreaSeries->setUpperSeries(pLineSeries);
        pAreaSeries->setLowerSeries(pTempLineSeries);
        pTempLineSeries=pLineSeries;
        m_listAreaSeriess.append(pAreaSeries);
        m_pChart->addSeries(pAreaSeries);
        connect(pAreaSeries,&QAreaSeries::hovered,[=](const QPointF &pointf,bool state){
            if(state)
            {
               QString strXY;
               strXY=QString("%1").arg(pointf.x());
               strXY+=",";
               strXY+=QString("%1").arg(pointf.y());
               QToolTip::showText(QCursor::pos(),strXY);
            }
        });
    }
    m_pChart->createDefaultAxes();
    m_pChart->setTitleBrush(QColor(70,70,70)); 
    m_pChart->legend()->setLabelColor(QColor(70,70,70));
    m_pChart->axisX()->setLabelsColor(QColor(70,70,70));
    m_pChart->axisY()->setLabelsColor(QColor(70,70,70));
}

void AreaWidget::clearArea()
{
   m_listLines.clear();
   qDeleteAll(m_listLineSeriess);
   m_listLineSeriess.clear();
   qDeleteAll(m_listAreaSeriess);
   m_listAreaSeriess.clear();
}

void AreaWidget::test()
{
    QList<QPointF> listPoints;
    for(quint8 i=10; i<20; i++)
    {
       listPoints.append(QPointF(i,qrand()%300));
    }
    addLine(listPoints,"numberone");
    QList<QPointF> listPoints1;
    for(quint8 i=15; i<20; i++)
    {
       listPoints1.append(QPointF(i,qrand()%300));
    }
    addLine(listPoints1,"numbertow");
    draw();
}

自定义极坐标

item.h

#ifndef SHAPEITEM_H
#define SHAPEITEM_H

#include <QGraphicsItem>
#include <QPen>

class ArcShapeItem : public QGraphicsItem
{
public:
    explicit ArcShapeItem(QGraphicsItem  *parent = nullptr);
    ~ArcShapeItem();
    void setShapeItem(int x,int y,int width,int angle);
    void setPen(const QPen &pen);
public:
    //绘图区域
    virtual QRectF boundingRect() const override;
    //绘图函数
    virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
private:
    int m_x,m_y,m_width;
    int m_angle;
    QPen m_pen;
};

#endif // SHAPEITEM_H

item.cpp

#include "ArcShapeItem.h"

#include <QPainter>
#include<QDebug>
ArcShapeItem::ArcShapeItem(QGraphicsItem  *parent) : QGraphicsItem(parent)
{

}

ArcShapeItem::~ArcShapeItem()
{

}

void ArcShapeItem::setShapeItem(int x, int y, int width,int angle)
{
    m_x=x;
    m_y=y;
    m_width=width;
    m_angle=angle;
}

void ArcShapeItem::setPen(const QPen& pen)
{
    m_pen=pen;
}

QRectF ArcShapeItem::boundingRect() const
{
    return QRectF(m_x, m_y,m_width, m_width);
}

void ArcShapeItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(option)
    Q_UNUSED(widget)
    // 矩形
    QRectF rect(m_x, m_y, m_width, m_width);
    // 起始角度
    int startAngle = 0;
    // 跨越度数
    double spanAngle = m_angle * 16;

    // 反走样
    painter->setRenderHint(QPainter::Antialiasing, true);

    // 设置画笔颜色、宽度
    painter->setPen(m_pen);

    // 绘制弧线
    painter->drawPie(rect, startAngle, spanAngle);
}

图表.h

#ifndef POLARWIDGET_H
#define POLARWIDGET_H

#include <QWidget>
#include <QGraphicsView>
#include "ArcShapeItem.h"
class LineItem:public QGraphicsLineItem
{
public:
    LineItem();

    // QGraphicsItem interface
    void setRatio(float ratio);

protected:
    virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event) override;
private:
    float m_ratio;
};
namespace Ui {
class PolarWidget;
}
struct Data
{
   QString legend;
   QList<QPointF> points;
};
class PolarWidget : public QWidget
{
    Q_OBJECT

public:
    explicit PolarWidget(QWidget *parent = 0);
    ~PolarWidget();
    void addLine(const QString &legend,const QList<QPointF> &points);
    void setTitle(const QString &title);
    void draw();
    void clearLines();
    void test();
private:
    void setAcrossAngle(float angle);
    void axisX();
    void setAxisXTickCount(int count);
    void setXRange(float start,float end);
    void axisY();
    void setAxisYTickCount(int count);
    void createColor();
    void rePaintAll();
private:
    Ui::PolarWidget *ui;
    QGraphicsView *m_pGraphicView;
    QGraphicsScene *m_pScene;
    QPen m_pen;
    QList<ArcShapeItem*>m_listShaps;
    int m_halfWidth,m_halfHeight;
    int m_axisXTickCount,m_axisYTickCount;
    float m_angle;
    float m_xStart,m_xEnd;
    float m_r;
    QList<Data>m_listLines;
    QString m_title="polarPicture";
    QList<LineItem*>m_lines;
    QList<QColor>m_listColor;
    QList<QGraphicsItem*> m_items;
    QGraphicsItemGroup *m_pTtemGroup=nullptr;
    // QWidget interface
protected:
    virtual void resizeEvent(QResizeEvent *event) override;
};
#endif // POLARWIDGET_H

.cpp

#include "PolarWidget.h"
#include "ui_PolarWidget.h"
#include<QDebug>
#include <QGraphicsSceneHoverEvent>
#include <QResizeEvent>
#include<QToolTip>
PolarWidget::PolarWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PolarWidget)
{
    ui->setupUi(this);
    //构建画布
    m_pGraphicView=new QGraphicsView;
    m_pGraphicView->setRenderHint(QPainter::Antialiasing, true);
    m_pGraphicView->setStyleSheet("background-color:rgb(230, 230, 230)");
    m_pGraphicView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    m_pGraphicView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    ui->verticalLayout->addWidget(m_pGraphicView);
    //构建场景,QGraphicsView中心点为(0,0)
    m_pScene = new QGraphicsScene();
    m_pGraphicView->setScene(m_pScene);
    m_halfWidth=m_pGraphicView->width()/2;
    m_halfHeight=m_pGraphicView->height()/2;
    m_pScene->setSceneRect(-m_halfWidth, -m_halfHeight, width(),height());
    m_pGraphicView->setScene(m_pScene);
    //画笔
    m_pen.setWidth(1);
    m_pen.setColor(QColor(30, 30, 30));
    createColor();
    axisX();
    axisY();
}

PolarWidget::~PolarWidget()
{
    qDeleteAll(m_listShaps);
    m_listShaps.clear();
    qDeleteAll(m_lines);
    m_lines.clear();
    qDeleteAll(m_items);
    m_items.clear();
    if(m_pTtemGroup!=nullptr)
    {
       delete m_pTtemGroup;
    }
    delete m_pScene;
    delete m_pGraphicView;
    delete ui;
}

void PolarWidget::addLine(const QString &legend, const QList<QPointF> &points)
{
    Data data;
    data.legend=legend;
    data.points=points;
    m_listLines.append(data);
}

void PolarWidget::setTitle(const QString &title)
{
    m_title = title;
}

void PolarWidget::draw()
{
    m_pTtemGroup = new QGraphicsItemGroup;
    QGraphicsSimpleTextItem *pTextItem=m_pScene->addSimpleText(m_title,QFont(QStringLiteral("楷体"),10));
    pTextItem->setPen(m_pen);
    int width=pTextItem->boundingRect().width();
    int height=pTextItem->boundingRect().height();
    pTextItem->setPos(-width/2,-m_r-50-height/2);
    LineItem* pLine=nullptr;
    QPen pen;
    pen.setWidth(2);
    if(m_listLines.size()>16)
    {
        return;
    }
    int posLoca=0;
    for(int i=0;i<m_listLines.size();i++)
    {
        float x1=0.0f,y1=0.0f;
        float detax=m_xEnd-m_xStart;
        float ratio=m_r/detax;
        pen.setColor(m_listColor.at(i));
        for(int j=0;j<m_listLines.at(i).points.size();j++)
        {
            float x2=(float)abs(m_listLines.at(i).points.at(j).x())*ratio*cosf(-m_listLines.at(i).points.at(j).y()*3.1415926/180);
            float y2=(float)abs(m_listLines.at(i).points.at(j).x())*ratio*sinf(-m_listLines.at(i).points.at(j).y()*3.1415926/180);
            pLine=new LineItem;
            pLine->setLine(QLineF(x1,y1,x2,y2));
            pLine->setRatio(ratio);
            m_pScene->addItem(pLine);
            pLine->setPen(pen);
            m_lines.append(pLine);
            x1=x2;
            y1=y2;
        }
        QGraphicsRectItem *pRectItem = new QGraphicsRectItem;
        pRectItem->setRect(posLoca,-4,10,10);
        pRectItem->setBrush(QBrush(m_listColor.at(i)));
        pRectItem->setPen(pen);

        QGraphicsSimpleTextItem *pTextItem=new QGraphicsSimpleTextItem;
        pTextItem->setText(m_listLines.at(i).legend);
        pTextItem->setFont(QFont(QStringLiteral("楷体"),10));
        pTextItem->setPen(m_pen);
        int widthText=pTextItem->boundingRect().width();
        int heightText=pTextItem->boundingRect().height();
        pTextItem->setPos(posLoca+10+2,-heightText/2);

        m_items.append(pRectItem);
        m_items.append(pTextItem);
        m_pTtemGroup->addToGroup(pRectItem);
        m_pTtemGroup->addToGroup(pTextItem);
        posLoca=posLoca+widthText+10+2+10;
    }
    int widthGrop=m_pTtemGroup->boundingRect().width();
    int heightGrop=m_pTtemGroup->boundingRect().height();
    m_pTtemGroup->setPos(-widthGrop/2,m_r+50-heightGrop/2);
    m_pScene->addItem(m_pTtemGroup);
}

void PolarWidget::clearLines()
{
    m_listLines.clear();
    rePaintAll();
}

void PolarWidget::axisY()
{
    setAxisYTickCount(13);
    float interval=m_angle/(m_axisYTickCount-1);
    float currAngle=0.0f;
    for(int i=0;i<m_axisYTickCount;i++)
    {
        float x=(float)m_r*cosf(currAngle*3.1415926/180);
        float y=(float)m_r*sinf(currAngle*3.1415926/180);
        QGraphicsLineItem *lineItem = m_pScene->addLine(QLineF(0,0,x ,y));
        lineItem->setPen(m_pen);
        QString strText=QString("%1").arg(currAngle);
        if(i==m_axisYTickCount-1)return;
        QGraphicsSimpleTextItem *pTextItem=m_pScene->addSimpleText(strText,QFont(QStringLiteral("楷体"),10));
        pTextItem->setPen(m_pen);
        float x1=(float)(m_r+20)*cosf(-currAngle*3.1415926/180);
        float y1=(float)(m_r+20)*sinf(-currAngle*3.1415926/180);
        int width=pTextItem->boundingRect().width();
        int height=pTextItem->boundingRect().height();
        pTextItem->setPos(x1-width/2,y1-height/2);
        currAngle+=interval;
    }
}

void PolarWidget::setAxisXTickCount(int count)
{
    m_axisXTickCount=count;
}

void PolarWidget::axisX()
{
    setAcrossAngle(360);
    setXRange(0.0f, 60.0f);
    setAxisXTickCount(7);
    float temp=(m_xEnd-m_xStart)/(m_axisXTickCount-1);
    float currX=m_xEnd;
    float rectWidth=m_pGraphicView->height()-150;
    m_r=rectWidth/2;
    float interval=(float)rectWidth/(m_axisXTickCount-1);
    ArcShapeItem*pArcAxis=nullptr;
    QGraphicsSimpleTextItem *pTextItem=nullptr;
    for(int i=0;i<m_axisXTickCount;i++)
    {
        pArcAxis=new ArcShapeItem;
        pArcAxis->setShapeItem(-rectWidth/2,-rectWidth/2,rectWidth,m_angle);
        m_pScene->addItem(pArcAxis);
        m_listShaps.append(pArcAxis);
        QString strText;
        if(i==0)
        {
            currX=m_xEnd;
            strText=QString("%1").arg(m_xEnd);
        }
        else if(i==m_axisXTickCount-1)
        {
            currX=m_xStart;
            strText=QString("%1").arg(m_xStart);
        }
        else
        {   currX-=temp;
            strText=QString("%1").arg(currX);
        }
        pTextItem=m_pScene->addSimpleText(strText,QFont(QStringLiteral("楷体"),10));
        pTextItem->setPen(m_pen);
        pTextItem->setPos(rectWidth/2,5);
        rectWidth -= interval;
    }
}

void PolarWidget::setAxisYTickCount(int count)
{
    m_axisYTickCount=count;
}

void PolarWidget::createColor()
{
    m_listColor.append(QColor(128,64,64));
    m_listColor.append(QColor(255,128,64));
    m_listColor.append(QColor(0,255,0));
    m_listColor.append(QColor(255,128,64));
    m_listColor.append(QColor(0,128,128));
    m_listColor.append(QColor(0,64,128));
    m_listColor.append(QColor(128,128,255));
    m_listColor.append(QColor(128,0,64));
    m_listColor.append(QColor(255,0,128));
    m_listColor.append(QColor(255,0,0));
    m_listColor.append(QColor(0,255,255));
    m_listColor.append(QColor(255,0,255));
    m_listColor.append(QColor(128,0,255));
    m_listColor.append(QColor(255,255,0));
    m_listColor.append(QColor(0,128,0));
    m_listColor.append(QColor(255,128,192));
}

void PolarWidget::rePaintAll()
{
    if(m_listShaps.size()>0)
    {
        qDeleteAll(m_listShaps);
        m_listShaps.clear();
    }
    if(m_lines.size()>0)
    {
       qDeleteAll(m_lines);
       m_lines.clear();
    }
    if(m_items.size()>0)
    {
       qDeleteAll(m_items);
       m_items.clear();
    }
    if(m_pTtemGroup!=nullptr)
    {
       delete m_pTtemGroup;
       m_pTtemGroup=nullptr;
    }
    m_pScene->clear();
    axisX();
    axisY();
    draw();
}

void PolarWidget::setXRange(float start, float end)
{
    m_xStart=start;
    m_xEnd=end;
}

void PolarWidget::resizeEvent(QResizeEvent *event)
{
    m_halfWidth=m_pGraphicView->width()/2;
    m_halfHeight=m_pGraphicView->height()/2;
    m_pScene->setSceneRect(-m_halfWidth, -m_halfHeight, width(),height());
    rePaintAll();
    event->ignore();
}

void PolarWidget::setAcrossAngle(float angle)
{
    m_angle=angle;
}

LineItem::LineItem()
{
  setAcceptHoverEvents(true);
}

void LineItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
    float r=sqrtf((powf(abs(event->pos().x()),2.0f)+powf(abs(event->pos().y()),2.0f)));
    float angle;
    angle=atan2(-event->pos().y(),event->pos().x());
    angle=angle*(180/3.14159);
    QString tipText=QString("%1").arg(r/m_ratio);
    tipText+=",";
    tipText+=QString("%1").arg(angle);
    QToolTip::showText(QCursor::pos(),tipText);
}

void LineItem::setRatio(float ratio)
{
    m_ratio = ratio;
}

void PolarWidget::test()
{
   QList<QPointF>points;
   for(int i=0;i<10;i++)
   {
       QPointF point;
       point.setX(i*5);
       point.setY(i*10+5);
       points.append(point);
   }
   addLine("one",points);
   QList<QPointF>points1;
   for(int i=0;i<10;i++)
   {
       QPointF point;
       point.setX(i*8);
       point.setY(i*10+8);
       points1.append(point);
   }
   addLine("tow",points1);
   QList<QPointF>points2;
   for(int i=0;i<10;i++)
   {
       QPointF point;
       point.setX(i*4);
       point.setY(i*12+4);
       points2.append(point);
   }
   addLine("tree",points2);
   draw();
}

上一篇下一篇

猜你喜欢

热点阅读