QDataVisualization实现三维柱状图附源代码

#include "mainwindow.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}


//MainWindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QtDataVisualization>
using namespace QtDataVisualization;

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
private slots:
    void on_pCmbScene_currentIndexChanged(int index);

    void on_comboBox_2_currentIndexChanged(int index);

    void on_comboBox_4_currentIndexChanged(int index);

    void on_spinBox_valueChanged(int arg1);

    void on_checkBox_clicked(bool checked);

    void on_checkBox_6_clicked(bool checked);

    void on_checkBox_2_clicked(bool checked);

    void on_comboBox_3_currentIndexChanged(int index);

    void on_checkBox_8_clicked(bool checked);

    void on_checkBox_3_clicked(bool checked);

    void on_pSliderH_valueChanged(int value);

    void on_pSliderV_valueChanged(int value);

    void on_pSliderZ_valueChanged(int value);

private:
    void initCreateGraphics3D();

private:
    //盛放图表的容器
    QWidget* m_pGraphContainer;
    //图表
    Q3DBars* m_pGraph3D;
    //图表序列
    QBar3DSeries* m_pSeries3D;
private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H


///MainWindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QSplitter>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    initCreateGraphics3D();
    //创建分隔窗口,水平分隔
    QSplitter* pSplitter = new QSplitter(Qt::Horizontal);
    //分割窗口是否可以变化
    pSplitter->setOpaqueResize(true);
    pSplitter->addWidget(ui->groupBox);
    pSplitter->addWidget(m_pGraphContainer);
    this->setCentralWidget(pSplitter);
}

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

void MainWindow::initCreateGraphics3D()
{
    //创建图表
    m_pGraph3D = new Q3DBars();
    //创建容器因为Q3DBars继承自QWindow,所以不能简单的直接继承QWidget
    m_pGraphContainer = QWidget::createWindowContainer(m_pGraph3D);
    //创建一个3行5列的数值坐标轴
    QStringList ListRowLabels;
    QStringList ListColLabels;
    ListRowLabels << "row1" << "row2" << "row3";
    ListColLabels << "col1" << "col2" << "col3" << "col4" << "col5";
    QValue3DAxis* pValue3DAxis = new QValue3DAxis;
    pValue3DAxis->setTitle("values");
    pValue3DAxis->setTitleVisible(true);
    //行坐标轴
    QCategory3DAxis* pRowCate3DAxis = new QCategory3DAxis;
    pRowCate3DAxis->setTitle("row Axis");
    pRowCate3DAxis->setTitleVisible(true);
    //将创建的行坐标列表加入行坐标轴
    pRowCate3DAxis->setLabels(ListRowLabels);
    //列坐标轴
    QCategory3DAxis* pColCate3DAxis = new QCategory3DAxis;
    pColCate3DAxis->setTitle("Col Axis");
    pColCate3DAxis->setTitleVisible(true);
    //将创建的列坐标列表放入列坐标轴
    pColCate3DAxis->setLabels(ListColLabels);

    m_pGraph3D->setValueAxis(pValue3DAxis);
    m_pGraph3D->setRowAxis(pRowCate3DAxis);
    m_pGraph3D->setColumnAxis(pColCate3DAxis);

    //创建序列
    m_pSeries3D = new QBar3DSeries;
    //棒图形状
    m_pSeries3D->setMesh(QAbstract3DSeries::MeshCylinder);
    //设置阴影策略
    m_pGraph3D->setShadowQuality(QAbstract3DGraph::ShadowQualityMedium);


    m_pSeries3D->setItemLabelFormat("(@rowLabel, @colLabel):%.1f");
    //将序列添加到图表
    m_pGraph3D->addSeries(m_pSeries3D);

    ///添加数据
    //创建数据数组
    QBarDataArray* pDataArray = new QBarDataArray;
    //指定数组的元素个数未图表柱状图的行数
    pDataArray->reserve(ListRowLabels.count());
    QBarDataRow* BarDataRow[3] = {Q_NULLPTR};
    for(int i = 0; i < ListRowLabels.count(); ++i)
        BarDataRow[i] = new QBarDataRow;
    *BarDataRow[0] << 1 << 2 << 3 << 4 << 5;
    *BarDataRow[1] << 5 << 3 << 7 << 2 << 1;
    *BarDataRow[2] << 4 << 4 << 9 << 6 << 8;
    for(int i = 0; i < ListRowLabels.count(); ++i)
        pDataArray->append(BarDataRow[i]);
    //将数据加入序列
    m_pSeries3D->dataProxy()->resetArray(pDataArray);

    //设置x y 坐标厚度比 和bar间距
    m_pGraph3D->setBarThickness(1.0f);
    m_pGraph3D->setBarSpacing(QSizeF(1.1, 1.1));
    //设置初始视角
    Q3DCamera::CameraPreset cameraPos = Q3DCamera::CameraPreset(Q3DCamera::CameraPresetFront);
    m_pGraph3D->scene()->activeCamera()->setCameraPreset(cameraPos);
    //设置初始主题
    Q3DTheme* pCurrentTheme = m_pGraph3D->activeTheme();
    pCurrentTheme->setType(Q3DTheme::ThemePrimaryColors);
    //设置初始背景
    m_pGraph3D->activeTheme()->setBackgroundEnabled(true);
    //设置初始显示网络
    m_pGraph3D->activeTheme()->setGridEnabled(true);
    //坐标轴标签背景
    m_pGraph3D->activeTheme()->setLabelBackgroundEnabled(true);
}


