HX_CoalFlowDetect/view/coalstatisticschartwidget.cpp

334 lines
12 KiB
C++
Raw Normal View History

2024-11-01 17:40:37 +08:00
#include "coalstatisticschartwidget.h"
#include "ui_coalstatisticschartwidget.h"
2024-11-05 17:48:05 +08:00
#include <QHttpPart>
2024-11-04 18:11:55 +08:00
#include <QJsonDocument>
#include <QJsonObject>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QUrl>
#include <QUrlQuery>
2024-11-01 17:40:37 +08:00
CoalStatisticsChartWidget::CoalStatisticsChartWidget(QWidget* parent)
: QWidget(parent)
, ui(new Ui::CoalStatisticsChartWidget)
{
ui->setupUi(this);
2024-11-05 17:48:05 +08:00
m_systemSetting = new QSettings("system.ini", QSettings::IniFormat);
2024-11-11 17:48:22 +08:00
initCustomCoalStatisticsBarChart();
2024-11-01 17:40:37 +08:00
}
CoalStatisticsChartWidget::~CoalStatisticsChartWidget()
{
delete ui;
}
2024-11-05 17:48:05 +08:00
void CoalStatisticsChartWidget::on_pushButton_coalStatisOk_clicked()
{
// 时间范围
QString beginTimeStr = ui->dateTimeEdit_startTime->text();
QString endTimeStr = ui->dateTimeEdit_endTime->text();
2024-11-11 17:48:22 +08:00
qDebug() << "beginTimeStr " << beginTimeStr;
qDebug() << "endTimeStr: " << endTimeStr;
// 统计单位
QString statisticUnit = ui->comboBox_unit->currentText();
2024-11-05 17:48:05 +08:00
// 创建 QDateTime 对象
QDateTime beginTime = QDateTime::fromString(beginTimeStr, "yyyy/MM/d HH:mm");
QDateTime endTime = QDateTime::fromString(endTimeStr, "yyyy/MM/d HH:mm");
2024-11-11 17:48:22 +08:00
// 格式化为 "yyyy-MM-d H" 形式
QString formattedBeginTime = beginTime.toString("yyyy-MM-dd HH");
QString formattedEndTime = endTime.toString("yyyy-MM-dd HH");
2024-11-05 17:48:05 +08:00
2024-11-11 17:48:22 +08:00
QString startTimestr = "起始时间" + formattedBeginTime;
QString endTimestr = "终止时间" + formattedEndTime;
2024-11-05 17:48:05 +08:00
2024-11-11 17:48:22 +08:00
emit querySuccessInfo(startTimestr);
emit querySuccessInfo(endTimestr);
2024-11-05 17:48:05 +08:00
2024-11-11 17:48:22 +08:00
// 输出格式化后的时间
qDebug() << "Formatted Begin Time: " << formattedBeginTime;
qDebug() << "Formatted End Time: " << formattedEndTime;
qDebug() << "statisticUnit:" << statisticUnit;
2024-11-05 17:48:05 +08:00
2024-11-11 17:48:22 +08:00
// 查询结果信息
QMap<QString, double> volumeDataMap;
2024-11-05 17:48:05 +08:00
2024-11-11 17:48:22 +08:00
getCoalStatisInfoByHttpRequest(formattedBeginTime, formattedEndTime, statisticUnit, volumeDataMap);
2024-11-05 17:48:05 +08:00
2024-11-11 17:48:22 +08:00
ui->label_totalVolume->clear();
ui->label_totalVolume->setText(QString("煤量总计:%1吨").arg(m_toalValue));
// 更新查询的数据至图表
// updateCoalStatisticsChart(volumeDataMap);
}
void CoalStatisticsChartWidget::initCoalStatisticsBarChart()
{
// 创建图表
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);
// // 初始化 QDateTimeAxis 作为 X 轴
// m_axisX = new QDateTimeAxis();
// m_axisX->setFormat("yyyy-MM-dd HH"); // 设置时间格式
// // m_axisX->setTitleText("时间");
// m_axisX->setLabelsColor(Qt::gray);
// // 设置 X 轴范围为最近6小时
// QDateTime currentTime = QDateTime::currentDateTime();
// QDateTime startTime = currentTime.addSecs(-6 * 3600); // 当前时间前6小时
// m_axisX->setRange(startTime, currentTime);
// m_chart->addAxis(m_axisX, Qt::AlignBottom);
// m_series->attachAxis(m_axisX);
// 设置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<QVBoxLayout*>(ui->widget_coalChart->layout());
if (!layout) {
layout = new QVBoxLayout(ui->widget_coalChart);
ui->widget_coalChart->setLayout(layout);
2024-11-05 17:48:05 +08:00
}
2024-11-11 17:48:22 +08:00
layout->setContentsMargins(0, 0, 0, 0);
layout->setSpacing(0);
2024-11-05 17:48:05 +08:00
2024-11-11 17:48:22 +08:00
if (layout->indexOf(m_chartView) == -1) {
layout->addWidget(m_chartView);
2024-11-05 17:48:05 +08:00
}
2024-11-11 17:48:22 +08:00
}
QList<QString> CoalStatisticsChartWidget::getLastSixFullHours(const QDateTime& currentTime)
{
// 将当前时间设置为整点
QDateTime roundedTime = currentTime;
roundedTime.setTime(QTime(currentTime.time().hour(), 0)); // 设置时间到整点
bool insertTime = false; // 用于控制每隔一个插入时间其他插入0
// 计算距当前整点数的前六个小时
QList<QString> timeList;
for (int i = 6; i >= 1; --i) {
QDateTime prevHour = roundedTime.addSecs(-i * 3600); // 向前推算每个整点
timeList.append(prevHour.toString("H"));
2024-11-05 17:48:05 +08:00
}
2024-11-11 17:48:22 +08:00
return timeList;
2024-11-05 17:48:05 +08:00
}
2024-11-11 17:48:22 +08:00
void CoalStatisticsChartWidget::initCustomCoalStatisticsBarChart()
2024-11-01 17:40:37 +08:00
{
// 创建图表
m_chart = new QChart();
m_chart->setBackgroundBrush(Qt::transparent);
m_chart->setMargins(QMargins(0, 0, 0, 0));
2024-11-04 18:11:55 +08:00
m_chart->legend()->hide();
2024-11-01 17:40:37 +08:00
2024-11-11 17:48:22 +08:00
// 创建图表视图
2024-11-01 17:40:37 +08:00
m_chartView = new QChartView(m_chart);
m_chartView->setBackgroundBrush(Qt::transparent);
m_chartView->setRenderHint(QPainter::Antialiasing);
m_chartView->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
2024-11-11 17:48:22 +08:00
// 创建柱状图数据集
2024-11-01 17:40:37 +08:00
m_series = new QBarSeries();
m_chart->addSeries(m_series);
2024-11-11 17:48:22 +08:00
// 创建X轴
m_axisX = new QBarCategoryAxis();
2024-11-05 17:48:05 +08:00
m_axisX->setLabelsColor(Qt::gray);
2024-11-11 17:48:22 +08:00
m_axisX->setLineVisible(true);
2024-11-05 17:48:05 +08:00
QDateTime currentTime = QDateTime::currentDateTime();
2024-11-11 17:48:22 +08:00
QList<QString> timeList = getLastSixFullHours(currentTime);
m_axisX->append(timeList);
m_chart->addAxis(m_axisX, Qt::AlignBottom); // 添加X轴到图表
m_series->attachAxis(m_axisX); // 将X轴与柱状图数据集关联
2024-11-05 17:48:05 +08:00
2024-11-11 17:48:22 +08:00
// 设置Y轴
2024-11-05 17:48:05 +08:00
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);
2024-11-01 17:40:37 +08:00
2024-11-11 17:48:22 +08:00
// 布局设置
2024-11-01 17:40:37 +08:00
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);
2024-11-11 17:48:22 +08:00
if (layout->indexOf(m_chartView) == -1) {
2024-11-01 17:40:37 +08:00
layout->addWidget(m_chartView);
}
}
2024-11-04 18:11:55 +08:00
2024-11-11 17:48:22 +08:00
void CoalStatisticsChartWidget::getCoalStatisInfoByHttpRequest(const QString& beginTime, const QString& endTime, const QString& statisticUnit, QMap<QString, double>& volumeDataMap)
2024-11-04 18:11:55 +08:00
{
2024-11-11 17:48:22 +08:00
2024-11-05 17:48:05 +08:00
// 使用类成员变量 QNetworkAccessManager
QNetworkAccessManager* manager = new QNetworkAccessManager(this);
QString coalStaticUrl = m_systemSetting->value("coalStaticInterface/url").toString();
2024-11-04 18:11:55 +08:00
// URL
2024-11-05 17:48:05 +08:00
QUrl url(coalStaticUrl);
2024-11-04 18:11:55 +08:00
QNetworkRequest request(url);
// Form data
2024-11-05 17:48:05 +08:00
QHttpMultiPart* multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
// begin_time 参数
QHttpPart beginTimePart;
beginTimePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"begin_time\""));
beginTimePart.setBody(beginTime.toUtf8());
2024-11-04 18:11:55 +08:00
2024-11-05 17:48:05 +08:00
// end_time 参数
QHttpPart endTimePart;
endTimePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"end_time\""));
endTimePart.setBody(endTime.toUtf8());
2024-11-11 17:48:22 +08:00
// 统计单位参数
QHttpPart statisticUnitPart;
statisticUnitPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"statistic_unit\""));
statisticUnitPart.setBody(statisticUnit.toUtf8());
2024-11-05 17:48:05 +08:00
// 将表单字段附加到 multipart 请求体中
multiPart->append(beginTimePart);
multiPart->append(endTimePart);
2024-11-11 17:48:22 +08:00
multiPart->append(statisticUnitPart);
2024-11-04 18:11:55 +08:00
// 发送 POST 请求
2024-11-05 17:48:05 +08:00
QNetworkReply* reply = manager->post(request, multiPart);
2024-11-04 18:11:55 +08:00
// 连接信号和槽
2024-11-11 17:48:22 +08:00
QObject::connect(reply, &QNetworkReply::finished, [this, reply, multiPart, &volumeDataMap]() {
2024-11-04 18:11:55 +08:00
if (reply->error() == QNetworkReply::NoError) {
// 处理成功的响应
QByteArray responseData = reply->readAll();
QJsonDocument jsonResponse = QJsonDocument::fromJson(responseData);
QJsonObject jsonObj = jsonResponse.object();
int code = jsonObj["code"].toInt();
QString message = jsonObj["message"].toString();
QJsonObject data = jsonObj["data"].toObject();
2024-11-11 17:48:22 +08:00
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);
// }
2024-11-04 18:11:55 +08:00
qDebug() << "Code:" << code;
qDebug() << "Message:" << message;
2024-11-11 17:48:22 +08:00
qDebug() << "Total Volume:" << m_toalValue;
2024-11-04 18:11:55 +08:00
} else {
// 处理错误
2024-11-05 17:48:05 +08:00
QString errorString = reply->errorString();
2024-11-11 17:48:22 +08:00
emit querySuccessInfo(errorString);
2024-11-05 17:48:05 +08:00
qDebug() << "Error:" << errorString;
2024-11-04 18:11:55 +08:00
}
2024-11-05 17:48:05 +08:00
// 清理资源
2024-11-04 18:11:55 +08:00
reply->deleteLater();
2024-11-05 17:48:05 +08:00
multiPart->deleteLater();
2024-11-04 18:11:55 +08:00
});
}
2024-11-11 17:48:22 +08:00
void CoalStatisticsChartWidget::updateCoalStatisticsChart(const QMap<QString, double>& volumeDataMap)
{
qDebug() << "更新图表程序" << volumeDataMap.size();
// 清除旧数据
m_series->clear();
// 创建新的数据集
QBarSet* set = new QBarSet("煤量");
// 用于存储时间标签
QList<QString> 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); // 设置为最大值并留出余量
}