一、基本画图

首先,给个简单的例子:

[cpp] view plaincopy
  1. // 生成数据,画出的是抛物线
  2. QVector<double> x(101), y(101); // initialize with entries 0..100
  3. for (int i=0; i<101; ++i)
  4. {
  5. x[i] = i/50.0 - 1; // x goes from -1 to 1
  6. y[i] = x[i]*x[i]; // let's plot a quadratic function
  7. }
  8. // 添加数据曲线(一个图像可以有多个数据曲线)
  9. customPlot->addGraph();
[cpp] view plaincopy
  1. // graph(0);可以获取某个数据曲线(按添加先后排序)
  2. // setData();为数据曲线关联数据
  3. customPlot->graph(0)->setData(x, y);
  4. // 为坐标轴添加标签
  5. customPlot->xAxis->setLabel("x");
  6. customPlot->yAxis->setLabel("y");
  7. // 设置坐标轴的范围,以看到所有数据
  8. customPlot->xAxis->setRange(-1, 1);
  9. customPlot->yAxis->setRange(0, 1);
[cpp] view plaincopy
  1. // 重画图像
  2. customPlot->replot();

上面代码生成的结果大致是这样的:

[cpp] view plaincopy
  1. 外观
[cpp] view plaincopy
  1. QCustomPlot的外观由很多方面特性组成,都可以改变:
[cpp] view plaincopy
  1. 坐标轴:
[cpp] view plaincopy
  1. QCustomPlot有四个QCPAxis成员变量,分别代表四个坐标轴:xAxis(下)yAxis(左)xAxis2(上)yAxis2(右)
[cpp] view plaincopy
  1. QCPAxis有相应的函数可以设置坐标轴的刻度、间距、范围等:
[cpp] view plaincopy
  1. setTickStep(double step);//设置刻度间距
  2. setTickVector(const QVector<double> &vec);//将坐标轴刻度设置为vec
  3. setAutoTickStep(bool on);//设置是否自动分配刻度间距
  4. setAutoTicks(bool on);//设置是否自动分配刻度
  5. setAutoTickCount(int approximateCount);//设置是否自动分配刻度数量
[cpp] view plaincopy
  1. 还有setBasePen、setTickPen、setTickLength、setSubTickLength、setSubTickPen、setTickLabelFont、setLabelFont、setTickLabelPadding、setLabelPadding、setRangeReversed等
[cpp] view plaincopy
  1. 等后面专门讲QCPAxis的时候再详细介绍

曲线风格:

[cpp] view plaincopy
  1. QCPGraph::setPen(const QPen &pen);

曲线画笔:

[cpp] view plaincopy
  1. QCPGraph::setLineStyle(LineStyle ls);

可以设置颜色、宽度、实虚等

曲线形状:

[cpp] view plaincopy
  1. QCPGraph::setScatterStyle(QCPScatterStyle &style);

曲线形状像*、+、x、o等等

填充曲线方式:

[cpp] view plaincopy
  1. QCPGraph::setBrush(const QBrush &brush);
[cpp] view plaincopy
  1. QCPGraph::setChannelFillGraph(otherGraph);//设置与某之间曲线填充
[cpp] view plaincopy
  1. QCPGraph::setBrush(Qt::NoBrush);//移除填充

以上会等到专门将QCPGraph和QCPScatterStyle类的时候再细讲网格:

[cpp] view plaincopy
  1. customPlot->yAxis->grid();
[cpp] view plaincopy
  1. setPen、setZeroLinePen、setSubGridVisible等
[cpp] view plaincopy
  1. 等讲QCPGrid类再细讲

二、高级画图

[cpp] view plaincopy
  1. 1、多曲线与多风格
