feat:添加折线图信息

This commit is contained in:
徐超 2024-11-01 17:40:37 +08:00
parent 03f215f312
commit 8e78e7c4ef
21 changed files with 936 additions and 62 deletions

View File

@ -27,15 +27,29 @@ CONFIG += c++11
SOURCES += \ SOURCES += \
main.cpp \ main.cpp \
mainwindow.cpp \ mainwindow.cpp \
view/coaldistributchartwidget.cpp view/beltspeedlinewidget.cpp \
view/coaldistributbarchartwidget.cpp \
view/coalstatisticschartwidget.cpp\
component/ZDateEdit/dateCommonInfo.cpp\
component/ZDateEdit/zdateedit.cpp\
component/ZDateEdit/zdatetimeedit.cpp\
component/ZDateEdit/ztimeedit.cpp\
HEADERS += \ HEADERS += \
mainwindow.h \ mainwindow.h \
view/coaldistributchartwidget.h view/beltspeedlinewidget.h \
view/coaldistributbarchartwidget.h \
view/coalstatisticschartwidget.h\
component/ZDateEdit/dateCommonInfo.h\
component/ZDateEdit/zdateedit.h\
component/ZDateEdit/zdatetimeedit.h\
component/ZDateEdit/ztimeedit.h\
FORMS += \ FORMS += \
mainwindow.ui \ mainwindow.ui \
view/coaldistributchartwidget.ui view/coalstatisticschartwidget.ui
# Default rules for deployment. # Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin qnx: target.path = /tmp/$${TARGET}/bin

BIN
Resource/logo.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 146 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 223 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 211 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 429 B

View File

