QCustomPlot 添加曲线、添加图例等常用功能

news/2024/10/18 7:55:29/

1、添加一条曲线

    ui->customPlot->addGraph();QVector<double> x(2510), y0(2510);for (int i=0; i<2500; ++i){x[i] = i;y0[i] = qExp(-i/1500.0)*qCos(i/100.0);}ui->customPlot->graph(0)->setData(x, y0);

在这里插入图片描述
2、坐标轴自适应曲线

 ui->customPlot->graph(0)->rescaleAxes();

在这里插入图片描述

3、改变曲线的颜色

 ui->customPlot->graph(0)->setPen(QPen(Qt::red));

在这里插入图片描述
4、添加图例

 ui->customPlot->legend->setVisible(true);

在这里插入图片描述
5、设置图例字体

 ui->customPlot->legend->setFont(QFont("Helvetica",14));

在这里插入图片描述
5、设置图例图标大小

 ui->customPlot->legend->setIconSize(100, 20);

在这里插入图片描述

6、设置曲线名

 ui->customPlot->graph(0)->setName("test");

在这里插入图片描述
7、设置背景色

 ui->customPlot->setBackground(QBrush(QColor(0, 248, 0)));

在这里插入图片描述
8、设置渐变背景色

    QLinearGradient gradient(0, 0, 0, 400);gradient.setColorAt(0, QColor(90, 90, 90));gradient.setColorAt(0.38, QColor(105, 105, 105));gradient.setColorAt(1, QColor(70, 70, 70));ui->customPlot->setBackground(QBrush(gradient));

在这里插入图片描述
9、添加图的title

    ui->customPlot->plotLayout()->insertRow(0);ui->customPlot->plotLayout()->addElement(0, 0, new QCPTextElement(ui->customPlot, "QCustomPlot Demo", QFont("sans", 12, QFont::Bold)));

在这里插入图片描述

10、设置X轴和Y轴标签

    ui->customPlot->xAxis->setLabel("Time");ui->customPlot->yAxis->setLabel("Electric Current");

在这里插入图片描述
11、设置两点之间的连接样式

    ui->customPlot->graph()->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCross, 10));

在这里插入图片描述
源码中,提供了以下样式
在这里插入图片描述
11、设置 可以拖拽曲线

   ui->customPlot->setInteraction(QCP::iRangeDrag,true);

在这里插入图片描述
12、设置鼠标滚轮可以缩放曲线

   ui->customPlot->setInteraction(QCP::iRangeZoom,true);

在这里插入图片描述
13、设置X轴和Y轴的缩放因子

   ui->customPlot->axisRect(0)->setRangeZoomFactor(0.2,0.2);

14、设置鼠标放于绘图区,变为手型

   ui->customPlot->setCursor(QCursor(Qt::PointingHandCursor));

15、设置 可以选中X轴,或者Y轴

   ui->customPlot->setInteraction( QCP::iSelectAxes,true);

在这里插入图片描述
在这里插入图片描述
16、选中X轴或者Y轴,滚动鼠标滚轮,只缩放X轴或者Y轴

   connect(ui->customPlot, &QCustomPlot::mouseWheel, this, &MainWindow::onMouseWheel);void MainWindow::onMouseWheel()
{if (ui->customPlot->xAxis->selectedParts().testFlag(QCPAxis::spAxis))ui->customPlot->axisRect()->setRangeZoom(ui->customPlot->xAxis->orientation());else if (ui->customPlot->yAxis->selectedParts().testFlag(QCPAxis::spAxis))ui->customPlot->axisRect()->setRangeZoom(ui->customPlot->yAxis->orientation());elseui->customPlot->axisRect()->setRangeZoom(Qt::Horizontal|Qt::Vertical);   
}

17、设置曲线样式

ui->customPlot->graph()->setLineStyle(QCPGraph::lsStepCenter);

在这里插入图片描述
18、显示上边X轴和右边Y轴

ui->customPlot->xAxis2->setVisible(true);ui->customPlot->xAxis2->setTickLabels(false);ui->customPlot->yAxis2->setVisible(true);ui->customPlot->yAxis2->setTickLabels(false);

在这里插入图片描述
19、选中图例,对应的曲线也选中

     connect(ui->customPlot, SIGNAL(selectionChangedByUser()), this, SLOT(selectionChanged()));ui->customPlot->setInteractions(QCP::iSelectLegend | QCP::iSelectPlottables);ui->customPlot->legend->setSelectableParts(QCPLegend::spItems);void MainWindow::selectionChanged()
{// synchronize selection of graphs with selection of corresponding legend items:for (int i=0; i<ui->customPlot->graphCount(); ++i){QCPGraph *graph = ui->customPlot->graph(i);QCPPlottableLegendItem *item = ui->customPlot->legend->itemWithPlottable(graph);if (item->selected()){item->setSelected(true);graph->setSelection(QCPDataSelection(graph->data()->dataRange()));}ui->customPlot->update();}
}

在这里插入图片描述
在这里插入图片描述
20、隐藏网格

ui->customPlot->xAxis->grid()->setVisible(false);
ui->customPlot->yAxis->grid()->setVisible(false);   