[cpp] view plaincopy
  1. <pre name="code" class="cpp">customPlot->setLocale(QLocale(QLocale::English, QLocale::UnitedKingdom)); // period as decimal separator and comma as thousand separator
  2. customPlot->legend->setVisible(true);
  3. QFont legendFont = font();  // start out with MainWindow's font..
  4. legendFont.setPointSize(9); // and make a bit smaller for legend
  5. customPlot->legend->setFont(legendFont);
  6. customPlot->legend->setBrush(QBrush(QColor(255,255,255,230)));
  7. // by default, the legend is in the inset layout of the main axis rect. So this is how we access it to change legend placement:
  8. customPlot->axisRect()->insetLayout()->setInsetAlignment(0, Qt::AlignBottom|Qt::AlignRight);
  9. // setup for graph 0: key axis left, value axis bottom
  10. // will contain left maxwell-like function
  11. customPlot->addGraph(customPlot->yAxis, customPlot->xAxis);
  12. customPlot->graph(0)->setPen(QPen(QColor(255, 100, 0)));
  13. customPlot->graph(0)->setBrush(QBrush(QPixmap("./dali.png"))); // fill with texture of specified png-image
  14. customPlot->graph(0)->setLineStyle(QCPGraph::lsLine);
  15. customPlot->graph(0)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssDisc, 5));
  16. customPlot->graph(0)->setName("Left maxwell function");
  17. // setup for graph 1: key axis bottom, value axis left (those are the default axes)
  18. // will contain bottom maxwell-like function
  19. customPlot->addGraph();
  20. customPlot->graph(1)->setPen(QPen(Qt::red));
  21. customPlot->graph(1)->setBrush(QBrush(QPixmap("./dali.png"))); // same fill as we used for graph 0
  22. customPlot->graph(1)->setLineStyle(QCPGraph::lsStepCenter);
  23. customPlot->graph(1)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, Qt::red, Qt::white, 7));
  24. customPlot->graph(1)->setErrorType(QCPGraph::etValue);
  25. customPlot->graph(1)->setName("Bottom maxwell function");
  26. // setup for graph 2: key axis top, value axis right
  27. // will contain high frequency sine with low frequency beating:
  28. customPlot->addGraph(customPlot->xAxis2, customPlot->yAxis2);
  29. customPlot->graph(2)->setPen(QPen(Qt::blue));
  30. customPlot->graph(2)->setName("High frequency sine");
  31. // setup for graph 3: same axes as graph 2
  32. // will contain low frequency beating envelope of graph 2
  33. customPlot->addGraph(customPlot->xAxis2, customPlot->yAxis2);
  34. QPen blueDotPen;
  35. blueDotPen.setColor(QColor(30, 40, 255, 150));
  36. blueDotPen.setStyle(Qt::DotLine);
  37. blueDotPen.setWidthF(4);
  38. customPlot->graph(3)->setPen(blueDotPen);
  39. customPlot->graph(3)->setName("Sine envelope");
  40. // setup for graph 4: key axis right, value axis top
  41. // will contain parabolically distributed data points with some random perturbance
  42. customPlot->addGraph(customPlot->yAxis2, customPlot->xAxis2);
  43. customPlot->graph(4)->setPen(QColor(50, 50, 50, 255));
  44. customPlot->graph(4)->setLineStyle(QCPGraph::lsNone);
  45. customPlot->graph(4)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, 4));
  46. customPlot->graph(4)->setName("Some random data around\na quadratic function");
  47. // generate data, just playing with numbers, not much to learn here:
  48. QVector<double> x0(25), y0(25);
  49. QVector<double> x1(15), y1(15), y1err(15);
  50. QVector<double> x2(250), y2(250);
  51. QVector<double> x3(250), y3(250);
  52. QVector<double> x4(250), y4(250);
  53. for (int i=0; i<25; ++i) // data for graph 0
  54. {
  55. x0[i] = 3*i/25.0;
  56. y0[i] = exp(-x0[i]*x0[i]*0.8)*(x0[i]*x0[i]+x0[i]);
  57. }
  58. for (int i=0; i<15; ++i) // data for graph 1
  59. {
  60. x1[i] = 3*i/15.0;;
  61. y1[i] = exp(-x1[i]*x1[i])*(x1[i]*x1[i])*2.6;
  62. y1err[i] = y1[i]*0.25;
  63. }
  64. for (int i=0; i<250; ++i) // data for graphs 2, 3 and 4
  65. {
  66. x2[i] = i/250.0*3*M_PI;
  67. x3[i] = x2[i];
  68. x4[i] = i/250.0*100-50;
  69. y2[i] = sin(x2[i]*12)*cos(x2[i])*10;
  70. y3[i] = cos(x3[i])*10;
  71. y4[i] = 0.01*x4[i]*x4[i] + 1.5*(rand()/(double)RAND_MAX-0.5) + 1.5*M_PI;
  72. }
  73. // pass data points to graphs:
  74. customPlot->graph(0)->setData(x0, y0);
  75. customPlot->graph(1)->setDataValueError(x1, y1, y1err);
  76. customPlot->graph(2)->setData(x2, y2);
  77. customPlot->graph(3)->setData(x3, y3);
  78. customPlot->graph(4)->setData(x4, y4);
  79. // activate top and right axes, which are invisible by default:
  80. customPlot->xAxis2->setVisible(true);
  81. customPlot->yAxis2->setVisible(true);
  82. // set ranges appropriate to show data:
  83. customPlot->xAxis->setRange(0, 2.7);
  84. customPlot->yAxis->setRange(0, 2.6);
  85. customPlot->xAxis2->setRange(0, 3.0*M_PI);
  86. customPlot->yAxis2->setRange(-70, 35);
  87. // set pi ticks on top axis:
  88. QVector<double> piTicks;
  89. QVector<QString> piLabels;
  90. piTicks << 0  << 0.5*M_PI << M_PI << 1.5*M_PI << 2*M_PI << 2.5*M_PI << 3*M_PI;
  91. piLabels << "0" << QString::fromUtf8("½π") << QString::fromUtf8("π") << QString::fromUtf8("1½π") << QString::fromUtf8("2π") << QString::fromUtf8("2½π") << QString::fromUtf8("3π");
  92. customPlot->xAxis2->setAutoTicks(false);
  93. customPlot->xAxis2->setAutoTickLabels(false);
  94. customPlot->xAxis2->setTickVector(piTicks);
  95. customPlot->xAxis2->setTickVectorLabels(piLabels);
  96. // add title layout element:
  97. customPlot->plotLayout()->insertRow(0);
  98. customPlot->plotLayout()->addElement(0, 0, new QCPPlotTitle(customPlot, "Way too many graphs in one plot"));
  99. // set labels:
  100. customPlot->xAxis->setLabel("Bottom axis with outward ticks");
  101. customPlot->yAxis->setLabel("Left axis label");
  102. customPlot->xAxis2->setLabel("Top axis label");
  103. customPlot->yAxis2->setLabel("Right axis label");
  104. // make ticks on bottom axis go outward:
  105. customPlot->xAxis->setTickLength(0, 5);
  106. customPlot->xAxis->setSubTickLength(0, 3);
  107. // make ticks on right axis go inward and outward:
  108. customPlot->yAxis2->setTickLength(3, 3);
  109. customPlot->yAxis2->setSubTickLength(1, 1);

