HX_CoalFlowDetect/view/beltspeedlinewidget.cpp

82 lines
2.9 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "beltspeedlinewidget.h"
#include <QDateTimeAxis>
#include <QRandomGenerator>
#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_chart->legend()->hide();
// 创建折线系列
m_series = new QLineSeries();
m_series->setPointsVisible(false); // 隐藏数据点的标记
// m_series->setMarkerShape(QLineSeries::MarkerShapeNone); // 隐藏标记形状
// 将系列添加到图表
m_chart->addSeries(m_series);
m_chart->createDefaultAxes();
// TODO:假设数据点,替换为你的实时数据
QDateTime currentTime = QDateTime::currentDateTime();
QRandomGenerator* generator = QRandomGenerator::global();
double previousSpeed = 10.0; // 初始速度值在中间
for (int i = 0; i < 120; ++i) { // 120个数据点代表最近两个小时
// 根据前一个速度值生成新的速度值保持在5到15之间
double fluctuation = generator->generateDouble() * 2.0 - 1.0; // 生成 -1.0 到 1.0 之间的浮动 // 小范围波动
double speed = previousSpeed + fluctuation;
// 确保速度在5到15之间
if (speed < 5.0) {
speed = 5.0;
} else if (speed > 15.0) {
speed = 15.0;
}
m_series->append(currentTime.addSecs(-i * 60).toMSecsSinceEpoch(), speed);
previousSpeed = 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);
}