在这里插入图片描述
21、设置网格颜色,子网格颜色,0线颜色

ui->customPlot->axisRect()->setBackground(QBrush(Qt::black));//背景黑色ui->customPlot->xAxis->grid()->setPen(QPen(QColor(180, 180, 180), 1, Qt::PenStyle::DashLine));//网格白色虚线ui->customPlot->yAxis->grid()->setPen(QPen(QColor(180, 180, 180), 1, Qt::PenStyle::DashLine));//网格白色虚线ui->customPlot->xAxis->grid()->setSubGridPen(QPen(QColor(50, 50, 50), 1, Qt::DotLine));//网格浅色点线ui->customPlot->yAxis->grid()->setSubGridPen(QPen(QColor(50, 50, 50), 1, Qt::DotLine));//网格浅色点线ui->customPlot->xAxis->grid()->setSubGridVisible(true);//显示x轴子网格线ui->customPlot->yAxis->grid()->setSubGridVisible(true);//显示要轴子网格线ui->customPlot->xAxis->grid()->setZeroLinePen(QPen(Qt::white));//x轴0线颜色白色ui->customPlot->yAxis->grid()->setZeroLinePen(QPen(Qt::white));//y轴0线颜色白色 

在这里插入图片描述
22、设置X轴和Y轴的取值范围

ui->customPlot->xAxis->setRange(-500, 500);
ui->customPlot->yAxis->setRange(0, 1);  

在这里插入图片描述

23、设置所有曲线的所有数据都能显示在plot中

ui->customPlot->graph(0)->rescaleAxes();for (int k = 1; k < ui->customPlot->graphCount(); k++)
{ui->customPlot->graph(k)->rescaleAxes(true);
}

在这里插入图片描述


http://www.ppmy.cn/news/703133.html

相关文章

HDU 2510 符号三角形

链接&#xff1a;http://acm.hdu.edu.cn/showproblem.php?pid2510 符号三角形 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1116 Accepted Submission(s): 578 Problem Description 符号三角形的 第1行有n个由…

A001-185-2510-李海鹏

我对需求分析与建模的认识与应有内容建议 作者&#xff1a;李海鹏 班级&#xff1a;18软件5班 学号&#xff1a;1814080902510 目录我对需求分析与建模的认识与应有内容建议 一、 摘要/关键字&#xff08;Abstract and Key words&#xff09; 二、 引言&#xff08;Introduc…

中山大学 计算机院博士录取名学,中山大学2021年博士研究生招生拟录取名单公示,2510人!...

原标题&#xff1a;中山大学2021年博士研究生招生拟录取名单公示&#xff0c;2510人&#xff01; 根据教育部与广东省教育考试院的统一工作部署&#xff0c;经校内各招生单位研究生招生工作领导小组审核&#xff0c;研究生院审定&#xff0c;现将我校2021年博士研究生拟录取名单…

ASEMI整流桥GBJ2510规格,GBJ2510封装,GBJ2510特性

编辑-Z ASEMI整流桥GBJ2510参数&#xff1a; 型号&#xff1a;GBJ2510 最大重复峰值反向电压&#xff08;VRRM&#xff09;&#xff1a;1000V 最大RMS电桥输入电压&#xff08;VRMS&#xff09;&#xff1a;700V 最大直流阻断电压&#xff08;VDC&#xff09;&#xff1a;…

找高清无水印视频素材,就上这9个网站。

推荐几个我的视频素材库&#xff0c;有免费、收费、商用&#xff0c;希望对大家有帮助&#xff01; 1、菜鸟图库 https://www.sucai999.com/video.html?vNTYwNDUx 菜鸟图库可以找到设计、办公、图片、视频、音频等各种素材。视频素材就有上千个&#xff0c;全部都很高清&…

GBJ2510-ASEMI电机专用25A整流桥GBJ2510

编辑-Z GBJ2510在GBJ-4封装里采用的4个芯片&#xff0c;其尺寸都是140MIL&#xff0c;是一款电机专用25A薄体扁桥。GBJ2510的浪涌电流Ifsm为350A&#xff0c;漏电流(Ir)为10uA&#xff0c;其工作时耐温度范围为-55~150摄氏度。GBJ2510采用光阻GPP硅芯片材质&#xff0c;里面有…

C++(20);模块module初识

C及C++之前一直是通过头文件的方式引入定义于其他文件中的标示符,而引入头文件,从本质上说相当于预处理器将头文件的内容拷贝了一份放入当前文件中,这样做有以下几个缺点: 1.由于拷贝了一份代码,而不是只包含要使用的部分,编译起来比较低效 2.如果在头文件A中引入另一个…

张书博:锂电池储能系统德国新标准VDE 2510-50

第五届储能技术在分布式能源与微电网中应用高层研讨会在深圳顺利召开&#xff0c;来自行业协会、科研院所、知名企业的代表共300人到场参会。会议由中国化学与物理电源行业协会、全国微电网与分布式电源并网标准化技术委员会联合主办。中国化学与物理电源行业协会储能应用分会、…