话不多说,先上效果,官方的例子很多,比我这个炫酷的也很多:
因为看了网上的资料,发现很少有人研究QtDataVisualization,所以这里我就来抛砖引玉一下。
1:简介QtDataVisualization
QtDataVisualization是Qt提供的用于数据三维显示的模块。在Qt 5.7以前只有商业版才有此模块,而从Qt 5.7开始此模块在社区版本里也可以免费使用了。(这里备注一下,如果你发现你的qt里面没有这个模块,那就再装一个Qt 的版本吧,怎么装的就不多说了,这里给一个参考地址:http://c.biancheng.net/view/3851.html)。Data Visualization 用于数据的三维显示,包括,三维柱状图,三维空间散点,三维曲面等。(这里你就会发现空间的东西他给了最基础的,点。对于曲面来说,这个时候你已经可以像MATLAB里面的数据模型一样的用各种数学函数表达式来看到效果了。但是他没有给线,和空间平面)相信到这里你已经看出来了,Qt提供的这个模块并不能和一些专业的三维显示类库相提并论,但是对于一些简单的三维数据显示还是比较实用的,例如一些科学计算结果的三维显示。
这里再说一下,为什么没有给线和空间的平面呢?首先我们来看一下人家的官方的文档怎么说的,看一下QSurfaceDataProxy(Qt的面数据代理)怎么解释的:
A surface data proxy handles surface related data in rows. For this it provides two auxiliary typedefs: QtDataVisualization::QSurfaceDataArray and QtDataVisualization::QSurfaceDataRow. QSurfaceDataArray is a QList that controls the rows. QSurfaceDataRow is a QVector that contains QSurfaceDataItem objects. For more information about how to feed the data to the proxy, see the sample code in the Q3DSurface documentation.
All rows must have the same number of items.
QSurfaceDataProxy takes ownership of all QSurfaceDataRow objects passed to it, whether directly or in a QSurfaceDataArray container. To use surface data row pointers to directly modify data after adding the array to the proxy, the appropriate signal must be emitted to update the graph.
To make a sensible surface, the x-value of each successive item in all rows must be either ascending or descending throughout the row. Similarly, the z-value of each successive item in all columns must be either ascending or descending throughout the column.(为了形成一个合理的曲面,所有行中每个连续项的x值必须在整行中升序或降序。类似地,所有列中每个连续项的z值必须在整个列中升序或降序。)
Note: Currently only surfaces with straight rows and columns are fully supported. Any row with items that do not have the exact same z-value or any column with items that do not have the exact same x-value may get clipped incorrectly if the whole surface does not completely fit within the visible x-axis or z-axis ranges.(注意:目前只有直行和直列的曲面才得到完全支持。如果整个曲面不完全符合可见的x轴或z轴范围,则任何具有不完全相同z值项的行或包含不具有完全相同x值的项的列可能会被错误地剪裁。)
Note: Surfaces with less than two rows or columns are not considered valid surfaces and will not be rendered.(注意:少于两行或两列的曲面不被视为有效曲面,因此不会进行渲染。)
Note: On some environments, surfaces with a lot of visible vertices may not render, because they exceed the per-draw vertex count supported by the graphics driver. This is mostly an issue on 32-bit and OpenGL ES2 platforms.(注意:在某些环境中,具有大量可见顶点的曲面可能无法渲染,因为它们超出了图形驱动程序支持的每次绘制顶点数。这在32位和opengles2平台上主要是一个问题。)
这里也就能说明问题了!
2:Data Visualization模块概述
Data Visualization 的三维显示功能主要由3种三维图形类来实现,分别是三维柱状图(Q3DBars),三维空间散点图(Q3DScatter),三维曲面图(Q3DSurface)。这3个类的父类是QAbstract3DGraph,是从QWindow继承而来的,继承关系
Data Visualization 与 Qt Charts 类似,都基于Qt 的图形视图结构,所以一个三维图形也是由图表、序列、坐标轴等元素组成的。Q3DBars、Q3DScatter、Q3DSurface 相当于Qt Charts中的QChart,而每一种三维图形对应一种三维序列,Data Visualization中的3种序列如图
一种序列类只能用于某种三维图形类,如QScatter3DSeries只能用作三维散点Q3DScatter的序列,而不能用作Q3DBars的序列。再一个图中,可以有多个同类型的序列,如三维柱状图Q3DBars中可以有多个QBar3DSeries序列,用作显示不同的柱状图。
与QChart有坐标轴类一样,三维图形也有坐标轴类。有两种三维坐标轴类,QValue3DAxis用于数值型坐标轴,QCategory3DAxis用于文字型坐标轴,它们都继承自QAbstract3DAxis
Data Visualization模块中有数据代理(data proxy)类,数据代理类就是与序列对应,用于存储序列的数据的类。因为三维图形类型不一样,存储数据的结构也不一样,例如三维散点序列QScatter3DSeries存储的是一些三维数据点的坐标,只需要用一维数组或者列表就能存储这些数据,而QSurface3DSeries序列存储的数据点在水平面上是均匀网格分布的,需要二维数组才可以存储相应的数据。为此,对于每一种序列,都有一个数据代理类,他们都继承自QAbstractDataProxy,每一个数据代理类还有一个基于项数据模型的数据代理子类
对于三维曲面序列QSurface3DSeries,还有一个专门用于显示地图高程数据的数据代理类QHeightMapSurfaceDataProxy,可以将一个图片表示的高程数据显示为三维的曲面。用户也可以根据需要从QAbstractDataProxy继承,定义自己的数据代理类。
要从项目中使用Data Visualization模块,需要再项目配置文件中(.pro文件)添加下面的语句:
Qt += datavisualization
在使用Data Visualization模块中的类的头文件或源程序文件中,还需要加下面两种语句:
#include <QtDataVisualization>
using namespace QtDataVisualization;