QCustomPlot 瀑布图色度条

1、单独应用在一个QWidget中。

通过新建一个瀑布图后移除瀑布图即可只留住色度条。

cpp 复制代码
    QCustomPlot *customPlot = ui->widget;
    QCPColorScale *colorScale = new QCPColorScale(customPlot);
    customPlot->plotLayout()->addElement(0, 1, colorScale); // add it to the right of the main axis rect
    colorScale->setType(QCPAxis::atRight); // scale shall be vertical bar with tick/axis labels right (actually atRight is already the default)
//    colorScale->axis()->setLabel("Magnetic Field Strength");

    colorScale->setGradient(QCPColorGradient::gpJet);
    colorScale->setDataRange(QCPRange(0,1));

    customPlot->plotLayout()->remove(customPlot->axisRect());
    customPlot->replot();

运行后

2、将瀑布图色度条移动至瀑布图内。

先上代码

cpp 复制代码
if(nullptr == m_pColorScale)
    {
        m_pColorScale = new QCPColorScale(fp3);
        m_pColorScale->setDataRange(QCPRange(0,50));//百分比
        m_pColorScale->setType(QCPAxis::atBottom);
//        m_pColorScale->axis()->setOffset(-18);


      
        m_pColorScale->axis()->setVisible(true);
        m_pColorScale->axis()->setTickLength(5,0);
        m_pColorScale->axis()->setSubTickLength(0,0);
  

        QFont font;
        font.setFamily(QStringLiteral("黑体"));
        font.setBold(true);
        m_pColorScale->axis()->setTickLabelFont(font);

        m_pColorScale->axis()->setNumberFormat("gbc");//g灵活的格式,b漂亮的指数形式,c乘号改成×
        m_pColorScale->axis()->setNumberPrecision(2);//精度1
        m_pColorScale->axis()->ticker()->setTickCount(4);
        m_pColorScale->axis()->ticker()->setTickStepStrategy(QCPAxisTicker::tssMeetTickCount);

        m_pColorScale->setRangeDrag(true);
        m_pColorScale->setRangeZoom(true);

        fp3->axisRect()->insetLayout()->addElement(m_pColorScale,QRectF(0,0.95,1,1));
        m_pColorMap->setColorScale(m_pColorScale);
      
    }

重点是最后这一句

cpp 复制代码
fp3->axisRect()->insetLayout()->addElement(m_pColorScale,QRectF(0,0.95,1,1));

这句话的作用是将色度条放入瀑布图框中。效果图如图。

原理解析:

首先应该参考我的另一篇文章介绍了QCustomPlot 的刷新原理。

1、色度条的深度理解

可以把它看成一个QCPAxisRect (矩形框)和一张底色图的结合体。官方采用的是QCPColorScaleAxisRectPrivate这个类,通过调用这个类的draw 函数进行刷新底色值及矩形框。色度条上的数值可以看成矩形框的x轴数值,色度条的边框可以看成是矩形框的坐标轴。通过下列代码可以获取到坐标轴,开发者就是这么设计的。

cpp 复制代码
m_pColorScale->axis()->axisRect()->axis(QCPAxis::atRight)->setBasePen(QPen(Qt::white, 2));
        m_pColorScale->axis()->axisRect()->axis(QCPAxis::atLeft)->setBasePen(QPen(Qt::white, 2));
        m_pColorScale->axis()->axisRect()->axis(QCPAxis::atTop)->setBasePen(QPen(Qt::white, 2));

2、矩形框的深度理解(QCPAxisRect)

矩形框中包含一个QCPLegend,这个legend 是怎么画到矩形框中的呢,通过查看源码

cpp 复制代码
 legend = new QCPLegend;
  legend->setVisible(false);
  defaultAxisRect->insetLayout()->addElement(legend, Qt::AlignRight|Qt::AlignTop);
  defaultAxisRect->insetLayout()->setMargins(QMargins(12, 12, 12, 12));

是通过一个insetLayout 添加上去的,QCustomer中的元素都是在布局中存在的,为了放置一个legend,一个QCPAxisRect默认带一个QCPLayoutInset,这个布局默认是为了放置图例,这里我们可以在这个布局上添加一个色度条。

相关推荐
苏克贝塔1 小时前
Qt 图形视图框架3-事件处理与传播
c++·qt
轩情吖1 小时前
Qt的信号与槽(二)
数据库·c++·qt·信号·connect·信号槽·
胖大和尚1 小时前
C++项目学习计划
开发语言·c++·学习
GiraKoo3 小时前
【GiraKoo】 C++20的新特性
c++
无聊的小坏坏3 小时前
力扣 239 题:滑动窗口最大值的两种高效解法
c++·算法·leetcode
黎明smaly3 小时前
【排序】插入排序
c语言·开发语言·数据结构·c++·算法·排序算法
CCF_NOI.3 小时前
(普及−)B3629 吃冰棍——二分/模拟
数据结构·c++·算法
byxdaz4 小时前
QT并发机制
qt
眠りたいです5 小时前
Mysql常用内置函数,复合查询及内外连接
linux·数据库·c++·mysql
雨落倾城夏未凉5 小时前
7.QObject定时器和QTimer定时器的区别
后端·qt