@ -0,0 +1,16 @@
QPushButton#popupButton{
border-image: url(:/component/ZDateEdit/Resources/popupButton.png);min-height:20px;max-height:20px;min-width:20px;max-width:20px;background-color:#ffffff;
}
QLabel{color:#cadfff; font-size:12px;}
QLabel#labelWeekend{color:#00caff;}
QLable#label_splite{font: bold 14px;}
QWidget#widgetTitle{background-color:#061f65;}
QWidget#baseWidget{background-color:#EE013B6B;border-radius: 11px;}
QComboBox{background-color: transparent; selection-background-color: transparent; color:#ffffff; font-size: 14px; min-height: 20px; selection-text-align: right;border:0px solid}
QComboBox::down-arrow{ image:url(:/component/ZDateEdit/Resources/ComboBoxArrow.png); }
QComboBox::down-arrow:on{ top: 1px; left: 1px; } QComboBox::drop-down{ width: 26px; border:none; }
QComboBox QAbstractItemView {border: none; outline:0px;} QComboBox QAbstractItemView::item{ height: 26px; min-height:26px;}
QComboBox QAbstractItemView::item:selected{ background-color: #0087f1; color: #e6ebf1;} QComboBox QAbstractItemView::item:!selected {background-color: #e6ebf1; color: #000000;}

View File

@ -1,5 +1,6 @@
#include "mainwindow.h" #include "mainwindow.h"
#include <QApplication> #include <QApplication>
#include <QFile>
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
@ -7,5 +8,12 @@ int main(int argc, char* argv[])
MainWindow w; MainWindow w;
w.showMaximized(); w.showMaximized();
QFile file(":/component/ZDateEdit/qss/zDateTimeEdit.qss");
if (file.open(QFile::ReadOnly)) {
QString qss = file.readAll();
a.setStyleSheet(qss);
file.close();
}
return a.exec(); return a.exec();
} }

View File

@ -1,27 +1,58 @@
#include "mainwindow.h" #include "mainwindow.h"
#include "ui_mainwindow.h" #include "ui_mainwindow.h"
#include "view/coaldistributchartwidget.h" #include "view/beltspeedlinewidget.h"
#include "view/coaldistributbarchartwidget.h"
#include "view/coalstatisticschartwidget.h"
#include <QDateTime> #include <QDateTime>
#include <QTimer> #include <QTimer>
MainWindow::MainWindow(QWidget *parent) MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent), ui(new Ui::MainWindow) { : QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this); ui->setupUi(this);
this->setWindowFlag(Qt::FramelessWindowHint); this->setWindowFlag(Qt::FramelessWindowHint);
this->setWindowFlags(windowFlags() | Qt::WindowMaximizeButtonHint | this->setWindowFlags(windowFlags() | Qt::WindowMaximizeButtonHint | Qt::WindowMinimizeButtonHint);
Qt::WindowMinimizeButtonHint);
calendarLoopPlay(); calendarLoopPlay();
setBeltWarnInfoTextEditStyle();
addcoalDistributChart(); addcoalDistributChart();
addbeltSpeedChat();
addCoalStatisticsChart();
} }
MainWindow::~MainWindow() { delete ui; } MainWindow::~MainWindow() { delete ui; }
void MainWindow::addBeltWarnInfoToTextEdit(const QString& consoleInfo)
{
m_textEditConsoleInfo.append(consoleInfo);
QDateTime startDateTime = QDateTime::currentDateTime();
QString formattedTime = startDateTime.toString("yyyy-MM-dd hh:mm:ss");
if (m_textEditConsoleInfo.isEmpty())
return;
QString lastConsoleInfo = m_textEditConsoleInfo.last();
qint64 timeDifferenceInMilliseconds = m_previousConsleInfoDateTime.secsTo(startDateTime);
if ((lastConsoleInfo == consoleInfo) && timeDifferenceInMilliseconds < 2)
return;
QString consoleInfoResult = QStringLiteral("[%1]%2").arg(formattedTime).arg(consoleInfo);
ui->textEdit_beltWarnInfo->append(consoleInfoResult);
m_previousConsleInfoDateTime = QDateTime::currentDateTime();
}
void MainWindow::on_pushButton_close_clicked() { this->close(); } void MainWindow::on_pushButton_close_clicked() { this->close(); }
void MainWindow::on_pushButton_max_clicked() { void MainWindow::on_pushButton_max_clicked()
{
if (isFullScreen()) { if (isFullScreen()) {
this->showMaximized(); this->showMaximized();
} else { } else {
@ -31,9 +62,10 @@ void MainWindow::on_pushButton_max_clicked() {
void MainWindow::on_pushButton_min_clicked() { this->showMinimized(); } void MainWindow::on_pushButton_min_clicked() { this->showMinimized(); }
void MainWindow::calendarLoopPlay() { void MainWindow::calendarLoopPlay()
{
ui->label_calendar->clear(); ui->label_calendar->clear();
QTimer *timer_calendar = new QTimer(this); QTimer* timer_calendar = new QTimer(this);
connect(timer_calendar, &QTimer::timeout, this, [this]() { connect(timer_calendar, &QTimer::timeout, this, [this]() {
QDateTime time = QDateTime::currentDateTime(); QDateTime time = QDateTime::currentDateTime();
QString str = time.toString("yyyy-MM-dd hh:mm:ss dddd"); QString str = time.toString("yyyy-MM-dd hh:mm:ss dddd");
@ -42,23 +74,48 @@ void MainWindow::calendarLoopPlay() {
timer_calendar->start(1000); //每一秒溢出一次进入槽函数 timer_calendar->start(1000); //每一秒溢出一次进入槽函数
} }
void MainWindow::addcoalDistributChart() { void MainWindow::setBeltWarnInfoTextEditStyle()
{
CoalDistributChartWidget *coalDistributChartWidget = ui->textEdit_beltWarnInfo->document()->setMaximumBlockCount(100);
new CoalDistributChartWidget(ui->widget_coalDistributChart); ui->textEdit_beltWarnInfo->setReadOnly(true);
ui->textEdit_beltWarnInfo->setTextInteractionFlags(Qt::NoTextInteraction);
// 可选:设置大小 }
coalDistributChartWidget->setMinimumSize(400, 300); // 设置最小大小以确保可见
void MainWindow::addcoalDistributChart()
// 确保将部件添加到布局中 {
QVBoxLayout *layout = // addBeltWarnInfoToTextEdit("添加煤流分布窗口成功!");
qobject_cast<QVBoxLayout *>(ui->widget_coalDistributChart->layout()); ui->textEdit_beltWarnInfo->append("添加煤流分布窗口成功!");
if (layout) {
layout->addWidget(coalDistributChartWidget); CoalDistributBarChartWidget* coalDistributBarChartWidget = new CoalDistributBarChartWidget(this);
} else {
// 如果没有布局,可以手动添加部件 // 检查布局是否已设置
ui->widget_coalDistributChart->setLayout(new QVBoxLayout()); if (!ui->widget_coalDistributChart->layout()) {
ui->widget_coalDistributChart->layout()->addWidget( ui->widget_coalDistributChart->setLayout(new QVBoxLayout);
coalDistributChartWidget); }
} coalDistributBarChartWidget->setContentsMargins(0, 0, 0, 0);
ui->widget_coalDistributChart->layout()->addWidget(coalDistributBarChartWidget);
}
void MainWindow::addbeltSpeedChat()
{
ui->textEdit_beltWarnInfo->append("添加皮带机速度曲线窗口成功!");
BeltSpeedLineWidget* beltSpeedLineWidget = new BeltSpeedLineWidget(this);
// 检查布局是否已设置
if (!ui->widget_beltSpeedChart->layout()) {
ui->widget_beltSpeedChart->setLayout(new QVBoxLayout);
}
beltSpeedLineWidget->setContentsMargins(0, 0, 0, 0);
ui->widget_beltSpeedChart->layout()->addWidget(beltSpeedLineWidget);
}
void MainWindow::addCoalStatisticsChart()
{
ui->textEdit_beltWarnInfo->append("添加煤量统计窗口成功!");
CoalStatisticsChartWidget* coalStatisticsChartWidget = new CoalStatisticsChartWidget(this);
// 检查布局是否已设置
if (!ui->widget_beltSpeedChart->layout()) {
ui->widget_beltSpeedChart->setLayout(new QVBoxLayout);
}
coalStatisticsChartWidget->setContentsMargins(0, 0, 0, 0);
ui->widget_coalStatisticsChart->layout()->addWidget(coalStatisticsChartWidget);
} }

View File

@ -1,6 +1,7 @@
#ifndef MAINWINDOW_H #ifndef MAINWINDOW_H
#define MAINWINDOW_H #define MAINWINDOW_H
#include <QDateTime>
#include <QMainWindow> #include <QMainWindow>
namespace Ui { namespace Ui {
@ -11,9 +12,16 @@ class MainWindow : public QMainWindow {
Q_OBJECT Q_OBJECT
public: public:
explicit MainWindow(QWidget *parent = nullptr); explicit MainWindow(QWidget* parent = nullptr);
~MainWindow(); ~MainWindow();
public slots:
/**
* @brief
* @param consoleInfo
*/
void addBeltWarnInfoToTextEdit(const QString& consoleInfo);
private slots: private slots:
void on_pushButton_close_clicked(); void on_pushButton_close_clicked();
@ -27,13 +35,38 @@ private:
*/ */
void calendarLoopPlay(); void calendarLoopPlay();
/**
* @brief setBeltWarnInfoTextEditStyle
*/
void setBeltWarnInfoTextEditStyle();
/** /**
* @brief addcoalDistributChart * @brief addcoalDistributChart
*/ */
void addcoalDistributChart(); void addcoalDistributChart();
/**
* @brief addbeltSpeedChat 线
*/
void addbeltSpeedChat();
/**
* @brief addCoalStatisticsChart
*/
void addCoalStatisticsChart();
private: private:
Ui::MainWindow *ui; Ui::MainWindow* ui;
/**
* @brief m_textEditConsoleInfo
*/
QList<QString> m_textEditConsoleInfo;
/**
* @brief m_previousConsleInfoDateTime
*/
QDateTime m_previousConsleInfoDateTime;
}; };
#endif // MAINWINDOW_H #endif // MAINWINDOW_H

View File

@ -205,7 +205,7 @@ QPushButton:pressed{
<widget class="QWidget" name="widget_body" native="true"> <widget class="QWidget" name="widget_body" native="true">
<layout class="QHBoxLayout" name="horizontalLayout_3" stretch="4,2"> <layout class="QHBoxLayout" name="horizontalLayout_3" stretch="4,2">
<item> <item>
<layout class="QVBoxLayout" name="verticalLayout" stretch="2,4,3"> <layout class="QVBoxLayout" name="verticalLayout" stretch="2,4,4">
<property name="spacing"> <property name="spacing">
<number>6</number> <number>6</number>
</property> </property>
@ -1040,6 +1040,9 @@ QToolButton:pressed{
</item> </item>
<item> <item>
<layout class="QHBoxLayout" name="horizontalLayout_5"> <layout class="QHBoxLayout" name="horizontalLayout_5">
<property name="spacing">
<number>20</number>
</property>
<item> <item>
<widget class="QWidget" name="widget_belt1" native="true"> <widget class="QWidget" name="widget_belt1" native="true">
<layout class="QVBoxLayout" name="verticalLayout_22"> <layout class="QVBoxLayout" name="verticalLayout_22">
@ -3026,7 +3029,7 @@ QPushButton:pressed{
</layout> </layout>
</item> </item>
<item> <item>
<widget class="QWidget" name="widget_2" native="true"/> <widget class="QWidget" name="widget_beltSpeedChart" native="true"/>
</item> </item>
</layout> </layout>
</item> </item>
@ -3038,7 +3041,7 @@ QPushButton:pressed{
</layout> </layout>
</item> </item>
<item> <item>
<layout class="QVBoxLayout" name="verticalLayout_3"> <layout class="QVBoxLayout" name="verticalLayout_3" stretch="3,3,4">
<property name="spacing"> <property name="spacing">
<number>6</number> <number>6</number>
</property> </property>
@ -3092,7 +3095,37 @@ QPushButton:pressed{
</layout> </layout>
</item> </item>
<item> <item>
<widget class="QWidget" name="widget_3" native="true"/> <widget class="QWidget" name="widget_beltWarnInfoEdit" native="true">
<layout class="QVBoxLayout" name="verticalLayout_43">
<property name="spacing">
<number>0</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QTextEdit" name="textEdit_beltWarnInfo">
<property name="styleSheet">
<string notr="true">#textEdit_beltWarnInfo{
background-color:transparent;
color: #FFFFFF;
font: 12pt &quot;Albertus Extra Bold&quot;;
border:no;
}</string>
</property>
</widget>
</item>
</layout>
</widget>
</item> </item>
</layout> </layout>
</widget> </widget>
@ -3153,6 +3186,21 @@ QPushButton:pressed{
}</string> }</string>
</property> </property>
<layout class="QVBoxLayout" name="verticalLayout_9" stretch="1,9"> <layout class="QVBoxLayout" name="verticalLayout_9" stretch="1,9">
<property name="spacing">
<number>0</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item> <item>
<layout class="QHBoxLayout" name="horizontalLayout_19"> <layout class="QHBoxLayout" name="horizontalLayout_19">
<item> <item>
@ -3192,7 +3240,25 @@ QPushButton:pressed{
</layout> </layout>
</item> </item>
<item> <item>
<widget class="QWidget" name="widget_5" native="true"/> <widget class="QWidget" name="widget_coalStatisticsChart" native="true">
<layout class="QVBoxLayout" name="verticalLayout_44">
<property name="spacing">
<number>0</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
</layout>
</widget>
</item> </item>
</layout> </layout>
</widget> </widget>

View File

@ -58,5 +58,12 @@
<file>Resource/image/startPress.png</file> <file>Resource/image/startPress.png</file>
<file>Resource/image/stopHover.png</file> <file>Resource/image/stopHover.png</file>
<file>Resource/image/stopPress.png</file> <file>Resource/image/stopPress.png</file>
<file>component/ZDateEdit/qss/zDateTimeEdit.qss</file>
<file>component/ZDateEdit/Resources/bk.png</file>
<file>component/ZDateEdit/Resources/ComboBoxArrow.png</file>
<file>component/ZDateEdit/Resources/DTNext.png</file>
<file>component/ZDateEdit/Resources/DTPrev.png</file>
<file>component/ZDateEdit/Resources/popupButton.png</file>
<file>component/ZDateEdit/Resources/popupButton2.png</file>
</qresource> </qresource>
</RCC> </RCC>

View File

@ -0,0 +1,66 @@
#include "beltspeedlinewidget.h"
#include <QDateTimeAxis>
#include <QVBoxLayout>
BeltSpeedLineWidget::BeltSpeedLineWidget(QWidget* parent)
: QWidget(parent)
{
initLineChart();
}
void BeltSpeedLineWidget::initLineChart()
{
// 创建图表视图
m_chartView = new QChartView(this);
m_chartView->setBackgroundBrush(Qt::transparent);
m_chartView->setStyleSheet("background: transparent;"); // 使用样式表确保透明
m_chartView->setRenderHint(QPainter::Antialiasing);
m_chartView->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
m_chart = new QChart();
m_chart->setBackgroundBrush(Qt::transparent);
m_chart->setMargins(QMargins(0, 0, 0, 0));
// 创建折线系列
m_series = new QLineSeries();
m_series->setPointsVisible(false); // 隐藏数据点的标记
// m_series->setMarkerShape(QLineSeries::MarkerShapeNone); // 隐藏标记形状
// 将系列添加到图表
m_chart->addSeries(m_series);
m_chart->createDefaultAxes();
// 假设数据点,替换为你的实时数据
QDateTime currentTime = QDateTime::currentDateTime();
for (int i = 0; i < 120; ++i) { // 120个数据点代表最近两个小时
double speed = qrand() % 21; // 生成0到20之间的随机速度
m_series->append(currentTime.addSecs(-i * 60).toMSecsSinceEpoch(), speed);
}
// 创建并设置X轴为日期时间轴
QDateTimeAxis* xAxis = new QDateTimeAxis();
xAxis->setFormat("hh:mm:ss");
xAxis->setRange(currentTime.addSecs(-7200), currentTime); // 设置范围为最近两个小时
xAxis->setLinePen(QPen(Qt::gray));
xAxis->setLabelsColor(Qt::gray);
m_chart->setAxisX(xAxis, m_series); // 将X轴应用到数据系列
// 设置Y轴范围
m_chart->axisY()->setRange(0, 20);
m_chart->axisY()->setTitleText("速度 (m/s)");
m_chart->axisY()->setTitleBrush(QBrush(Qt::gray));
m_chart->axisY()->setLinePen(QPen(Qt::gray));
m_chart->axisY()->setLabelsColor(Qt::gray);
// 设置图表到视图并将视图设置为中心部件
m_chartView->setChart(m_chart);
QVBoxLayout* layout = new QVBoxLayout(this);
layout->setContentsMargins(0, 0, 0, 0);
layout->setSpacing(0); // 设置间距为0
layout->addWidget(m_chartView);
setLayout(layout);
}

View File

@ -0,0 +1,26 @@
#ifndef BELTSPEEDLINEWIDGET_H
#define BELTSPEEDLINEWIDGET_H
#include <QObject>
#include <QWidget>
#include <QtCharts>
QT_CHARTS_USE_NAMESPACE
class BeltSpeedLineWidget : public QWidget {
Q_OBJECT
public:
explicit BeltSpeedLineWidget(QWidget* parent = nullptr);
public slots:
private:
void initLineChart();
private:
QChartView* m_chartView; // 图表视图
QChart* m_chart; // 图表
QLineSeries* m_series; // 数据系列
};
#endif // BELTSPEEDLINEWIDGET_H

View File

@ -0,0 +1,104 @@
#include "coaldistributbarchartwidget.h"
#include <QDateTimeAxis>
#include <QList>
#include <QRandomGenerator>
#include <QScopedPointer>
#include <QVBoxLayout>
#include <QtCharts/QChart>
#include <QBarSeries>
#include <QBarSet>
#include <QChart>
#include <QChartView>
#include <QDateTimeAxis>
#include <QTimer>
#include <QVBoxLayout>
#include <memory>
CoalDistributBarChartWidget::CoalDistributBarChartWidget(QWidget* parent)
: QWidget(parent)
{
initializeChart();
// setupTimer();
}
void CoalDistributBarChartWidget::updateChart()
{
double newValue = QRandomGenerator::global()->bounded(0, 99); // 随机生成0-900的Y值
// 更新当前柱子的值
QBarSet* currentBarSet = static_cast<QBarSet*>(m_series->barSets().at(currentIndex));
currentBarSet->replace(0, newValue); // 更新当前柱子的值
// 循环更新下一个柱子的索引
currentIndex = (currentIndex + 1) % m_series->count(); // 循环到下一个柱子
}
void CoalDistributBarChartWidget::initializeChart()
{
// 创建图标
m_chart = new QChart();
m_chart->setBackgroundBrush(Qt::transparent);
m_chart->setMargins(QMargins(0, 0, 0, 0));
// 创建窗口并设置图表
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);
// 创建300个柱状集
// for (int i = 0; i < 300; ++i) {
// QBarSet* barSet = new QBarSet(QString::number(i)); // 使用柱子编号作为名称
// barSet->append(0); // 初始化值为0
// m_series->append(barSet);
// }
// 设置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); // 隐藏X轴刻度线
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); // 确保标题可见
QVBoxLayout* layout = new QVBoxLayout(this);
layout->setContentsMargins(0, 0, 0, 0);
layout->setSpacing(0); // 设置间距为0
layout->addWidget(m_chartView);
setLayout(layout);
}
void CoalDistributBarChartWidget::setupTimer()
{
// 定时器设置
m_timer = new QTimer(this);
connect(m_timer, &QTimer::timeout, this, &CoalDistributBarChartWidget::updateChart);
m_timer->start(1000); // 每秒更新一次
}

View File

@ -0,0 +1,50 @@
#ifndef COALDISTRIBUTBARCHARTWIDGET_H
#define COALDISTRIBUTBARCHARTWIDGET_H
#include <QDateTime>
#include <QObject>
#include <QTimer>
#include <QWidget>
#include <QDateTimeAxis>
#include <QtCharts/QBarSeries>
#include <QtCharts/QBarSet>
#include <QtCharts/QCategoryAxis>
#include <QtCharts/QChartView>
#include <QtCharts/QValueAxis>
QT_CHARTS_USE_NAMESPACE
class CoalDistributBarChartWidget : public QWidget {
Q_OBJECT
public:
explicit CoalDistributBarChartWidget(QWidget* parent = nullptr);
private slots:
void updateChart();
private:
void initializeChart();
void setupTimer();
private:
QChart* m_chart;
QChartView* m_chartView;
QBarSeries* m_series;
QTimer* m_timer;
QDateTimeAxis* m_axisX;
int currentIndex; // 当前更新的柱子的索引
static const int MaxDataPoints = 300; // 最大数据点数5分钟每秒1条
QBarSet* m_set;
// QList<QString> m_timeLabels;
QList<QDateTime> m_timeLabels; // 存储时间标签
QList<double> m_values; // 存储比例值
// QList<QDateTime> timestamps; // 存储时间戳
};
#endif // COALDISTRIBUTBARCHARTWIDGET_H

View File

@ -0,0 +1,77 @@
#include "coalstatisticschartwidget.h"
#include "ui_coalstatisticschartwidget.h"
CoalStatisticsChartWidget::CoalStatisticsChartWidget(QWidget* parent)
: QWidget(parent)
, ui(new Ui::CoalStatisticsChartWidget)
{
ui->setupUi(this);
initCoalStatisticsBarChart();
}
CoalStatisticsChartWidget::~CoalStatisticsChartWidget()
{
delete ui;
}
void CoalStatisticsChartWidget::initCoalStatisticsBarChart()
{
// 创建图表
m_chart = new QChart();
m_chart->setBackgroundBrush(Qt::transparent);
m_chart->setMargins(QMargins(0, 0, 0, 0));
// 创建图表视图并设置
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);
}
}

View File

@ -0,0 +1,34 @@
#ifndef COALSTATISTICSCHARTWIDGET_H
#define COALSTATISTICSCHARTWIDGET_H
#include <QWidget>
#include <QtCharts>
QT_CHARTS_USE_NAMESPACE
namespace Ui {
class CoalStatisticsChartWidget;
}
class CoalStatisticsChartWidget : public QWidget {
Q_OBJECT
public:
explicit CoalStatisticsChartWidget(QWidget* parent = nullptr);
~CoalStatisticsChartWidget();
private:
/**
* @brief initCoalStatisticsBarChart
*/
void initCoalStatisticsBarChart();
private:
Ui::CoalStatisticsChartWidget* ui;
QChart* m_chart;
QChartView* m_chartView;
QBarSeries* m_series;
QTimer* m_timer;
};
#endif // COALSTATISTICSCHARTWIDGET_H

View File

@ -0,0 +1,316 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>CoalStatisticsChartWidget</class>
<widget class="QWidget" name="CoalStatisticsChartWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>630</width>
<height>350</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2" stretch="1,1,8">
<property name="spacing">
<number>6</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="spacing">
<number>15</number>
</property>
<property name="leftMargin">
<number>10</number>
</property>
<property name="rightMargin">
<number>10</number>
</property>
<item>
<widget class="QLabel" name="label">
<property name="layoutDirection">
<enum>Qt::LeftToRight</enum>
</property>
<property name="styleSheet">
<string notr="true">QLabel{
color: rgb(255, 255, 255);
font: 10pt &quot;黑体&quot;;
}</string>
</property>
<property name="text">
<string>开始时间</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item>
<widget class="ZDateTimeEdit" name="dateTimeEdit_startTime">
<property name="minimumSize">
<size>
<width>0</width>
<height>25</height>
</size>
</property>
<property name="styleSheet">
<string notr="true">QDateTimeEdit {
background-color: rgb(51, 52, 56);
color: white; /* 字体颜色 */
border: 1px solid rgb(180, 186, 188); /* 边框颜色和宽度 */
border-radius: 0px; /* 圆角 */
padding: 5px; /* 内边距 */
font-size: 14px; /* 字体大小 */
font-family: &quot;Arial&quot;, sans-serif; /* 字体样式 */
}
QDateTimeEdit::up-button {
subcontrol-origin: padding;
subcontrol-position: top right;
}
QDateTimeEdit::down-button {
subcontrol-origin: padding;
subcontrol-position: bottom right;
}
QDateTimeEdit::up-button, QDateTimeEdit::down-button {
width: 0px;
height: 0px;
}
</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_2">
<property name="styleSheet">
<string notr="true">QLabel{
color: rgb(255, 255, 255);
font: 10pt &quot;黑体&quot;;
}</string>
</property>
<property name="text">
<string>结束时间</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item>
<widget class="ZDateTimeEdit" name="dateTimeEdit_endTime">
<property name="minimumSize">
<size>
<width>0</width>
<height>25</height>
</size>
</property>
<property name="styleSheet">
<string notr="true">QDateTimeEdit {
background-color: rgb(51, 52, 56);
color: white; /* 字体颜色 */
border: 1px solid rgb(180, 186, 188); /* 边框颜色和宽度 */
border-radius: 0px; /* 圆角 */
padding: 5px; /* 内边距 */
font-size: 14px; /* 字体大小 */
font-family: &quot;Arial&quot;, sans-serif; /* 字体样式 */
}
QDateTimeEdit::up-button {
subcontrol-origin: padding;
subcontrol-position: top right;
}
QDateTimeEdit::down-button {
subcontrol-origin: padding;
subcontrol-position: bottom right;
}
QDateTimeEdit::up-button, QDateTimeEdit::down-button {
width: 0px;
height: 0px;
}
</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_3">
<property name="styleSheet">
<string notr="true">QLabel{
color: rgb(255, 255, 255);
font: 10pt &quot;黑体&quot;;
}</string>
</property>
<property name="text">
<string>统计单位</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="comboBox_unit">
<property name="minimumSize">
<size>
<width>0</width>
<height>25</height>
</size>
</property>
<property name="styleSheet">
<string notr="true">QComboBox{
background-color: rgb(51, 52, 56);
color: white; /* 字体颜色 */
border: 1px solid rgb(180, 186, 188); /* 边框颜色和宽度 */
border-radius: 0px; /* 圆角 */
padding: 5px; /* 内边距 */
font-size: 14px; /* 字体大小 */
font-family: &quot;Arial&quot;, sans-serif; /* 字体样式 */
}</string>
</property>
<item>
<property name="text">
<string>时</string>
</property>
</item>
<item>
<property name="text">
<string>日</string>
</property>
</item>
<item>
<property name="text">
<string>月</string>
</property>
</item>
<item>
<property name="text">
<string>年</string>
</property>
</item>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<property name="leftMargin">
<number>10</number>
</property>
<property name="rightMargin">
<number>10</number>
</property>
<item>
<widget class="QLabel" name="label_4">
<property name="styleSheet">
<string notr="true">QLabel{
color: rgb(255, 255, 255);
font: 10pt &quot;黑体&quot;;
}</string>
</property>
<property name="text">
<string>煤量总计:000吨</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QRadioButton" name="radioButton_hour">
<property name="styleSheet">
<string notr="true">QRadioButton{
color: rgb(255, 255, 255);
}</string>
</property>
<property name="text">
<string>时</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="radioButton_day">
<property name="styleSheet">
<string notr="true">QRadioButton{
color: rgb(255, 255, 255);
}</string>
</property>
<property name="text">
<string>日</string>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="radioButton_month">
<property name="styleSheet">
<string notr="true">QRadioButton{
color: rgb(255, 255, 255);
}</string>
</property>
<property name="text">
<string>月</string>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="radioButton_year">
<property name="styleSheet">
<string notr="true">QRadioButton{
color: rgb(255, 255, 255);
}</string>
</property>
<property name="text">
<string>年</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QWidget" name="widget_coalChart" native="true">
<layout class="QVBoxLayout" name="verticalLayout_3"/>
</widget>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>ZDateTimeEdit</class>
<extends>QDateTimeEdit</extends>
<header>component/ZDateEdit/zdatetimeedit.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>