qt中类的收集
QCustomPlot类
QCustomPlot
是一个基于Qt的画图和数据可视化C++控件,QCustomPlot 致力于提供美观的界面,高质量的2D画图、图画和图表,为实时可视化应用程序提供高性能。
官网链接:https://www.qcustomplot.com/index.php/introduction
下载链接:https://www.qcustomplot.com/index.php/download
学习教程推荐:https://www.jianshu.com/p/cfc2637ef3c4
QCustomPlot几个重要的类:
- QCustomPlot 图表类:用于图表的显示和交互。
- QCPLayer 图层类:管理图层元素(QCPLayerable),所有可显示的对象都是继承自图层元素。
- QCPAbstractPlottable 绘图元素:包含 折线图(QCPGraph)、曲线图(QCPCurve)、柱状图(QCPBars)、QCPStaticBox(盒子图)、QCPColorMap(色谱图)、QCPFinancial(金融图)。
- QCPAxisRect 坐标轴矩形:一个坐标轴矩形默认包含上下左右四个坐标轴,但是可以添加多个坐标轴。
使用方法:将下载下来的qcustomplot.h和qcustomplot.cpp加入工程中。
在使用qcustomplot的文件添加头文件
#include "qcustomplot.h"
。
示例:绘制一条曲线
// 初始化向量x和y
QVector<double> x(101), y(101);
// 101个坐标
for(int i =0;i<101;i++){
x[i] = i/50.0 - 1;
y[i] = x[i]*x[i];
}
// 添加数据曲线
ui->customplot->addGraph();
// graph(0)获取第一个数据曲线
// setData()关联数据到曲线。
ui->customplot->graph(0)->setData(x,y);
// setName()给数据曲线添加图例名称
ui->customplot->graph(0)->setName("第一个示例");
// setLabel()添加标签
ui->customplot->xAxis->setLabel("x轴");
ui->customplot->yAxis->setLabel("y轴");
// setRange 设置坐标轴范围
ui->customplot->xAxis->setRange(-1,1);
ui->customplot->yAxis->setRange(0,1);
// 显示图例
ui->customplot->legend->setVisible(true);
// 重绘图像
ui->customplot->replot();
示例:绘制两条曲线
QCustomPlot * customPlot_2 = ui->customplot_2;
customPlot_2->addGraph();
customPlot_2->graph(0)->setPen(QPen(Qt::blue)); // 设置曲线颜色
customPlot_2->graph(0)->setBrush(QBrush(QColor(0,0,255,20))); // 设置曲线与x轴包围区的颜色
customPlot_2->addGraph();
customPlot_2->graph(1)->setPen(QPen(Qt::red));
QVector<double> x0(251), y0(251), y1(251);
for(int i = 0; i < 251; i++){
x0[i] = i;
y0[i] = qExp(-i/150.0)*qCos(i/10.0);
y1[i] = qExp(-i/150.0);
}
// 设置边框右侧yAxis2和上侧xAxis2的刻度线显示,但不显示刻度值
customPlot_2->xAxis2->setVisible(true);
customPlot_2->xAxis2->setTickLabels(false);
customPlot_2->yAxis2->setVisible(true);
customPlot_2->yAxis2->setTickLabels(false);
// 设置上下两个x轴的范围总是相等
connect(customPlot_2->xAxis, SIGNAL(rangeChanged(QCPRange)), customPlot_2->xAxis2, SLOT(setRange(QCPRange)));
// 设置左右两个y轴的范围总是相等
connect(customPlot_2->yAxis, SIGNAL(rangeChanged(QCPRange)), customPlot_2->yAxis2, SLOT(setRange(QCPRange)));
customPlot_2->graph(0)->setData(x0,y0);
customPlot_2->graph(1)->setData(x0,y1);
// 自动调整XY轴的范围,以便显示graph()中所有的点
customPlot_2->graph(0)->rescaleAxes();
// 自动调整XY轴的方位,以便显示graph(1)中所有的点
customPlot_2->graph(1)->rescaleAxes(true);
// 设置鼠标拖拽的范围、滚动缩放轴的范围、左键点选图层(每条曲线独占一个图层)
customPlot_2->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);
ui->customplot_2->replot();
示例:绘制多条曲线
QCustomPlot *customplot = ui->MultipleCurve;
QPen pen;
QStringList lineNames;
lineNames << "lsNone"<<"lsLine"<<"lsStepLeft"<<"lsStepRight"<<"lsStepCenter"<<"lsImpulse";
for(int i = QCPGraph::lsNone;i<=QCPGraph::lsImpulse;++i){
customplot->addGraph();
pen.setColor(QColor(qSin(i*1+1.2)*80+80, qSin(i*0.3+0)*80+80, qSin(i*0.3+1.5)*80+80));
// 设置画笔
customplot->graph()->setPen(pen);
// 图例名称
customplot->graph()->setName(lineNames.at(i-QCPGraph::lsNone));
// 线段风格
customplot->graph()->setLineStyle((QCPGraph::LineStyle)i);
// 设置散点形状
customplot->graph()->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, 5));
// 显示图例
customplot->legend->setVisible(true);
QVector<double> x(15),y(15);
for(int j = 0; j<15;++j){
x[j] = j/15.0 *5 *3.14+0.01;
y[j] = 7 * qSin(x[j])/x[j] - (i-QCPGraph::lsNone)*5 + (QCPGraph::lsImpulse)*5+2;
}
customplot->graph()->setData(x,y);
// 自动调整X、Y轴的范围
customplot->graph()->rescaleAxes(true);
}
解释说明:
enum QCPGraph::LineStyle
:定义图形线在绘图中的视觉表示方式。
参数 描述 lsNone
数据点不与任何线连接 lsLine
数据点有一条直线连接 lsStepLeft
将线绘制为阶跃,其中阶跃高度为左侧数据点的值 lsStepRight
将线绘制为阶跃,其中阶跃高度为右侧数据点的值 lsStepCenter
当阶跃位于两个数据点之间时,将线绘制为阶跃 lsImpulse
每个数据点由一条平行于值轴的线表示(脉冲线)
enum QCPScatterStyle::ScatterShape
:设置散点形状
名称 形状 描述 ssNone
不绘制散点符号 ssDot
·
单点 ssCross
x
交叉 ssPlus
+
加 ssCircle
o
圆圈 ssDisc
⚫️ 实心圆圈 ···
···
···
示例:柱状图
QCustomPlot *customplot = ui->customplotBars;
QCPAxis *xAxis = customplot->xAxis; // x轴
QCPAxis *yAxis = customplot->yAxis; // y轴
QCPBars *bars = new QCPBars(xAxis, yAxis); // 使用xAxis、yAxis作为柱状图的x、y轴
bars->setAntialiased(false); // 为了更好的边框效果、关闭抗锯齿
bars->setName("Bars"); // 设置图例
bars->setPen(QPen(QColor(0,136,221).lighter(130)));
bars->setBrush(QColor(20,68,106)); // 设置柱状图的画刷颜色
QVector<double> ticks;
QVector<QString> labels;
ticks << 1 << 2 << 3 << 4 << 5 << 6 << 7; // 轴的范围
labels << "A" << "B" << "C" << "D" << "E" << "F" << "G"; // 轴刻度文字的显示
QSharedPointer<QCPAxisTickerText> textTicker(new QCPAxisTickerText);
textTicker->addTicks(ticks, labels);
xAxis->setTicker(textTicker); // 设为文字轴
xAxis->setTickLabelRotation(60); // 轴刻度文字旋转60度
xAxis->setSubTicks(false); // 不显示子刻度
xAxis->setTickLength(0,4); // 轴内外刻度的长度分别为0和4,即轴内刻度线不显示
xAxis->setRange(0,8); // 设置x轴范围
xAxis->setLabel("x");
xAxis->setUpperEnding(QCPLineEnding::esSpikeArrow);
yAxis->setRange(0,12.1); // 设置y轴范围
yAxis->setPadding(35); // y轴的内边距
yAxis->setLabel("y");
yAxis->setUpperEnding(QCPLineEnding::esSpikeArrow);
QVector<double> fossilData;
fossilData<<10<<9<<2<<5<<7<<4<<1; // y轴的坐标值
bars->setData(ticks, fossilData);
QCustomPlot 图层介绍
QCustomPlot
类管理所有的图层,默认自带了六个图层,分别是:
- 背景层
backgroudn
- 网格层
grid
- 绘图层
main
- 坐标轴层
axes
- 图例层
legend
- overlay 层
overlay
根据层的顺序不同,绘制的顺序也不同 ,越在底下的层越早绘制。