效果图:

2、日期和时间数据曲线

[cpp] view plaincopy
  1. // set locale to english, so we get english month names:
  2. customPlot->setLocale(QLocale(QLocale::English, QLocale::UnitedKingdom));
  3. // seconds of current time, we'll use it as starting point in time for data:
  4. double now = QDateTime::currentDateTime().toTime_t();
  5. srand(8); // set the random seed, so we always get the same random data
  6. // create multiple graphs:
  7. for (int gi=0; gi<5; ++gi)
  8. {
  9. customPlot->addGraph();
  10. QPen pen;
  11. pen.setColor(QColor(0, 0, 255, 200));
  12. customPlot->graph()->setLineStyle(QCPGraph::lsLine);
  13. customPlot->graph()->setPen(pen);
  14. customPlot->graph()->setBrush(QBrush(QColor(255/4.0*gi,160,50,150)));
  15. // generate random walk data:
  16. QVector<double> time(250), value(250);
  17. for (int i=0; i<250; ++i)
  18. {
  19. time[i] = now + 24*3600*i;
  20. if (i == 0)
  21. value[i] = (i/50.0+1)*(rand()/(double)RAND_MAX-0.5);
  22. else
  23. value[i] = fabs(value[i-1])*(1+0.02/4.0*(4-gi)) + (i/50.0+1)*(rand()/(double)RAND_MAX-0.5);
  24. }
  25. customPlot->graph()->setData(time, value);
  26. }
  27. // configure bottom axis to show date and time instead of number:
  28. customPlot->xAxis->setTickLabelType(QCPAxis::ltDateTime);
  29. customPlot->xAxis->setDateTimeFormat("MMMM\nyyyy");
  30. // set a more compact font size for bottom and left axis tick labels:
  31. customPlot->xAxis->setTickLabelFont(QFont(QFont().family(), 8));
  32. customPlot->yAxis->setTickLabelFont(QFont(QFont().family(), 8));
  33. // set a fixed tick-step to one tick per month:
  34. customPlot->xAxis->setAutoTickStep(false);
  35. customPlot->xAxis->setTickStep(2628000); // one month in seconds
  36. customPlot->xAxis->setSubTickCount(3);
  37. // apply manual tick and tick label for left axis:
  38. customPlot->yAxis->setAutoTicks(false);
  39. customPlot->yAxis->setAutoTickLabels(false);
  40. customPlot->yAxis->setTickVector(QVector<double>() << 5 << 55);
  41. customPlot->yAxis->setTickVectorLabels(QVector<QString>() << "Not so\nhigh" << "Very\nhigh");
  42. // set axis labels:
  43. customPlot->xAxis->setLabel("Date");
  44. customPlot->yAxis->setLabel("Random wobbly lines value");
  45. // make top and right axes visible but without ticks and labels:
  46. customPlot->xAxis2->setVisible(true);
  47. customPlot->yAxis2->setVisible(true);
  48. customPlot->xAxis2->setTicks(false);
  49. customPlot->yAxis2->setTicks(false);
  50. customPlot->xAxis2->setTickLabels(false);
  51. customPlot->yAxis2->setTickLabels(false);
  52. // set axis ranges to show all data:
  53. customPlot->xAxis->setRange(now, now+24*3600*249);
  54. customPlot->yAxis->setRange(0, 60);
  55. // show legend:
  56. customPlot->legend->setVisible(true);

