Qt development technology: Q3D chart development notes (2): Q3DBar three-dimensional histogram introduction, Demo and detailed code

If the article is an original article, please indicate the source of the original article when reprinting.
The blog address of this article: https://hpzwl.blog.csdn.net/article/details/130150728

Dear readers, knowledge is infinite but manpower is limited. You can either change your needs, find professionals, or do your own research.

Red Fatty Network Science and Technology Blog Encyclopedia: Development technology collection (including Qt practical technology, Raspberry Pi, 3D, OpenCV, OpenGL, ffmpeg, OSG, single-chip microcomputer, combination of software and hardware, etc.) is continuously being updated... (click on the portal)

Qt Development Column: Development Technology (click on the portal)

Previous: " Qt Development Technology: Q3D Chart Development Notes (1): Q3DScatter 3D Scatter Diagram Introduction, Demo and Detailed Code Explanation "
Next: Stay tuned...


foreword

  qt provides q3d for 3D development. Although this framework has not been widely used and is not so successful, and its performance is also very lacking, ordinary point-to-point application display is still possible.
  Among them are gorgeous three-dimensional charts, which can be used when the amount of data is not large.
  The previous article introduced the basic q3d scatter plot, and this article introduces the basic histogram.


Demo: Q3DScatter scatter plot demonstration effect

  insert image description here

  insert image description here

  insert image description here

download link:

  Demo v1.0.0 running package download address: https://download.csdn.net/download/qq21497936/87688822
  QQ group download: Please click on the main avatar of the blog to enter the homepage of the blog, check the right side, there is the contact information of the QQ group, ( Click "File" to search for "q3d", and the group and blog posts will be updated simultaneously)
  Baidu Netdisk download address: https://pan.baidu.com/s/14uQ3Hbhwys3aWxArxf9fXw?pwd=1234


3D chart provided by Q3D

  Depends on QtDataVisualization. When installing qt, choose to install the QtDataVisualization module.

Q3DScatter scatter plot

  The performance of Q3D scatter plot supports about 1000 points without lagging. It depends on the PC. What is the concept of 1000 points? It can be understood as: 10x10x10 area, one data point for each area.
  insert image description here

Q3DBars histogram

  The histogram of Q3D is similar to the scatter plot in performance.
   insert image description here

Q3DSurface plane bump map, plane texture map

  The histogram of Q3D is similar to the scatter plot in performance.
  insert image description here


# Q3DBar Histogram## Introduction The Q3DBars class provides methods for rendering three-dimensional bar charts. This class enables developers to render bars in 3D and view them by freely rotating the scene.
  • Rotation : done by holding down the right mouse button and moving the mouse.
  • Zooming : done with the mouse wheel.
  • Selection : If selection is enabled, selection is made with the left mouse button.
  • Reset POV : By clicking the mouse wheel, the scene can be reset to the default camera view.
      On touch devices, rotation is done by tapping and moving, selection is done by tapping and holding, and zooming is done by pinching.
      If no axes are explicitly set to the Q3DBar, temporary default axes with no labels are created. These default axes can be modified via the axes accessor, but as soon as any axis for an orientation is explicitly set, the default axis for that orientation will be destroyed.
    Q3DBars supports displaying multiple series at the same time. Not all series have to have the same number of rows and columns. Row and column labels are taken from the first series added unless explicitly defined as row and column axes.

Construct a minimal Q3D histogram

  First, construct a Q3DBars instance. Since in this example we are running the graphics as a top-level window, we need to clear the Qt::FramelessWindowHint flag, which is set by default:

Q3DBars bars;
bars.setFlags(bars.flags() ^ Qt::FramelessWindowHint);

  After constructing the Q3DBar, the data window can be set by changing the range on the row and column axes. This is not mandatory as the data window will by default display all data in the series. If the amount of data is large, it is usually best to display only a portion of it. For example, let's set the data window to display the first five rows and columns:

bars.rowAxis()->setRange(0, 4);
bars.columnAxis()->setRange(0, 4);

  Now the Q3DBars are ready to receive data to be rendered. Create a sequence with a row of 5 values:

QBar3DSeries *series = new QBar3DSeries;
QBarDataRow *data = new QBarDataRow;
*data << 1.0f << 3.0f << 7.5f << 5.0f << 2.2f;
series->dataProxy()->addRow(data);
bars.addSeries(series);

  NOTE: The data window is set to 5 x 5, but only one row of data has been added. The rest of the lines that are not added will be blank.
  Finally, make it visible:

bars.show();

  The complete code required to create and display this plot is:

#include <QtDataVisualization>

using namespace QtDataVisualization;

int main(int argc, char **argv)
{
    
    
    QGuiApplication app(argc, argv);

    Q3DBars bars;
    bars.setFlags(bars.flags() ^ Qt::FramelessWindowHint);
    bars.rowAxis()->setRange(0, 4);
    bars.columnAxis()->setRange(0, 4);
    QBar3DSeries *series = new QBar3DSeries;
    QBarDataRow *data = new QBarDataRow;
    *data << 1.0f << 3.0f << 7.5f << 5.0f << 2.2f;
    series->dataProxy()->addRow(data);
    bars.addSeries(series);
    bars.show();

    return app.exec();
}

  running result:
  insert image description here

  The scene can be rotated, zoomed in, and an item can be selected to see its position, but other interactions are not included in this minimal code example.


