feat:qcutsomplots

This commit is contained in:
“Diviner66”, 2024-11-11 22:19:24 +08:00
parent c76b687c2a
commit 47793afcca
8 changed files with 44130 additions and 686 deletions

View File

@ -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

View File

@ -7,18 +7,19 @@
#include <QDateTime>
#include <QTimer>
MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
MainWindow::MainWindow(QWidget *parent)
: 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,17 +39,18 @@ 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);
QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
if (mouseEvent->type() == QEvent::MouseButtonPress) {
if (mouseEvent->button() == Qt::LeftButton) {
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,10 +109,9 @@ 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);
QTimer *timer_calendar = new QTimer(this);
connect(timer_calendar, &QTimer::timeout, this, [this]() {
QDateTime time = QDateTime::currentDateTime();
QString str = time.toString("yyyy-MM-dd hh:mm:ss dddd");
@ -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,10 +178,9 @@ void MainWindow::addcoalDistributChart()
coalDistributBarChartWidget);
}
void MainWindow::addbeltSpeedChat()
{
void MainWindow::addbeltSpeedChat() {
ui->textEdit_beltWarnInfo->append("添加皮带机速度曲线窗口成功!");
BeltSpeedLineWidget* beltSpeedLineWidget = new BeltSpeedLineWidget(this);
BeltSpeedLineWidget *beltSpeedLineWidget = new BeltSpeedLineWidget(this);
// 检查布局是否已设置
if (!ui->widget_beltSpeedChart->layout()) {
ui->widget_beltSpeedChart->setLayout(new QVBoxLayout);
@ -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();

View File

@ -13,18 +13,18 @@ class MainWindow : public QMainWindow {
Q_OBJECT
public:
explicit MainWindow(QWidget* parent = nullptr);
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
protected:
bool eventFilter(QObject* watched, QEvent* event) override;
bool eventFilter(QObject *watched, QEvent *event) override;
public slots:
/**
* @brief
* @param consoleInfo
*/
void addBeltWarnInfoToTextEdit(const QString& consoleInfo);
void addBeltWarnInfoToTextEdit(const QString &consoleInfo);
private slots:
void on_pushButton_close_clicked();
@ -39,6 +39,11 @@ private:
*/
void calendarLoopPlay();
/**
* @brief createSystemINI
*/
void createSystemINI();
/**
* @brief setSystemTitle
*/
@ -70,7 +75,7 @@ private:
void addCoalBeltVideo();
private:
Ui::MainWindow* ui;
Ui::MainWindow *ui;
/**
* @brief m_textEditConsoleInfo
@ -85,11 +90,11 @@ private:
/**
* @brief m_systemSetting
*/
QSettings* m_systemSetting;
QSettings *m_systemSetting;
// 窗口拖拽
QPoint m_mousePoint;
bool m_mousePressed { false };
bool m_mousePressed{false};
};
#endif // MAINWINDOW_H

View File

@ -19,21 +19,18 @@
#include <memory>
CoalDistributBarChartWidget::CoalDistributBarChartWidget(QWidget* parent)
: QWidget(parent)
, m_tcpSocket(new QTcpSocket(this))
{
CoalDistributBarChartWidget::CoalDistributBarChartWidget(QWidget *parent)
: 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();
@ -75,7 +70,7 @@ void CoalDistributBarChartWidget::initializeChart()
m_chart->addSeries(m_series);
// 设置X轴为据煤溜摄像机距离
QValueAxis* axisX = new QValueAxis;
QValueAxis *axisX = new QValueAxis;
axisX->setRange(1, 300);
axisX->setTickCount(10);
axisX->setLabelFormat("%d");
@ -86,7 +81,7 @@ void CoalDistributBarChartWidget::initializeChart()
m_series->attachAxis(axisX);
// 设置Y轴为百分比
QValueAxis* axisY = new QValueAxis;
QValueAxis *axisY = new QValueAxis;
axisY->setTitleText("占比(%)");
axisY->setTitleBrush(QBrush(Qt::gray));
axisY->setLinePen(QPen(Qt::gray));
@ -101,22 +96,21 @@ void CoalDistributBarChartWidget::initializeChart()
m_set = new QBarSet("测试1");
m_set->setColor(QColor(95, 180, 255));
m_set->setBorderColor(QColor(95, 180, 255));
QRandomGenerator* generator = QRandomGenerator::global();
QRandomGenerator *generator = QRandomGenerator::global();
for (int i = 0; i < 300; i++) {
int randomNumber = generator->bounded(10, 51);
*m_set << randomNumber;
}
m_series->append(m_set);
QVBoxLayout* layout = new QVBoxLayout(this);
QVBoxLayout *layout = new QVBoxLayout(this);
layout->setContentsMargins(0, 0, 0, 0);
layout->setSpacing(0); // 设置间距为0
layout->addWidget(m_chartView);
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); // 使用大端字节序

View File

@ -1,4 +1,4 @@
#include "coalstatisticschartwidget.h"
#include "coalstatisticschartwidget.h"
#include "ui_coalstatisticschartwidget.h"
#include <QHttpPart>
@ -10,67 +10,53 @@
#include <QUrl>
#include <QUrlQuery>
CoalStatisticsChartWidget::CoalStatisticsChartWidget(QWidget* parent)
: QWidget(parent)
, ui(new Ui::CoalStatisticsChartWidget)
{
CoalStatisticsChartWidget::CoalStatisticsChartWidget(QWidget *parent)
: 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 &currentTime) {
// 将当前时间设置为整点
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,34 +200,111 @@ 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);
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);
QNetworkRequest request(url);
// Form data
QHttpMultiPart* multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
// 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 请求体中
@ -230,10 +313,10 @@ void CoalStatisticsChartWidget::getCoalStatisInfoByHttpRequest(const QString& be
multiPart->append(statisticUnitPart);
// 发送 POST 请求
QNetworkReply* reply = manager->post(request, multiPart);
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,15 +360,15 @@ 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();
// 清除旧数据
m_series->clear();
// 创建新的数据集
QBarSet* set = new QBarSet("煤量");
QBarSet *set = new QBarSet("煤量");
// 用于存储时间标签
QList<QString> categories;
@ -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); // 设置为最大值并留出余量
}

View File

@ -1,6 +1,8 @@
#ifndef COALSTATISTICSCHARTWIDGET_H
#ifndef COALSTATISTICSCHARTWIDGET_H
#define COALSTATISTICSCHARTWIDGET_H
#include "component/qcustomplot/qcustomplot.h"
#include <QWidget>
#include <QtCharts>
@ -20,11 +22,11 @@ class CoalStatisticsChartWidget : public QWidget {
Q_OBJECT
public:
explicit CoalStatisticsChartWidget(QWidget* parent = nullptr);
explicit CoalStatisticsChartWidget(QWidget *parent = nullptr);
~CoalStatisticsChartWidget();
signals:
void querySuccessInfo(QString& queryInfo);
void querySuccessInfo(QString &queryInfo);
private slots:
@ -39,18 +41,30 @@ 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
* @return
*/
QList<QString> getLastSixFullHours(const QDateTime& currentTime);
QList<QString> getLastSixFullHours(const QDateTime &currentTime);
/**
* @brief initCustomCoalStatisticsBarChart
*/
void initCustomCoalStatisticsBarChart();
void initCustomPlotBarChart();
/**
* @brief getCoalStatisInfoByHttpRequest Http请求获取煤量统计信息
* @param startTime
@ -58,30 +72,40 @@ 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
* @param volumeDataMap
*/
void updateCoalStatisticsChart(const QMap<QString, double>& volumeDataMap);
void updateCoalStatisticsChart(const QMap<QString, double> &volumeDataMap);
private:
Ui::CoalStatisticsChartWidget* ui;
Ui::CoalStatisticsChartWidget *ui;
QChart* m_chart;
QChartView* m_chartView;
QBarSeries* m_series;
QBarCategoryAxis* m_axisX;
QChart *m_chart;
QChartView *m_chartView;
QBarSeries *m_series;
QBarCategoryAxis *m_axisX;
// QDateTimeAxis* m_axisX;
// QValueAxis* m_axisX;
QValueAxis* m_axisY;
QValueAxis *m_axisY;
QTimer* m_timer;
QTimer *m_timer;
QSettings* m_systemSetting;
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