From a39e745617a554e7344ca90a049084ad5105709f Mon Sep 17 00:00:00 2001 From: duanshengchao <519970194@qq.com> Date: Wed, 13 Aug 2025 17:54:09 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=E6=9B=B4=E6=94=B9QCustomPlot=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=E6=BA=90=E7=A0=81=EF=BC=8C=E4=BD=BFY=E8=BD=B4?= =?UTF-8?q?=E5=9C=A8=E5=B7=A6=E5=8F=B3=E4=B8=8D=E5=90=8C=E8=BE=B9=E6=97=B6?= =?UTF-8?q?=E7=9A=84label=E6=98=BE=E7=A4=BA=E6=96=B9=E5=90=91=E4=B8=80?= =?UTF-8?q?=E8=87=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dataPanel/dpBaseChart.h | 43 +++++++++++++++++++++++--------------- util/Chart/qcustomplot.cpp | 19 +++++++++++++++-- 2 files changed, 43 insertions(+), 19 deletions(-) diff --git a/dataPanel/dpBaseChart.h b/dataPanel/dpBaseChart.h index 1f79f36..6c65284 100644 --- a/dataPanel/dpBaseChart.h +++ b/dataPanel/dpBaseChart.h @@ -50,16 +50,23 @@ protected: bIsDefaultAxis = false; } + ~Axis() + { + //执行plot->axisRect()->removeAxis(axis)时,axis会delete掉 + /*if(qAxis && !bIsDefaultAxis) //默认坐标轴由所属plot管理 + { + delete qAxis; + qAxis = nullptr; + }*/ + } + void setQCPAxis(QCPAxis* axis, bool isDefualtAxis) { qAxis = axis; bIsDefaultAxis = isDefualtAxis; if(qAxis) { - if(_cfg.unit.isEmpty()) - qAxis->setLabel(_cfg.name); - else - qAxis->setLabel(QString("%1(%2)").arg(_cfg.name,_cfg.unit)); + setAxisLabel(); qAxis->setSubTicks(false); //颜色 qAxis->setBasePen(style.axisColor); @@ -81,13 +88,7 @@ protected: _cfg.unit = cfg.unit; _cfg.dataType = cfg.dataType; - if(qAxis) //更新轴的名称 - { - if(_cfg.unit.isEmpty()) - qAxis->setLabel(_cfg.name); - else - qAxis->setLabel(QString("%1(%2)").arg(_cfg.name,_cfg.unit)); - } + setAxisLabel(); //更新轴的名称 } void setStyle(const ChartStyle chartStyle) @@ -95,15 +96,23 @@ protected: style = chartStyle; } - ~Axis() + private: + void setAxisLabel() { - //执行plot->axisRect()->removeAxis(axis)时,axis会delete掉 - /*if(qAxis && !bIsDefaultAxis) //默认坐标轴由所属plot管理 + if(qAxis) { - delete qAxis; - qAxis = nullptr; - }*/ + QString labelText = ""; + if(_cfg.unit.isEmpty()) + labelText = _cfg.name; + else + labelText = QString("%1(%2)").arg(_cfg.name,_cfg.unit); + //因为QCPAxis左右不同位置时,lable的方向不一致,且没有提供设置方法,在这里通过字符串反转的方式来矫正 + // if(qAxis->axisType() == QCPAxis::atLeft) + // std::reverse(labelText.begin(), labelText.end()); + qAxis->setLabel(labelText); + } } + }; struct Graph diff --git a/util/Chart/qcustomplot.cpp b/util/Chart/qcustomplot.cpp index 72b5bfb..3d6e670 100644 --- a/util/Chart/qcustomplot.cpp +++ b/util/Chart/qcustomplot.cpp @@ -9949,6 +9949,13 @@ void QCPAxisPainterPrivate::draw(QCPPainter *painter) case QCPAxis::atBottom: origin = axisRect.bottomLeft() +QPoint(0, +offset); break; } + /*if(type == QCPAxis::atRight) //dsc add + { + painter->setPen(QPen(QColor(Qt::red))); + //painter->drawRect(axisRect); + painter->drawPoint(origin); + }*/ + double xCor = 0, yCor = 0; // paint system correction, for pixel exact matches (affects baselines and ticks of top/right axes) switch (type) { @@ -10050,16 +10057,24 @@ void QCPAxisPainterPrivate::draw(QCPPainter *painter) if (type == QCPAxis::atLeft) { QTransform oldTransform = painter->transform(); + //原始代码 painter->translate((origin.x()-margin-labelBounds.height()), origin.y()); painter->rotate(-90); + //dsc更改 + //painter->translate((origin.x()-margin), 0); + //painter->rotate(90); painter->drawText(0, 0, axisRect.height(), labelBounds.height(), Qt::TextDontClip | Qt::AlignCenter, label); painter->setTransform(oldTransform); } else if (type == QCPAxis::atRight) { QTransform oldTransform = painter->transform(); - painter->translate((origin.x()+margin+labelBounds.height()), origin.y()-axisRect.height()); - painter->rotate(90); + //原始代码 + //painter->translate((origin.x()+margin+labelBounds.height()), origin.y()-axisRect.height()); + //painter->rotate(90); + //dsc更改 + painter->translate((origin.x()+margin), origin.y()); + painter->rotate(-90); painter->drawText(0, 0, axisRect.height(), labelBounds.height(), Qt::TextDontClip | Qt::AlignCenter, label); painter->setTransform(oldTransform); }