瀑布图
配置qcustomplot的例子网上有很多了,记录下通过qcustomplot实现的功率谱和瀑布图代码:
void WaveDisplay::plotWaterfall(MCustomPlot* p_imag)
{
mCustomPlotLs = p_imag;
mCustomPlotLs->plotLayout()->clear(); // clear default axis rect so we can start from scratch
mCustomPlotLs->showTracer(true);
QCPLayoutGrid *subLayoutF = new QCPLayoutGrid;
QCPLayoutGrid *subLayoutL = new QCPLayoutGrid;
mCustomPlotLs->setInteractions(QCP::iRangeDrag //可平移
| QCP::iRangeZoom //可滚轮缩放
| QCP::iSelectPlottables //可选中曲线
| QCP::iSelectLegend ); //可选中图例
wideAxisRectF = new QCPAxisRect(mCustomPlotLs);
wideAxisRectL = new QCPAxisRect(mCustomPlotLs);
mCustomPlotLs->plotLayout()->addElement(1, 0, subLayoutF); // insert axis rect in first row
mCustomPlotLs->plotLayout()->addElement(0, 0, subLayoutL); // sub layout in second row (grid layout will grow accordingly)
QCPMarginGroup *marginGroup = new QCPMarginGroup(mCustomPlotLs);
wideAxisRectF->setMarginGroup(QCP::msBottom, marginGroup);
wideAxisRectL->setMarginGroup(QCP::msBottom, marginGroup);
subLayoutF->addElement(0, 0, wideAxisRectF);
wideAxisRectF->setupFullAxesBox(true);
wideAxisRectF->axis(QCPAxis::atLeft)->setRange(-130, 0); // 设置轴上限和下限
wideAxisRectF->axis(QCPAxis::atBottom)->setRange(0, 50);
wideAxisRectF->axis(QCPAxis::atBottom)->ticker()->setTickCount(10);//10个主刻度
wideAxisRectF->axis(QCPAxis::atBottom)->setLabel("频率(KHz)");
wideAxisRectF->axis(QCPAxis::atLeft)->setLabel("dB");
wideAxisRectF->axis(QCPAxis::atLeft)->setPadding(20);
wideAxisRectF->axis(QCPAxis::atRight)->setTickLabels(true);
wideAxisRectF->axis(QCPAxis::atRight)->setPadding(30);
subLayoutL->addElement(0, 0, wideAxisRectL);
wideAxisRectL->setupFullAxesBox(true);
wideAxisRectL->axis(QCPAxis::atLeft)->setRange(0, 100);
wideAxisRectL->axis(QCPAxis::atLeft)->setSubTicks(false);
wideAxisRectL->axis(QCPAxis::atLeft)->setTicks(false);
wideAxisRectL->axis(QCPAxis::atBottom)->setRange(0, 50);
wideAxisRectL->axis(QCPAxis::atBottom)->ticker()->setTickCount(10);//10个主刻度
wideAxisRectL->axis(QCPAxis::atBottom)->setLabel("频率(KHz)");
wideAxisRectL->setRangeZoomFactor(0.8,1); // x轴设置缩放比例,y轴缩放比例为1,则不进行缩放
// x轴以时间形式显示
wideAxisRectL->addAxis(QCPAxis::atLeft)->setTickLabelColor(QColor(255,255,255)); // 左边添加一个纵轴
QSharedPointer<QCPAxisTickerTime> timeTicker(new QCPAxisTickerTime);
timeTicker->setTimeFormat("%h:%m:%s");
wideAxisRectL->axis(QCPAxis::atLeft, 1)->setTicker(timeTicker);
connect(wideAxisRectF->axis(QCPAxis::atBottom), SIGNAL(rangeChanged(QCPRange)), wideAxisRectL->axis(QCPAxis::atBottom), SLOT(setRange(QCPRange)));
connect(wideAxisRectL->axis(QCPAxis::atBottom), SIGNAL(rangeChanged(QCPRange)), wideAxisRectF->axis(QCPAxis::atBottom), SLOT(setRange(QCPRange)));
// 功率谱
mainGraphPower = mCustomPlotLs->addGraph(wideAxisRectF->axis(QCPAxis::atBottom), wideAxisRectF->axis(QCPAxis::atLeft)); // 添加图形
mainGraphPower->rescaleKeyAxis(); // 自动调整轴范围以展示全部数据
QPen pen;
pen.setWidth(1);//线宽
pen.setStyle(Qt::PenStyle::SolidLine);//虚线 solidline、dashline、dotline、dashdotline、dashdotdotline
pen.setColor(QColor(255, 253, 85, 255));
mainGraphPower->setPen(pen);
mainGraphPower->setSmooth(true); // 开启平滑曲线
// 瀑布图
mainGraphTime = mCustomPlotLs->addGraph(wideAxisRectL->axis(QCPAxis::atBottom), wideAxisRectL->axis(QCPAxis::atLeft, 1));
mainGraphTime->rescaleKeyAxis();
colorMapPower = new QCPColorMap(wideAxisRectL->axis(QCPAxis::atBottom), wideAxisRectL->axis(QCPAxis::atLeft));
int nx = 100;
int ny = 100;
colorMapPower->data()->setSize(nx, ny); // we want the color map to have nx * ny data points
colorMapPower->data()->setRange(QCPRange(0, 100), QCPRange(0, 100));
QCPColorScale *colorScale = new QCPColorScale(mCustomPlotLs); // 添加色条
subLayoutL->addElement(0, 1, colorScale);
colorScale->axis()->setTicks(false);
colorScale->axis()->setTickLabels(false);
colorScale->setType(QCPAxis::atRight); // 刻度应为垂直条,刻度线/坐标轴标签右侧(实际上,右侧已经是默认值)
colorMapPower->setColorScale(colorScale); // 将颜色图与色标关联
colorScale->axis()->setLabel("信号强度");
colorScale->axis()->setLabelColor(Qt::white);
QCPColorGradient gradient; // 色条使用的颜色渐变
gradient.setColorStopAt(0.0, QColor(246,239,166)); // 设置色条开始时的颜色 QColor(246,239,166)
gradient.setColorStopAt(1.0, QColor(191,68,76)); // 设置色条结束时的颜色
colorMapPower->setGradient(QCPColorGradient::gpJet); // QCPColorGradient::gpJet QCPColorGradient::gpPolar
colorMapPower->rescaleDataRange();
mCustomPlotLs->replot(QCustomPlot::rpQueuedReplot); // 异步重绘
}