2024-12-25 08:34:51 +08:00
|
|
|
|
#include "dpLineChart.h"
|
|
|
|
|
|
#include "util/Chart/qcustomplot.h"
|
|
|
|
|
|
|
|
|
|
|
|
dpLineChart::dpLineChart(QWidget* parent)
|
|
|
|
|
|
:dpBaseWidget(parent)
|
|
|
|
|
|
{
|
|
|
|
|
|
setAttribute(Qt::WA_TranslucentBackground,true);
|
|
|
|
|
|
|
|
|
|
|
|
m_pCustomPlot = new QCustomPlot(this);
|
|
|
|
|
|
initQCP();
|
|
|
|
|
|
|
2025-01-04 18:18:19 +08:00
|
|
|
|
m_timeRange = 60 * 1000;
|
|
|
|
|
|
|
2024-12-25 08:34:51 +08:00
|
|
|
|
QBoxLayout* mainLayout = new QBoxLayout(QBoxLayout::LeftToRight);
|
|
|
|
|
|
mainLayout->setContentsMargins(0, 1, 0, 0);
|
|
|
|
|
|
mainLayout->addWidget(m_pCustomPlot);
|
|
|
|
|
|
setLayout(mainLayout);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
dpLineChart::~dpLineChart()
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void dpLineChart::initQCP()
|
|
|
|
|
|
{
|
2025-07-09 10:58:52 +08:00
|
|
|
|
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);
|
|
|
|
|
|
|
2025-01-04 18:18:19 +08:00
|
|
|
|
//m_pCustomPlot->axisRect()->setupFullAxesBox();
|
|
|
|
|
|
m_pCustomPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
|
|
|
|
|
|
m_pCustomPlot->axisRect()->setRangeDrag(Qt::Horizontal); //只允许x轴方向的拖拽
|
|
|
|
|
|
double zoomFactor = m_pCustomPlot->axisRect()->rangeZoomFactor(Qt::Horizontal);
|
|
|
|
|
|
m_pCustomPlot->axisRect()->setRangeZoomFactor(zoomFactor, 1); //只做x轴的缩放
|
2024-12-25 08:34:51 +08:00
|
|
|
|
m_pCustomPlot->xAxis->setSubTicks(false);
|
|
|
|
|
|
m_pCustomPlot->xAxis2->setTicks(false);
|
|
|
|
|
|
m_pCustomPlot->xAxis2->setSubTicks(false);
|
|
|
|
|
|
m_pCustomPlot->yAxis->setSubTicks(false);
|
2025-07-09 10:58:52 +08:00
|
|
|
|
//m_pCustomPlot->yAxis2->setTicks(false);
|
2024-12-25 08:34:51 +08:00
|
|
|
|
m_pCustomPlot->yAxis2->setSubTicks(false);
|
2025-07-09 10:58:52 +08:00
|
|
|
|
m_pCustomPlot->yAxis2->setVisible(true);
|
|
|
|
|
|
connect(m_pCustomPlot->xAxis, qOverload<const QCPRange &>(&QCPAxis::rangeChanged), //rangeChanged有两个版本,qOverload可以指定版本
|
|
|
|
|
|
this, &dpLineChart::onSignal_rangeChanged_xAxis);
|
2024-12-25 08:34:51 +08:00
|
|
|
|
//背景颜色
|
2025-07-09 10:58:52 +08:00
|
|
|
|
m_pCustomPlot->setBackground(QBrush(m_chartStyle.bgColor));
|
2024-12-25 08:34:51 +08:00
|
|
|
|
//坐标轴颜色
|
2025-07-09 10:58:52 +08:00
|
|
|
|
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);
|
2024-12-25 08:34:51 +08:00
|
|
|
|
//坐标刻度颜色
|
2025-07-09 10:58:52 +08:00
|
|
|
|
m_pCustomPlot->xAxis->setTickPen(QPen(m_chartStyle.tickColor));
|
|
|
|
|
|
m_pCustomPlot->yAxis->setTickPen(QPen(m_chartStyle.tickColor));
|
|
|
|
|
|
m_pCustomPlot->yAxis2->setTickPen(QPen(m_chartStyle.tickColor));
|
2024-12-25 08:34:51 +08:00
|
|
|
|
//坐标刻度Label颜色
|
2025-07-09 10:58:52 +08:00
|
|
|
|
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);
|
2024-12-25 08:34:51 +08:00
|
|
|
|
//网格线颜色
|
2025-07-09 10:58:52 +08:00
|
|
|
|
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);
|
2025-01-04 18:18:19 +08:00
|
|
|
|
//x轴用时间格式
|
|
|
|
|
|
QSharedPointer<QCPAxisTickerDateTime> timeTicker(new QCPAxisTickerDateTime);
|
|
|
|
|
|
timeTicker->setDateTimeFormat("hh:mm:ss:zzz\nyyyy/MM/dd");
|
|
|
|
|
|
//qDebug() << timeTicker->dateTimeFormat();
|
|
|
|
|
|
m_pCustomPlot->xAxis->setTicker(timeTicker);
|
|
|
|
|
|
//qDebug() << m_pCustomPlot->xAxis->range();
|
2024-12-25 08:34:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void dpLineChart::setTimeRange(TimeUnit unit)
|
|
|
|
|
|
{
|
2025-01-04 18:18:19 +08:00
|
|
|
|
switch(unit)
|
|
|
|
|
|
{
|
|
|
|
|
|
case TU_Year:
|
|
|
|
|
|
m_timeRange = m_curDateTime.date().daysInYear() * 24 * 60 * 60 * (qint64)1000;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case TU_Month:
|
|
|
|
|
|
m_timeRange = m_curDateTime.date().daysInMonth() * 24 * 60 * 60 * (qint64)1000;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case TU_Day:
|
|
|
|
|
|
m_timeRange = 24 * 60 * 60 * 1000;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case TU_Hour:
|
|
|
|
|
|
m_timeRange = 60 * 60 *1000;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case TU_Minute_30:
|
|
|
|
|
|
m_timeRange = 30 * 60 *1000;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case TU_Minute_20:
|
|
|
|
|
|
m_timeRange = 20 * 60 *1000;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case TU_Minute_15:
|
|
|
|
|
|
m_timeRange = 15 * 60 *1000;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case TU_Minute_10:
|
|
|
|
|
|
m_timeRange = 10 * 60 *1000;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case TU_Minute_5:
|
|
|
|
|
|
m_timeRange = 5 * 60 *1000;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case TU_Minute_3:
|
|
|
|
|
|
m_timeRange = 3 * 60 *1000;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case TU_Minute_1:
|
|
|
|
|
|
m_timeRange = 60 * 1000;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case TU_Second_30:
|
|
|
|
|
|
m_timeRange = 30 * 1000;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case TU_Second_10:
|
|
|
|
|
|
m_timeRange = 10 * 1000;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case TU_Second_1:
|
|
|
|
|
|
m_timeRange = 1 * 1000;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case TU_MSecond_500:
|
|
|
|
|
|
m_timeRange = 500;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case TU_MSecond_100:
|
|
|
|
|
|
m_timeRange = 100;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case TU_MSecond_50:
|
|
|
|
|
|
m_timeRange = 50;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case TU_MSecond_10:
|
|
|
|
|
|
m_timeRange = 10;
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-12-25 08:34:51 +08:00
|
|
|
|
|
2025-01-04 18:18:19 +08:00
|
|
|
|
void dpLineChart::setDateTime(const QDateTime& dateTime)
|
|
|
|
|
|
{
|
|
|
|
|
|
qint64 timeValue = dateTime.toMSecsSinceEpoch() / 1000.0;
|
|
|
|
|
|
//qint64 timeValue = QCPAxisTickerDateTime::dateTimeToKey(dateTime);
|
|
|
|
|
|
m_pCustomPlot->xAxis->setRange(timeValue, m_timeRange / 1000.0, Qt::AlignRight);
|
|
|
|
|
|
m_pCustomPlot->replot();
|
|
|
|
|
|
m_curDateTime = dateTime;
|
2024-12-25 08:34:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-04 18:18:19 +08:00
|
|
|
|
void dpLineChart::viewHistoricalData(const QDateTime& dateTime)
|
2024-12-25 08:34:51 +08:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2025-01-04 18:18:19 +08:00
|
|
|
|
|
|
|
|
|
|
void dpLineChart::onSignal_rangeChanged_xAxis(const QCPRange& range)
|
|
|
|
|
|
{
|
|
|
|
|
|
// qDebug() << "m_timeRange: " << m_timeRange;
|
|
|
|
|
|
// qDebug() << "range size: " << range.size();
|
|
|
|
|
|
if(m_timeRange != range.size() * 1000)
|
|
|
|
|
|
m_timeRange = range.size() * 1000;
|
|
|
|
|
|
}
|