67 lines
2.3 KiB
C++
67 lines
2.3 KiB
C++
#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);
|
||
}
|