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-01 17:40:37 +08:00
|
|
|
initCoalStatisticsBarChart();
|
2024-11-04 18:11:55 +08:00
|
|
|
|
|
|
|
getCoalStatisInfoByHttpRequest("2024-09-01 08", "2024-09-02 08");
|
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();
|
|
|
|
|
|
|
|
qDebug() << "beginTimeStr" << beginTimeStr;
|
|
|
|
qDebug() << "endTimeStr" << endTimeStr;
|
|
|
|
|
|
|
|
// 创建 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
int unitIndex = ui->comboBox_unit->currentIndex();
|
|
|
|
|
|
|
|
// 选择的时间单位
|
|
|
|
switch (unitIndex) {
|
|
|
|
case TimeUnit::Hour: {
|
|
|
|
// 计算整时
|
|
|
|
QList<QDateTime> dateTimes;
|
|
|
|
int minutes = beginTime.time().minute(); // 获取分钟部分
|
|
|
|
beginTime = beginTime.addSecs(-minutes * 60); // 向下取整
|
|
|
|
|
|
|
|
// 遍历时间范围
|
|
|
|
while (beginTime <= endTime) {
|
|
|
|
dateTimes.append(beginTime);
|
|
|
|
beginTime = beginTime.addSecs(3600); // 增加1小时
|
|
|
|
}
|
|
|
|
|
|
|
|
// 输出结果
|
|
|
|
for (const QDateTime& dateTime : dateTimes) {
|
|
|
|
qDebug() << "整小时数" << dateTime.toString("yyyy/MM/d HH:mm");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TimeUnit::Day: {
|
|
|
|
// 计算整天
|
|
|
|
QList<QDateTime> dateTimes;
|
|
|
|
beginTime.setTime(QTime(0, 0)); // 将起始时间设置为当天的00:00:00
|
|
|
|
|
|
|
|
// 遍历时间范围
|
|
|
|
while (beginTime <= endTime) {
|
|
|
|
dateTimes.append(beginTime);
|
|
|
|
beginTime = beginTime.addDays(1); // 增加1天
|
|
|
|
}
|
|
|
|
|
|
|
|
// 输出结果
|
|
|
|
for (const QDateTime& dateTime : dateTimes) {
|
|
|
|
qDebug() << "整天数" << dateTime.toString("yyyy/MM/d");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TimeUnit::Month: {
|
|
|
|
// 计算整月
|
|
|
|
QList<QDateTime> dateTimes;
|
|
|
|
beginTime.setDate(QDate(beginTime.date().year(), beginTime.date().month(), 1)); // 设置为当月的1号
|
|
|
|
|
|
|
|
// 遍历时间范围
|
|
|
|
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<QDateTime> 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-01 17:40:37 +08:00
|
|
|
void CoalStatisticsChartWidget::initCoalStatisticsBarChart()
|
|
|
|
{
|
|
|
|
// 创建图表
|
|
|
|
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
|
|
|
|
|
|
|
// 创建图表视图并设置
|
|
|
|
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轴为时间
|
2024-11-05 17:48:05 +08:00
|
|
|
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);
|
|
|
|
|
|
|
|
// 获取当前时间并设置时间范围为最近六个小时
|
|
|
|
QDateTime currentTime = QDateTime::currentDateTime();
|
|
|
|
QDateTime startTime = currentTime.addSecs(-6 * 3600); // 当前时间减去六个小时
|
|
|
|
|
|
|
|
// m_axisX->setLabelsAngle(45);
|
|
|
|
|
|
|
|
// 设置X轴时间范围
|
|
|
|
m_axisX->setRange(startTime, currentTime);
|
|
|
|
m_chart->addAxis(m_axisX, Qt::AlignBottom);
|
|
|
|
m_series->attachAxis(m_axisX);
|
2024-11-01 17:40:37 +08:00
|
|
|
|
|
|
|
// 设置Y轴为百分比
|
2024-11-05 17:48:05 +08:00
|
|
|
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);
|
2024-11-01 17:40:37 +08:00
|
|
|
|
|
|
|
// 获取 widget_coalChart 的布局
|
|
|
|
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_chartView) != -1) {
|
|
|
|
layout->addWidget(m_chartView);
|
|
|
|
}
|
|
|
|
}
|
2024-11-04 18:11:55 +08:00
|
|
|
|
|
|
|
void CoalStatisticsChartWidget::getCoalStatisInfoByHttpRequest(const QString& beginTime, const QString& endTime)
|
|
|
|
{
|
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());
|
|
|
|
|
|
|
|
// 将表单字段附加到 multipart 请求体中
|
|
|
|
multiPart->append(beginTimePart);
|
|
|
|
multiPart->append(endTimePart);
|
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-05 17:48:05 +08:00
|
|
|
QObject::connect(reply, &QNetworkReply::finished, [this, reply, multiPart]() {
|
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-05 17:48:05 +08:00
|
|
|
double totalVolume = data["total_volum"].toDouble();
|
2024-11-04 18:11:55 +08:00
|
|
|
|
|
|
|
qDebug() << "Code:" << code;
|
|
|
|
qDebug() << "Message:" << message;
|
|
|
|
qDebug() << "Total Volume:" << totalVolume;
|
|
|
|
} else {
|
|
|
|
// 处理错误
|
2024-11-05 17:48:05 +08:00
|
|
|
QString errorString = reply->errorString();
|
|
|
|
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
|
|
|
});
|
|
|
|
}
|