效果图:

三、曲线、柱形图、统计图...

到目前为止,我们为图像添加曲线都是使用

[cpp] view plaincopy
  1. QCustomPlot::addGraph();
  2. QCustomPlot::graph();

其实,除了 QCPGraph ,QCustomPlot 还提供了多个画图类:

QCPCurve:与QCPGraph 类似,差别在于它是用于展示参数化曲线,可以有循环。

QCPBars:柱形图,如果有多个QCPBars ,可以依次重叠。

QCPStatisticalBox、QCPColorMap、QCPFinancial
与QCPGraph 不同的是,这些画图类在添加到QCustomPlot 的时候需要使用new创建一个实例,而不能直接

[cpp] view plaincopy
  1. <span style="color: rgb(53, 53, 53); font-family: monospace; line-height: 19.5px; background-color: rgb(240, 240, 240);">addPlottable</span>();

简单例子如下:

[cpp] view plaincopy
  1. QCPBars *myBars = new QCPBars(customPlot->xAxis, customPlot->yAxis);
  2. customPlot->addPlottable(myBars);
  3. // now we can modify properties of myBars:
  4. myBars->setName("Bars Series 1");
  5. QVector<double> keyData;
  6. QVector<double> valueData;
  7. keyData << 1 << 2 << 3;
  8. valueData << 2 << 4 << 8;
  9. myBars->setData(keyData, valueData);
  10. customPlot->rescaleAxes();
  11. customPlot->replot();

好吧,这篇就到这里。水平有限,如有出错,敬请指出,互相学习。

