From 4967df66288990b1fd054bc63bc4b3cfb6e2bd5e Mon Sep 17 00:00:00 2001 From: duanshengchao <519970194@qq.com> Date: Wed, 3 Sep 2025 16:27:22 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E7=9B=B4=E6=96=B9=E5=9B=BE=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E6=95=B0=E6=8D=AE=E5=AE=9E=E6=97=B6=E5=B1=95=E7=A4=BA?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dataPanel/dpBarsChart.cpp | 99 ++++++++++++++++++++++++++++++++++++++- dataPanel/dpBarsChart.h | 28 +++++++++-- dataPanel/dpLineChart.h | 3 -- 3 files changed, 123 insertions(+), 7 deletions(-) diff --git a/dataPanel/dpBarsChart.cpp b/dataPanel/dpBarsChart.cpp index 999849a..bb68a43 100644 --- a/dataPanel/dpBarsChart.cpp +++ b/dataPanel/dpBarsChart.cpp @@ -1,6 +1,103 @@ #include "dpBarsChart.h" #include "dataManager.h" +CustomBars::CustomBars(QCPAxis* keyAxis, QCPAxis* valueAxis) + : QCPBars(keyAxis, valueAxis), + m_textAlignment(Qt::AlignCenter), + m_spacing(5), + m_font(QFont(QLatin1String("sans serif"), 12)) +{} + +void CustomBars::setTextAlignment(Qt::Alignment alignment) +{ + m_textAlignment = alignment; +} + +void CustomBars::setSpacing(double spacing) +{ + m_spacing = spacing; +} + +void CustomBars::setFont(QFont font) +{ + m_font = font; +} + +void CustomBars::draw(QCPPainter* painter) +{ + ///源码拷贝 + if (!mKeyAxis || !mValueAxis) { qDebug() << Q_FUNC_INFO << "invalid key or value axis"; return; } + if (mDataContainer->isEmpty()) return; + + QCPBarsDataContainer::const_iterator visibleBegin, visibleEnd; + getVisibleDataBounds(visibleBegin, visibleEnd); + + // loop over and draw segments of unselected/selected data: + QList selectedSegments, unselectedSegments, allSegments; + getDataSegments(selectedSegments, unselectedSegments); + allSegments << unselectedSegments << selectedSegments; + for (int i=0; i= unselectedSegments.size(); + QCPBarsDataContainer::const_iterator begin = visibleBegin; + QCPBarsDataContainer::const_iterator end = visibleEnd; + mDataContainer->limitIteratorsToDataRange(begin, end, allSegments.at(i)); + if (begin == end) + continue; + + for (QCPBarsDataContainer::const_iterator it=begin; it!=end; ++it) + { + // check data validity if flag set: +#ifdef QCUSTOMPLOT_CHECK_DATA + if (QCP::isInvalidData(it->key, it->value)) + qDebug() << Q_FUNC_INFO << "Data point at" << it->key << "of drawn range invalid." << "Plottable name:" << name(); +#endif + // draw bar: + if (isSelectedSegment && mSelectionDecorator) + { + mSelectionDecorator->applyBrush(painter); + mSelectionDecorator->applyPen(painter); + } else + { + painter->setBrush(mBrush); + painter->setPen(mPen); + } + applyDefaultAntialiasingHint(painter); + painter->drawPolygon(getBarRect(it->key, it->value)); + + ///---自定义逻辑-start--- + //draw text + QRectF barRect = getBarRect(it->key, it->value); + painter->setFont(m_font); + QString text = QString::number(it->value, 'g', 2); //获取当前value值,保留两位精度 + QRectF textRect = painter->fontMetrics().boundingRect(0, 0, 0, 0, Qt::TextDontClip | m_textAlignment, text); //计算文字占用大小 + if(mKeyAxis.data()->orientation() == Qt::Horizontal) //水平轴为keyAxis + { + if(mKeyAxis.data()->axisType() == QCPAxis::atTop) //上轴,文字放到柱图下方 + textRect.moveTopLeft(barRect.bottomLeft() + QPointF(0, m_spacing)); + else //下轴,文字放到柱图上方 + textRect.moveBottomLeft(barRect.topLeft() - QPointF(0, m_spacing)); + textRect.setWidth(barRect.width()); + } + else //垂直轴为keyAxis + { + if(mKeyAxis.data()->axisType() == QCPAxis::atLeft) //左轴,文字放到柱图右侧 + textRect.moveTopLeft(barRect.topRight() + QPointF(m_spacing, 0)); + else //右轴,文字放到柱图左侧 + textRect.moveTopRight(barRect.topLeft() - QPointF(m_spacing, 0)); + textRect.setWidth(barRect.height()); + } + painter->drawText(textRect, Qt::TextDontClip | m_textAlignment, text); + ///---自定义逻辑-end--- + } + } + + ///源码拷贝 + // draw other selection decoration that isn't just line/scatter pens and brushes: + if (mSelectionDecorator) + mSelectionDecorator->drawDecoration(painter, selection()); +} + dpBarsChart::dpBarsChart(QWidget* parent) :dpBaseChart(parent) { @@ -139,7 +236,7 @@ void dpBarsChart::synchronizeConfigData(const configurationResults& cfg) bars.dataID = dataID; bars.dataType = dataType; bars.color = colorData.value(); - QCPBars* qBars = new QCPBars(keyAxis, valueAxis); + CustomBars* qBars = new CustomBars(keyAxis, valueAxis); qBars->setName(bars.name); qBars->setPen(bars.color); //边框颜色 qBars->setBrush(bars.color); //填充颜色 diff --git a/dataPanel/dpBarsChart.h b/dataPanel/dpBarsChart.h index c85e8bf..defcd30 100644 --- a/dataPanel/dpBarsChart.h +++ b/dataPanel/dpBarsChart.h @@ -8,12 +8,34 @@ ** 柱状图展示面板,用来展示统计数据(如各类报警、事件的数量) ** 采用QCustomPlot实现 ** +** CustomBars +** 自定义Bars,继承自QCPBars,可以展示实时数据等信息 +** ******************************************************************************/ #include "dpBaseChart.h" -class QCustomPlot; -class QCPBarsGroup; +class CustomBars : public QCPBars +{ +public: + explicit CustomBars(QCPAxis* keyAxis, QCPAxis* valueAxis); + + Qt::Alignment textAlignment() { return m_textAlignment; } + double spacing() { return m_spacing; } + QFont font() { return m_font; } + + void setTextAlignment(Qt::Alignment); + void setSpacing(double); + void setFont(QFont); + +protected: + virtual void draw(QCPPainter* painter) override; + +private: + Qt::Alignment m_textAlignment; //文字对齐方式 + double m_spacing; //文字与柱状图之间的间距,单位为像素 + QFont m_font; //文字的字体 +}; class dpBarsChart : public dpBaseChart { @@ -40,7 +62,7 @@ private: QString dataID; RealTimeDataType dataType; QVector keys; - QPointer qBars; + QPointer qBars; Bars() { diff --git a/dataPanel/dpLineChart.h b/dataPanel/dpLineChart.h index 9cbfff5..d9f9abd 100644 --- a/dataPanel/dpLineChart.h +++ b/dataPanel/dpLineChart.h @@ -12,9 +12,6 @@ #include "dpBaseChart.h" -class QCustomPlot; -class QCPRange; - class dpLineChart : public dpBaseChart { Q_OBJECT