Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion chaco/text_plot_1d.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ class TextPlot1D(Base1DPlot):
#: alignment of text relative to non-index direction
alignment = Enum("center", "left", "right", "top", "bottom")

#: alignment of text relative to index direction and centered around the
#: provided value point
index_alignment = Enum("right", "center", "left", "top", "bottom")

#: offset of text relative to non-index direction in pixels
text_offset = Float

Expand Down Expand Up @@ -108,9 +112,32 @@ def _render(self, gc, pts, labels):
gc.clip_to_rect(self.x, self.y, self.width, self.height)
for pt, label in sm.zip(pts, labels):
with gc:
gc.translate_ctm(*pt)
x, y = self._get_index_text_position(gc, pt, label)
gc.translate_ctm(x, y)
label.draw(gc)

def _get_index_text_position(self, gc, pt, label):
""" Compute the text label position in the index direction """
x, y = pt
width, height = label.get_bounding_box(gc)

if self.orientation == 'v':
position, width = y, height
else:
position = x

if self.index_alignment == 'center':
position -= width/2.0
elif self.index_alignment in ['left', 'bottom']:
position -= width
# If alignment is 'right' or 'top' we do nothing as that already
# matches the default behavior

if self.orientation == 'v':
return x, position
else:
return position, y

def _get_text_position(self):
""" Compute the text label position in the non-index direction """
x, y = self.position
Expand Down