I am annotating key events in my plots by overlaying them with static vertical lines with labels. A mouse hover event expands the label to provide more data. Both labels (default and expanded) contain multiple lines.
This is done with the following approach:
class VerticalLineAnnotation(pyqtgraph.InfiniteLine):
...
def hoverEvent(self, ev):
if ev.isExit():
self._expand_label(False)
else:
self._expand_label(True)
super().hoverEvent(ev)
...
def _expand_label(self, expand):
if self._expanded_label == expand:
return
self._expanded_label = expand
if expand:
self.label.setFormat(self._hover_label)
else:
self.label.setFormat(self._default_label)
self.update()
However, I currently must also adjust the position of the label since its height changes:
def _expand_label(self, expand):
if self._expanded_label == expand:
return
self._expanded_label = expand
if expand:
self._label_pos = self.label.orthoPos
self.label.setFormat(self._hover_label)
self.label.setPosition(self._label_pos + self._hover_label_pos_offset)
else:
self._label_pos = self.label.orthoPos - self._hover_label_pos_offset
self.label.setFormat(self._default_label)
self.label.setPosition(self._label_pos)
self.update()
Furthermore, if the size of the plot item changes, then the location of the label also changes, since I cannot use `position=1` (do to the multiple lines).
This problem can be solved if I were able to align the top left corner of the label at `position` instead of the text item's vertical center.
How would I make that change? Or better yet, can this be added as a feature specified in labelOpts?