添加一条曲线
QCPGraph* pGraph = pCustomPlot->addGraph();
设置数据
PCustomPlot->graph()->setData(x,y);
设置鼠标可以对图像放大缩小,拖拽
pCustomPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
重绘,每次更新数据需要重绘(或者设置多个数据之后一起进行重绘)
pCustomPlot->replot();
设置Y舟范围
pCustomPlot->yAxis->setRange(0,20);
设置X轴为时间轴
QCPAxisTickerDateTime //时间坐标轴必须要用智能指针
QSharedPointer<QCPAXisTickDateTime> timer(new QCPAxisTickerDateTime);
设置时间格式
timer->setDateTimeFormar("yyyy-MM-dd");
设置时间轴一共几格
timer->setTickCount(6);
设置X轴label旋转30度
pCustomPlot->xAxis->setTickLabelRotation(30);
设置坐标轴为时间格式
pCustomPlot->xAxis->setTicker(timer);
设置Y轴坐标自动缩放,能正常显示所有数据
pCustomPlot->yAxis->rescale(true);
设置曲线画笔
pCustomPlot->graph()->setPen(QPen(QColor(250,20,20)));
设置显示AxisTag(显示当前最新数据的提示)
AxisTag* pAxisTag = new AxisTag(pCustomPlot->graph()->valueAxis());
设置AxisTag的画笔
pAxisTag->setPen(pCustomPlot->graph()->pen());
设置AxisTag位置以及文本
pAxisTag->updatePositon(valuue); //value为当前AxisTag要显示的值
pAxisTag->setText(QString::number(value,'f',2)); //设置要显示的文本
移除QCustomPlot项
pCustomPlot->removeItem();
设置背景颜色
QBrush backRole;
backRole.setColor("skyblue");
backRole.setStyle(Qt::SolidPattern);
pCustomPlot->setBackground(backRole);
设置Legend背景透明
pCustomPlot->legend->setBrush(QColor(255,255,255,0));
QCPLayerable 可分层对象,所有可绘制到屏幕上的对象都是它的派生类
QCPAbstractItem 抽象类项,子类显示一些特殊的图形,比如图片QCPItemPixmap、文字QCPItemText、可加入箭头的直线QCPItemLine等等
QCPAbstractPlotable 可绘制的图抽象类,子类用来显示数据系列的图形,例如柱状图QCPBars、曲线QCPGraph、弧线QCPCurse、统计学箱QCPStatisticalBox
QCpLayoutElement 可布局元素
我们可将QCustomPlot 当做一个二维图表,一个绘图板的类,它继承自QWidget
设置图例位置
pCustomPlot->axisRect()->insetLayout()->setInsetAlignment(0,Qt::AlignLeft | QAlignTop);
pCustomPlot->legend->addElement(row,col,pCustomPlot->legend->item(1));
pCustomPlot->plotLayout->addElement(count,0,pCustomPlot->legend);
pCustomPlot->plotLayout->setRowStretchFactor(count,0.001);
设置X轴、Y轴颜色
pCustomPlot->xAxis->setBasePen(QColor(255,0,0));
pCcustomPlot->yAxis->setBasePen(QColor(255,0,0));
设置Tick颜色
pCustomPlot->xAxis->setTickPen(QColor(255,0,0));
pCustomPlot->yAxis->setSubTickPen(QPen(Qt::blue));
设置tick在坐标轴里面和外部的长度
pCustomPlot->xAxis->setTickLength(30,15); //里30px 外15px
设置坐标轴tick提示文字颜色
pCustomPlot->xAxis->setTickLabelColor(QColor(Qt::red));
设置X轴网格的颜色
pCustomPlot->xAxis->grid()->setPen(QPen(Qt::red));
设置坐标轴尾端箭头
pCustomPlot->xAxis->setUpperEnding(QCPLineEnding::esSpikeArrow);
设置坐标轴上小刻度和箭头颜色(两个只能同时设置)
pCustomPlot->yAxis->setSubTickPen(QPen(Qt::red));
设置整个区域绘图颜色
pCustomPlot->setBackground(backRole);
设置坐标轴区域的颜色
pCustomPlot->axisRect()->setBackground(Qt::red);