Q3Ddemo construction process analysis

Step 1: Confirm the installation of the QtDataVisualization module

  How to confirm it is to check whether there is a Q3dscatter class in the help file. Generally, the corresponding help file will be available only after the module is installed. If not, reinstall qt or install the module separately.
   insert image description here

Step 2: Add modules to the project configuration file

  Q3d is in the data visualization module and needs to be added in the pro or pri configuration file.

QT += datavisualization

  insert image description here

Step 3: Add the header files used

Use to add header files to Q3DBar related classes, mainly use Q3DBar, QBar3DSeries, QBarDataRow and so on.

#include <Q3DBars>
#include <Q3DTheme>
#include <QBar3DSeries>
#include <QVector3D>

  insert image description here

Step 4: Add a Namespace

  At this time, the corresponding class still cannot be used, and the namespace needs to be added:

using namespace QtDataVisualization;

  insert image description here

Step 5: Q3D's icon base construction framework

  The following is the Q3DBar basic construction process including comments, which is related to the axis (axis name and axis usage, etc.) If there is 0 in the middle, it is not easy to handle, you need to add 0 to occupy the place, QBarDataRow automatically sorts in sequence)

_pQ3DBars = new Q3DBars();
_pContainer = QWidget::createWindowContainer(_pQ3DBars, this);

// 设置轴文本
{
    
    
    QStringList strList;
    _pQ3DBars->rowAxis()->setTitle("年");
    _pQ3DBars->rowAxis()->setTitleVisible(true);
    strList.clear();
    strList << "2010" << "2011" << "2012" << "2013" << "2014"
               << "2015" << "2016" << "2017" << "2018" << "2019"
               << "2020" << "2021" << "2022" << "2023" ;
    _pQ3DBars->rowAxis()->setLabels(strList);
    _pQ3DBars->columnAxis()->setTitle("月");
    strList.clear();
    strList << "1" << "2" << "3" << "4" << "5"
                << "6" << "7" << "8" << "9" << "10"
                << "11" << "12";
    _pQ3DBars->columnAxis()->setLabels(strList);
    _pQ3DBars->columnAxis()->setTitleVisible(true);
    _pQ3DBars->valueAxis()->setTitle("销售额(万元)");
    _pQ3DBars->valueAxis()->setTitleVisible(true);
}
// 设置轴范围
{
    
    
    _pQ3DBars->rowAxis()->setRange(2010 - 2010, 2023 - 2010);   // 从0开始
    _pQ3DBars->columnAxis()->setRange(1 - 1, 12 - 1);           // 从0开始
    _pQ3DBars->valueAxis()->setRange(0, 40);
}

// 生成一个曲线
_pBar3DSeries = new QBar3DSeries(_pQ3DBars);
// 设置渲染平滑
_pBar3DSeries->setMeshSmooth(true);

// 视图添加该曲线
_pQ3DBars->addSeries(_pBar3DSeries);

// 设置阴影质量
_pQ3DBars->setShadowQuality(QAbstract3DGraph::ShadowQualitySoftLow);
// 设置视角
_pQ3DBars->scene()->activeCamera()->setCameraPreset(Q3DCamera::CameraPresetIsometricLeft);
// 设置子网格
_pQ3DBars->activeTheme()->setGridEnabled(true);

#if 1
// 添加模拟数据
QBarDataArray data;
for(int year = 2010; year <= 2023; year++)
{
    
    
    QBarDataRow *pBarDataRow = new QBarDataRow;
    for(int month = 1; month <= 12; month++)
    {
    
    
        if(year == 2023 && month >= 4)
        {
    
    
            LOG << year << month;
            // 当前2023年4月,无数据
            *pBarDataRow << 0;
        }else{
    
    
            *pBarDataRow << ((year - 2010) + month);
        }
    }
    data << pBarDataRow;
}
// 添加数据(自动冲掉之前的数据)
_pBar3DSeries->dataProxy()->addRows(data);
#endif

Demo source code

Q3dBarWidget.h
#ifndef Q3DBARWIDGET_H
#define Q3DBARWIDGET_H

#include <QWidget>
#include <Q3DBars>
#include <Q3DTheme>
#include <QBar3DSeries>
#include <QVector3D>


using namespace QtDataVisualization;

namespace Ui {
    
    
class Q3dBarWidget;
}

class Q3dBarWidget : public QWidget
{
    
    
    Q_OBJECT

public:
    explicit Q3dBarWidget(QWidget *parent = 0);
    ~Q3dBarWidget();

protected:
    void initControl();


protected:
    void resizeEvent(QResizeEvent *event);

private:
    Ui::Q3dBarWidget *ui;

private:
    Q3DBars *_pQ3DBars;             // q3d柱状视图
    QWidget *_pContainer;           // q3d窗口容器
    QBar3DSeries *_pBar3DSeries;    // q3d柱状图数据
};

