diff --git a/mainwindow.cpp b/mainwindow.cpp index 763d32a..6ebdb65 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -19,6 +19,8 @@ MainWindow::MainWindow(QWidget* parent) m_systemSetting = new QSettings("system.ini", QSettings::IniFormat); + this->setWindowTitle(m_systemSetting->value("system/name").toString()); + calendarLoopPlay(); setSystemTitle(); @@ -69,6 +71,7 @@ bool MainWindow::eventFilter(QObject* watched, QEvent* event) void MainWindow::addBeltWarnInfoToTextEdit(const QString& consoleInfo) { + qDebug() << consoleInfo; m_textEditConsoleInfo.append(consoleInfo); QDateTime startDateTime = QDateTime::currentDateTime(); QString formattedTime = startDateTime.toString("yyyy-MM-dd hh:mm:ss"); @@ -77,10 +80,10 @@ void MainWindow::addBeltWarnInfoToTextEdit(const QString& consoleInfo) return; QString lastConsoleInfo = m_textEditConsoleInfo.last(); - qint64 timeDifferenceInMilliseconds = m_previousConsleInfoDateTime.secsTo(startDateTime); + // qint64 timeDifferenceInMilliseconds = m_previousConsleInfoDateTime.secsTo(startDateTime); - if ((lastConsoleInfo == consoleInfo) && timeDifferenceInMilliseconds < 2) - return; + // if ((lastConsoleInfo == consoleInfo) && timeDifferenceInMilliseconds < 2) + // return; QString consoleInfoResult = QStringLiteral("[%1]%2").arg(formattedTime).arg(consoleInfo); ui->textEdit_beltWarnInfo->append(consoleInfoResult); @@ -168,6 +171,8 @@ void MainWindow::addCoalStatisticsChart() coalStatisticsChartWidget->setContentsMargins(0, 0, 0, 0); ui->widget_coalStatisticsChart->layout()->addWidget( coalStatisticsChartWidget); + + connect(coalStatisticsChartWidget, &CoalStatisticsChartWidget::querySuccessInfo, this, &MainWindow::addBeltWarnInfoToTextEdit); } void MainWindow::addCoalBeltVideo() diff --git a/mainwindow.ui b/mainwindow.ui index 677791a..13d21c4 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -14,7 +14,7 @@ MainWindow - + :/Resource/image/logo.png:/Resource/image/logo.png @@ -1670,6 +1670,9 @@ QPushButton:pressed{ + + false + 0 @@ -2266,6 +2269,9 @@ QPushButton:pressed{ + + false + 0 @@ -3300,6 +3306,8 @@ QPushButton:pressed{ 1 - + + + diff --git a/view/coaldistributbarchartwidget.cpp b/view/coaldistributbarchartwidget.cpp index bf44aff..df2bc8a 100644 --- a/view/coaldistributbarchartwidget.cpp +++ b/view/coaldistributbarchartwidget.cpp @@ -44,7 +44,7 @@ CoalDistributBarChartWidget::~CoalDistributBarChartWidget() void CoalDistributBarChartWidget::updateChart() { int radio = getCoalRatioByTCPSocket(); - if (radio <= 0 || radio > 100) { + if (radio < 0 || radio > 90) { return; } diff --git a/view/coalstatisticschartwidget.cpp b/view/coalstatisticschartwidget.cpp index d1cbdbb..285afc0 100644 --- a/view/coalstatisticschartwidget.cpp +++ b/view/coalstatisticschartwidget.cpp @@ -18,9 +18,7 @@ CoalStatisticsChartWidget::CoalStatisticsChartWidget(QWidget* parent) m_systemSetting = new QSettings("system.ini", QSettings::IniFormat); - initCoalStatisticsBarChart(); - - getCoalStatisInfoByHttpRequest("2024-09-01 08", "2024-09-02 08"); + initCustomCoalStatisticsBarChart(); } CoalStatisticsChartWidget::~CoalStatisticsChartWidget() @@ -34,96 +32,41 @@ void CoalStatisticsChartWidget::on_pushButton_coalStatisOk_clicked() QString beginTimeStr = ui->dateTimeEdit_startTime->text(); QString endTimeStr = ui->dateTimeEdit_endTime->text(); - qDebug() << "beginTimeStr" << beginTimeStr; - qDebug() << "endTimeStr" << endTimeStr; + qDebug() << "beginTimeStr " << beginTimeStr; + qDebug() << "endTimeStr: " << endTimeStr; + + // 统计单位 + QString statisticUnit = ui->comboBox_unit->currentText(); // 创建 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()) { - qDebug() << "Invalid date/time format!"; - return; - } + // 格式化为 "yyyy-MM-d H" 形式 + QString formattedBeginTime = beginTime.toString("yyyy-MM-dd HH"); + QString formattedEndTime = endTime.toString("yyyy-MM-dd HH"); - int unitIndex = ui->comboBox_unit->currentIndex(); + QString startTimestr = "起始时间" + formattedBeginTime; + QString endTimestr = "终止时间" + formattedEndTime; - // 选择的时间单位 - switch (unitIndex) { - case TimeUnit::Hour: { - // 计算整时 - QList dateTimes; - int minutes = beginTime.time().minute(); // 获取分钟部分 - beginTime = beginTime.addSecs(-minutes * 60); // 向下取整 + emit querySuccessInfo(startTimestr); + emit querySuccessInfo(endTimestr); - // 遍历时间范围 - while (beginTime <= endTime) { - dateTimes.append(beginTime); - beginTime = beginTime.addSecs(3600); // 增加1小时 - } + // 输出格式化后的时间 + qDebug() << "Formatted Begin Time: " << formattedBeginTime; + qDebug() << "Formatted End Time: " << formattedEndTime; + qDebug() << "statisticUnit:" << statisticUnit; - // 输出结果 - for (const QDateTime& dateTime : dateTimes) { - qDebug() << "整小时数" << dateTime.toString("yyyy/MM/d HH:mm"); - } - break; - } - case TimeUnit::Day: { - // 计算整天 - QList dateTimes; - beginTime.setTime(QTime(0, 0)); // 将起始时间设置为当天的00:00:00 + // 查询结果信息 + QMap volumeDataMap; - // 遍历时间范围 - while (beginTime <= endTime) { - dateTimes.append(beginTime); - beginTime = beginTime.addDays(1); // 增加1天 - } + getCoalStatisInfoByHttpRequest(formattedBeginTime, formattedEndTime, statisticUnit, volumeDataMap); - // 输出结果 - for (const QDateTime& dateTime : dateTimes) { - qDebug() << "整天数" << dateTime.toString("yyyy/MM/d"); - } - break; - } - case TimeUnit::Month: { - // 计算整月 - QList dateTimes; - beginTime.setDate(QDate(beginTime.date().year(), beginTime.date().month(), 1)); // 设置为当月的1号 + ui->label_totalVolume->clear(); + ui->label_totalVolume->setText(QString("煤量总计:%1吨").arg(m_toalValue)); - // 遍历时间范围 - while (beginTime <= endTime) { - dateTimes.append(beginTime); - beginTime = beginTime.addMonths(1); // 增加1个月 - } - - // 输出结果 - for (const QDateTime& dateTime : dateTimes) { - qDebug() << "整月数" << dateTime.toString("yyyy/MM"); - } - break; - } - case TimeUnit::Year: { - // 计算整年 - QList dateTimes; - beginTime.setDate(QDate(beginTime.date().year(), 1, 1)); // 设置为当年的1月1日 - - // 遍历时间范围 - while (beginTime <= endTime) { - dateTimes.append(beginTime); - beginTime = beginTime.addYears(1); // 增加1年 - } - - // 输出结果 - for (const QDateTime& dateTime : dateTimes) { - qDebug() << "整年数" << dateTime.toString("yyyy"); - } - break; - } - default: - qDebug() << "无效的时间单位!"; - break; - } + // 更新查询的数据至图表 + // updateCoalStatisticsChart(volumeDataMap); } void CoalStatisticsChartWidget::initCoalStatisticsBarChart() @@ -134,68 +77,126 @@ void CoalStatisticsChartWidget::initCoalStatisticsBarChart() m_chart->setMargins(QMargins(0, 0, 0, 0)); m_chart->legend()->hide(); - // 创建图表视图并设置 + // 创建图表视图 m_chartView = new QChartView(m_chart); m_chartView->setBackgroundBrush(Qt::transparent); - m_chartView->setStyleSheet("background: transparent;"); m_chartView->setRenderHint(QPainter::Antialiasing); m_chartView->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); - // 创建柱状图 + // 创建柱状图数据集 m_series = new QBarSeries(); m_chart->addSeries(m_series); - // 设置X轴为时间 - m_axisX = new QDateTimeAxis; - m_axisX->setFormat("yyyy/MM/d H"); // 设置日期时间格式 - // m_axisX->setTitleText("时间"); - m_axisX->setTitleBrush(QBrush(Qt::gray)); - m_axisX->setLinePen(QPen(Qt::gray)); - m_axisX->setLabelsColor(Qt::gray); + // // 初始化 QDateTimeAxis 作为 X 轴 + // m_axisX = new QDateTimeAxis(); + // m_axisX->setFormat("yyyy-MM-dd HH"); // 设置时间格式 + // // m_axisX->setTitleText("时间"); + // m_axisX->setLabelsColor(Qt::gray); - // 获取当前时间并设置时间范围为最近六个小时 - QDateTime currentTime = QDateTime::currentDateTime(); - QDateTime startTime = currentTime.addSecs(-6 * 3600); // 当前时间减去六个小时 + // // 设置 X 轴范围为最近6小时 + // QDateTime currentTime = QDateTime::currentDateTime(); + // QDateTime startTime = currentTime.addSecs(-6 * 3600); // 当前时间前6小时 + // m_axisX->setRange(startTime, currentTime); - // m_axisX->setLabelsAngle(45); + // m_chart->addAxis(m_axisX, Qt::AlignBottom); + // m_series->attachAxis(m_axisX); - // 设置X轴时间范围 - m_axisX->setRange(startTime, currentTime); - m_chart->addAxis(m_axisX, Qt::AlignBottom); - m_series->attachAxis(m_axisX); - - // 设置Y轴为百分比 + // 设置Y轴 m_axisY = new QValueAxis; m_axisY->setTitleText("吨"); - m_axisY->setTitleBrush(QBrush(Qt::gray)); - m_axisY->setLinePen(QPen(Qt::gray)); m_axisY->setLabelsColor(Qt::gray); m_axisY->setRange(0, 100); m_chart->addAxis(m_axisY, Qt::AlignLeft); m_series->attachAxis(m_axisY); - m_axisY->setTitleFont(QFont("Arial", 10)); - m_axisY->setTitleVisible(true); - // 获取 widget_coalChart 的布局 + // 布局设置 QVBoxLayout* layout = static_cast(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_chartView) != -1) { + if (layout->indexOf(m_chartView) == -1) { layout->addWidget(m_chartView); } } -void CoalStatisticsChartWidget::getCoalStatisInfoByHttpRequest(const QString& beginTime, const QString& endTime) +QList CoalStatisticsChartWidget::getLastSixFullHours(const QDateTime& currentTime) { + // 将当前时间设置为整点 + QDateTime roundedTime = currentTime; + roundedTime.setTime(QTime(currentTime.time().hour(), 0)); // 设置时间到整点 + + bool insertTime = false; // 用于控制每隔一个插入时间,其他插入0 + + // 计算距当前整点数的前六个小时 + QList timeList; + for (int i = 6; i >= 1; --i) { + + QDateTime prevHour = roundedTime.addSecs(-i * 3600); // 向前推算每个整点 + + timeList.append(prevHour.toString("H")); + } + + return timeList; +} + +void CoalStatisticsChartWidget::initCustomCoalStatisticsBarChart() +{ + // 创建图表 + m_chart = new QChart(); + m_chart->setBackgroundBrush(Qt::transparent); + m_chart->setMargins(QMargins(0, 0, 0, 0)); + m_chart->legend()->hide(); + + // 创建图表视图 + m_chartView = new QChartView(m_chart); + m_chartView->setBackgroundBrush(Qt::transparent); + m_chartView->setRenderHint(QPainter::Antialiasing); + m_chartView->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); + + // 创建柱状图数据集 + m_series = new QBarSeries(); + m_chart->addSeries(m_series); + + // 创建X轴 + m_axisX = new QBarCategoryAxis(); + m_axisX->setLabelsColor(Qt::gray); + m_axisX->setLineVisible(true); + + QDateTime currentTime = QDateTime::currentDateTime(); + QList timeList = getLastSixFullHours(currentTime); + m_axisX->append(timeList); + m_chart->addAxis(m_axisX, Qt::AlignBottom); // 添加X轴到图表 + m_series->attachAxis(m_axisX); // 将X轴与柱状图数据集关联 + + // 设置Y轴 + m_axisY = new QValueAxis; + m_axisY->setTitleText("吨"); + m_axisY->setLabelsColor(Qt::gray); + m_axisY->setRange(0, 100); + m_chart->addAxis(m_axisY, Qt::AlignLeft); + m_series->attachAxis(m_axisY); + + // 布局设置 + QVBoxLayout* layout = static_cast(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_chartView) == -1) { + layout->addWidget(m_chartView); + } +} + +void CoalStatisticsChartWidget::getCoalStatisInfoByHttpRequest(const QString& beginTime, const QString& endTime, const QString& statisticUnit, QMap& volumeDataMap) +{ + // 使用类成员变量 QNetworkAccessManager QNetworkAccessManager* manager = new QNetworkAccessManager(this); @@ -218,15 +219,21 @@ void CoalStatisticsChartWidget::getCoalStatisInfoByHttpRequest(const QString& be endTimePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"end_time\"")); endTimePart.setBody(endTime.toUtf8()); + // 统计单位参数 + QHttpPart statisticUnitPart; + statisticUnitPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"statistic_unit\"")); + statisticUnitPart.setBody(statisticUnit.toUtf8()); + // 将表单字段附加到 multipart 请求体中 multiPart->append(beginTimePart); multiPart->append(endTimePart); + multiPart->append(statisticUnitPart); // 发送 POST 请求 QNetworkReply* reply = manager->post(request, multiPart); // 连接信号和槽 - QObject::connect(reply, &QNetworkReply::finished, [this, reply, multiPart]() { + QObject::connect(reply, &QNetworkReply::finished, [this, reply, multiPart, &volumeDataMap]() { if (reply->error() == QNetworkReply::NoError) { // 处理成功的响应 QByteArray responseData = reply->readAll(); @@ -236,14 +243,30 @@ void CoalStatisticsChartWidget::getCoalStatisInfoByHttpRequest(const QString& be int code = jsonObj["code"].toInt(); QString message = jsonObj["message"].toString(); QJsonObject data = jsonObj["data"].toObject(); - double totalVolume = data["total_volum"].toDouble(); + m_toalValue = data["total_volum"].toDouble(); + QJsonObject statisticUnitVolum = data["statistic_unit_volum"].toObject(); + + int statisticUnitVolumSize = statisticUnitVolum.count(); + QString statisticUnitVolumSizeString = "查询结果数" + QString::number(statisticUnitVolumSize); + emit querySuccessInfo(statisticUnitVolumSizeString); + + QString totalVolumeString = "总吨数" + QString::number(m_toalValue); + emit querySuccessInfo(totalVolumeString); + + // 遍历 statistic_unit_volum 中的键值对并存入 volumeDataMap + // for (const QString& key : statisticUnitVolum.keys()) { + // double value = statisticUnitVolum.value(key).toDouble(); + // volumeDataMap.insert(key, value); + // } qDebug() << "Code:" << code; qDebug() << "Message:" << message; - qDebug() << "Total Volume:" << totalVolume; + qDebug() << "Total Volume:" << m_toalValue; } else { // 处理错误 QString errorString = reply->errorString(); + + emit querySuccessInfo(errorString); qDebug() << "Error:" << errorString; } @@ -252,3 +275,59 @@ void CoalStatisticsChartWidget::getCoalStatisInfoByHttpRequest(const QString& be multiPart->deleteLater(); }); } + +void CoalStatisticsChartWidget::updateCoalStatisticsChart(const QMap& volumeDataMap) +{ + qDebug() << "更新图表程序" << volumeDataMap.size(); + + // 清除旧数据 + m_series->clear(); + + // 创建新的数据集 + QBarSet* set = new QBarSet("煤量"); + + // 用于存储时间标签 + QList categories; + + // 将字符串格式的时间转换为 QDateTime,并准备时间标签 + for (auto it = volumeDataMap.begin(); it != volumeDataMap.end(); ++it) { + // 将时间字符串转换为 QDateTime 对象 + QDateTime time = QDateTime::fromString(it.key(), "yyyy-MM-dd HH"); + + // 如果转换失败,跳过这个数据项 + if (!time.isValid()) { + continue; + } + + *set << it.value(); // 将数据量加入到数据集 + + // 根据选择的时间单位,格式化时间标签 + QString label; + if (ui->comboBox_unit->currentText() == QStringLiteral("时")) { + label = time.toString("H"); + } else if (ui->comboBox_unit->currentText() == QStringLiteral("日")) { + label = time.toString("dd"); // 使用 'dd' 来格式化日期 + } else if (ui->comboBox_unit->currentText() == QStringLiteral("月")) { + label = time.toString("MM"); // 使用 'MM' 来格式化月份 + } else if (ui->comboBox_unit->currentText() == QStringLiteral("年")) { + label = time.toString("yyyy"); // 使用 'yyyy' 来格式化年份 + } + + categories.append(label); // 将时间标签添加到列表 + } + + // 将数据集添加到 series + m_series->append(set); + + // 使用 QCategoryAxis 设置 X 轴为字符串格式的时间标签 + m_axisX->clear(); // 清除之前的标签 + m_axisX->append(categories); // 将新的时间标签添加到 X 轴 + + // 将 X 轴绑定到 series + // m_chart->addAxis(m_axisX, Qt::AlignBottom); + // m_series->attachAxis(m_axisX); + + // 更新 Y 轴范围 + double maxValue = *std::max_element(volumeDataMap.begin(), volumeDataMap.end()); + m_axisY->setRange(0, maxValue + 10); // 设置为最大值并留出余量 +} diff --git a/view/coalstatisticschartwidget.h b/view/coalstatisticschartwidget.h index 22d0c78..c200c9c 100644 --- a/view/coalstatisticschartwidget.h +++ b/view/coalstatisticschartwidget.h @@ -23,6 +23,9 @@ public: explicit CoalStatisticsChartWidget(QWidget* parent = nullptr); ~CoalStatisticsChartWidget(); +signals: + void querySuccessInfo(QString& queryInfo); + private slots: /** @@ -37,9 +40,31 @@ private: void initCoalStatisticsBarChart(); /** - * @brief getCoalStatisInfoByHttpRequest通过Http请求获取煤量统计信息 + * @brief getLastSixFullHours 计算前六个整点时间 + * @param currentTime 当前时间 + * @return 六个整点数 */ - void getCoalStatisInfoByHttpRequest(const QString& startTime, const QString& endTime); + QList getLastSixFullHours(const QDateTime& currentTime); + + /** + * @brief initCustomCoalStatisticsBarChart 初始化用户自定义柱状图 + */ + void initCustomCoalStatisticsBarChart(); + + /** + * @brief getCoalStatisInfoByHttpRequest 通过Http请求获取煤量统计信息 + * @param startTime 请求起始时间 + * @param endTime 请求终止时间 + * @param statisticUnit 统计单位 + * @param volumeDataMap 体积统计结果Map + */ + void getCoalStatisInfoByHttpRequest(const QString& startTime, const QString& endTime, const QString& statisticUnit, QMap& volumeDataMap); + + /** + * @brief updateCoalStatisticsChart 更新煤溜统计柱状图数据 + * @param volumeDataMap 统计结果数据 + */ + void updateCoalStatisticsChart(const QMap& volumeDataMap); private: Ui::CoalStatisticsChartWidget* ui; @@ -47,12 +72,16 @@ private: QChart* m_chart; QChartView* m_chartView; QBarSeries* m_series; - QDateTimeAxis* m_axisX; + QBarCategoryAxis* m_axisX; + // QDateTimeAxis* m_axisX; + // QValueAxis* m_axisX; QValueAxis* m_axisY; QTimer* m_timer; QSettings* m_systemSetting; + + double m_toalValue = 0.0; }; #endif // COALSTATISTICSCHARTWIDGET_H diff --git a/view/coalstatisticschartwidget.ui b/view/coalstatisticschartwidget.ui index 00fda3c..edeb446 100644 --- a/view/coalstatisticschartwidget.ui +++ b/view/coalstatisticschartwidget.ui @@ -13,6 +13,12 @@ Form + + #CoalStatisticsChartWidget +{ + background-color:transparent; +} + 6 @@ -63,7 +69,7 @@ - 0 + 120 25 @@ -115,7 +121,7 @@ QDateTimeEdit::up-button, QDateTimeEdit::down-button { - 0 + 120 25 @@ -208,13 +214,13 @@ QDateTimeEdit::up-button, QDateTimeEdit::down-button { - 0 - 0 + 45 + 20 - 45 + 50 20 @@ -251,7 +257,7 @@ QPushButton:pressed{ 10 - + QLabel{ color: rgb(255, 255, 255); @@ -276,65 +282,16 @@ QPushButton:pressed{ - - - - QRadioButton{ - - color: rgb(255, 255, 255); -} - - - - - - true - - - - - - - QRadioButton{ - - color: rgb(255, 255, 255); -} - - - - - - - - - - QRadioButton{ - - color: rgb(255, 255, 255); -} - - - - - - - - - - QRadioButton{ - - color: rgb(255, 255, 255); -} - - - - - - + + #widget_coalChart{ + + background-color:transparent; +} +