From 45fa0f7b3d61cdc2f377fbd3e7f9809ecb936a23 Mon Sep 17 00:00:00 2001 From: Jenni Portman Date: Thu, 10 Jan 2019 20:43:40 +0000 Subject: [PATCH] ENH: Add option with different alignments for label in index direction --- chaco/text_plot_1d.py | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/chaco/text_plot_1d.py b/chaco/text_plot_1d.py index ad493da80..b47b8e010 100644 --- a/chaco/text_plot_1d.py +++ b/chaco/text_plot_1d.py @@ -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 @@ -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