PDA

View Full Version : How do I modify the Callout example to support multiple y axes on the same side?



anand765
28th June 2018, 19:34
Link for Callout example in Qt's official documentation (https://doc.qt.io/archives/qt-5.10/qtcharts-callout-example.html)

This example basically displays a line series and a spline series in a scene inside a qgraphicsview and everytime we hover over any point on the line, it displays the corresponding tooltip and if we click on that particular point, the tooltip stays there permanently until the program is closed.

This example is made for charts with a single x axis and a single y axis. What I am trying to do is to implement this in a chart with multiple y axes, on the left side. I tried to use QValueAxis for this purpose: I defined axisX that is to be shared by both the series and two axes Y1 and Y2, one for each series. In the original example, createdefaultaxes() was used to create the single x and single y axis. Here's my code for the QValueAxis part:


QValueAxis *axisX = new QValueAxis;
QValueAxis *axisY1 = new QValueAxis;
QValueAxis *axisY2 = new QValueAxis;

m_chart->addAxis(axisX, Qt::AlignBottom);
m_chart->addAxis(axisY1, Qt::AlignLeft);
m_chart->addAxis(axisY2, Qt::AlignLeft);

series->attachAxis(axisX);
series->attachAxis(axisY1);
series2->attachAxis(axisX);
series2->attachAxis(axisY2);

m_chart->setAcceptHoverEvents(true);

setRenderHint(QPainter::Antialiasing);
scene()->addItem(m_chart);

connect(series, &QLineSeries::clicked, this, &View::keepCallout);
connect(series, &QLineSeries::hovered, this, &View::tooltip);

connect(series2, &QSplineSeries::clicked, this, &View::keepCallout);
connect(series2, &QSplineSeries::hovered, this, &View::tooltip);

this->setMouseTracking(true);

// keepCallout() function
{
m_callouts.append(m_tooltip);//m_callouts is a QList of "Callout" class type
m_tooltip = new Callout(m_chart);
}

//tooltip(QPointF point, bool state) function
{
if (m_tooltip == 0)
m_tooltip = new Callout(m_chart);

if (state) {
m_tooltip->setText(QString("X: %1 \nY: %2").arg(point.x()).arg(point.y()));
m_tooltip->setAnchor(point);
m_tooltip->setZValue(11);
m_tooltip->updateGeometry();
m_tooltip->show();
} else {
m_tooltip->hide();
}
}

Result with single y axis: https://i.stack.imgur.com/8LhvG.jpg
Result with multiple y axis (the tooltip appears at some distance perpendicularly below the mouse pointer)https://i.stack.imgur.com/0ev9r.jpg

So yeah that's the problem. I can't seem to get the tooltip to appear at the point where my mouse is hovering. Instead it appears at a point which is at a small distance from that point.

As for the Callout code, I can't seem to figure out which code snippets from the entire code are the most relevant in context of this question (I'm kindof a beginner in qgraphics stuff) so this is the link (https://doc.qt.io/archives/qt-5.10/qtcharts-callout-example.html) for the entire. It would be great if someone could help me out here. Also, since I'm new here, please let me know if there are any changes I need to make while asking questions like these.