QCustomPlot 标记点和文本标签

标记点

1、含义:指向QCPGraph的数据点的Item 
2、new一个新的标记点

m_sameTimeTracer = new QCPItemTracer(SameTimeCompareCustomPlot);
m_sameTimeTracer->setParent(SameTimeCompareCustomPlot); //设置标记点的父类
m_sameTimeTracer->setPen(QColor(255,255,255));
m_sameTimeTracer->setBrush(QBrush(QColor(255,0,0),Qt::SolidPattern));
m_sameTimeTracer->setStyle(QCPItemTracer::tsCircle);    //设置标记点的样式
m_sameTimeTracer->setSize(10);  //标记点的大小
m_sameTimeTracer->setVisible(false);

3、思路: 
循环遍历当前曲线的数据点,并将数据点的坐标与鼠标点击的位置坐标进行对比,当鼠标点击的位置在数据点附近的小范围内,则标记该数据点为标记点

QRect rect(0,0,1,1);    //设置小矩形框作为数据点附近的范围
QPoint p;
double posKey;
p.setX(e->pos().x());
p.setY(e->pos().y());
foreach(QCPData data,graph->data()->values())
{
      key = data.key;
      value = data.value;
      posKey = SameTimeCompareCustomPlot->xAxis->coordToPixel(key);
      if(qAbs(posKey-e->pos().x())<=10)
      {
         double posx = graph->keyAxis()->coordToPixel(key);
         double posy = graph->valueAxis()->coordToPixel(value);
         rect.setRect(posx-10,posy-10,21,21);
         if(!rect.contains(e->pos()))
            continue;
          else
             break;
       }
      if(posKey-e->pos().x()>10)
         break;
}

if(rect.contains(e->pos())) //绘制
{
    m_sameTimeTracer->setGraph(graph);
    m_sameTimeTracer->setGraphKey(key);
    m_sameTimeTracer->setVisible(true);
    pressPos.setX(key);
    pressPos.setY(value);
    SameTimeCompareCustomPlot->replot();
 }

文本标签

1、文本标签可以在用户点击数据点是显示该点的数据等功能。 
2、new一个新的文本标签

m_sameTimeTextTip = new QCPItemText(SameTimeCompareCustomPlot);      
m_sameTimeTextTip->setPositionAlignment(Qt::AlignTop|Qt::AlignHCenter);   m_sameTimeTextTip->position->setType(QCPItemPosition::ptAbsolute);
QFont font;
font.setPixelSize(10);
m_sameTimeTextTip->setFont(font); // make font a bit larger
m_sameTimeTextTip->setPen(QPen(Qt::black)); // show black border around text
m_sameTimeTextTip->setBrush(Qt::white);
m_sameTimeTextTip->setVisible(false);

3、得到点击的点的数据即可展示

m_sameTimeTextTip->setText("X轴: "+QString::number(pressPos.x()) +"\nY轴: "+QString::number(pressPos.y(),'f',3));
m_sameTimeTextTip->position->setCoords(e->pos().x()-30,e->pos().y()+15);
m_sameTimeTextTip->setVisible(true);
SameTimeCompareCustomPlot->replot();
  •  

代码

connect(SameTimeCompareCustomPlot, SIGNAL(mousePress(QMouseEvent *)), this, SLOT(slot_SameTimeMousePressEvent4Plot(QMouseEvent*)));

/**
 *  获取选中点的数值
 * @brief CADBCompareView::slot_SameTimeMousePressEvent4Plot
 * @param e
 */
void CADBCompareView::slot_SameTimeMousePressEvent4Plot(QMouseEvent *e)
{
    QCPGraph *graph = qobject_cast(SameTimeCompareCustomPlot->plottableAt(e->pos(),true));
    QPointF pressPos(0,0);//跟踪鼠标点击事件点击位置
    QRect rect(0,0,1,1);
    double key = 0,value = 0;
    if(graph)
    {
        if(m_sameTimeTracer == NULL)
        {
            m_sameTimeTracer = new QCPItemTracer(SameTimeCompareCustomPlot);
            m_sameTimeTracer->setParent(SameTimeCompareCustomPlot);
            m_sameTimeTracer->setPen(QColor(255,255,255));
            m_sameTimeTracer->setBrush(QBrush(QColor(255,0,0),Qt::SolidPattern));
            m_sameTimeTracer->setStyle(QCPItemTracer::tsCircle);
            m_sameTimeTracer->setSize(10);
            m_sameTimeTracer->setVisible(false);
        }
        if(m_sameTimeTextTip == NULL)
        {
            m_sameTimeTextTip = new QCPItemText(SameTimeCompareCustomPlot);
            m_sameTimeTextTip->setPositionAlignment(Qt::AlignTop|Qt::AlignHCenter);
            m_sameTimeTextTip->position->setType(QCPItemPosition::ptAbsolute);
            QFont font;
            font.setPixelSize(10);
            m_sameTimeTextTip->setFont(font); // make font a bit larger
            m_sameTimeTextTip->setPen(QPen(Qt::black)); // show black border around text
            m_sameTimeTextTip->setBrush(Qt::white);
            m_sameTimeTextTip->setVisible(false);
        }
        QPoint p;
        double posKey;
        p.setX(e->pos().x());
        p.setY(e->pos().y());
        foreach(QCPData data,graph->data()->values())
        {
            key = data.key;
            value = data.value;
            posKey = SameTimeCompareCustomPlot->xAxis->coordToPixel(key);
            if(qAbs(posKey-e->pos().x())<=10)
            {
                double posx = graph->keyAxis()->coordToPixel(key);
                double posy = graph->valueAxis()->coordToPixel(value);
                rect.setRect(posx-10,posy-10,21,21);
                if(!rect.contains(e->pos()))
                    continue;
                else
                    break;
            }
            if(posKey-e->pos().x()>10)
                break;
        }
        if(!graph->realVisibility())
        {
            m_sameTimeTracer->setVisible(false);
            pressPos.setX(0);
            pressPos.setY(0);
            SameTimeCompareCustomPlot->replot();
        }else if(rect.contains(e->pos()))
        {
            m_sameTimeTracer->setGraph(graph);
            m_sameTimeTracer->setGraphKey(key);
            m_sameTimeTracer->setVisible(true);
            pressPos.setX(key);
            pressPos.setY(value);
            SameTimeCompareCustomPlot->replot();
        }else
        {
            if(m_sameTimeTracer->visible())
            {
                m_sameTimeTracer->setVisible(false);
                pressPos.setX(0);
                pressPos.setY(0);
                SameTimeCompareCustomPlot->replot();
            }
        }
    }else
    {
        if(!m_sameTimeTracer == NULL)
        {
            if(m_sameTimeTracer->visible())
            {
                m_sameTimeTracer->setVisible(false);
                pressPos.setX(0);
                pressPos.setY(0);
                SameTimeCompareCustomPlot->replot();
            }
        }
    }

    if(pressPos.x()!=0 && pressPos.y()!=0)
    {
        m_sameTimeTextTip->setText("X轴: "+QString::number(pressPos.x()) +"\nY轴: "+QString::number(pressPos.y(),'f',3));
        m_sameTimeTextTip->position->setCoords(e->pos().x()-30,e->pos().y()+15);
        m_sameTimeTextTip->setVisible(true);
        SameTimeCompareCustomPlot->replot();
    }else
    {
        if(m_sameTimeTextTip != NULL)
        {
            m_sameTimeTextTip->setVisible(false);
            SameTimeCompareCustomPlot->replot();
        }
    }
}

你可能感兴趣的:(C++)