头文件
#include <QWidget>
#include <QTimer>
#include <QtCharts/QChartView>//显示图表
#include <QtCharts/QLineSeries>//线系列
#include <QtCharts/QSplineSeries>
#include <QtCharts/QValueAxis>
#include <QDateTime>
#include <QDateTimeAxis>
#include<QDebug>
QSplineSeries* mm_series;
QStringList m_titles;
QValueAxis m_axis;
QDateTimeAxis AxisX;
QValueAxis AxisY;
qreal m_step;
qreal m_x;
qreal m_y;
qreal min_y = 0;
qreal max_y = 0;
QDateTime start;
QDateTime end;
QTimer m_timer;
chart.setTheme(QChart::ChartThemeDark);//设置系统主题
chart.setTitle( QStringLiteral("飞参实时波形图") );//设置标题
chart.setTitleFont(QFont("微软雅黑",10));
// chart.legend()->hide();
chart.setAnimationOptions(QChart::SeriesAnimations); // 动画方式
// x轴(时间轴方式)
AxisX.setTitleText(QStringLiteral("时间(分:秒)")); // x轴显示标题
// AxisX.setGridLineVisible(false); // 隐藏背景网格X轴框线
AxisX.setFormat("mm:ss"); // x轴格式
AxisX.setLabelsAngle(0); // x轴显示的文字倾斜角度
AxisX.setTickCount(6); // 轴上点的个数
start = QDateTime::currentDateTime();
end = start.addSecs(110);
AxisX.setRange(start, end); // 范围
// y轴
AxisY.setTitleText(QStringLiteral("数值")); // y轴显示标题
//AxisY.setGridLineVisible(false); // 隐藏背景网格Y轴框线
AxisY.setTickCount(6); // 轴上点的个数
AxisY.setRange(0, 1000);
// 图表视图
chart.addAxis(&AxisX, Qt::AlignBottom); // 设置x轴位置
chart.addAxis(&AxisY, Qt::AlignLeft); // 设置y轴位置
QChartView *chartView = new QChartView(&chart);
chartView->setRenderHint(QPainter::Antialiasing);
ui->verticalLayout->addWidget(chartView);
//添加一条曲线
mm_series = new QSplineSeries();
mm_series->setName("name");
chart.addSeries(mm_series);
chart.legend()->setAlignment(Qt::AlignTop); //设置曲线对应标签
// 线段
mm_series->setPen(QPen(QColor(255,255,0), 2, Qt::SolidLine));// 设置线段pen
mm_series->attachAxis(&AxisX); // 线段依附的x轴
mm_series->attachAxis(&AxisY);
//test专用
connect(&m_timer, &QTimer::timeout, this, &chartwidget::on_timerevent);
// m_timer.start(100);
cpp
void chartwidget::append_data(QDateTime x, double y)
{
//如果y值不在范围,则调整y
if(y < min_y)
{
min_y = y;
AxisY.setRange(min_y*0.9, max_y*1.1);
}
else if (y > max_y)
{
max_y = y;
AxisY.setRange(min_y, max_y*1.1);
}
//如果x值不在范围,则调整x
if(x > end)
{
end = end.addSecs(30);
AxisX.setRange(start, end);
}
//把这个数值添加到曲线
mm_series.append(x.toMSecsSinceEpoch(), y);
QApplication::processEvents();
}
//添加一根曲线
void chartwidget::add_one_serie(QString name, QColor color)
{
QSplineSeries* m_series = new QSplineSeries();
m_series->setName(name);
chart.addSeries(m_series);
chart.legend()->setAlignment(Qt::AlignTop); //设置曲线对应标签
// 线段
m_series->setPen(QPen(color, 2, Qt::SolidLine)); // 设置线段pen
m_series->attachAxis(&AxisX); // 线段依附的x轴
m_series->attachAxis(&AxisY);
}
//弄个定时器测试用
void chartwidget::on_timerevent()
{
qDebug()<<__FUNCTION__<<endl;
QDateTime x = QDateTime::currentDateTime();
if(x > end)
{
end = end.addSecs(30);
AxisX.setRange(start, end);
}
for(int i = 0;i< 2;i++)
{
QDateTime x = start.addSecs(i);
// 生成一个介于0到99之间的随机整数
int randomNumber =QRandomGenerator::global()->bounded(1000);
mm_series->append(x.toMSecsSinceEpoch(),randomNumber);
}
QApplication::processEvents();
}