qcustomplot 高级轴的使用-添加多个矩形图形

1、解决y轴标签对其问题。

2、布局多个矩形axisRect。

3、轴联动。

#ifndef DATDEFINE_H
#define DATDEFINE_H
enum MInteraction {
    mNone,    //空模式
    mDefault, //默认模式
    mSelect   //选择模式
};
#endif // DATDEFINE_H
//*.h
#ifndef MPLOT_H
#define MPLOT_H

#include "qcustomplot.h"
#include "datdefine.h"

class QCPMarginGroup;

class MPlot : public QCustomPlot
{
    Q_OBJECT
public:
    explicit MPlot(QWidget *parent = nullptr);
    ~MPlot();

    /**
     * @brief setMInteractionType 设置用户与图交互类型
     * @param iType     mNone->空模式, mDefault->默认模式, mSelect->选择模式
     */
    void setMInteractionType(MInteraction iType);

    /**
     * @brief addAxisRect  高级轴矩形添加
     * @param strY         y轴名称
     * @param strX         x轴名称
     * @param strTitile    矩形标题
     * @param nIndex       矩形序号
     * @return             当前添加矩形
     */
    QCPAxisRect *addAxisRect(Qt::Orientation ori = Qt::Vertical, QString &strY = QString("s"), QString &strX = QString("t"), QString &strTitile = QString("dis"));
signals:

public slots:
    

private:
  /**
   * @brief initPlotProperty
   */
  void initPlotProperty();

  /**
   * @brief clearDefaultRect 创建高级轴,必须清空默认矩形轴
   */
  void clearDefaultRect();

  /**
   * @brief insertLayout 布局策略
   * @param rect
   * @param row
   * @param col
   * @param ori
   */
  void addLayoutElement(QCPAxisRect*rect, int row, int col);

  /**
   * @brief getRowCol 行列生成
   * @param row
   * @param col
   * @param ori
   */
  void getRowCol(int &row, int &col, Qt::Orientation ori);

//  void initRectTile(QCPAxisRect* rect, int row, int col, QString &strTile);
  void initAxis(QCPAxis *xPAxis, QCPAxis*yPAxis, QString &strY, QString &strX);

private:
  QCPMarginGroup *marginGroup;
};



#endif // MPLOT_H

//*.cpp
#include "mplot.h"
#include <QMenu>
#include <QDebug>

MPlot::MPlot(QWidget *parent)
    : QCustomPlot(parent)
    , marginGroup(NULL)
{
    initPlotProperty();
     clearDefaultRect();
}

MPlot::~MPlot()
{
    qDebug() << __FUNCTION__;
}

void MPlot::setMInteractionType(MInteraction iType)
{
    QCP::Interactions interaction;
    switch (iType) {
    case mNone:
        interaction |= QCP::iNone;
        break;
    case mDefault: //默认模式:可拖拽、可选择、缩放(滚轮缩放、右键拉框缩放),右键取消选择
        interaction |= QCP::iRangeZoom | QCP::iSelectPlottables | QCP::iRangeDrag;
        break;
    case mSelect: //选择模式:不可拖拽、可选中(支持多选)、右键拉框缩放、右键取消选中
        interaction |= QCP::iRangeZoom | QCP::iSelectPlottables | QCP::iMultiSelect;
        break;
    default:
        break;
    }
    this->setInteractions(interaction);
}

QCPAxisRect *MPlot::addAxisRect(Qt::Orientation ori, QString &strY, QString &strX, QString &strTitile)
{
     QCPAxisRect *rect = NULL;
     if(!rect)
     {
         rect = new QCPAxisRect(this);
         //初始化标题
         //初始化轴
         int row, col;
         getRowCol(row, col, ori);
         qDebug() << "row" << row << "col" << col;
//         initRectTile(rect, row, col, strTitile);
         initAxis(rect->axis(QCPAxis::atLeft), rect->axis(QCPAxis::atBottom), strY, strX);
         addLayoutElement(rect, row, col);
         rect->setMarginGroup(QCP::msLeft, marginGroup);
         // move newly created axes on "axes" layer and grids on "grid" layer:
         foreach (QCPAxisRect *rect, this->axisRects())
         {
             foreach (QCPAxis *axis, rect->axes())
             {
                 axis->setLayer("axes");
                 axis->grid()->setLayer("grid");
             }
         }
     }


    return rect;
}

void MPlot::initPlotProperty()
{
    QMargins margins(15,5,5,5);
    this->setContentsMargins(margins);
    //与图表交互:默认为缩放、拖拽、图形可选择
    setMInteractionType(mDefault);
    marginGroup = new QCPMarginGroup(this);
}

void MPlot::clearDefaultRect()
{
    // 布局高级轴,先从布局管理器中清空默认矩形轴
    this->plotLayout()->clear();
}

void MPlot::addLayoutElement(QCPAxisRect *rect, int row, int col)
{
    if(rect)
    {
        
        this->plotLayout()->addElement(row, col, rect);
        this->plotLayout()->simplify();//去掉空白部分
    }
}

void MPlot::getRowCol(int &row, int &col, Qt::Orientation ori)
{
    static int i = 0, j = 0;
    if(Qt::Horizontal == ori)
    {
        //生成行
       row = 0;
       col = i;
       ++i;
    }
    else if(Qt::Vertical == ori)
    {
       //生成列
        row = j;
        col = 0;
        ++j;
    }else
    {
    }
}

void MPlot::initRectTile(QCPAxisRect *rect,int row, int col, QString &strTile)
{
    // set title of plot:
    this->plotLayout()->insertRow(0);
    this->plotLayout()->addElement(0, 0, new QCPTextElement(rect->parentPlot(), strTile));
}

void MPlot::initAxis(QCPAxis *xPAxis, QCPAxis *yPAxis, QString& strY, QString &strX)
{
    if(xPAxis && yPAxis)
    {
        xPAxis->setLabel(strY);
        yPAxis->setLabel(strX);
        //设置0刻度线
        xPAxis->grid()->setZeroLinePen(Qt::NoPen);
        yPAxis->grid()->setZeroLinePen(Qt::NoPen);
    }
}


个坐标轴。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值