文章目录
大致效果如下:
相关代码如下:
centerWidgetLayout->addLayout(hboxLayout,1);
customplot = new QCustomPlot(centerWidget);
centerWidgetLayout->addWidget(customplot,100);
int xPointNum = 1300 ;
customplot->plotLayout()->clear(); //删除默认的
//多个Y轴
axisRect1 = new QCPAxisRect(customplot);
customplot->plotLayout()->addElement(0,0, axisRect1 );
axisRect1->axis(QCPAxis::atLeft)->setLabel("s");
axisRect1->axis(QCPAxis::atBottom)->setRange(0, xPointNum);
axisRect1->axis(QCPAxis::atLeft)->setRange(0, 55);
//axisRect1->axis(QCPAxis::atRight)->setVisible(true);
//一个X 多个平行的Y轴
axisRect1->addAxis(QCPAxis::atRight);
axisRect1->axis(QCPAxis::atRight , 0)->setPadding(0);
axisRect1->axis(QCPAxis::atRight , 1)->setPadding(10);
axisRect1->axis(QCPAxis::atBottom, 0)->setPadding(20);
axisRect1->axis(QCPAxis::atLeft)->setRange(0, 100);
axisRect1->axis(QCPAxis::atRight, 0)->setRange(0, 50);
axisRect1->axis(QCPAxis::atRight, 0)->setLabel("v");
axisRect1->axis(QCPAxis::atRight, 1)->setRange(0, 200);
axisRect1->axis(QCPAxis::atRight, 1)->setLabel("a");
axisRect1->setupFullAxesBox(true);
axisRect1->axis(QCPAxis::atRight, 0)->setTickLabels(true);
//绘图
graph1 = customplot->addGraph(axisRect1->axis(QCPAxis::atBottom), axisRect1->axis(QCPAxis::atLeft , 0));
graph2 = customplot->addGraph(axisRect1->axis(QCPAxis::atBottom), axisRect1->axis(QCPAxis::atRight, 0));
graph3 = customplot->addGraph(axisRect1->axis(QCPAxis::atBottom), axisRect1->axis(QCPAxis::atRight, 1));
graph1->setPen(QPen(Qt::red));
graph2->setPen(QPen(Qt::black));
graph3->setPen(QPen(Qt::blue));
m_pHorReffer = new QCPItemStraightLine(customplot);
m_pHorReffer->setPen(QPen(Qt::red, 2, Qt::DotLine));
//绑定坐标
m_pHorReffer->setClipAxisRect(axisRect1);
m_pHorReffer->point1->setCoords( 30, 750);//设置点1的坐标
m_pHorReffer->point2->setCoords( 30, 0) ;//设置点2的坐标
connect(customplot, SIGNAL(mousePress(QMouseEvent*)), this, SLOT(MymousePressEvent(QMouseEvent*)));
//mTag1 = new AxisTag(graph1->valueAxis());
mTag1 = new AxisTag(graph1->valueAxis());
mTag1->setPen(graph1->pen());
mTag1->updatePosition(0.0);
mTag1->setText(QString::number( 0, 'f', 2));
//customplot->setInteractions(QCP::iRangeDrag|QCP::iRangeZoom| QCP::iSelectAxes |
// QCP::iSelectLegend | QCP::iSelectPlottables);
//生成游标
tracer = new QCPItemTracer(customplot);
tracer->setPen(QPen(Qt::red));
tracer->setBrush(QBrush(Qt::red));
tracer->setStyle(QCPItemTracer::tsCircle);
tracer->position->setTypeX(QCPItemPosition::ptPlotCoords);
tracer->position->setTypeY(QCPItemPosition::ptAxisRectRatio);
tracer->position->setCoords(0.5, 0);
tracer->setSize(4);//设置大小
mArrow = new QCPItemLine(customplot);
mArrow->setLayer("overlay");
mArrow->setClipToAxisRect(false);
mArrow->setHead(QCPLineEnding::esSpikeArrow);
mArrow->end->setParentAnchor(tracer->position);
mArrow->start->setParentAnchor(mArrow->end);
mArrow->start->setCoords(0, 10);
tracerLabel = new QCPItemText(customplot);
tracerLabel->setLayer("overlay");
tracerLabel->setClipToAxisRect(false);
tracerLabel->setPadding(QMargins(3, 0, 3, 0));
tracerLabel->setPen(QPen(Qt::black));
tracerLabel->setBrush(QBrush(Qt::green));
tracerLabel->setPositionAlignment(Qt::AlignTop | Qt::AlignHCenter);//上中间
tracerLabel->position->setParentAnchor(mArrow->start); //将游标说明锚固在tracer位置处,实现自动跟随
tracer->position->setCoords(4, 1);
//tracerLabel->position->setCoords(0, 15);
mArrow->start->setCoords(0, 15);
mArrow->end->setCoords(0, 0);
tracerLabel->setText("###");