#include "coalstatisticschartwidget.h" #include "ui_coalstatisticschartwidget.h" #include #include #include #include #include #include #include #include CoalStatisticsChartWidget::CoalStatisticsChartWidget(QWidget* parent) : QWidget(parent) , ui(new Ui::CoalStatisticsChartWidget) { ui->setupUi(this); m_systemSetting = new QSettings("system.ini", QSettings::IniFormat); initCoalStatisticsBarChart(); getCoalStatisInfoByHttpRequest("2024-09-01 08", "2024-09-02 08"); } CoalStatisticsChartWidget::~CoalStatisticsChartWidget() { delete ui; } 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 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 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 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 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; } } 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->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); // 获取当前时间并设置时间范围为最近六个小时 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); // 设置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) { layout->addWidget(m_chartView); } } void CoalStatisticsChartWidget::getCoalStatisInfoByHttpRequest(const QString& beginTime, const QString& endTime) { // 使用类成员变量 QNetworkAccessManager QNetworkAccessManager* manager = new QNetworkAccessManager(this); QString coalStaticUrl = m_systemSetting->value("coalStaticInterface/url").toString(); // URL QUrl url(coalStaticUrl); QNetworkRequest request(url); // Form data QHttpMultiPart* multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType); // begin_time 参数 QHttpPart beginTimePart; beginTimePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"begin_time\"")); beginTimePart.setBody(beginTime.toUtf8()); // 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); // 发送 POST 请求 QNetworkReply* reply = manager->post(request, multiPart); // 连接信号和槽 QObject::connect(reply, &QNetworkReply::finished, [this, reply, multiPart]() { 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(); double totalVolume = data["total_volum"].toDouble(); qDebug() << "Code:" << code; qDebug() << "Message:" << message; qDebug() << "Total Volume:" << totalVolume; } else { // 处理错误 QString errorString = reply->errorString(); qDebug() << "Error:" << errorString; } // 清理资源 reply->deleteLater(); multiPart->deleteLater(); }); }