refactor:更改QCustomPlot相关源码,使Y轴在左右不同边时的label显示方向一致

This commit is contained in:
duanshengchao 2025-08-13 17:54:09 +08:00
parent 2b10e7e9ef
commit a39e745617
2 changed files with 43 additions and 19 deletions

View File

@ -50,16 +50,23 @@ protected:
bIsDefaultAxis = false; bIsDefaultAxis = false;
} }
~Axis()
{
//执行plot->axisRect()->removeAxis(axis)时axis会delete掉
/*if(qAxis && !bIsDefaultAxis) //默认坐标轴由所属plot管理
{
delete qAxis;
qAxis = nullptr;
}*/
}
void setQCPAxis(QCPAxis* axis, bool isDefualtAxis) void setQCPAxis(QCPAxis* axis, bool isDefualtAxis)
{ {
qAxis = axis; qAxis = axis;
bIsDefaultAxis = isDefualtAxis; bIsDefaultAxis = isDefualtAxis;
if(qAxis) if(qAxis)
{ {
if(_cfg.unit.isEmpty()) setAxisLabel();
qAxis->setLabel(_cfg.name);
else
qAxis->setLabel(QString("%1(%2)").arg(_cfg.name,_cfg.unit));
qAxis->setSubTicks(false); qAxis->setSubTicks(false);
//颜色 //颜色
qAxis->setBasePen(style.axisColor); qAxis->setBasePen(style.axisColor);
@ -81,13 +88,7 @@ protected:
_cfg.unit = cfg.unit; _cfg.unit = cfg.unit;
_cfg.dataType = cfg.dataType; _cfg.dataType = cfg.dataType;
if(qAxis) //更新轴的名称 setAxisLabel(); //更新轴的名称
{
if(_cfg.unit.isEmpty())
qAxis->setLabel(_cfg.name);
else
qAxis->setLabel(QString("%1(%2)").arg(_cfg.name,_cfg.unit));
}
} }
void setStyle(const ChartStyle chartStyle) void setStyle(const ChartStyle chartStyle)
@ -95,15 +96,23 @@ protected:
style = chartStyle; style = chartStyle;
} }
~Axis() private:
void setAxisLabel()
{ {
//执行plot->axisRect()->removeAxis(axis)时axis会delete掉 if(qAxis)
/*if(qAxis && !bIsDefaultAxis) //默认坐标轴由所属plot管理
{ {
delete qAxis; QString labelText = "";
qAxis = nullptr; 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 struct Graph

View File

@ -9949,6 +9949,13 @@ void QCPAxisPainterPrivate::draw(QCPPainter *painter)
case QCPAxis::atBottom: origin = axisRect.bottomLeft() +QPoint(0, +offset); break; 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) double xCor = 0, yCor = 0; // paint system correction, for pixel exact matches (affects baselines and ticks of top/right axes)
switch (type) switch (type)
{ {
@ -10050,16 +10057,24 @@ void QCPAxisPainterPrivate::draw(QCPPainter *painter)
if (type == QCPAxis::atLeft) if (type == QCPAxis::atLeft)
{ {
QTransform oldTransform = painter->transform(); QTransform oldTransform = painter->transform();
//原始代码
painter->translate((origin.x()-margin-labelBounds.height()), origin.y()); painter->translate((origin.x()-margin-labelBounds.height()), origin.y());
painter->rotate(-90); 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->drawText(0, 0, axisRect.height(), labelBounds.height(), Qt::TextDontClip | Qt::AlignCenter, label);
painter->setTransform(oldTransform); painter->setTransform(oldTransform);
} }
else if (type == QCPAxis::atRight) else if (type == QCPAxis::atRight)
{ {
QTransform oldTransform = painter->transform(); 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->drawText(0, 0, axisRect.height(), labelBounds.height(), Qt::TextDontClip | Qt::AlignCenter, label);
painter->setTransform(oldTransform); painter->setTransform(oldTransform);
} }