feat:qcutsomplots

This commit is contained in:
“Diviner66”, 2024-11-11 22:19:24 +08:00
parent c76b687c2a
commit 47793afcca
8 changed files with 44130 additions and 686 deletions

View File

@ -7,6 +7,8 @@
QT += core gui QT += core gui
QT += charts QT += charts
QT += network QT += network
QT += printsupport
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = HX_CoalFlowDetect TARGET = HX_CoalFlowDetect
@ -26,6 +28,7 @@ DEFINES += QT_DEPRECATED_WARNINGS
CONFIG += c++11 CONFIG += c++11
SOURCES += \ SOURCES += \
component/qcustomplot/qcustomplot.cpp \
controller/coaldistributmodbusthread.cpp \ controller/coaldistributmodbusthread.cpp \
main.cpp \ main.cpp \
mainwindow.cpp \ mainwindow.cpp \
@ -38,6 +41,7 @@ SOURCES += \
component/ZDateEdit/ztimeedit.cpp\ component/ZDateEdit/ztimeedit.cpp\
HEADERS += \ HEADERS += \
component/qcustomplot/qcustomplot.h \
controller/coaldistributmodbusthread.h \ controller/coaldistributmodbusthread.h \
mainwindow.h \ mainwindow.h \
view/beltspeedlinewidget.h \ view/beltspeedlinewidget.h \

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -8,17 +8,18 @@
#include <QTimer> #include <QTimer>
MainWindow::MainWindow(QWidget *parent) MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
, ui(new Ui::MainWindow)
{
ui->setupUi(this); ui->setupUi(this);
this->setWindowFlag(Qt::FramelessWindowHint); this->setWindowFlag(Qt::FramelessWindowHint);
this->setWindowFlags(windowFlags() | Qt::WindowMaximizeButtonHint | Qt::WindowMinimizeButtonHint); this->setWindowFlags(windowFlags() | Qt::WindowMaximizeButtonHint |
Qt::WindowMinimizeButtonHint);
ui->widget_head->installEventFilter(this); ui->widget_head->installEventFilter(this);
m_systemSetting = new QSettings("system.ini", QSettings::IniFormat); m_systemSetting = new QSettings("system.ini", QSettings::IniFormat);
createSystemINI();
this->setWindowTitle(m_systemSetting->value("system/name").toString()); this->setWindowTitle(m_systemSetting->value("system/name").toString());
calendarLoopPlay(); calendarLoopPlay();
@ -38,8 +39,7 @@ MainWindow::MainWindow(QWidget* parent)
MainWindow::~MainWindow() { delete ui; } MainWindow::~MainWindow() { delete ui; }
bool MainWindow::eventFilter(QObject* watched, QEvent* event) bool MainWindow::eventFilter(QObject *watched, QEvent *event) {
{
// 处理鼠标按下、释放和移动事件 // 处理鼠标按下、释放和移动事件
if (watched == ui->widget_head) { if (watched == ui->widget_head) {
QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event); QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
@ -48,7 +48,9 @@ bool MainWindow::eventFilter(QObject* watched, QEvent* event)
m_mousePressed = true; // 启动拖拽 m_mousePressed = true; // 启动拖拽
m_mousePoint = mouseEvent->globalPos() - this->pos(); m_mousePoint = mouseEvent->globalPos() - this->pos();
this->setWindowFlags(windowFlags() | Qt::WindowMaximizeButtonHint | Qt::WindowMinimizeButtonHint | Qt::Window | Qt::WindowStaysOnTopHint); this->setWindowFlags(windowFlags() | Qt::WindowMaximizeButtonHint |
Qt::WindowMinimizeButtonHint | Qt::Window |
Qt::WindowStaysOnTopHint);
this->show(); // 确保窗口显示 this->show(); // 确保窗口显示
return true; return true;
@ -61,7 +63,9 @@ bool MainWindow::eventFilter(QObject* watched, QEvent* event)
this->move(mouseEvent->globalPos() - m_mousePoint); this->move(mouseEvent->globalPos() - m_mousePoint);
// 每次移动时设置为最上方 // 每次移动时设置为最上方
this->setWindowFlags(windowFlags() | Qt::WindowMaximizeButtonHint | Qt::WindowMinimizeButtonHint | Qt::Window | Qt::WindowStaysOnTopHint); this->setWindowFlags(windowFlags() | Qt::WindowMaximizeButtonHint |
Qt::WindowMinimizeButtonHint | Qt::Window |
Qt::WindowStaysOnTopHint);
this->show(); this->show();
return true; return true;
} }
@ -69,8 +73,7 @@ bool MainWindow::eventFilter(QObject* watched, QEvent* event)
} }
} }
void MainWindow::addBeltWarnInfoToTextEdit(const QString& consoleInfo) void MainWindow::addBeltWarnInfoToTextEdit(const QString &consoleInfo) {
{
qDebug() << consoleInfo; qDebug() << consoleInfo;
m_textEditConsoleInfo.append(consoleInfo); m_textEditConsoleInfo.append(consoleInfo);
QDateTime startDateTime = QDateTime::currentDateTime(); QDateTime startDateTime = QDateTime::currentDateTime();
@ -80,12 +83,15 @@ void MainWindow::addBeltWarnInfoToTextEdit(const QString& consoleInfo)
return; return;
QString lastConsoleInfo = m_textEditConsoleInfo.last(); QString lastConsoleInfo = m_textEditConsoleInfo.last();
// qint64 timeDifferenceInMilliseconds = m_previousConsleInfoDateTime.secsTo(startDateTime); // qint64 timeDifferenceInMilliseconds =
// m_previousConsleInfoDateTime.secsTo(startDateTime);
// if ((lastConsoleInfo == consoleInfo) && timeDifferenceInMilliseconds < 2) // if ((lastConsoleInfo == consoleInfo) && timeDifferenceInMilliseconds <
// 2)
// return; // return;
QString consoleInfoResult = QStringLiteral("[%1]%2").arg(formattedTime).arg(consoleInfo); QString consoleInfoResult =
QStringLiteral("[%1]%2").arg(formattedTime).arg(consoleInfo);
ui->textEdit_beltWarnInfo->append(consoleInfoResult); ui->textEdit_beltWarnInfo->append(consoleInfoResult);
m_previousConsleInfoDateTime = QDateTime::currentDateTime(); m_previousConsleInfoDateTime = QDateTime::currentDateTime();
@ -93,8 +99,7 @@ void MainWindow::addBeltWarnInfoToTextEdit(const QString& consoleInfo)
void MainWindow::on_pushButton_close_clicked() { this->close(); } void MainWindow::on_pushButton_close_clicked() { this->close(); }
void MainWindow::on_pushButton_max_clicked() void MainWindow::on_pushButton_max_clicked() {
{
if (isFullScreen()) { if (isFullScreen()) {
this->showMaximized(); this->showMaximized();
} else { } else {
@ -104,8 +109,7 @@ void MainWindow::on_pushButton_max_clicked()
void MainWindow::on_pushButton_min_clicked() { this->showMinimized(); } void MainWindow::on_pushButton_min_clicked() { this->showMinimized(); }
void MainWindow::calendarLoopPlay() void MainWindow::calendarLoopPlay() {
{
ui->label_calendar->clear(); ui->label_calendar->clear();
QTimer *timer_calendar = new QTimer(this); QTimer *timer_calendar = new QTimer(this);
connect(timer_calendar, &QTimer::timeout, this, [this]() { connect(timer_calendar, &QTimer::timeout, this, [this]() {
@ -116,8 +120,35 @@ void MainWindow::calendarLoopPlay()
timer_calendar->start(1000); //每一秒溢出一次进入槽函数 timer_calendar->start(1000); //每一秒溢出一次进入槽函数
} }
void MainWindow::setSystemTitle() void MainWindow::createSystemINI() {
{ // 写入数据到INI文件
// 判断并设置 "system/name"
if (!m_systemSetting->contains("system/name")) {
m_systemSetting->setValue("system/name", "煤流监测平台");
}
// 判断并设置 "coalStaticInterface/url"
if (!m_systemSetting->contains("coalStaticInterface/url")) {
m_systemSetting->setValue("coalStaticInterface/url", 30);
}
// 判断并设置 "addr/ip"
if (!m_systemSetting->contains("addr/ip")) {
m_systemSetting->setValue("addr/ip", "127.0.0.1");
}
// 判断并设置 "addr/port"
if (!m_systemSetting->contains("addr/port")) {
m_systemSetting->setValue("addr/port", 3000);
}
// 判断并设置 "camera/rtsp"
if (!m_systemSetting->contains("camera/rtsp")) {
m_systemSetting->setValue("camera/rtsp", 3000);
}
}
void MainWindow::setSystemTitle() {
QString titleName = m_systemSetting->value("system/name").toString(); QString titleName = m_systemSetting->value("system/name").toString();
if (!titleName.isEmpty()) { if (!titleName.isEmpty()) {
ui->label_title->clear(); ui->label_title->clear();
@ -125,19 +156,18 @@ void MainWindow::setSystemTitle()
} }
} }
void MainWindow::setBeltWarnInfoTextEditStyle() void MainWindow::setBeltWarnInfoTextEditStyle() {
{
ui->textEdit_beltWarnInfo->document()->setMaximumBlockCount(100); ui->textEdit_beltWarnInfo->document()->setMaximumBlockCount(100);
ui->textEdit_beltWarnInfo->setReadOnly(true); ui->textEdit_beltWarnInfo->setReadOnly(true);
ui->textEdit_beltWarnInfo->setTextInteractionFlags(Qt::NoTextInteraction); ui->textEdit_beltWarnInfo->setTextInteractionFlags(Qt::NoTextInteraction);
} }
void MainWindow::addcoalDistributChart() void MainWindow::addcoalDistributChart() {
{
// addBeltWarnInfoToTextEdit("添加煤流分布窗口成功!"); // addBeltWarnInfoToTextEdit("添加煤流分布窗口成功!");
ui->textEdit_beltWarnInfo->append("添加煤流分布窗口成功!"); ui->textEdit_beltWarnInfo->append("添加煤流分布窗口成功!");
CoalDistributBarChartWidget* coalDistributBarChartWidget = new CoalDistributBarChartWidget(this); CoalDistributBarChartWidget *coalDistributBarChartWidget =
new CoalDistributBarChartWidget(this);
// 检查布局是否已设置 // 检查布局是否已设置
if (!ui->widget_coalDistributChart->layout()) { if (!ui->widget_coalDistributChart->layout()) {
@ -148,8 +178,7 @@ void MainWindow::addcoalDistributChart()
coalDistributBarChartWidget); coalDistributBarChartWidget);
} }
void MainWindow::addbeltSpeedChat() void MainWindow::addbeltSpeedChat() {
{
ui->textEdit_beltWarnInfo->append("添加皮带机速度曲线窗口成功!"); ui->textEdit_beltWarnInfo->append("添加皮带机速度曲线窗口成功!");
BeltSpeedLineWidget *beltSpeedLineWidget = new BeltSpeedLineWidget(this); BeltSpeedLineWidget *beltSpeedLineWidget = new BeltSpeedLineWidget(this);
// 检查布局是否已设置 // 检查布局是否已设置
@ -160,10 +189,10 @@ void MainWindow::addbeltSpeedChat()
ui->widget_beltSpeedChart->layout()->addWidget(beltSpeedLineWidget); ui->widget_beltSpeedChart->layout()->addWidget(beltSpeedLineWidget);
} }
void MainWindow::addCoalStatisticsChart() void MainWindow::addCoalStatisticsChart() {
{
ui->textEdit_beltWarnInfo->append("添加煤量统计窗口成功!"); ui->textEdit_beltWarnInfo->append("添加煤量统计窗口成功!");
CoalStatisticsChartWidget* coalStatisticsChartWidget = new CoalStatisticsChartWidget(this); CoalStatisticsChartWidget *coalStatisticsChartWidget =
new CoalStatisticsChartWidget(this);
// 检查布局是否已设置 // 检查布局是否已设置
if (!ui->widget_beltSpeedChart->layout()) { if (!ui->widget_beltSpeedChart->layout()) {
ui->widget_beltSpeedChart->setLayout(new QVBoxLayout); ui->widget_beltSpeedChart->setLayout(new QVBoxLayout);
@ -172,14 +201,17 @@ void MainWindow::addCoalStatisticsChart()
ui->widget_coalStatisticsChart->layout()->addWidget( ui->widget_coalStatisticsChart->layout()->addWidget(
coalStatisticsChartWidget); coalStatisticsChartWidget);
connect(coalStatisticsChartWidget, &CoalStatisticsChartWidget::querySuccessInfo, this, &MainWindow::addBeltWarnInfoToTextEdit); connect(coalStatisticsChartWidget,
&CoalStatisticsChartWidget::querySuccessInfo, this,
&MainWindow::addBeltWarnInfoToTextEdit);
} }
void MainWindow::addCoalBeltVideo() void MainWindow::addCoalBeltVideo() {
{ // QString url =
// QString url = "https://stream7.iqilu.com/10339/upload_transcode/202002/18/" // "https://stream7.iqilu.com/10339/upload_transcode/202002/18/"
// "20200218114723HDu3hhxqIT.mp4"; // "20200218114723HDu3hhxqIT.mp4";
// QString url = "rtsp://admin:1234qwer@192.168.80.82:554/Streaming/Channels/101"; // QString url =
// "rtsp://admin:1234qwer@192.168.80.82:554/Streaming/Channels/101";
QString url = m_systemSetting->value("camera/rtsp").toString(); QString url = m_systemSetting->value("camera/rtsp").toString();
ui->widget_beltVideo->setUrl(url); ui->widget_beltVideo->setUrl(url);
ui->widget_beltVideo->open(); ui->widget_beltVideo->open();

View File

@ -39,6 +39,11 @@ private:
*/ */
void calendarLoopPlay(); void calendarLoopPlay();
/**
* @brief createSystemINI
*/
void createSystemINI();
/** /**
* @brief setSystemTitle * @brief setSystemTitle
*/ */

View File

@ -20,20 +20,17 @@
#include <memory> #include <memory>
CoalDistributBarChartWidget::CoalDistributBarChartWidget(QWidget *parent) CoalDistributBarChartWidget::CoalDistributBarChartWidget(QWidget *parent)
: QWidget(parent) : QWidget(parent), m_tcpSocket(new QTcpSocket(this)) {
, m_tcpSocket(new QTcpSocket(this))
{
initializeChart(); initializeChart();
m_systemSetting = new QSettings("system.ini", QSettings::IniFormat); m_systemSetting = new QSettings("system.ini", QSettings::IniFormat);
connectTCPSocket(); // connectTCPSocket();
setupTimer(); // setupTimer();
} }
CoalDistributBarChartWidget::~CoalDistributBarChartWidget() CoalDistributBarChartWidget::~CoalDistributBarChartWidget() {
{
if (m_tcpSocket->state() == QAbstractSocket::ConnectedState) { if (m_tcpSocket->state() == QAbstractSocket::ConnectedState) {
m_tcpSocket->disconnectFromHost(); m_tcpSocket->disconnectFromHost();
m_tcpSocket->waitForDisconnected(); m_tcpSocket->waitForDisconnected();
@ -41,8 +38,7 @@ CoalDistributBarChartWidget::~CoalDistributBarChartWidget()
delete m_tcpSocket; delete m_tcpSocket;
} }
void CoalDistributBarChartWidget::updateChart() void CoalDistributBarChartWidget::updateChart() {
{
int radio = getCoalRatioByTCPSocket(); int radio = getCoalRatioByTCPSocket();
if (radio < 0 || radio > 90) { if (radio < 0 || radio > 90) {
return; return;
@ -54,8 +50,7 @@ void CoalDistributBarChartWidget::updateChart()
m_set->insert(0, radio); m_set->insert(0, radio);
} }
void CoalDistributBarChartWidget::initializeChart() void CoalDistributBarChartWidget::initializeChart() {
{
// 创建图标 // 创建图标
m_chart = new QChart(); m_chart = new QChart();
@ -115,8 +110,7 @@ void CoalDistributBarChartWidget::initializeChart()
setLayout(layout); setLayout(layout);
} }
void CoalDistributBarChartWidget::setupTimer() void CoalDistributBarChartWidget::setupTimer() {
{
// 定时器设置 // 定时器设置
m_timer = new QTimer(this); m_timer = new QTimer(this);
connect(m_timer, &QTimer::timeout, this, connect(m_timer, &QTimer::timeout, this,
@ -124,8 +118,7 @@ void CoalDistributBarChartWidget::setupTimer()
m_timer->start(1000); // 每秒更新一次 m_timer->start(1000); // 每秒更新一次
} }
void CoalDistributBarChartWidget::connectTCPSocket() void CoalDistributBarChartWidget::connectTCPSocket() {
{
QString ipAddress = m_systemSetting->value("addr/ip").toString(); QString ipAddress = m_systemSetting->value("addr/ip").toString();
quint16 port = m_systemSetting->value("addr/port").toInt(); quint16 port = m_systemSetting->value("addr/port").toInt();
@ -136,8 +129,7 @@ void CoalDistributBarChartWidget::connectTCPSocket()
} }
} }
int CoalDistributBarChartWidget::getCoalRatioByTCPSocket() int CoalDistributBarChartWidget::getCoalRatioByTCPSocket() {
{
if (m_tcpSocket->state() != QAbstractSocket::ConnectedState) { if (m_tcpSocket->state() != QAbstractSocket::ConnectedState) {
qDebug() << "未连接到服务器"; qDebug() << "未连接到服务器";
return -1; // 返回错误值 return -1; // 返回错误值
@ -218,8 +210,7 @@ int CoalDistributBarChartWidget::getCoalRatioByTCPSocket()
return decimalValue; // 返回煤量占比 return decimalValue; // 返回煤量占比
} }
QByteArray CoalDistributBarChartWidget::createModbusTcpRequest() QByteArray CoalDistributBarChartWidget::createModbusTcpRequest() {
{
QByteArray request; QByteArray request;
QDataStream stream(&request, QIODevice::WriteOnly); QDataStream stream(&request, QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::BigEndian); // 使用大端字节序 stream.setByteOrder(QDataStream::BigEndian); // 使用大端字节序

View File

@ -1,4 +1,4 @@
#include "coalstatisticschartwidget.h" #include "coalstatisticschartwidget.h"
#include "ui_coalstatisticschartwidget.h" #include "ui_coalstatisticschartwidget.h"
#include <QHttpPart> #include <QHttpPart>
@ -11,66 +11,52 @@
#include <QUrlQuery> #include <QUrlQuery>
CoalStatisticsChartWidget::CoalStatisticsChartWidget(QWidget *parent) CoalStatisticsChartWidget::CoalStatisticsChartWidget(QWidget *parent)
: QWidget(parent) : QWidget(parent), ui(new Ui::CoalStatisticsChartWidget) {
, ui(new Ui::CoalStatisticsChartWidget)
{
ui->setupUi(this); ui->setupUi(this);
m_systemSetting = new QSettings("system.ini", QSettings::IniFormat); m_systemSetting = new QSettings("system.ini", QSettings::IniFormat);
initCustomCoalStatisticsBarChart(); ui->dateTimeEdit_startTime->setDateTime(
QDateTime::currentDateTime().addSecs(-8 * 3600));
// initCustomCoalStatisticsBarChart();
initCustomPlotBarChart();
} }
CoalStatisticsChartWidget::~CoalStatisticsChartWidget() CoalStatisticsChartWidget::~CoalStatisticsChartWidget() { delete ui; }
{
delete ui;
}
void CoalStatisticsChartWidget::on_pushButton_coalStatisOk_clicked() void CoalStatisticsChartWidget::on_pushButton_coalStatisOk_clicked() {
{
// 时间范围 // 时间范围
QString beginTimeStr = ui->dateTimeEdit_startTime->text(); QString beginTimeStr = ui->dateTimeEdit_startTime->text();
QString endTimeStr = ui->dateTimeEdit_endTime->text(); QString endTimeStr = ui->dateTimeEdit_endTime->text();
qDebug() << "beginTimeStr " << beginTimeStr; // 定义输出格式化后的时间
qDebug() << "endTimeStr: " << endTimeStr; QString formattedBeginTime;
QString formattedEndTime;
formatTimeRange(beginTimeStr, endTimeStr, formattedBeginTime,
formattedEndTime);
// 统计单位 // 统计单位
QString statisticUnit = ui->comboBox_unit->currentText(); QString statisticUnit = ui->comboBox_unit->currentText();
// 创建 QDateTime 对象 m_volumeDataMap.clear();
QDateTime beginTime = QDateTime::fromString(beginTimeStr, "yyyy/MM/d HH:mm"); getCoalStatisInfoByHttpRequest(formattedBeginTime, formattedEndTime,
QDateTime endTime = QDateTime::fromString(endTimeStr, "yyyy/MM/d HH:mm"); statisticUnit);
// 格式化为 "yyyy-MM-d H" 形式
QString formattedBeginTime = beginTime.toString("yyyy-MM-dd HH");
QString formattedEndTime = endTime.toString("yyyy-MM-dd HH");
QString startTimestr = "起始时间" + formattedBeginTime;
QString endTimestr = "终止时间" + formattedEndTime;
emit querySuccessInfo(startTimestr);
emit querySuccessInfo(endTimestr);
// 输出格式化后的时间
qDebug() << "Formatted Begin Time: " << formattedBeginTime;
qDebug() << "Formatted End Time: " << formattedEndTime;
qDebug() << "statisticUnit:" << statisticUnit;
// 查询结果信息
QMap<QString, double> volumeDataMap;
getCoalStatisInfoByHttpRequest(formattedBeginTime, formattedEndTime, statisticUnit, volumeDataMap);
ui->label_totalVolume->clear(); ui->label_totalVolume->clear();
ui->label_totalVolume->setText(QString("煤量总计:%1吨").arg(m_toalValue)); ui->label_totalVolume->setText(QString("煤量总计:%1吨").arg(m_toalValue));
QString volumeDataMapStr = "总数" + QString::number(m_volumeDataMap.size());
emit querySuccessInfo(volumeDataMapStr);
// 更新查询的数据至图表 // 更新查询的数据至图表
// updateCoalStatisticsChart(volumeDataMap); // updateCoalStatisticsChart(volumeDataMap);
} }
void CoalStatisticsChartWidget::initCoalStatisticsBarChart() void CoalStatisticsChartWidget::initCoalStatisticsBarChart() {
{
// 创建图表 // 创建图表
m_chart = new QChart(); m_chart = new QChart();
m_chart->setBackgroundBrush(Qt::transparent); m_chart->setBackgroundBrush(Qt::transparent);
@ -110,7 +96,8 @@ void CoalStatisticsChartWidget::initCoalStatisticsBarChart()
m_series->attachAxis(m_axisY); m_series->attachAxis(m_axisY);
// 布局设置 // 布局设置
QVBoxLayout* layout = static_cast<QVBoxLayout*>(ui->widget_coalChart->layout()); QVBoxLayout *layout =
static_cast<QVBoxLayout *>(ui->widget_coalChart->layout());
if (!layout) { if (!layout) {
layout = new QVBoxLayout(ui->widget_coalChart); layout = new QVBoxLayout(ui->widget_coalChart);
ui->widget_coalChart->setLayout(layout); ui->widget_coalChart->setLayout(layout);
@ -123,8 +110,27 @@ void CoalStatisticsChartWidget::initCoalStatisticsBarChart()
} }
} }
QList<QString> CoalStatisticsChartWidget::getLastSixFullHours(const QDateTime& currentTime) void CoalStatisticsChartWidget::formatTimeRange(const QString &beginTimeStr,
{ const QString &endTimeStr,
QString &formattedBeginTime,
QString &formattedEndTime) {
// 创建 QDateTime 对象
QDateTime beginTime = QDateTime::fromString(beginTimeStr, "yyyy/MM/d HH:mm");
QDateTime endTime = QDateTime::fromString(endTimeStr, "yyyy/MM/d HH:mm");
// 判断转换是否成功
if (!beginTime.isValid() || !endTime.isValid()) {
qWarning() << "Invalid time format!";
return;
}
// 格式化为 "yyyy-MM-dd HH" 形式
formattedBeginTime = beginTime.toString("yyyy-MM-dd HH");
formattedEndTime = endTime.toString("yyyy-MM-dd HH");
}
QList<QString>
CoalStatisticsChartWidget::getLastSixFullHours(const QDateTime &currentTime) {
// 将当前时间设置为整点 // 将当前时间设置为整点
QDateTime roundedTime = currentTime; QDateTime roundedTime = currentTime;
roundedTime.setTime(QTime(currentTime.time().hour(), 0)); // 设置时间到整点 roundedTime.setTime(QTime(currentTime.time().hour(), 0)); // 设置时间到整点
@ -137,14 +143,13 @@ QList<QString> CoalStatisticsChartWidget::getLastSixFullHours(const QDateTime& c
QDateTime prevHour = roundedTime.addSecs(-i * 3600); // 向前推算每个整点 QDateTime prevHour = roundedTime.addSecs(-i * 3600); // 向前推算每个整点
timeList.append(prevHour.toString("H")); timeList.append(prevHour.toString("YYYY/MM/dd H"));
} }
return timeList; return timeList;
} }
void CoalStatisticsChartWidget::initCustomCoalStatisticsBarChart() void CoalStatisticsChartWidget::initCustomCoalStatisticsBarChart() {
{
// 创建图表 // 创建图表
m_chart = new QChart(); m_chart = new QChart();
m_chart->setBackgroundBrush(Qt::transparent); m_chart->setBackgroundBrush(Qt::transparent);
@ -181,7 +186,8 @@ void CoalStatisticsChartWidget::initCustomCoalStatisticsBarChart()
m_series->attachAxis(m_axisY); m_series->attachAxis(m_axisY);
// 布局设置 // 布局设置
QVBoxLayout* layout = static_cast<QVBoxLayout*>(ui->widget_coalChart->layout()); QVBoxLayout *layout =
static_cast<QVBoxLayout *>(ui->widget_coalChart->layout());
if (!layout) { if (!layout) {
layout = new QVBoxLayout(ui->widget_coalChart); layout = new QVBoxLayout(ui->widget_coalChart);
ui->widget_coalChart->setLayout(layout); ui->widget_coalChart->setLayout(layout);
@ -194,13 +200,87 @@ void CoalStatisticsChartWidget::initCustomCoalStatisticsBarChart()
} }
} }
void CoalStatisticsChartWidget::getCoalStatisInfoByHttpRequest(const QString& beginTime, const QString& endTime, const QString& statisticUnit, QMap<QString, double>& volumeDataMap) void CoalStatisticsChartWidget::initCustomPlotBarChart() {
{ // 创建 QCustomPlot 图表
m_customPlot =
new QCustomPlot(ui->widget_coalChart); // 假设 ui->widget_coalChart 是容器
m_customPlot->setBackground(Qt::transparent); // 设置背景透明
// 设置布局
QVBoxLayout *layout =
static_cast<QVBoxLayout *>(ui->widget_coalChart->layout());
if (!layout) {
layout = new QVBoxLayout(ui->widget_coalChart);
ui->widget_coalChart->setLayout(layout);
}
layout->setContentsMargins(0, 0, 0, 0); // 设置布局的边距
layout->setSpacing(0); // 设置布局的间距
if (layout->indexOf(m_customPlot) == -1) {
layout->addWidget(m_customPlot); // 将 QCustomPlot 添加到布局中
}
// 设置 X 轴
m_customPlot->xAxis->setLabel("时间");
m_customPlot->xAxis->setBasePen(QPen(Qt::gray)); // 设置轴线颜色
m_customPlot->xAxis->setTickPen(QPen(Qt::gray)); // 设置刻度线颜色
m_customPlot->xAxis->setSubTickPen(QPen(Qt::gray)); // 设置子刻度线颜色
m_customPlot->xAxis->setTickLabelColor(Qt::gray); // 设置刻度标签颜色
m_customPlot->xAxis->setTickLabelRotation(
60); // 设置刻度标签的旋转角度,避免重叠
// 设置 Y 轴
m_customPlot->yAxis->setLabel("");
m_customPlot->yAxis->setBasePen(QPen(Qt::gray));
m_customPlot->yAxis->setTickPen(QPen(Qt::gray));
m_customPlot->yAxis->setSubTickPen(QPen(Qt::gray));
m_customPlot->yAxis->setTickLabelColor(Qt::gray);
m_customPlot->yAxis->setRange(0, 100); // 设置 Y 轴的范围
// 创建柱状图
m_barGraph =
new QCPBars(m_customPlot->xAxis, m_customPlot->yAxis); // 创建柱状图对象
m_barGraph->setWidth(0.2); // 设置柱子的宽度
m_barGraph->setPen(QPen(Qt::transparent)); // 设置柱子没有边框
m_barGraph->setBrush(QColor(0, 120, 255)); // 设置柱子的颜色
// 获取时间列表并设置到 X 轴
QDateTime currentTime = QDateTime::currentDateTime();
QList<QString> timeList = getLastSixFullHours(currentTime);
QVector<QString> ticks;
QVector<QString> labels;
for (int i = 0; i < timeList.size(); ++i) {
ticks.append(timeList[i]); // 设置刻度的位置
// labels.append(timeList[i]); // 设置刻度标签(时间)
// m_customPlot->xAxis->setLabel(timeList[i]);
}
m_customPlot->xAxis->setTicks(true); // 设置 X 轴刻度的位置
m_customPlot->xAxis->setTickLabels(true); // 设置刻度标签
// 设置 Y 轴的数据,这里用随机数作为示例
QVector<double> data; // Y 轴的数据
for (int i = 0; i < timeList.size(); ++i) {
data.append(rand() % 100); // 使用随机数填充数据,实际应用中应替换为真实数据
}
m_barGraph->setData(ticks, data); // 设置柱状图的数据
// 重新绘制图表
m_customPlot->replot();
}
void CoalStatisticsChartWidget::getCoalStatisInfoByHttpRequest(
const QString &beginTime, const QString &endTime,
const QString &statisticUnit) {
// 使用类成员变量 QNetworkAccessManager // 使用类成员变量 QNetworkAccessManager
QNetworkAccessManager *manager = new QNetworkAccessManager(this); QNetworkAccessManager *manager = new QNetworkAccessManager(this);
QString coalStaticUrl = m_systemSetting->value("coalStaticInterface/url").toString(); QString coalStaticUrl =
m_systemSetting->value("coalStaticInterface/url").toString();
// URL // URL
QUrl url(coalStaticUrl); QUrl url(coalStaticUrl);
@ -211,17 +291,20 @@ void CoalStatisticsChartWidget::getCoalStatisInfoByHttpRequest(const QString& be
// begin_time 参数 // begin_time 参数
QHttpPart beginTimePart; QHttpPart beginTimePart;
beginTimePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"begin_time\"")); beginTimePart.setHeader(QNetworkRequest::ContentDispositionHeader,
QVariant("form-data; name=\"begin_time\""));
beginTimePart.setBody(beginTime.toUtf8()); beginTimePart.setBody(beginTime.toUtf8());
// end_time 参数 // end_time 参数
QHttpPart endTimePart; QHttpPart endTimePart;
endTimePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"end_time\"")); endTimePart.setHeader(QNetworkRequest::ContentDispositionHeader,
QVariant("form-data; name=\"end_time\""));
endTimePart.setBody(endTime.toUtf8()); endTimePart.setBody(endTime.toUtf8());
// 统计单位参数 // 统计单位参数
QHttpPart statisticUnitPart; QHttpPart statisticUnitPart;
statisticUnitPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"statistic_unit\"")); statisticUnitPart.setHeader(QNetworkRequest::ContentDispositionHeader,
QVariant("form-data; name=\"statistic_unit\""));
statisticUnitPart.setBody(statisticUnit.toUtf8()); statisticUnitPart.setBody(statisticUnit.toUtf8());
// 将表单字段附加到 multipart 请求体中 // 将表单字段附加到 multipart 请求体中
@ -233,7 +316,7 @@ void CoalStatisticsChartWidget::getCoalStatisInfoByHttpRequest(const QString& be
QNetworkReply *reply = manager->post(request, multiPart); QNetworkReply *reply = manager->post(request, multiPart);
// 连接信号和槽 // 连接信号和槽
QObject::connect(reply, &QNetworkReply::finished, [this, reply, multiPart, &volumeDataMap]() { QObject::connect(reply, &QNetworkReply::finished, [this, reply, multiPart]() {
if (reply->error() == QNetworkReply::NoError) { if (reply->error() == QNetworkReply::NoError) {
// 处理成功的响应 // 处理成功的响应
QByteArray responseData = reply->readAll(); QByteArray responseData = reply->readAll();
@ -247,17 +330,18 @@ void CoalStatisticsChartWidget::getCoalStatisInfoByHttpRequest(const QString& be
QJsonObject statisticUnitVolum = data["statistic_unit_volum"].toObject(); QJsonObject statisticUnitVolum = data["statistic_unit_volum"].toObject();
int statisticUnitVolumSize = statisticUnitVolum.count(); int statisticUnitVolumSize = statisticUnitVolum.count();
QString statisticUnitVolumSizeString = "查询结果数" + QString::number(statisticUnitVolumSize); QString statisticUnitVolumSizeString =
"查询结果数" + QString::number(statisticUnitVolumSize);
emit querySuccessInfo(statisticUnitVolumSizeString); emit querySuccessInfo(statisticUnitVolumSizeString);
QString totalVolumeString = "总吨数" + QString::number(m_toalValue); QString totalVolumeString = "总吨数" + QString::number(m_toalValue);
emit querySuccessInfo(totalVolumeString); emit querySuccessInfo(totalVolumeString);
// 遍历 statistic_unit_volum 中的键值对并存入 volumeDataMap // 遍历 statistic_unit_volum 中的键值对并存入 volumeDataMap
// for (const QString& key : statisticUnitVolum.keys()) { for (const QString &key : statisticUnitVolum.keys()) {
// double value = statisticUnitVolum.value(key).toDouble(); double value = statisticUnitVolum.value(key).toDouble();
// volumeDataMap.insert(key, value); m_volumeDataMap.insert(key, value);
// } }
qDebug() << "Code:" << code; qDebug() << "Code:" << code;
qDebug() << "Message:" << message; qDebug() << "Message:" << message;
@ -276,8 +360,8 @@ void CoalStatisticsChartWidget::getCoalStatisInfoByHttpRequest(const QString& be
}); });
} }
void CoalStatisticsChartWidget::updateCoalStatisticsChart(const QMap<QString, double>& volumeDataMap) void CoalStatisticsChartWidget::updateCoalStatisticsChart(
{ const QMap<QString, double> &volumeDataMap) {
qDebug() << "更新图表程序" << volumeDataMap.size(); qDebug() << "更新图表程序" << volumeDataMap.size();
// 清除旧数据 // 清除旧数据
@ -328,6 +412,7 @@ void CoalStatisticsChartWidget::updateCoalStatisticsChart(const QMap<QString, do
// m_series->attachAxis(m_axisX); // m_series->attachAxis(m_axisX);
// 更新 Y 轴范围 // 更新 Y 轴范围
double maxValue = *std::max_element(volumeDataMap.begin(), volumeDataMap.end()); double maxValue =
*std::max_element(volumeDataMap.begin(), volumeDataMap.end());
m_axisY->setRange(0, maxValue + 10); // 设置为最大值并留出余量 m_axisY->setRange(0, maxValue + 10); // 设置为最大值并留出余量
} }

View File

@ -1,6 +1,8 @@
#ifndef COALSTATISTICSCHARTWIDGET_H #ifndef COALSTATISTICSCHARTWIDGET_H
#define COALSTATISTICSCHARTWIDGET_H #define COALSTATISTICSCHARTWIDGET_H
#include "component/qcustomplot/qcustomplot.h"
#include <QWidget> #include <QWidget>
#include <QtCharts> #include <QtCharts>
@ -39,6 +41,16 @@ private:
*/ */
void initCoalStatisticsBarChart(); void initCoalStatisticsBarChart();
/**
* @brief formatTimeRange
* @param beginTimeStr
* @param endTimeStr
* @param formattedBeginTime
* @param formattedEndTime
*/
void formatTimeRange(const QString &beginTimeStr, const QString &endTimeStr,
QString &formattedBeginTime, QString &formattedEndTime);
/** /**
* @brief getLastSixFullHours * @brief getLastSixFullHours
* @param currentTime * @param currentTime
@ -51,6 +63,8 @@ private:
*/ */
void initCustomCoalStatisticsBarChart(); void initCustomCoalStatisticsBarChart();
void initCustomPlotBarChart();
/** /**
* @brief getCoalStatisInfoByHttpRequest Http请求获取煤量统计信息 * @brief getCoalStatisInfoByHttpRequest Http请求获取煤量统计信息
* @param startTime * @param startTime
@ -58,7 +72,9 @@ private:
* @param statisticUnit * @param statisticUnit
* @param volumeDataMap Map * @param volumeDataMap Map
*/ */
void getCoalStatisInfoByHttpRequest(const QString& startTime, const QString& endTime, const QString& statisticUnit, QMap<QString, double>& volumeDataMap); void getCoalStatisInfoByHttpRequest(const QString &startTime,
const QString &endTime,
const QString &statisticUnit);
/** /**
* @brief updateCoalStatisticsChart * @brief updateCoalStatisticsChart
@ -79,9 +95,17 @@ private:
QTimer *m_timer; QTimer *m_timer;
QCustomPlot *m_customPlot;
QCPBars *m_barGraph;
QSettings *m_systemSetting; QSettings *m_systemSetting;
double m_toalValue = 0.0; double m_toalValue = 0.0;
/**
* @brief m_volumeDataMap
*/
QMap<QString, double> m_volumeDataMap;
}; };
#endif // COALSTATISTICSCHARTWIDGET_H #endif // COALSTATISTICSCHARTWIDGET_H