3d旋转相册代码源码_Qt Data Visualization ,让数据3D可视化

点击标题下「蓝色微信名」可快速关注

在前面我们已经见识了Qt Charts可以非常简单地实现常见的图表,但Qt Charts只能显示平面图,如果想更进一步,让数据以3D形式进行显示,该怎么实现呢?在Qt中提供的Qt Data Visualization模块可以帮助我们快速创建3D柱形图、散点和曲面图。下面是Qt自带示例程序的效果。

49b885139fc8b63a13a46191c3044721.png

c0172e3b1fdbd3898d6fef52fdc75575.png

今天我们来演示一个3D柱形图,实现柱形图的动态展示功能。先看下最后效果。

a5798c4854756b52d58aa454da4b272a.png

如果没啥感觉,那就看看动态效果吧。

8df9c67a9a1c629fc89db9d834fb3dc7.png

6f95151cfda65f7ebd00bf5b9a3ff7f3.png

开发环境:Win 7 + Qt 5.12.0(注意:在安装Qt时需要选择安装Qt Data Visualization模块)

实现基本的柱形图

首先新建空的Qt Quick应用。将main.qml内容修改如下:

import QtQuick 2.9
import QtQuick.Window 2.2
import QtDataVisualization 1.2

Window {
    visible: true
    width: 800
    height: 450

    Bars3D {
        id: bars3D
        width: parent.width
        height: parent.height
        theme: Theme3D { type: Theme3D.ThemePrimaryColors}
        scene.activeCamera.xRotation : 40.0
        scene.activeCamera.yRotation : 45.0

        Bar3DSeries {
            id: series
            itemLabelFormat: "@colLabel, @rowLabel: @valueLabel"
            ItemModelBarDataProxy {
                itemModel: dataModel
                rowRole: "year"
                columnRole: "city"
                valueRole: "expenses"
            }
        }
    }

    ListModel {
        id: dataModel
        ListElement{ year: "2018"; city: "beijing";  price: "67822"; }
        ListElement{ year: "2018"; city: "nanjing";  price: "26714"; }
        ListElement{ year: "2018"; city: "shenzhen"; price: "50900"; }
        ListElement{ year: "2018"; city: "hangzhou"; price: "30729"; }
        ListElement{ year: "2017"; city: "beijing";  price: "67951"; }
        ListElement{ year: "2017"; city: "nanjing";  price: "26127"; }
        ListElement{ year: "2017"; city: "shenzhen"; price: "46879"; }
        ListElement{ year: "2017"; city: "hangzhou"; price: "22934"; }
    }
}

这里只需要指定Bars3D、Bar3DSeries两个类型就可以实现3D柱形图,前者用来渲染3D柱形图,后者用来指定具体的图形系列,在一个Bars3D里面可以定义多个系列。scene.activeCamera用来设置场景相机,这里指定了X、Y轴上的旋转角度,这样可以更好地显示图表。ItemModelBarDataProxy用来将下面ListModel中的数据映射到图形系列中,简单来说就是在3D坐标轴中X、Y、Z轴分别对应哪些数据,这里通过rowRole、columnRole和valueRole三个属性来指定。itemLabelFormat用来设置点击一个具体的柱形时显示标签的格式。通过下面的运行效果图,可以更加清楚地看到它们的对应关系。

643dc12f0cac33bfc0e5e2c801ff66ab.png

这里使用theme属性指定了3D柱形图的主题样式,咱们使用了现成的主题。Qt自带了9个现成可用的主题,部分效果如下图所示。

0b10e721e269340ccd602ad162d63a1c.png 08e007b304f06e2f6b1a3a7f3b370511.png c08592be8f473e6126477926380c12d4.png 39fd389b742c8e27efe5f992e6c42135.png 使用自定义主题

很多时候自带的主题并不能满足我们的需求,这时就需要自定义主题。在根对象中添加如下代码:

ThemeColor {
    id: dynamicColor
    color: "yellow"
}

