首页 > 技术知识 > 正文

qt图表库qcustomplot使用心得记录二(常用功能集合)

本系列讲述的是我使用qt图表库qcustomplot中的使用心得分享,借此记录我的学习内容,也希望可以给与初学者一些帮助。

本篇文章主要记录一些常用的功能,有很多功能qcustomplot已经封装好了可以供我们使用,但是一个一个查接口太费事了,这里就记录一下我所知道和我所用到的一些功能。

1.坐标轴基本设置

设置x轴y轴标签:

myCustomPlot->xAxis->setLabel(“t(s)”);

myCustomPlot->yAxis->setLabel(“v(m/s)”);

设置量程

myCustomPlot->xAxis->setRange(0,2);

myCustomPlot->yAxis->setRange(0,1);

根据值获取像素点

myCustomPlot->xAxis->coordToPixel(value)

myCustomPlot->yAxis->coordToPixel(value)

根据像素点获取值

myCustomPlot->xAxis->pixelToCoord(pixel)

myCustomPlot->yAxis->pixelToCoord(pixel)

设置刻度间距
setTickStep(double step);

将坐标轴刻度设置为vec

setTickVector(const QVector &vec);

设置是否自动分配刻度间距

setAutoTickStep(bool on);

设置是否自动分配刻度

setAutoTicks(bool on);

设置是否自动分配刻度数量

setAutoTickCount(int approximateCount);

##### 2.曲线基本设置

QPen graphPen;

graphPen.setColor(QColor(255,0,0));

graphPen.setWidth(1);
<

设置线画笔

graph->setPen(graphPen);

设置线风格

graph->setLineStyle(QCPGraph::lsLine);

设置点风格

graph->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssDisc, 4));

清除线图

graph->data().data()->clear();

设置图线(图例)名字

graph->setName(QString::fromLocal8Bit(“线1”));

删除图例

graph->removeFromLegend();

线图数据全部显示

graph->rescaleValueAxis(true);

graph->rescaleKeyAxis(true);

##### 4.图例基本设置

不显示图例

myCustomPlot->legend->setVisible(false);

图例显示位置(右上角) myCustomPlot->axisRect()->insetLayout()->setInsetAlignment(0,Qt::AlignTop|Qt::AlignRight);

##### 3.图表设置

设置基本坐标轴(左侧Y轴和下方X轴)可拖动、可缩放、曲线可选、legend可选、设置伸缩比例,使所有图例可见

myCustomPlot->setInteractions(QCP::iRangeDrag|QCP::iRangeZoom| QCP::iSelectAxes | QCP::iSelectLegend | QCP::iSelectPlottables);

设置伸缩比例

myCustomPlot->axisRect()->setRangeZoomFactor(x轴,y轴);

添加曲线
myCustomPlot->addGraph();
重画图像

customPlot->replot();
<

猜你喜欢