Qt数据可视化:Qt Data Visualization 与 Qt Charts 简介

Qt 最近开放了不少商业许可的模块,吸纳Qt Data Visualization 与 Qt Charts 模块无疑是为了在数据可视化方面弥补框架的不足。

Qt Charts

Qt Charts 在Qt4时代就有了,不过一直以来是商业许可的。从5.7.0开始,Qt Charts 也纳入了开放许可证。这个Qt Charts 总体还是不错的,虽然和JS的前端库的灵活程度相比,仍然显得逊色,但对于开发重工工具软件的人来说,仍旧是一大福音。毕竟,C++/Qt也可以有原生的现代2D Plot 库了(以前靠Qwt,可是Qwt的美工实在不敢恭维)。

感兴趣的同学可以去看看N个例子

这里是一个条形图的范例代码:

#include <QtWidgets/QApplication>
#include <QtWidgets/QMainWindow>
#include <QtCharts>
int main(int argc, char *argv[])
{
	QApplication a(argc, argv);
	//产生5个数据集
	QBarSet *set0 = new QBarSet("Jane"); set0->setLabelColor(QColor(0,0,0));
	QBarSet *set1 = new QBarSet("John"); set1->setLabelColor(QColor(96,0,0));
	QBarSet *set2 = new QBarSet("Axel"); set2->setLabelColor(QColor(0,96,0));
	QBarSet *set3 = new QBarSet("Mary"); set3->setLabelColor(QColor(0,0,96));
	QBarSet *set4 = new QBarSet("Sama"); set4->setLabelColor(QColor(0,96,96));
	*set0 << 1 << 2 << 3 << 4 << 5 << 6;
	*set1 << 5 << 0 << 0 << 4 << 0 << 7;
	*set2 << 3 << 5 << 8 << 13 << 8 << 5;
	*set3 << 5 << 6 << 7 << 3 << 4 << 5;
	*set4 << 9 << 7 << 5 << 3 << 1 << 2;

	//把5个数据集放入柱状图主题里
	QBarSeries *series = new QBarSeries();
	series->setLabelsVisible(true);
	series->setLabelsPosition(QAbstractBarSeries::LabelsOutsideEnd);
	series->append(set0);
	series->append(set1);
	series->append(set2);
	series->append(set3);
	series->append(set4);

	//产生一个图标对象,注意,它其实是QGraphicsWidget的子类
	QChart *chart = new QChart();
	chart->addSeries(series);	//加入柱状图
	chart->setTitle("Simple barchart example");
	chart->setAnimationOptions(QChart::SeriesAnimations);

	//X轴下标
	QStringList categories;
	categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun";
	QBarCategoryAxis *axisX = new QBarCategoryAxis();
	axisX->append(categories);
	chart->setAxisX(axisX, series);

	//Y轴设置范围
	QValueAxis * axisY = new QValueAxis();
	axisY->setRange(0,20);
	chart->setAxisY(axisY, series);

	//图例
	chart->legend()->setVisible(true);
	chart->legend()->setAlignment(Qt::AlignBottom);

	//产生窗口,这个QChartView其实是QGraphicsView的子类。
	QChartView *chartView = new QChartView(chart);
	chartView->setRenderHint(QPainter::Antialiasing);
	//支持缩放
	chartView->setRubberBand(QChartView::RectangleRubberBand);
	QMainWindow window;
	window.setCentralWidget(chartView);
	window.resize(800, 480);
	window.show();

	return a.exec();
}

运行效果:
img-HrXzxrP8-1657600337043# Qt Data Visualization
如果说QtCharts是2D美图的控件库,那这个Qt Data Visualization就是纯3D的了!基本上无所不包。类似Matlab中的三维plot,mesh,可以说是做数据可视化的同学的一大福音。看起来并不复杂,详细的范例和文档,是Qt的特色

这个模块避免了用户自己创建场景。比如例子 surface中,给了一副DEM(数字高程)PNG图片:
img-ZphQy8Gt-1657600337046
如果按照以前的想法,想显示这样的山峰,最好有Matlab或者 Octave,C++是非常复杂的。
但是,有了这个模块后,就简单了!
首先,用支持dem图片格式的代理,定义数据源(套路和QtChart惊人一致)

	QImage heightMapImage(":/maps/mountain");
	//成员变量为 QHeightMapSurfaceDataProxy * 类型,用于读取灰度(高程)
    m_heightMapProxy = new QHeightMapSurfaceDataProxy(heightMapImage);
    //这个是数据源,QSurface3DSeries 类似QtCharts里的series
    m_heightMapSeries = new QSurface3DSeries(m_heightMapProxy);

而后,产生3D画布,并绑定到一个窗口:

    Q3DSurface *graph = new Q3DSurface();
    graph->setAxisX(new QValue3DAxis);
    graph->setAxisY(new QValue3DAxis);
    graph->setAxisZ(new QValue3DAxis);
    QWidget *container = QWidget::createWindowContainer(graph);

只要为这个画布添加XYZ坐标轴(控制范围)以及数据集合,即完成了初步的显示:

        m_graph->axisX()->setRange(34.0f, 40.0f);
        m_graph->axisY()->setAutoAdjustRange(true);
        m_graph->axisZ()->setRange(18.0f, 24.0f);
        m_graph->addSeries(m_heightMapSeries);

img-ziFyNg99-1657600337048]
完整代码:


