2024-11-01 17:40:37 +08:00
|
|
|
#include "coalstatisticschartwidget.h"
|
|
|
|
#include "ui_coalstatisticschartwidget.h"
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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轴为时间
|
|
|
|
QValueAxis* axisX = new QValueAxis;
|
|
|
|
axisX->setRange(1, 300);
|
|
|
|
axisX->setTickCount(10);
|
|
|
|
axisX->setLabelFormat("%d");
|
|
|
|
axisX->setLinePen(QPen(Qt::gray));
|
|
|
|
axisX->setLabelsColor(Qt::gray);
|
|
|
|
axisX->setMinorTickCount(0);
|
|
|
|
m_chart->addAxis(axisX, Qt::AlignBottom);
|
|
|
|
m_series->attachAxis(axisX);
|
|
|
|
|
|
|
|
// 设置Y轴为百分比
|
|
|
|
QValueAxis* axisY = new QValueAxis;
|
|
|
|
axisY->setTitleText("吨");
|
|
|
|
axisY->setTitleBrush(QBrush(Qt::gray));
|
|
|
|
axisY->setLinePen(QPen(Qt::gray));
|
|
|
|
axisY->setLabelsColor(Qt::gray);
|
|
|
|
axisY->setRange(0, 100);
|
|
|
|
m_chart->addAxis(axisY, Qt::AlignLeft);
|
|
|
|
m_series->attachAxis(axisY);
|
|
|
|
|
|
|
|
// 设置Y轴标题位置
|
|
|
|
axisY->setTitleFont(QFont("Arial", 10));
|
|
|
|
axisY->setTitleVisible(true);
|
|
|
|
|
|
|
|
// 获取 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)
|
|
|
|
{
|
|
|
|
QNetworkAccessManager* manager = new QNetworkAccessManager();
|
|
|
|
|
|
|
|
// URL
|
|
|
|
QUrl url("http://192.168.5.205:5002/api/get/coal/statistics/value/");
|
|
|
|
QNetworkRequest request(url);
|
|
|
|
|
|
|
|
// Form data
|
|
|
|
QUrlQuery params;
|
|
|
|
params.addQueryItem("begin_time", beginTime);
|
|
|
|
params.addQueryItem("end_time", endTime);
|
|
|
|
|
|
|
|
// 设置请求头
|
|
|
|
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
|
|
|
|
|
|
|
|
// 发送 POST 请求
|
|
|
|
QNetworkReply* reply = manager->post(request, params.query(QUrl::FullyEncoded).toUtf8());
|
|
|
|
|
|
|
|
// 连接信号和槽
|
|
|
|
QObject::connect(reply, &QNetworkReply::finished, [reply]() {
|
|
|
|
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_volume"].toDouble();
|
|
|
|
|
|
|
|
qDebug() << "Code:" << code;
|
|
|
|
qDebug() << "Message:" << message;
|
|
|
|
qDebug() << "Total Volume:" << totalVolume;
|
|
|
|
} else {
|
|
|
|
// 处理错误
|
|
|
|
qDebug() << "Error:" << reply->errorString();
|
|
|
|
}
|
|
|
|
reply->deleteLater();
|
|
|
|
});
|
|
|
|
}
|