QCustomplot基础应用相关推荐

  1. QCustomPlot基础教程(十)——QCustomPlot中图例的相关设置

    目录 1.前言 2.显示图例 3.设置图例位置 4.设置图例背景 5.设置图例名称 6.设置边框隐藏 7.其他设置 8.拓展1--将图例统一放在下方 9.拓展2--设置图例与曲线同步选中 1.前言 基 ...

  2. QCustomPlot基础教程(八)——QCustomPlot将绘制的图形保存导出

    目录 一.函数介绍 二.代码示例 一.函数介绍 QCustomPlot提供了四种常用的save接口,其格式如下: saveBmp(const QString &fileName, int wi ...

  3. qcustomplot基础练习(曲线)

    一.工程创建 1.创建qt 工程 2.将下载的QCustomPlot源文件的.c .h文件分别添加到源文件和头文件中 3.在./pro中添加 QT += widgets printsupport 4. ...

  4. (一)QCustomPlot常见属性设置、多曲线绘制、动态曲线绘制、生成游标、矩形放大等功能实现

    系列文章目录 提示:这里是该系列文章的所有文章的目录 第一章: (一)QCustomPlot常见属性设置.多曲线绘制.动态曲线绘制.生成游标.矩形放大等功能实现 第二章: (二)QCustomPlot ...

  5. 物联网(X):QCustomPlot

    预先准备 添加源文件和头文件 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets printsupport添加打印支持 在界面新建widget并且提升为qc ...

  6. 【总目录4】C/C++、OpenCV、Qt、单片机总结大全

    本目录主要为C/C++相关目录,主要包含C/C++相关知识.计算机视觉库OpenCV的C++实现.Qt和单片机等. 上文目录链接:https://blog.csdn.net/didi_ya/artic ...

  7. 【总目录4】CC++、OpenCV、Qt、单片机总结大全

    本目录主要为C/C++相关目录,主要包含C/C++相关知识.计算机视觉库OpenCV的C++实现.Qt和单片机等. 上文目录链接:https://blog.csdn.net/didi_ya/artic ...

  8. 使用QCustomPlot绘图的基础

    使用QCustomPlot绘图的基础 首先你用QCustomPlot::addGraph创建一个曲线图然后你给曲线图赋一些数据点(一对QVector<double>为x,y的值)并且定义曲 ...

  9. QCustomplot怎么实现对大数据量的自适应采样显示不卡顿

    我在之前使用QCustomplot时候,当时需要特别大的数据量的显示,结果图像显示的特别卡顿,CPU占用率也特别高,然后当时在解决时候是自己写了采样抽取数据的函数,最后解决的. 然后这回在阅读手册时候 ...

最新文章

  1. 如何用bat文件快速计算项目代码行数
  2. sqlite不存在记录则插入数据
  3. Mac python3.x使用HTMLTestRunner.py
  4. Windows下Socket库的初始化和关闭
  5. servlet的一些简单理解
  6. 原博客文章(Apache初配2008/4/8)
  7. 字节数组转换为图片_每日一课 | Python 3 TypeError:无法将“字节”对象隐式转换为str...
  8. 使用Vs code上传github需要输入密码和用户名解决
  9. Echart 地图实例
  10. tlc5620输出三角波流程图_[笔记].串型DAC TLC5620生成锯齿波、三角波实验,Verilog版本...
  11. 记录大三第一次面试经历
  12. python的文件操作os_​Python:目录和文件的操作模块os.path
  13. python调用java完整教程
  14. 2.3、Segment Routing基础之IGP Segment 类型详解
  15. 摩尔定律、安迪-比尔定律、反摩尔定律
  16. Android游戏引擎汇总,架构师花费近一年时间整理出来的安卓核心知识
  17. 链路层---->MAC地址,链路层与网络层对比
  18. Python批量复制一个文件夹中的全部excel数据并粘贴至一个excel文件中-openpyxl模块
  19. 分享:一个自由职业者的心得体会
  20. 浅谈阻塞/非阻塞、同步/异步——从linux read()系统调用出发

热门文章

  1. mphil in engineering最后颁发的学位是
  2. 复盘王者荣耀手游开发全过程,Unity引擎使用帧同步放弃状态同步
  3. 2.18-2.20 特殊权限和软硬链接
  4. 从Powershell 入侵脚本学到的如何执行后台runspace~
  5. Windows 7 提示AtBroke.exe初始化失败
  6. RDD编程 下(Spark自学四)
  7. Maven发布jar包到Nexus私库
  8. 2.let和const命令
  9. dependencies与devDependencies之间的区别
  10. 【腾讯Bugly干货分享】H5 视频直播那些事