Skip to content

Commit

Permalink
Draw point labels inside plot if possible
Browse files Browse the repository at this point in the history
Prior to this commit, if a point was on
the perifery of a plot, the label could
be drawn off the plot.  This commit moves
the label into the viewport and keeps the
label close to the point.
  • Loading branch information
keithvetter committed Mar 15, 2024
1 parent 95e1cf7 commit f09c4b5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
15 changes: 11 additions & 4 deletions libkoviz/bookview_curves.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,15 +309,22 @@ void CurvesView::_paintCurve(const QModelIndex& curveIdx,
QTransform I;
painter.setTransform(I);
double top = tbox.y()-fontMetrics().ascent();
QPoint drawPt;
if ( top >= 0 ) {
// Draw label over curve
painter.drawText(tbox.topLeft()-QPointF(0,5),label);
drawPt = tbox.topLeft().toPoint()-QPoint(0,5);
} else {
// Draw label under curve since it would drawn off page
painter.drawText(tbox.topLeft()+
QPointF(0,fontMetrics().ascent())
+QPointF(0,5),label);
drawPt = tbox.topLeft().toPoint()+
QPoint(0,fontMetrics().ascent()) + QPoint(0,5);
}
QRect labelRect = painter.fontMetrics().boundingRect(label);
labelRect.moveTo(tbox.topLeft().toPoint());
if ( labelRect.right() > painter.viewport().right() ) {
// Shift label to left of point so it stays in viewport
drawPt = drawPt - QPoint(labelRect.width(),0);
}
painter.drawText(drawPt,label);
painter.setTransform(Tscaled);

} else if ( path->elementCount() == 0 ) {
Expand Down
13 changes: 10 additions & 3 deletions libkoviz/layoutitem_curves.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,14 +340,21 @@ void CurvesLayoutItem::_printCoplot(const QTransform& T,
// It's a flatline
label = QString("Flatline=%1").arg(yString);
}
int h = painter->fontMetrics().height();
QColor color( _bookModel->getDataString(curveIdx,
"CurveColor","Curve"));
QPen pen = painter->pen();
pen.setColor(color);
painter->setPen(pen);
painter->drawText(
curveBBox.topLeft()-QPointF(0,h+10),label);

int h = painter->fontMetrics().descent();
QPoint drawPt = curveBBox.topLeft().toPoint()-QPoint(0,h);
QRect L = painter->fontMetrics().boundingRect(label);
L.moveTo(drawPt);
if ( L.right() > painter->viewport().right() ) {
// Move label into viewport
drawPt = drawPt - QPoint(L.width(),0);
}
painter->drawText(drawPt,label);
}
}

Expand Down

0 comments on commit f09c4b5

Please sign in to comment.