#endif // Q3DBARWIDGET_H

Q3dBarWidget.cpp

#include "Q3dBarWidget.h"
#include "ui_Q3dBarWidget.h"
#include <Q3DTheme>


#include <QDebug>
#include <QDateTime>
//#define LOG qDebug()<<__FILE__<<__LINE__
//#define LOG qDebug()<<__FILE__<<__LINE__<<__FUNCTION__
//#define LOG qDebug()<<__FILE__<<__LINE__<<QThread()::currentThread()
//#define LOG qDebug()<<__FILE__<<__LINE__<<QDateTime::currentDateTime().toString("yyyy-MM-dd")
#define LOG qDebug()<<__FILE__<<__LINE__<<QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss:zzz")

Q3dBarWidget::Q3dBarWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Q3dBarWidget),
    _pQ3DBars(0),
    _pContainer(0),
    _pBar3DSeries(0)
{
    
    
    ui->setupUi(this);

    QString version = "v1.0.0";
    setWindowTitle(QString("q3d柱状图示例 %1(作者:长沙红胖子 QQ:21497936 WX:15173255813 www.hpzwl.com").arg(version));

    initControl();
}

Q3dBarWidget::~Q3dBarWidget()
{
    
    
    delete ui;
}


void Q3dBarWidget::initControl()
{
    
    
    _pQ3DBars = new Q3DBars();
    _pContainer = QWidget::createWindowContainer(_pQ3DBars, this);

    // 设置轴文本
    {
    
    
        QStringList strList;
        _pQ3DBars->rowAxis()->setTitle("年");
        _pQ3DBars->rowAxis()->setTitleVisible(true);
        strList.clear();
        strList << "2010" << "2011" << "2012" << "2013" << "2014"
                << "2015" << "2016" << "2017" << "2018" << "2019"
                << "2020" << "2021" << "2022" << "2023" ;
        _pQ3DBars->rowAxis()->setLabels(strList);
        _pQ3DBars->columnAxis()->setTitle("月");
        strList.clear();
        strList << "1" << "2" << "3" << "4" << "5"
                << "6" << "7" << "8" << "9" << "10"
                << "11" << "12";
        _pQ3DBars->columnAxis()->setLabels(strList);
        _pQ3DBars->columnAxis()->setTitleVisible(true);
        _pQ3DBars->valueAxis()->setTitle("销售额(万元)");
        _pQ3DBars->valueAxis()->setTitleVisible(true);
    }
    // 设置轴范围
    {
    
    
        _pQ3DBars->rowAxis()->setRange(2010 - 2010, 2023 - 2010);   // 从0开始
        _pQ3DBars->columnAxis()->setRange(1 - 1, 12 - 1);           // 从0开始
        _pQ3DBars->valueAxis()->setRange(0, 40);
    }

    // 生成一个曲线
    _pBar3DSeries = new QBar3DSeries(_pQ3DBars);
    // 设置渲染平滑
    _pBar3DSeries->setMeshSmooth(true);

    // 视图添加该曲线
    _pQ3DBars->addSeries(_pBar3DSeries);

    // 设置阴影质量
    _pQ3DBars->setShadowQuality(QAbstract3DGraph::ShadowQualitySoftLow);
    // 设置视角
    _pQ3DBars->scene()->activeCamera()->setCameraPreset(Q3DCamera::CameraPresetIsometricLeft);
    // 设置子网格
    _pQ3DBars->activeTheme()->setGridEnabled(true);

#if 1
    // 添加模拟数据
    QBarDataArray data;
    for(int year = 2010; year <= 2023; year++)
    {
    
    
        QBarDataRow *pBarDataRow = new QBarDataRow;
        for(int month = 1; month <= 12; month++)
        {
    
    
            if(year == 2023 && month >= 4)
            {
    
    
                LOG << year << month;
                // 当前2023年4月,无数据
                *pBarDataRow << 0;
            }else{
    
    
                *pBarDataRow << ((year - 2010) + month);
            }
        }
        data << pBarDataRow;
    }
    // 添加数据(自动冲掉之前的数据)
    _pBar3DSeries->dataProxy()->addRows(data);
#endif

}

void Q3dBarWidget::resizeEvent(QResizeEvent *event)
{
    
    
    if(_pContainer)
    {
    
    
        _pContainer->setGeometry(rect());
    }
}

Project template v1.1.0

  insert image description here


Previous: " Qt Development Technology: Q3D Chart Development Notes (1): Q3DScatter 3D Scatter Diagram Introduction, Demo and Detailed Code Explanation "
Next: Stay tuned...


If the article is an original article, please indicate the source of the original article when reprinting.
The blog address of this article: https://hpzwl.blog.csdn.net/article/details/130150728

Guess you like

Origin blog.csdn.net/qq21497936/article/details/130150728
Recommended