feat:qcutsomplots
This commit is contained in:
parent
c76b687c2a
commit
47793afcca
|
@ -7,6 +7,8 @@
|
|||
QT += core gui
|
||||
QT += charts
|
||||
QT += network
|
||||
QT += printsupport
|
||||
|
||||
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
|
||||
|
||||
TARGET = HX_CoalFlowDetect
|
||||
|
@ -26,6 +28,7 @@ DEFINES += QT_DEPRECATED_WARNINGS
|
|||
CONFIG += c++11
|
||||
|
||||
SOURCES += \
|
||||
component/qcustomplot/qcustomplot.cpp \
|
||||
controller/coaldistributmodbusthread.cpp \
|
||||
main.cpp \
|
||||
mainwindow.cpp \
|
||||
|
@ -38,6 +41,7 @@ SOURCES += \
|
|||
component/ZDateEdit/ztimeedit.cpp\
|
||||
|
||||
HEADERS += \
|
||||
component/qcustomplot/qcustomplot.h \
|
||||
controller/coaldistributmodbusthread.h \
|
||||
mainwindow.h \
|
||||
view/beltspeedlinewidget.h \
|
||||
|
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
100
mainwindow.cpp
100
mainwindow.cpp
|
@ -8,17 +8,18 @@
|
|||
#include <QTimer>
|
||||
|
||||
MainWindow::MainWindow(QWidget *parent)
|
||||
: QMainWindow(parent)
|
||||
, ui(new Ui::MainWindow)
|
||||
{
|
||||
: QMainWindow(parent), ui(new Ui::MainWindow) {
|
||||
ui->setupUi(this);
|
||||
this->setWindowFlag(Qt::FramelessWindowHint);
|
||||
this->setWindowFlags(windowFlags() | Qt::WindowMaximizeButtonHint | Qt::WindowMinimizeButtonHint);
|
||||
this->setWindowFlags(windowFlags() | Qt::WindowMaximizeButtonHint |
|
||||
Qt::WindowMinimizeButtonHint);
|
||||
|
||||
ui->widget_head->installEventFilter(this);
|
||||
|
||||
m_systemSetting = new QSettings("system.ini", QSettings::IniFormat);
|
||||
|
||||
createSystemINI();
|
||||
|
||||
this->setWindowTitle(m_systemSetting->value("system/name").toString());
|
||||
|
||||
calendarLoopPlay();
|
||||
|
@ -38,8 +39,7 @@ MainWindow::MainWindow(QWidget* parent)
|
|||
|
||||
MainWindow::~MainWindow() { delete ui; }
|
||||
|
||||
bool MainWindow::eventFilter(QObject* watched, QEvent* event)
|
||||
{
|
||||
bool MainWindow::eventFilter(QObject *watched, QEvent *event) {
|
||||
// 处理鼠标按下、释放和移动事件
|
||||
if (watched == ui->widget_head) {
|
||||
QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
|
||||
|
@ -48,7 +48,9 @@ bool MainWindow::eventFilter(QObject* watched, QEvent* event)
|
|||
m_mousePressed = true; // 启动拖拽
|
||||
m_mousePoint = mouseEvent->globalPos() - this->pos();
|
||||
|
||||
this->setWindowFlags(windowFlags() | Qt::WindowMaximizeButtonHint | Qt::WindowMinimizeButtonHint | Qt::Window | Qt::WindowStaysOnTopHint);
|
||||
this->setWindowFlags(windowFlags() | Qt::WindowMaximizeButtonHint |
|
||||
Qt::WindowMinimizeButtonHint | Qt::Window |
|
||||
Qt::WindowStaysOnTopHint);
|
||||
this->show(); // 确保窗口显示
|
||||
|
||||
return true;
|
||||
|
@ -61,7 +63,9 @@ bool MainWindow::eventFilter(QObject* watched, QEvent* event)
|
|||
this->move(mouseEvent->globalPos() - m_mousePoint);
|
||||
|
||||
// 每次移动时设置为最上方
|
||||
this->setWindowFlags(windowFlags() | Qt::WindowMaximizeButtonHint | Qt::WindowMinimizeButtonHint | Qt::Window | Qt::WindowStaysOnTopHint);
|
||||
this->setWindowFlags(windowFlags() | Qt::WindowMaximizeButtonHint |
|
||||
Qt::WindowMinimizeButtonHint | Qt::Window |
|
||||
Qt::WindowStaysOnTopHint);
|
||||
this->show();
|
||||
return true;
|
||||
}
|
||||
|
@ -69,8 +73,7 @@ bool MainWindow::eventFilter(QObject* watched, QEvent* event)
|
|||
}
|
||||
}
|
||||
|
||||
void MainWindow::addBeltWarnInfoToTextEdit(const QString& consoleInfo)
|
||||
{
|
||||
void MainWindow::addBeltWarnInfoToTextEdit(const QString &consoleInfo) {
|
||||
qDebug() << consoleInfo;
|
||||
m_textEditConsoleInfo.append(consoleInfo);
|
||||
QDateTime startDateTime = QDateTime::currentDateTime();
|
||||
|
@ -80,12 +83,15 @@ 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)
|
||||
// if ((lastConsoleInfo == consoleInfo) && timeDifferenceInMilliseconds <
|
||||
// 2)
|
||||
// return;
|
||||
|
||||
QString consoleInfoResult = QStringLiteral("[%1]%2").arg(formattedTime).arg(consoleInfo);
|
||||
QString consoleInfoResult =
|
||||
QStringLiteral("[%1]%2").arg(formattedTime).arg(consoleInfo);
|
||||
ui->textEdit_beltWarnInfo->append(consoleInfoResult);
|
||||
|
||||
m_previousConsleInfoDateTime = QDateTime::currentDateTime();
|
||||
|
@ -93,8 +99,7 @@ void MainWindow::addBeltWarnInfoToTextEdit(const QString& consoleInfo)
|
|||
|
||||
void MainWindow::on_pushButton_close_clicked() { this->close(); }
|
||||
|
||||
void MainWindow::on_pushButton_max_clicked()
|
||||
{
|
||||
void MainWindow::on_pushButton_max_clicked() {
|
||||
if (isFullScreen()) {
|
||||
this->showMaximized();
|
||||
} else {
|
||||
|
@ -104,8 +109,7 @@ void MainWindow::on_pushButton_max_clicked()
|
|||
|
||||
void MainWindow::on_pushButton_min_clicked() { this->showMinimized(); }
|
||||
|
||||
void MainWindow::calendarLoopPlay()
|
||||
{
|
||||
void MainWindow::calendarLoopPlay() {
|
||||
ui->label_calendar->clear();
|
||||
QTimer *timer_calendar = new QTimer(this);
|
||||
connect(timer_calendar, &QTimer::timeout, this, [this]() {
|
||||
|
@ -116,8 +120,35 @@ void MainWindow::calendarLoopPlay()
|
|||
timer_calendar->start(1000); //每一秒溢出一次进入槽函数
|
||||
}
|
||||
|
||||
void MainWindow::setSystemTitle()
|
||||
{
|
||||
void MainWindow::createSystemINI() {
|
||||
// 写入数据到INI文件
|
||||
// 判断并设置 "system/name"
|
||||
if (!m_systemSetting->contains("system/name")) {
|
||||
m_systemSetting->setValue("system/name", "煤流监测平台");
|
||||
}
|
||||
|
||||
// 判断并设置 "coalStaticInterface/url"
|
||||
if (!m_systemSetting->contains("coalStaticInterface/url")) {
|
||||
m_systemSetting->setValue("coalStaticInterface/url", 30);
|
||||
}
|
||||
|
||||
// 判断并设置 "addr/ip"
|
||||
if (!m_systemSetting->contains("addr/ip")) {
|
||||
m_systemSetting->setValue("addr/ip", "127.0.0.1");
|
||||
}
|
||||
|
||||
// 判断并设置 "addr/port"
|
||||
if (!m_systemSetting->contains("addr/port")) {
|
||||
m_systemSetting->setValue("addr/port", 3000);
|
||||
}
|
||||
|
||||
// 判断并设置 "camera/rtsp"
|
||||
if (!m_systemSetting->contains("camera/rtsp")) {
|
||||
m_systemSetting->setValue("camera/rtsp", 3000);
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::setSystemTitle() {
|
||||
QString titleName = m_systemSetting->value("system/name").toString();
|
||||
if (!titleName.isEmpty()) {
|
||||
ui->label_title->clear();
|
||||
|
@ -125,19 +156,18 @@ void MainWindow::setSystemTitle()
|
|||
}
|
||||
}
|
||||
|
||||
void MainWindow::setBeltWarnInfoTextEditStyle()
|
||||
{
|
||||
void MainWindow::setBeltWarnInfoTextEditStyle() {
|
||||
ui->textEdit_beltWarnInfo->document()->setMaximumBlockCount(100);
|
||||
ui->textEdit_beltWarnInfo->setReadOnly(true);
|
||||
ui->textEdit_beltWarnInfo->setTextInteractionFlags(Qt::NoTextInteraction);
|
||||
}
|
||||
|
||||
void MainWindow::addcoalDistributChart()
|
||||
{
|
||||
void MainWindow::addcoalDistributChart() {
|
||||
// addBeltWarnInfoToTextEdit("添加煤流分布窗口成功!");
|
||||
ui->textEdit_beltWarnInfo->append("添加煤流分布窗口成功!");
|
||||
|
||||
CoalDistributBarChartWidget* coalDistributBarChartWidget = new CoalDistributBarChartWidget(this);
|
||||
CoalDistributBarChartWidget *coalDistributBarChartWidget =
|
||||
new CoalDistributBarChartWidget(this);
|
||||
|
||||
// 检查布局是否已设置
|
||||
if (!ui->widget_coalDistributChart->layout()) {
|
||||
|
@ -148,8 +178,7 @@ void MainWindow::addcoalDistributChart()
|
|||
coalDistributBarChartWidget);
|
||||
}
|
||||
|
||||
void MainWindow::addbeltSpeedChat()
|
||||
{
|
||||
void MainWindow::addbeltSpeedChat() {
|
||||
ui->textEdit_beltWarnInfo->append("添加皮带机速度曲线窗口成功!");
|
||||
BeltSpeedLineWidget *beltSpeedLineWidget = new BeltSpeedLineWidget(this);
|
||||
// 检查布局是否已设置
|
||||
|
@ -160,10 +189,10 @@ void MainWindow::addbeltSpeedChat()
|
|||
ui->widget_beltSpeedChart->layout()->addWidget(beltSpeedLineWidget);
|
||||
}
|
||||
|
||||
void MainWindow::addCoalStatisticsChart()
|
||||
{
|
||||
void MainWindow::addCoalStatisticsChart() {
|
||||
ui->textEdit_beltWarnInfo->append("添加煤量统计窗口成功!");
|
||||
CoalStatisticsChartWidget* coalStatisticsChartWidget = new CoalStatisticsChartWidget(this);
|
||||
CoalStatisticsChartWidget *coalStatisticsChartWidget =
|
||||
new CoalStatisticsChartWidget(this);
|
||||
// 检查布局是否已设置
|
||||
if (!ui->widget_beltSpeedChart->layout()) {
|
||||
ui->widget_beltSpeedChart->setLayout(new QVBoxLayout);
|
||||
|
@ -172,14 +201,17 @@ void MainWindow::addCoalStatisticsChart()
|
|||
ui->widget_coalStatisticsChart->layout()->addWidget(
|
||||
coalStatisticsChartWidget);
|
||||
|
||||
connect(coalStatisticsChartWidget, &CoalStatisticsChartWidget::querySuccessInfo, this, &MainWindow::addBeltWarnInfoToTextEdit);
|
||||
connect(coalStatisticsChartWidget,
|
||||
&CoalStatisticsChartWidget::querySuccessInfo, this,
|
||||
&MainWindow::addBeltWarnInfoToTextEdit);
|
||||
}
|
||||
|
||||
void MainWindow::addCoalBeltVideo()
|
||||
{
|
||||
// QString url = "https://stream7.iqilu.com/10339/upload_transcode/202002/18/"
|
||||
void MainWindow::addCoalBeltVideo() {
|
||||
// QString url =
|
||||
// "https://stream7.iqilu.com/10339/upload_transcode/202002/18/"
|
||||
// "20200218114723HDu3hhxqIT.mp4";
|
||||
// QString url = "rtsp://admin:1234qwer@192.168.80.82:554/Streaming/Channels/101";
|
||||
// QString url =
|
||||
// "rtsp://admin:1234qwer@192.168.80.82:554/Streaming/Channels/101";
|
||||
QString url = m_systemSetting->value("camera/rtsp").toString();
|
||||
ui->widget_beltVideo->setUrl(url);
|
||||
ui->widget_beltVideo->open();
|
||||
|
|
|
@ -39,6 +39,11 @@ private:
|
|||
*/
|
||||
void calendarLoopPlay();
|
||||
|
||||
/**
|
||||
* @brief createSystemINI 创建系统配置文件
|
||||
*/
|
||||
void createSystemINI();
|
||||
|
||||
/**
|
||||
* @brief setSystemTitle 设置系统标题
|
||||
*/
|
||||
|
|
|
@ -20,20 +20,17 @@
|
|||
#include <memory>
|
||||
|
||||
CoalDistributBarChartWidget::CoalDistributBarChartWidget(QWidget *parent)
|
||||
: QWidget(parent)
|
||||
, m_tcpSocket(new QTcpSocket(this))
|
||||
{
|
||||
: QWidget(parent), m_tcpSocket(new QTcpSocket(this)) {
|
||||
initializeChart();
|
||||
|
||||
m_systemSetting = new QSettings("system.ini", QSettings::IniFormat);
|
||||
|
||||
connectTCPSocket();
|
||||
// connectTCPSocket();
|
||||
|
||||
setupTimer();
|
||||
// setupTimer();
|
||||
}
|
||||
|
||||
CoalDistributBarChartWidget::~CoalDistributBarChartWidget()
|
||||
{
|
||||
CoalDistributBarChartWidget::~CoalDistributBarChartWidget() {
|
||||
if (m_tcpSocket->state() == QAbstractSocket::ConnectedState) {
|
||||
m_tcpSocket->disconnectFromHost();
|
||||
m_tcpSocket->waitForDisconnected();
|
||||
|
@ -41,8 +38,7 @@ CoalDistributBarChartWidget::~CoalDistributBarChartWidget()
|
|||
delete m_tcpSocket;
|
||||
}
|
||||
|
||||
void CoalDistributBarChartWidget::updateChart()
|
||||
{
|
||||
void CoalDistributBarChartWidget::updateChart() {
|
||||
int radio = getCoalRatioByTCPSocket();
|
||||
if (radio < 0 || radio > 90) {
|
||||
return;
|
||||
|
@ -54,8 +50,7 @@ void CoalDistributBarChartWidget::updateChart()
|
|||
m_set->insert(0, radio);
|
||||
}
|
||||
|
||||
void CoalDistributBarChartWidget::initializeChart()
|
||||
{
|
||||
void CoalDistributBarChartWidget::initializeChart() {
|
||||
// 创建图标
|
||||
m_chart = new QChart();
|
||||
|
||||
|
@ -115,8 +110,7 @@ void CoalDistributBarChartWidget::initializeChart()
|
|||
setLayout(layout);
|
||||
}
|
||||
|
||||
void CoalDistributBarChartWidget::setupTimer()
|
||||
{
|
||||
void CoalDistributBarChartWidget::setupTimer() {
|
||||
// 定时器设置
|
||||
m_timer = new QTimer(this);
|
||||
connect(m_timer, &QTimer::timeout, this,
|
||||
|
@ -124,8 +118,7 @@ void CoalDistributBarChartWidget::setupTimer()
|
|||
m_timer->start(1000); // 每秒更新一次
|
||||
}
|
||||
|
||||
void CoalDistributBarChartWidget::connectTCPSocket()
|
||||
{
|
||||
void CoalDistributBarChartWidget::connectTCPSocket() {
|
||||
QString ipAddress = m_systemSetting->value("addr/ip").toString();
|
||||
quint16 port = m_systemSetting->value("addr/port").toInt();
|
||||
|
||||
|
@ -136,8 +129,7 @@ void CoalDistributBarChartWidget::connectTCPSocket()
|
|||
}
|
||||
}
|
||||
|
||||
int CoalDistributBarChartWidget::getCoalRatioByTCPSocket()
|
||||
{
|
||||
int CoalDistributBarChartWidget::getCoalRatioByTCPSocket() {
|
||||
if (m_tcpSocket->state() != QAbstractSocket::ConnectedState) {
|
||||
qDebug() << "未连接到服务器";
|
||||
return -1; // 返回错误值
|
||||
|
@ -218,8 +210,7 @@ int CoalDistributBarChartWidget::getCoalRatioByTCPSocket()
|
|||
return decimalValue; // 返回煤量占比
|
||||
}
|
||||
|
||||
QByteArray CoalDistributBarChartWidget::createModbusTcpRequest()
|
||||
{
|
||||
QByteArray CoalDistributBarChartWidget::createModbusTcpRequest() {
|
||||
QByteArray request;
|
||||
QDataStream stream(&request, QIODevice::WriteOnly);
|
||||
stream.setByteOrder(QDataStream::BigEndian); // 使用大端字节序
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
#include "coalstatisticschartwidget.h"
|
||||
#include "coalstatisticschartwidget.h"
|
||||
#include "ui_coalstatisticschartwidget.h"
|
||||
|
||||
#include <QHttpPart>
|
||||
|
@ -11,66 +11,52 @@
|
|||
#include <QUrlQuery>
|
||||
|
||||
CoalStatisticsChartWidget::CoalStatisticsChartWidget(QWidget *parent)
|
||||
: QWidget(parent)
|
||||
, ui(new Ui::CoalStatisticsChartWidget)
|
||||
{
|
||||
: QWidget(parent), ui(new Ui::CoalStatisticsChartWidget) {
|
||||
ui->setupUi(this);
|
||||
|
||||
m_systemSetting = new QSettings("system.ini", QSettings::IniFormat);
|
||||
|
||||
initCustomCoalStatisticsBarChart();
|
||||
ui->dateTimeEdit_startTime->setDateTime(
|
||||
QDateTime::currentDateTime().addSecs(-8 * 3600));
|
||||
|
||||
// initCustomCoalStatisticsBarChart();
|
||||
|
||||
initCustomPlotBarChart();
|
||||
}
|
||||
|
||||
CoalStatisticsChartWidget::~CoalStatisticsChartWidget()
|
||||
{
|
||||
delete ui;
|
||||
}
|
||||
CoalStatisticsChartWidget::~CoalStatisticsChartWidget() { delete ui; }
|
||||
|
||||
void CoalStatisticsChartWidget::on_pushButton_coalStatisOk_clicked()
|
||||
{
|
||||
void CoalStatisticsChartWidget::on_pushButton_coalStatisOk_clicked() {
|
||||
// 时间范围
|
||||
QString beginTimeStr = ui->dateTimeEdit_startTime->text();
|
||||
QString endTimeStr = ui->dateTimeEdit_endTime->text();
|
||||
|
||||
qDebug() << "beginTimeStr " << beginTimeStr;
|
||||
qDebug() << "endTimeStr: " << endTimeStr;
|
||||
// 定义输出格式化后的时间
|
||||
QString formattedBeginTime;
|
||||
QString formattedEndTime;
|
||||
|
||||
formatTimeRange(beginTimeStr, endTimeStr, formattedBeginTime,
|
||||
formattedEndTime);
|
||||
|
||||
// 统计单位
|
||||
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");
|
||||
|
||||
// 格式化为 "yyyy-MM-d H" 形式
|
||||
QString formattedBeginTime = beginTime.toString("yyyy-MM-dd HH");
|
||||
QString formattedEndTime = endTime.toString("yyyy-MM-dd HH");
|
||||
|
||||
QString startTimestr = "起始时间" + formattedBeginTime;
|
||||
QString endTimestr = "终止时间" + formattedEndTime;
|
||||
|
||||
emit querySuccessInfo(startTimestr);
|
||||
emit querySuccessInfo(endTimestr);
|
||||
|
||||
// 输出格式化后的时间
|
||||
qDebug() << "Formatted Begin Time: " << formattedBeginTime;
|
||||
qDebug() << "Formatted End Time: " << formattedEndTime;
|
||||
qDebug() << "statisticUnit:" << statisticUnit;
|
||||
|
||||
// 查询结果信息
|
||||
QMap<QString, double> volumeDataMap;
|
||||
|
||||
getCoalStatisInfoByHttpRequest(formattedBeginTime, formattedEndTime, statisticUnit, volumeDataMap);
|
||||
m_volumeDataMap.clear();
|
||||
getCoalStatisInfoByHttpRequest(formattedBeginTime, formattedEndTime,
|
||||
statisticUnit);
|
||||
|
||||
ui->label_totalVolume->clear();
|
||||
ui->label_totalVolume->setText(QString("煤量总计:%1吨").arg(m_toalValue));
|
||||
|
||||
QString volumeDataMapStr = "总数" + QString::number(m_volumeDataMap.size());
|
||||
|
||||
emit querySuccessInfo(volumeDataMapStr);
|
||||
|
||||
// 更新查询的数据至图表
|
||||
// updateCoalStatisticsChart(volumeDataMap);
|
||||
}
|
||||
|
||||
void CoalStatisticsChartWidget::initCoalStatisticsBarChart()
|
||||
{
|
||||
void CoalStatisticsChartWidget::initCoalStatisticsBarChart() {
|
||||
// 创建图表
|
||||
m_chart = new QChart();
|
||||
m_chart->setBackgroundBrush(Qt::transparent);
|
||||
|
@ -110,7 +96,8 @@ void CoalStatisticsChartWidget::initCoalStatisticsBarChart()
|
|||
m_series->attachAxis(m_axisY);
|
||||
|
||||
// 布局设置
|
||||
QVBoxLayout* layout = static_cast<QVBoxLayout*>(ui->widget_coalChart->layout());
|
||||
QVBoxLayout *layout =
|
||||
static_cast<QVBoxLayout *>(ui->widget_coalChart->layout());
|
||||
if (!layout) {
|
||||
layout = new QVBoxLayout(ui->widget_coalChart);
|
||||
ui->widget_coalChart->setLayout(layout);
|
||||
|
@ -123,8 +110,27 @@ void CoalStatisticsChartWidget::initCoalStatisticsBarChart()
|
|||
}
|
||||
}
|
||||
|
||||
QList<QString> CoalStatisticsChartWidget::getLastSixFullHours(const QDateTime& currentTime)
|
||||
{
|
||||
void CoalStatisticsChartWidget::formatTimeRange(const QString &beginTimeStr,
|
||||
const QString &endTimeStr,
|
||||
QString &formattedBeginTime,
|
||||
QString &formattedEndTime) {
|
||||
// 创建 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()) {
|
||||
qWarning() << "Invalid time format!";
|
||||
return;
|
||||
}
|
||||
|
||||
// 格式化为 "yyyy-MM-dd HH" 形式
|
||||
formattedBeginTime = beginTime.toString("yyyy-MM-dd HH");
|
||||
formattedEndTime = endTime.toString("yyyy-MM-dd HH");
|
||||
}
|
||||
|
||||
QList<QString>
|
||||
CoalStatisticsChartWidget::getLastSixFullHours(const QDateTime ¤tTime) {
|
||||
// 将当前时间设置为整点
|
||||
QDateTime roundedTime = currentTime;
|
||||
roundedTime.setTime(QTime(currentTime.time().hour(), 0)); // 设置时间到整点
|
||||
|
@ -137,14 +143,13 @@ QList<QString> CoalStatisticsChartWidget::getLastSixFullHours(const QDateTime& c
|
|||
|
||||
QDateTime prevHour = roundedTime.addSecs(-i * 3600); // 向前推算每个整点
|
||||
|
||||
timeList.append(prevHour.toString("H"));
|
||||
timeList.append(prevHour.toString("YYYY/MM/dd H"));
|
||||
}
|
||||
|
||||
return timeList;
|
||||
}
|
||||
|
||||
void CoalStatisticsChartWidget::initCustomCoalStatisticsBarChart()
|
||||
{
|
||||
void CoalStatisticsChartWidget::initCustomCoalStatisticsBarChart() {
|
||||
// 创建图表
|
||||
m_chart = new QChart();
|
||||
m_chart->setBackgroundBrush(Qt::transparent);
|
||||
|
@ -181,7 +186,8 @@ void CoalStatisticsChartWidget::initCustomCoalStatisticsBarChart()
|
|||
m_series->attachAxis(m_axisY);
|
||||
|
||||
// 布局设置
|
||||
QVBoxLayout* layout = static_cast<QVBoxLayout*>(ui->widget_coalChart->layout());
|
||||
QVBoxLayout *layout =
|
||||
static_cast<QVBoxLayout *>(ui->widget_coalChart->layout());
|
||||
if (!layout) {
|
||||
layout = new QVBoxLayout(ui->widget_coalChart);
|
||||
ui->widget_coalChart->setLayout(layout);
|
||||
|
@ -194,13 +200,87 @@ void CoalStatisticsChartWidget::initCustomCoalStatisticsBarChart()
|
|||
}
|
||||
}
|
||||
|
||||
void CoalStatisticsChartWidget::getCoalStatisInfoByHttpRequest(const QString& beginTime, const QString& endTime, const QString& statisticUnit, QMap<QString, double>& volumeDataMap)
|
||||
{
|
||||
void CoalStatisticsChartWidget::initCustomPlotBarChart() {
|
||||
// 创建 QCustomPlot 图表
|
||||
m_customPlot =
|
||||
new QCustomPlot(ui->widget_coalChart); // 假设 ui->widget_coalChart 是容器
|
||||
m_customPlot->setBackground(Qt::transparent); // 设置背景透明
|
||||
|
||||
// 设置布局
|
||||
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_customPlot) == -1) {
|
||||
layout->addWidget(m_customPlot); // 将 QCustomPlot 添加到布局中
|
||||
}
|
||||
|
||||
// 设置 X 轴
|
||||
m_customPlot->xAxis->setLabel("时间");
|
||||
m_customPlot->xAxis->setBasePen(QPen(Qt::gray)); // 设置轴线颜色
|
||||
m_customPlot->xAxis->setTickPen(QPen(Qt::gray)); // 设置刻度线颜色
|
||||
m_customPlot->xAxis->setSubTickPen(QPen(Qt::gray)); // 设置子刻度线颜色
|
||||
m_customPlot->xAxis->setTickLabelColor(Qt::gray); // 设置刻度标签颜色
|
||||
m_customPlot->xAxis->setTickLabelRotation(
|
||||
60); // 设置刻度标签的旋转角度,避免重叠
|
||||
|
||||
// 设置 Y 轴
|
||||
m_customPlot->yAxis->setLabel("吨");
|
||||
m_customPlot->yAxis->setBasePen(QPen(Qt::gray));
|
||||
m_customPlot->yAxis->setTickPen(QPen(Qt::gray));
|
||||
m_customPlot->yAxis->setSubTickPen(QPen(Qt::gray));
|
||||
m_customPlot->yAxis->setTickLabelColor(Qt::gray);
|
||||
m_customPlot->yAxis->setRange(0, 100); // 设置 Y 轴的范围
|
||||
|
||||
// 创建柱状图
|
||||
m_barGraph =
|
||||
new QCPBars(m_customPlot->xAxis, m_customPlot->yAxis); // 创建柱状图对象
|
||||
m_barGraph->setWidth(0.2); // 设置柱子的宽度
|
||||
m_barGraph->setPen(QPen(Qt::transparent)); // 设置柱子没有边框
|
||||
m_barGraph->setBrush(QColor(0, 120, 255)); // 设置柱子的颜色
|
||||
|
||||
// 获取时间列表并设置到 X 轴
|
||||
QDateTime currentTime = QDateTime::currentDateTime();
|
||||
QList<QString> timeList = getLastSixFullHours(currentTime);
|
||||
|
||||
QVector<QString> ticks;
|
||||
QVector<QString> labels;
|
||||
for (int i = 0; i < timeList.size(); ++i) {
|
||||
ticks.append(timeList[i]); // 设置刻度的位置
|
||||
// labels.append(timeList[i]); // 设置刻度标签(时间)
|
||||
|
||||
// m_customPlot->xAxis->setLabel(timeList[i]);
|
||||
}
|
||||
|
||||
m_customPlot->xAxis->setTicks(true); // 设置 X 轴刻度的位置
|
||||
m_customPlot->xAxis->setTickLabels(true); // 设置刻度标签
|
||||
|
||||
// 设置 Y 轴的数据,这里用随机数作为示例
|
||||
QVector<double> data; // Y 轴的数据
|
||||
for (int i = 0; i < timeList.size(); ++i) {
|
||||
data.append(rand() % 100); // 使用随机数填充数据,实际应用中应替换为真实数据
|
||||
}
|
||||
|
||||
m_barGraph->setData(ticks, data); // 设置柱状图的数据
|
||||
|
||||
// 重新绘制图表
|
||||
m_customPlot->replot();
|
||||
}
|
||||
|
||||
void CoalStatisticsChartWidget::getCoalStatisInfoByHttpRequest(
|
||||
const QString &beginTime, const QString &endTime,
|
||||
const QString &statisticUnit) {
|
||||
|
||||
// 使用类成员变量 QNetworkAccessManager
|
||||
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
|
||||
|
||||
QString coalStaticUrl = m_systemSetting->value("coalStaticInterface/url").toString();
|
||||
QString coalStaticUrl =
|
||||
m_systemSetting->value("coalStaticInterface/url").toString();
|
||||
|
||||
// URL
|
||||
QUrl url(coalStaticUrl);
|
||||
|
@ -211,17 +291,20 @@ void CoalStatisticsChartWidget::getCoalStatisInfoByHttpRequest(const QString& be
|
|||
|
||||
// begin_time 参数
|
||||
QHttpPart beginTimePart;
|
||||
beginTimePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"begin_time\""));
|
||||
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.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.setHeader(QNetworkRequest::ContentDispositionHeader,
|
||||
QVariant("form-data; name=\"statistic_unit\""));
|
||||
statisticUnitPart.setBody(statisticUnit.toUtf8());
|
||||
|
||||
// 将表单字段附加到 multipart 请求体中
|
||||
|
@ -233,7 +316,7 @@ void CoalStatisticsChartWidget::getCoalStatisInfoByHttpRequest(const QString& be
|
|||
QNetworkReply *reply = manager->post(request, multiPart);
|
||||
|
||||
// 连接信号和槽
|
||||
QObject::connect(reply, &QNetworkReply::finished, [this, reply, multiPart, &volumeDataMap]() {
|
||||
QObject::connect(reply, &QNetworkReply::finished, [this, reply, multiPart]() {
|
||||
if (reply->error() == QNetworkReply::NoError) {
|
||||
// 处理成功的响应
|
||||
QByteArray responseData = reply->readAll();
|
||||
|
@ -247,17 +330,18 @@ void CoalStatisticsChartWidget::getCoalStatisInfoByHttpRequest(const QString& be
|
|||
QJsonObject statisticUnitVolum = data["statistic_unit_volum"].toObject();
|
||||
|
||||
int statisticUnitVolumSize = statisticUnitVolum.count();
|
||||
QString statisticUnitVolumSizeString = "查询结果数" + QString::number(statisticUnitVolumSize);
|
||||
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);
|
||||
// }
|
||||
for (const QString &key : statisticUnitVolum.keys()) {
|
||||
double value = statisticUnitVolum.value(key).toDouble();
|
||||
m_volumeDataMap.insert(key, value);
|
||||
}
|
||||
|
||||
qDebug() << "Code:" << code;
|
||||
qDebug() << "Message:" << message;
|
||||
|
@ -276,8 +360,8 @@ void CoalStatisticsChartWidget::getCoalStatisInfoByHttpRequest(const QString& be
|
|||
});
|
||||
}
|
||||
|
||||
void CoalStatisticsChartWidget::updateCoalStatisticsChart(const QMap<QString, double>& volumeDataMap)
|
||||
{
|
||||
void CoalStatisticsChartWidget::updateCoalStatisticsChart(
|
||||
const QMap<QString, double> &volumeDataMap) {
|
||||
qDebug() << "更新图表程序" << volumeDataMap.size();
|
||||
|
||||
// 清除旧数据
|
||||
|
@ -328,6 +412,7 @@ void CoalStatisticsChartWidget::updateCoalStatisticsChart(const QMap<QString, do
|
|||
// m_series->attachAxis(m_axisX);
|
||||
|
||||
// 更新 Y 轴范围
|
||||
double maxValue = *std::max_element(volumeDataMap.begin(), volumeDataMap.end());
|
||||
double maxValue =
|
||||
*std::max_element(volumeDataMap.begin(), volumeDataMap.end());
|
||||
m_axisY->setRange(0, maxValue + 10); // 设置为最大值并留出余量
|
||||
}
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
#ifndef COALSTATISTICSCHARTWIDGET_H
|
||||
#ifndef COALSTATISTICSCHARTWIDGET_H
|
||||
#define COALSTATISTICSCHARTWIDGET_H
|
||||
|
||||
#include "component/qcustomplot/qcustomplot.h"
|
||||
|
||||
#include <QWidget>
|
||||
#include <QtCharts>
|
||||
|
||||
|
@ -39,6 +41,16 @@ private:
|
|||
*/
|
||||
void initCoalStatisticsBarChart();
|
||||
|
||||
/**
|
||||
* @brief formatTimeRange 格式化时间范围
|
||||
* @param beginTimeStr
|
||||
* @param endTimeStr
|
||||
* @param formattedBeginTime
|
||||
* @param formattedEndTime
|
||||
*/
|
||||
void formatTimeRange(const QString &beginTimeStr, const QString &endTimeStr,
|
||||
QString &formattedBeginTime, QString &formattedEndTime);
|
||||
|
||||
/**
|
||||
* @brief getLastSixFullHours 计算前六个整点时间
|
||||
* @param currentTime 当前时间
|
||||
|
@ -51,6 +63,8 @@ private:
|
|||
*/
|
||||
void initCustomCoalStatisticsBarChart();
|
||||
|
||||
void initCustomPlotBarChart();
|
||||
|
||||
/**
|
||||
* @brief getCoalStatisInfoByHttpRequest 通过Http请求获取煤量统计信息
|
||||
* @param startTime 请求起始时间
|
||||
|
@ -58,7 +72,9 @@ private:
|
|||
* @param statisticUnit 统计单位
|
||||
* @param volumeDataMap 体积统计结果Map
|
||||
*/
|
||||
void getCoalStatisInfoByHttpRequest(const QString& startTime, const QString& endTime, const QString& statisticUnit, QMap<QString, double>& volumeDataMap);
|
||||
void getCoalStatisInfoByHttpRequest(const QString &startTime,
|
||||
const QString &endTime,
|
||||
const QString &statisticUnit);
|
||||
|
||||
/**
|
||||
* @brief updateCoalStatisticsChart 更新煤溜统计柱状图数据
|
||||
|
@ -79,9 +95,17 @@ private:
|
|||
|
||||
QTimer *m_timer;
|
||||
|
||||
QCustomPlot *m_customPlot;
|
||||
QCPBars *m_barGraph;
|
||||
|
||||
QSettings *m_systemSetting;
|
||||
|
||||
double m_toalValue = 0.0;
|
||||
|
||||
/**
|
||||
* @brief m_volumeDataMap 查询出煤量数据
|
||||
*/
|
||||
QMap<QString, double> m_volumeDataMap;
|
||||
};
|
||||
|
||||
#endif // COALSTATISTICSCHARTWIDGET_H
|
||||
|
|
Loading…
Reference in New Issue