QChart 绘制实时曲线

一、画曲线

 

 

 

 1.1画直线QLineSeries

   QLineSeries m_series ;
   QPen green(Qt::red);
   green.setWidth(2);
   m_series.setPen(green);
  m_series.append(m_x, m_y);  //向曲线添加数据
 

 

 

 

1.2画弧线QSplineSeries

 

 

 

二、画坐标

 

 

 

 2.1数字量坐标QValueAxis  (用的广泛)

 

    QValueAxis *axisX = new QValueAxis; //建议使用动态分配的方式
    axisX->setRange(0, 2000);        //设置坐标范围
    axisX->setLabelFormat("%g");     //设置坐标显示格式(比如整形%d %i,浮点型%f)
    axisX->setTitleText("Samples");     //设置坐标标题
    axisX->setTickCount(5);    //设置网格数量,5根线,四个网格

 

 
 

 

2.2时间日期坐标 QDateTimeAxis

 

 

 

 

    QDateTimeAxis *axisX = new QDateTimeAxis;
    axisX->setTickCount(10);
    axisX->setFormat("MMM yyyy");  //设置以月年格式显示
    axisX->setTitleText("Date");

 

 

三、画整体图表QChart

 

    QChart *chart = new QChart();
    chart->addSeries(series);  //把曲线添加到图表
    chart->setTitle("Sunspots count (by Space Weather Prediction Center)"); //设置大标题
   chart->setAxisX(axisX,series); //把坐标添加上

 

 
 

四、显示图表 QChartView 

 

  QChartView *chartView = new QChartView(chart); //把图标添加到图标显示控件上
   QMainWindow window;
    window.setCentralWidget(chartView); //把显示控件放到主窗口中心
   window.show();

 

 

五、更新曲线

 

 5.1追加的方式更新

 m_series.append(m_x, m_y);

 

 5.2整体刷新

 m_series->replace(m_buffer);

 

 

六、动态显示

 

 

 

    qreal dwidth= chart.plotArea().width()/(m_axis.tickCount()*2); //一次滚动多少宽度
    qreal dx= 10/(m_axis.tickCount()*2); //横坐标偏移量

    m_x += dx;
    m_y=sin(m_x);
    m_series.append(m_x, m_y);

    /*满屏之后滚动窗口*/
    if(m_x>10)
    chart.scroll(dwidth, 0); //dwidth 代表的窗口横坐标方向滚动的区域大小
                             //dwidth 的单位不是横坐标的单位,而是窗口像素

 

 七、代码:

 参考:https://blog.csdn.net/mars_xiaolei/article/details/85242869  非常感谢这位博主

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QTime"
#include "QDebug"
#include "qmath.h"
#include "QValueAxis"
#include "QDateTimeAxis"
#include "QRandomGenerator"


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setWindowTitle("动态正弦波形图");
    connect(&m_timer,SIGNAL(timeout()),this,SLOT(RealtimeDataSlot()));
    m_timer.setInterval(1000);
    m_x=0;
    m_y=0;

    chart.setTheme(QChart::ChartThemeDark);//设置系统主题
    chart.setTitle("动态正弦波形图");//设置标题
    chart.setTitleFont(QFont("微软雅黑",10));
    chart.legend()->hide();


    QPen green(Qt::red);
    green.setWidth(2);
    m_series.setPen(green);
    m_series.append(m_x, m_y);

    chart.addSeries(&m_series);
    chart.createDefaultAxes();
    chart.setAxisX(&m_axis,&m_series);
    m_axis.setTickCount(5);
    chart.axisX()->setRange(0,10);
    chart.axisY()->setRange(-1, 1);



    QChartView *chartView = new QChartView(&chart);
    QGridLayout *baseLayout = new QGridLayout(); //便于显示,创建网格布局

    chartView->setRenderHint(QPainter::Antialiasing);

    baseLayout->addWidget(chartView, 0, 0);
    ui->widgetWaveForm->setLayout(baseLayout); //显示到QWidget控件

    m_timer.start();
}
void MainWindow::RealtimeDataSlot()
{
    qreal dwidth= chart.plotArea().width()/(m_axis.tickCount()*2); //一次滚动多少宽度
    qreal dx= 10/(m_axis.tickCount()*2); //横坐标偏移量

    m_x += dx;
    m_y=sin(m_x);
    m_series.append(m_x, m_y);

    /*满屏之后滚动窗口*/
    if(m_x>10)
    chart.scroll(dwidth, 0); //dwidth 代表的窗口横坐标方向滚动的区域大小
                             //dwidth 的单位不是横坐标的单位,而是窗口像素

}
MainWindow::~MainWindow()
{
    delete ui;
}

 

转载于:https://www.cnblogs.com/shenLong1356/p/11230390.html

  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值