void MainWindow::on_pCmbScene_currentIndexChanged(int index)
{
    Q3DCamera::CameraPreset cameraPos = Q3DCamera::CameraPreset(index);
    m_pGraph3D->scene()->activeCamera()->setCameraPreset(cameraPos);
}

void MainWindow::on_comboBox_2_currentIndexChanged(int index)
{
    Q3DTheme* pCurrentTheme = m_pGraph3D->activeTheme();
    pCurrentTheme->setType(Q3DTheme::Theme(index));
}

void MainWindow::on_comboBox_4_currentIndexChanged(int index)
{
    m_pGraph3D->setSelectionMode(QAbstract3DGraph::SelectionFlag(index));
}



void MainWindow::on_spinBox_valueChanged(int arg1)
{
   m_pGraphContainer->setFont(QFont("宋体",arg1));
}

void MainWindow::on_checkBox_clicked(bool checked)
{
    m_pGraph3D->activeTheme()->setBackgroundEnabled(checked);
}

void MainWindow::on_checkBox_6_clicked(bool checked)
{
    m_pGraph3D->activeTheme()->setGridEnabled(checked);
}

void MainWindow::on_checkBox_2_clicked(bool checked)
{
//    m_pGraph3D->activeTheme()->set
}

void MainWindow::on_comboBox_3_currentIndexChanged(int index)
{
    QAbstract3DSeries::Mesh mesh;
    mesh = QAbstract3DSeries::Mesh(index +1);
    m_pSeries3D->setMesh(mesh);
}

void MainWindow::on_checkBox_8_clicked(bool checked)
{
    m_pSeries3D->setItemLabelFormat("value at (@rowLabel, @colLabel):%.1f");
    m_pSeries3D->setItemLabelVisible(checked);
}

void MainWindow::on_checkBox_3_clicked(bool checked)
{
    m_pGraph3D->valueAxis()->setReversed(checked);
}

void MainWindow::on_pSliderH_valueChanged(int value)
{
    Q_UNUSED(value);
    int xRot = ui->pSliderH->value();
    int yRot = ui->pSliderV->value();
    int zRoom = ui->pSliderZ->value();
    m_pGraph3D->scene()->activeCamera()->setCameraPosition(xRot, yRot, zRoom);
}

void MainWindow::on_pSliderV_valueChanged(int value)
{
    Q_UNUSED(value);
    int xRot = ui->pSliderH->value();
    int yRot = ui->pSliderV->value();
    int zRoom = ui->pSliderZ->value();
    m_pGraph3D->scene()->activeCamera()->setCameraPosition(xRot, yRot, zRoom);
}

void MainWindow::on_pSliderZ_valueChanged(int value)
{
    Q_UNUSED(value);
    int xRot = ui->pSliderH->value();
    int yRot = ui->pSliderV->value();
    int zRoom = ui->pSliderZ->value();
    m_pGraph3D->scene()->activeCamera()->setCameraPosition(xRot, yRot, zRoom);
}

 

记住pro文件添加

QT       +=datavisualization
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值