当前位置: 首页 > news >正文

【QT】-怎么实现瀑布图

为什么 QCustomPlot 是最科学的选择?
在这里插入图片描述
一、QCustomPlot 实现瀑布图步骤(附代码)
步骤 1:下载并集成 QCustomPlot
官网下载:https://www.qcustomplot.com/
将 qcustomplot.h 和 qcustomplot.cpp 加入你的项目
在 .pro 文件中添加:

QT += widgets printsupport
HEADERS += qcustomplot.h
SOURCES += qcustomplot.cpp

步骤 2:创建瀑布图核心代码

#include "qcustomplot.h"// 假设你有一个二维数据:data[time][frequency] = amplitude
QVector<QVector<double>> waterfallData; // 你的瀑布图数据void setupWaterfallPlot(QCustomPlot *customPlot)
{// 1. 创建 ColorMap(瀑布图本质是颜色映射图)QCPColorMap *colorMap = new QCPColorMap(customPlot->xAxis, customPlot->yAxis);customPlot->addPlottable(colorMap);// 2. 填充数据int timePoints = waterfallData.size();      // 时间点数量int freqPoints = waterfallData[0].size();   // 频率点数量colorMap->data()->setSize(timePoints, freqPoints);colorMap->data()->setRange(QCPRange(0, timePoints-1), QCPRange(0, freqPoints-1));for (int t = 0; t < timePoints; ++t) {for (int f = 0; f < freqPoints; ++f) {colorMap->data()->setCell(t, f, waterfallData[t][f]);}}// 3. 设置颜色渐变(如:蓝-绿-红)QCPColorGradient gradient;gradient.setColorStopAt(0, QColor(0, 0, 255));     // 蓝色(低能量)gradient.setColorStopAt(0.5, QColor(0, 255, 0));   // 绿色(中能量)gradient.setColorStopAt(1, QColor(255, 0, 0));     // 红色(高能量)colorMap->setGradient(gradient);// 4. 设置坐标轴标签customPlot->xAxis->setLabel("Time (s)");customPlot->yAxis->setLabel("Frequency (Hz)");// 5. 刷新显示customPlot->rescaleAxes();customPlot->replot();
}

步骤 3:实时更新瀑布图(如频谱分析)

// 每次获取新数据时调用
void updateWaterfall(QCustomPlot *customPlot, const QVector<double> &newSpectrum)
{QCPColorMap *colorMap = qobject_cast<QCPColorMap*>(customPlot->plottable(0));if (!colorMap) return;int timePoints = colorMap->data()->keySize();int freqPoints = colorMap->data()->valueSize();// 数据滚动:旧数据前移,新数据插入最后一行for (int t = 0; t < timePoints - 1; ++t) {for (int f = 0; f < freqPoints; ++f) {double val = colorMap->data()->cell(t+1, f);colorMap->data()->setCell(t, f, val);}}// 插入新频谱数据for (int f = 0; f < freqPoints; ++f) {colorMap->data()->setCell(timePoints-1, f, newSpectrum[f]);}customPlot->replot();
}

效果预览
你的瀑布图将显示为:
频率 (Hz)
^
| 红色(高能量)
| 绿色(中能量)
| 蓝色(低能量)
±---------------> 时间 (s)
X轴:时间(或帧号)
Y轴:频率(或通道)
颜色:能量强度(通过 QCPColorGradient 自定义)
🧩 二、为什么不用 Qt Charts?
Qt Charts 的 QLineSeries + QAreaSeries 可以模拟瀑布图,但:

❌ 无原生支持 → 需手动绘制数百条曲线
❌ 性能差 → 大数据量卡顿
❌ 颜色映射难 → 需为每条曲线单独设色
❌ 无滚动优化 → 实时更新效率低
Qt Charts 适合:柱状图、折线图、饼图等基础图表,不适合科学/工程级瀑布图。

三、高级优化技巧(QCustomPlot)

  1. 提升性能(大数据量)
    colorMap->setInterpolate(false); // 关闭插值,提升速度
    colorMap->setDataRange(QCPRange(0, 100)); // 固定数据范围,避免重计算
  2. 添加颜色标尺(Legend)
    cpp
    QCPColorScale *colorScale = new QCPColorScale(customPlot);
    customPlot->plotLayout()->addElement(0, 1, colorScale); // 右侧添加标尺
    colorMap->setColorScale(colorScale);
    colorScale->axis()->setLabel(“Amplitude (dB)”);
  3. 支持鼠标交互(缩放/拖拽)
    cpp
    customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
    四、行业应用案例
    音频分析:实时频谱瀑布图(如 Audacity)
    通信系统:信道频率响应随时间变化
    雷达/声呐:目标回波强度-距离-时间图
    医疗设备:脑电图(EEG)时频分析
    ✅ 五、总结:最科学方案
    Qt 瀑布图 = QCustomPlot + QCPColorMap + 数据滚动更新

优势:
✅ 10行代码快速出图
✅ 支持实时大数据(>10万点)
✅ 原生颜色映射 + 标尺
✅ 开源免费(GPL/商业许可可选)
✅ 活跃社区(GitHub 5k+ stars)
学习资源:
官网:https://www.qcustomplot.com/
GitHub:https://github.com/DerManu/QCustomPlot
示例:下载包中的 colorMapDemo 和 realtimeDataDemo

http://www.dtcms.com/a/377408.html

相关文章:

  • 【Leetcode hot 100】94.二叉树的中序遍历
  • 渗透测试真的能发现系统漏洞吗
  • 【芯片设计-信号完整性 SI 学习 1.2 -- loopback 回环测试】
  • Android App瘦身方法介绍
  • MySQL修改字段类型避坑指南:如何应对数据截断与转换错误?
  • Linux权限以及常用热键集合
  • 成品油加油站综合监管迈入 “云时代”!智慧物联网涉税数据采集平台推行工作全面推进
  • c primer plus 第五章复习题和练习题
  • C++设计模式,高级开发,算法原理实战,系统设计与实战(视频教程)
  • Spring 统一功能处理
  • ES6基础入门教程(80问答)
  • 第3讲 机器学习入门指南
  • InnoDB 逻辑存储结构:好似 “小区管理” 得层级结构
  • copyparty 是一款使用单个 Python 文件实现的内网文件共享工具,具有跨平台、低资源占用等特点,适合需要本地化文件管理的场景
  • C# 哈希查找算法实操
  • 一个C#开发的Windows驱动程序管理工具!
  • 环境变量
  • Codeforces Round 1049 (Div. 2)
  • Eclipse下载安装图文教程(非常详细,适合新手)
  • vue2迁移到vite[保姆级教程]
  • 基于webpack的场景解决
  • Vite 中的 import.meta.env 与通用 process.env.NODE_ENV 的区别与最佳实践
  • 除了Webpack,还有哪些构建工具可以实现不同环境使用不同API地址?
  • sklearn聚类
  • I.MX6UL:汇编LED驱动实验
  • 计算机毕设 java 高校机房综合管控系统 基于 SSM+Vue 的高校机房管理平台 Java+MySQL 的设备与预约全流程系统
  • 设计模式-建造者观察者抽象工厂状态
  • 第5讲 机器学习生态构成
  • JAVA秋招面经
  • LVS群集