Theme3D {
    id: dynamicColorTheme
    type: Theme3D.ThemeEbony
    baseColors: [dynamicColor]
    font.pointSize: 50
    labelBorderEnabled: false
    labelBackgroundColor: "green"
    labelTextColor: "white"
    backgroundColor: "white"
    gridLineColor: "green"
}

这里通过Theme3D来定义一个3D主题,我们指定了type为现成的themeEbony主题,表明是基于该主题进行修改的。baseColors属性用来指定所有图形系列的颜色,它是一个列表,其中使用ThemeColor类型来指定颜色,因为我们现在只有一个系列,所有只指定了dynamicColor一个颜色。

下面将前面Bars3D对象的theme属性更改为:

theme: dynamicColorTheme

这时运行程序,效果如下图所示。

e690afa7fb9dcc035e0f42949696f36b.png

动态展示3D图表

为了更好地展示3D效果,我们使用顺序动画组执行了一系列动画,在根对象最后添加如下代码:

SequentialAnimation {
    id: cameraAnimation
    running: true

    NumberAnimation {
        target: bars3D.scene.activeCamera
        property:"xRotation"
        from: 40.0; to: 10.0
        duration: 4000
        easing.type: Easing.InOutBack
    }

    NumberAnimation {
        target: bars3D.scene.activeCamera
        property:"yRotation"
        from: 5.0; to: 45.0
        duration: 3000
        easing.type: Easing.InQuart
    }

    ColorAnimation { target:  dynamicColor;
        property: "color";
        to: "red"; duration: 2000;
        easing.type: Easing.InCirc}
    ColorAnimation {target:  dynamicColor;
        property: "color";
        to: "yellow"; duration: 2000;
        easing.type: Easing.OutCirc}

    NumberAnimation {
        target: bars3D.scene.activeCamera
        property:"xRotation"
        from: 10.0; to:40.0
        duration: 2000
        easing.type: Easing.OutQuad
    }

    ColorAnimation { target:  dynamicColorTheme;
        property: "labelBackgroundColor";
        to: "green"; duration: 1000 ;
        easing.type: Easing.OutQuad}
    ColorAnimation { target:  dynamicColorTheme;
        property: "labelTextColor";
        to: "white"; duration: 1000 ;
        easing.type: Easing.OutQuad}
    ColorAnimation { target:  dynamicColorTheme;
        property: "backgroundColor";
        to: "white"; duration: 1000;
        easing.type: Easing.OutQuad }
    ColorAnimation {target:  dynamicColorTheme;
        property: "gridLineColor";
        to: "green"; duration: 500;
        easing.type: Easing.OutQuad}

    NumberAnimation {
        target: bars3D.scene.activeCamera
        property:"zoomLevel"
        from: 75.0; to:95.0
        duration: 3000
        easing.type: Easing.InOutQuart
    }
}

这里通过NumberAnimation和ColorAnimation为相机旋转、图表颜色等添加动画效果,从而实现了动态显示3D图表的效果。为了更加炫酷,我们在执行动画前,需要对图表的默认属性值进行一些修改。

1.将ThemeColor对象中:

color: "yellow"

一行代码注释或者删除掉;

2.将Theme3D对象中几个指定颜色的属性修改为透明:

labelBackgroundColor: "transparent"
labelTextColor: "transparent"
backgroundColor: "transparent"
gridLineColor: "transparent"

3.将Bars3D对象中定义相机的语句修改如下:

scene.activeCamera.xRotation: 45.0
scene.activeCamera.yRotation: 5.0
scene.activeCamera.zoomLevel: 75

现在运行程序,就可以看到最终效果了。

结语

怎么样,是不是感觉用QML来实现这样炫酷的3D图表很简单呢?心动不如行动,抽时间打开Qt Creator,赶快来亲自测试下吧。因为这里只是新技术尝鲜演示,不是基础教程,所以如果有的小伙伴接受起来感觉困难,不要灰心,可以等我们后期的系列教程来从头学习。

如果需要下载源码,可以点击下面的


相关文章:

当QtCharts遇上QtGraphicalEffects,梦幻科技曲线!

d8dc13ccf639f1d47ff702b96e6110a3.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值