zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    CMS 阶段性了结
    Java 代码监控 JVM 运行状态 —— 记一次 JVM 调优的毛招
    ClassLoader PPT 总结
    记一次 CMS 回收异常问题 —— 跨代引用和循环依赖
    读懂 GC 日志
    ClassLoader 学习笔记
    java 可见性简单总结
    Kafka 基本原理整理
    秒杀系统个人总结
    centos 7 mongodb4.0 安装配置
  • 原文地址:https://www.cnblogs.com/shenLong1356/p/11230390.html
Copyright © 2011-2022 走看看