Qt_Chart柱状图实现

1.效果

2.描述

在安装Qt的时候必须勾选QChart模块,默认安装是不带这个模块的,如果没有需要重新安装。
工程下载链接 下载地址

3.代码

CHistogram.cpp

#include "CHistogram.h"

CHistogram::CHistogram(QWidget *parent)
    :QFrame(parent)
{
    initUI();
}

void CHistogram::refreshUI(QVector<int> vecData)
{
    if(vecData.isEmpty())
        return ;
    vecData.resize(7);
    for(int i = 0; i < vecData.size(); i++){
        m_pBarSet->replace(i,vecData.at(i));
    }
}

void CHistogram::initUI()
{
    this->resize(600,400);
    QFont fontText("SimHei");
    fontText.setPixelSize(12);

    QBarSeries *pBarSeries = new QBarSeries();
    pBarSeries->setLabelsVisible(true);
    pBarSeries->setLabelsFormat("@valueh");
    pBarSeries->setLabelsPosition(QAbstractBarSeries::LabelsInsideEnd);
    connect(pBarSeries,&QBarSeries::hovered,[=](bool bStatus,int index,QBarSet *pBarSet){ //鼠标悬浮柱状条
        Q_UNUSED(bStatus)
        Q_UNUSED(index)
        Q_UNUSED(pBarSet)
    });

    m_pBarSet = new QBarSet("运行时间");
    m_pBarSet->setBrush(QBrush("#dd5da4f2"));
    m_pBarSet->setBorderColor("#dd5da4f2");
    m_pBarSet->setLabelFont(fontText);
    *m_pBarSet<< 5 << 7 << 8 << 12 << 20 << 24 << 16;
    pBarSeries->append(m_pBarSet);


    /*X轴设置*/
    QBarCategoryAxis *pAxisX = new QBarCategoryAxis();
    QStringList strListDate;
    for(int i = 6;i >= 0; i--) {
        strListDate.append(QDate::currentDate().addDays(-i).toString("M.dd"));
    }
    pAxisX->append(strListDate);
    pAxisX->setGridLineColor("transparent");    //参考线颜色透明
    pAxisX->setLinePenColor("#5da4f2");         //标尺 线颜色
    pAxisX->setLabelsColor("#5da4f2");          //文本颜色
    pAxisX->setLabelsFont(fontText);
    /*Y轴设置*/
    QValueAxis *pAxisY = new QValueAxis();
    pAxisY->setTickCount(13); //包含0那一条横线
    pAxisY->setLabelFormat("%dh");
    pAxisY->setRange(0,24);
    pAxisY->setGridLineColor("#5da4f2");
    pAxisY->setLinePenColor("#5da4f2"); //标尺 线颜色
    pAxisY->setLabelsColor("#5da4f2");  //文本颜色
    pAxisY->setLabelsFont(fontText);

    QChart *pChart = new QChart();
    pChart->addSeries(pBarSeries);
    pChart->setTitle("一周运行时间");
    pChart->setAnimationOptions(QChart::SeriesAnimations);
    pChart->createDefaultAxes();
    pChart->setAxisX(pAxisX);
    pChart->setAxisY(pAxisY);
    pChart->legend()->setVisible(true); //设置图例为显示状态
    pChart->legend()->setAlignment(Qt::AlignBottom);//设置图例的显示位置在底部

    QChartView *pChartView = new QChartView(pChart,this);
    pChartView->setRenderHint(QPainter::Antialiasing);
    pChartView->resize(this->size());

}

CHistogram.h

#ifndef CHISTOGRAM_H
#define CHISTOGRAM_H

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace QtCharts;
class CHistogram : public QFrame
{
    Q_OBJECT
public:
    explicit CHistogram(QWidget *parent = 0);

    void refreshUI(QVector<int> vecData);
private:
    void initUI();

private:
    QBarSet *m_pBarSet = nullptr;
};

#endif // CHISTOGRAM_H

4.测试代码

Dialog.cpp

#include "Dialog.h"
#include 


Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
{
    m_pH = new CHistogram(this);

    this->startTimer(2000);
}

Dialog::~Dialog()
{

}

void Dialog::timerEvent(QTimerEvent *event)
{
    static bool bIsa;
    bIsa = !bIsa;
    QVector<int> vec1;
    vec1<<1<<23<<3<<14<<12<<6<<7;

    QVector<int> vec2;
    vec2<<17<<6<<5<<18<<3<<12<<1;

    if(bIsa)
        m_pH->refreshUI(vec1);
    else
        m_pH->refreshUI(vec2);
}


Dialog.h

#ifndef DIALOG_H
#define DIALOG_H

#include 
#include 
#include "CHistogram.h"

class Dialog : public QDialog
{
    Q_OBJECT

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

    void timerEvent(QTimerEvent *event);

    CHistogram *m_pH;
};

#endif // DIALOG_H

你可能感兴趣的:(Qt,qt)