refactor:调整chart样式配置的相关代码

This commit is contained in:
duanshengchao 2025-07-09 10:58:52 +08:00
parent d9a74285e9
commit 4c6d184f0f
5 changed files with 47 additions and 24 deletions

View File

@ -12,6 +12,7 @@
#include <QWidget>
#include <QDateTime>
#include <QPen>
#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:
};

View File

@ -356,7 +356,6 @@ void dpConfigurationDialog::onBtnClicked_remove_type()
if(itemDataType == dataType)
{
removeDataSelected(i);
//i++;
}
}

View File

@ -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<const QCPRange &>(&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<QCPAxisTickerDateTime> timeTicker(new QCPAxisTickerDateTime);
timeTicker->setDateTimeFormat("hh:mm:ss:zzz\nyyyy/MM/dd");

View File

@ -34,6 +34,7 @@ private:
void initQCP();
QCustomPlot* m_pCustomPlot;
ChartStyle m_chartStyle;
qint64 m_timeRange;
QDateTime m_curDateTime;
};

View File

@ -401,6 +401,9 @@ void DataPanel::configurationComplete()
{
if(m_pConfigurationWidget->isVisible())
m_pConfigurationWidget->hide();
//读取配置数据根据所选的数据类型数量同步创建不同的Y坐标轴
}
void DataPanel::onToolBtnClicked_setting()