From 4c6d184f0fcafbac753f5ad85f9a260550bfbd45 Mon Sep 17 00:00:00 2001 From: duanshengchao <519970194@qq.com> Date: Wed, 9 Jul 2025 10:58:52 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=E8=B0=83=E6=95=B4chart=E6=A0=B7?= =?UTF-8?q?=E5=BC=8F=E9=85=8D=E7=BD=AE=E7=9A=84=E7=9B=B8=E5=85=B3=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dataPanel/dpBaseWidget.h | 12 +++++++ dataPanel/dpConfigurationDialog.cpp | 1 - dataPanel/dpLineChart.cpp | 54 +++++++++++++++++------------ dataPanel/dpLineChart.h | 1 + source/dataPanel.cpp | 3 ++ 5 files changed, 47 insertions(+), 24 deletions(-) diff --git a/dataPanel/dpBaseWidget.h b/dataPanel/dpBaseWidget.h index b8ee00a..dad2b24 100644 --- a/dataPanel/dpBaseWidget.h +++ b/dataPanel/dpBaseWidget.h @@ -12,6 +12,7 @@ #include #include +#include #include "global.h" class dpBaseWidget : public QWidget @@ -26,6 +27,17 @@ public: virtual void setDateTime(const QDateTime&) {} virtual void viewHistoricalData(const QDateTime&) {} +protected: + struct ChartStyle + { + QColor bgColor; //背景颜色 + QColor axisColor; //坐标轴颜色 + QColor tickColor; //刻度颜色 + QColor tickLabelColor; //刻度label颜色 + QFont tickLabelFont; //刻度label字体 + QPen gridPen; //网格线颜色 + }; + private: }; diff --git a/dataPanel/dpConfigurationDialog.cpp b/dataPanel/dpConfigurationDialog.cpp index 8ba59ca..a7fc068 100644 --- a/dataPanel/dpConfigurationDialog.cpp +++ b/dataPanel/dpConfigurationDialog.cpp @@ -356,7 +356,6 @@ void dpConfigurationDialog::onBtnClicked_remove_type() if(itemDataType == dataType) { removeDataSelected(i); - //i++; } } diff --git a/dataPanel/dpLineChart.cpp b/dataPanel/dpLineChart.cpp index ae7556a..594009b 100644 --- a/dataPanel/dpLineChart.cpp +++ b/dataPanel/dpLineChart.cpp @@ -23,6 +23,13 @@ dpLineChart::~dpLineChart() void dpLineChart::initQCP() { + m_chartStyle.bgColor = Qt::transparent; + m_chartStyle.axisColor = QColor(87, 100, 120); + m_chartStyle.tickColor = QColor(87, 100, 120); + m_chartStyle.tickLabelColor = QColor(250, 250, 250); + m_chartStyle.tickLabelFont = QFont("黑体", 12); + m_chartStyle.gridPen = QPen(QColor(87, 100, 120), 1, Qt::DotLine); + //m_pCustomPlot->axisRect()->setupFullAxesBox(); m_pCustomPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom); m_pCustomPlot->axisRect()->setRangeDrag(Qt::Horizontal); //只允许x轴方向的拖拽 @@ -32,35 +39,36 @@ void dpLineChart::initQCP() m_pCustomPlot->xAxis2->setTicks(false); m_pCustomPlot->xAxis2->setSubTicks(false); m_pCustomPlot->yAxis->setSubTicks(false); - m_pCustomPlot->yAxis2->setTicks(false); + //m_pCustomPlot->yAxis2->setTicks(false); m_pCustomPlot->yAxis2->setSubTicks(false); - connect(m_pCustomPlot->xAxis, SIGNAL(rangeChanged(const QCPRange&)), this, SLOT(onSignal_rangeChanged_xAxis(const QCPRange&))); + m_pCustomPlot->yAxis2->setVisible(true); + connect(m_pCustomPlot->xAxis, qOverload(&QCPAxis::rangeChanged), //rangeChanged有两个版本,qOverload可以指定版本 + this, &dpLineChart::onSignal_rangeChanged_xAxis); //背景颜色 - m_pCustomPlot->setBackground(QBrush(Qt::transparent)); + m_pCustomPlot->setBackground(QBrush(m_chartStyle.bgColor)); //坐标轴颜色 - QColor axisColor(87, 100, 120); - m_pCustomPlot->xAxis->setBasePen(axisColor); - m_pCustomPlot->xAxis2->setBasePen(axisColor); - m_pCustomPlot->yAxis->setBasePen(axisColor); - m_pCustomPlot->yAxis2->setBasePen(axisColor); + m_pCustomPlot->xAxis->setBasePen(m_chartStyle.axisColor); + m_pCustomPlot->xAxis2->setBasePen(m_chartStyle.axisColor); + m_pCustomPlot->yAxis->setBasePen(m_chartStyle.axisColor); + m_pCustomPlot->yAxis2->setBasePen(m_chartStyle.axisColor); //坐标刻度颜色 - QColor tickColor(87, 100, 120); - m_pCustomPlot->xAxis->setTickPen(QPen(tickColor)); - m_pCustomPlot->yAxis->setTickPen(QPen(tickColor)); + m_pCustomPlot->xAxis->setTickPen(QPen(m_chartStyle.tickColor)); + m_pCustomPlot->yAxis->setTickPen(QPen(m_chartStyle.tickColor)); + m_pCustomPlot->yAxis2->setTickPen(QPen(m_chartStyle.tickColor)); //坐标刻度Label颜色 - QColor tickLabelColor(250, 250, 250); - m_pCustomPlot->xAxis->setTickLabelColor(tickLabelColor); - m_pCustomPlot->xAxis->setTickLabelFont(QFont(QString::fromWCharArray(L"黑体"), 12)); - m_pCustomPlot->yAxis->setTickLabelColor(tickLabelColor); - m_pCustomPlot->yAxis->setTickLabelFont(QFont(QString::fromWCharArray(L"黑体"), 12)); + m_pCustomPlot->xAxis->setTickLabelColor(m_chartStyle.tickLabelColor); + m_pCustomPlot->xAxis->setTickLabelFont(m_chartStyle.tickLabelFont); + m_pCustomPlot->yAxis->setTickLabelColor(m_chartStyle.tickLabelColor); + m_pCustomPlot->yAxis->setTickLabelFont(m_chartStyle.tickLabelFont); + m_pCustomPlot->yAxis2->setTickLabelColor(m_chartStyle.tickLabelColor); + m_pCustomPlot->yAxis2->setTickLabelFont(m_chartStyle.tickLabelFont); //网格线颜色 - QColor gridColor(87, 100, 120); - m_pCustomPlot->xAxis->grid()->setPen(QPen(gridColor, 1, Qt::DotLine)); - m_pCustomPlot->xAxis->grid()->setZeroLinePen(QPen(gridColor, 1, Qt::DotLine)); - //m_pCustomPlot->xAxis2->grid()->setPen(QPen(gridColor, 1, Qt::DotLine)); - m_pCustomPlot->yAxis->grid()->setPen(QPen(gridColor, 1, Qt::DotLine)); - m_pCustomPlot->yAxis->grid()->setZeroLinePen(QPen(gridColor, 1, Qt::DotLine)); - //m_pCustomPlot->yAxis2->grid()->setPen(QPen(gridColor, 1, Qt::DotLine)); + m_pCustomPlot->xAxis->grid()->setPen(m_chartStyle.gridPen); + m_pCustomPlot->xAxis->grid()->setZeroLinePen(m_chartStyle.gridPen); + //m_pCustomPlot->xAxis2->grid()->setPen(m_chartStyle.gridPen); + m_pCustomPlot->yAxis->grid()->setPen(m_chartStyle.gridPen); + m_pCustomPlot->yAxis->grid()->setZeroLinePen(m_chartStyle.gridPen); + m_pCustomPlot->yAxis2->grid()->setPen(m_chartStyle.gridPen); //x轴用时间格式 QSharedPointer timeTicker(new QCPAxisTickerDateTime); timeTicker->setDateTimeFormat("hh:mm:ss:zzz\nyyyy/MM/dd"); diff --git a/dataPanel/dpLineChart.h b/dataPanel/dpLineChart.h index 8df955f..279ed06 100644 --- a/dataPanel/dpLineChart.h +++ b/dataPanel/dpLineChart.h @@ -34,6 +34,7 @@ private: void initQCP(); QCustomPlot* m_pCustomPlot; + ChartStyle m_chartStyle; qint64 m_timeRange; QDateTime m_curDateTime; }; diff --git a/source/dataPanel.cpp b/source/dataPanel.cpp index 0558796..332f2f1 100644 --- a/source/dataPanel.cpp +++ b/source/dataPanel.cpp @@ -401,6 +401,9 @@ void DataPanel::configurationComplete() { if(m_pConfigurationWidget->isVisible()) m_pConfigurationWidget->hide(); + + //读取配置数据,根据所选的数据类型数量同步创建不同的Y坐标轴 + } void DataPanel::onToolBtnClicked_setting()