#include <QtDataVisualization>
#include <QApplication>
#include <QtWidgets>
#include <QScreen>
using namespace QtDataVisualization;
int main(int argc, char **argv)
{
	//! [0]
	QApplication app(argc, argv);
	//读取图形
	QImage heightMapImage(":/maps/mountain");
	//成员变量为 QHeightMapSurfaceDataProxy * 类型,用于读取灰度(高程)
	QHeightMapSurfaceDataProxy *  heightMapProxy = new QHeightMapSurfaceDataProxy(heightMapImage);
	//这个是数据源,QSurface3DSeries 类似QtCharts里的series
	QSurface3DSeries * heightMapSeries = new QSurface3DSeries(heightMapProxy);

	//建立窗体
	Q3DSurface *graph = new Q3DSurface();
	QWidget *container = QWidget::createWindowContainer(graph);

	//加入主题
	graph->axisX()->setAutoAdjustRange(true);
	graph->axisY()->setAutoAdjustRange(true);
	graph->axisZ()->setAutoAdjustRange(true);
	graph->addSeries(heightMapSeries);

	container->show();
	return app.exec();
}

下面这个是完整例子,道理一样的,不过演示的效果更全面!

img-NBG28lEX-1657600337049

还有好多有意思的模块

看到这两个好玩的东东,就接着翻了Qt 5.9的现有模块,真的是眼花缭乱。
http://doc.qt.io/qt-5/qtmodules.html
Qt Core 、Qt GUI、Qt Multimedia、Qt Multimedia Widgets、Qt Network、Qt Concurrent、
Qt QML、Qt Quick、Qt Quick Controls、Qt Quick Dialogs、Qt Quick Layouts
Qt SQL、Qt Test、Qt Widgets
Qt 3D、Enginio (Deprecated)、Qt D-Bus
Qt Android Extras、Active Qt、Qt Mac Extras、Qt Windows Extras
Qt Bluetooth、Qt Canvas 3D
Qt Gamepad、Qt Graphical Effects、Qt Image Formats
Qt Location、Qt NFC、Qt OpenGL (Deprecated)
Qt Platform Headers、Qt Positioning
Qt Print Support、Qt Purchasing
Qt Quick Controls 2、Qt Quick Extras
Qt Quick Widgets、Qt Script (Deprecated)
Qt SCXML、Qt Script Tools (Deprecated)
Qt Sensors、Qt Serial Bus
Qt Serial Port、Qt SVG、
Qt WebChannel、Qt WebEngine、Qt WebSockets
Qt WebView、Qt X11 Extras
Qt XML、Qt XML Patterns
Qt Wayland Compositor
Qt Charts、Qt Data Visualization
Qt Virtual Keyboard

--------------------------- Qt Data Visualization 5.7.0 --------------------------- Qt Data Visualization module provides multiple graph types to visualize data in 3D space both with C++ and Qt Quick 2. System Requirements =================== - Qt 5.2.1 or newer - OpenGL 2.1 or newer (recommended) or OpenGL ES2 (reduced feature set) - Manipulating Qt Data Visualization graphs with QML Designer requires Qt Creator 3.3 or newer Building ======== Configure the project with qmake: qmake After running qmake, build the project with make: (Linux) make (Windows with MinGw) mingw32-make (Windows with Visual Studio) nmake (OS X) make The above generates the default makefiles for your configuration, which is typically the release build if you are using precompiled binary Qt distribution. To build both debug and release, or one specifically, use one of the following qmake lines instead. For debug builds: qmake CONFIG+=debug make or qmake CONFIG+=debug_and_release make debug For release builds: qmake CONFIG+=release make or qmake CONFIG+=debug_and_release make release For both builds (Windows/OS X only): qmake CONFIG+="debug_and_release build_all" make After building, install the module to your Qt directory: make install If you want to uninstall the module: make uninstall Building as a statically linked library ======================================= The same as above applies, you will just have to add static to the CONFIG: qmake CONFIG+=static Documentation ============= The documentation can be generated with: make docs The documentation is generated into the doc folder under the build folder. Both Qt Assistant (qtdatavisualization.qch) and in HTML format (qtdatavisualization subfolder) documentation is generated. Please refer to the generated documentation for more information: doc/qtdatavisualization/qtdatavisualization-index.html Known Issues ============ - Some platforms like Android and WinRT cannot handle multiple native windows properly, so only the Qt Quick 2 versions of graphs are available in practice for those platforms. - Shadows are not supported with OpenGL ES2 (including Angle builds in Windows). - Anti-aliasing doesn't work with OpenGL ES2 (including Angle builds in Windows). - QCustom3DVolume items are not supported with OpenGL ES2 (including Angle builds in Windows). - Surfaces with non-straight rows and columns do not always render properly. - Q3DLight class (and Light3D QML item) are currently not usable for anything. - Changing most of Q3DScene properties affecting subviewports currently has no effect. - Widget based examples layout incorrectly in iOS. - Reparenting a graph to an item in another QQuickWindow is not supported. - Android builds of QML applications importing QtDataVisualization also require "QT += datavisualization" in the pro file. This is because Qt Data Visualization QML plugin has a dependency to Qt Data Visualization C++ library, which Qt Creator doesn't automatically add to the deployment package. - Only OpenGL ES2 emulation is available for software renderer (that is, when using QCoreApplication::setAttribute(Qt::AA_UseSoftwareOpenGL))
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

丁劲犇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值