Skip to content

Commit

Permalink
Merge pull request #1133 from ioam/annotation_length
Browse files Browse the repository at this point in the history
Define length on Annotation Elements
  • Loading branch information
jlstevens committed Apr 14, 2017
2 parents 951ef20 + ae3c972 commit 16a6aea
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
9 changes: 9 additions & 0 deletions holoviews/core/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ def __getitem__(self, key):
raise NotImplementedError("%s currently does not support getitem" %
type(self).__name__)

def __nonzero__(self):
"""
Subclasses may override this to signal that the Element contains
no data and can safely be dropped during indexing.
"""
return True

__bool__ = __nonzero__


@classmethod
def collapse_data(cls, data, function=None, kdims=None, **kwargs):
Expand Down
29 changes: 25 additions & 4 deletions holoviews/element/annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class Annotation(Element2D):
def __init__(self, data, **params):
super(Annotation, self).__init__(data, **params)

def __len__(self):
return 1

def __getitem__(self, key):
if key in self.dimensions(): return self.dimension_values(key)
Expand Down Expand Up @@ -146,16 +148,31 @@ class Arrow(Annotation):
def __init__(self, x, y, text='', direction='<',
points=40, arrowstyle='->', **params):

info = (direction.lower(), text, (x,y), points, arrowstyle)
info = (x, y, text, direction, points, arrowstyle)
super(Arrow, self).__init__(info, x=x, y=y,
text=text, direction=direction,
points=points, arrowstyle=arrowstyle,
**params)

def __setstate__(self, d):
"""
Add compatibility for unpickling old Arrow types with different
.data format.
"""
super(Arrow, self).__setstate__(d)
if len(self.data) == 5:
direction, text, (x, y), points, arrowstyle = self.data
self.data = (x, y, text, direction, points, arrowstyle)


# Note: This version of clone is identical in Text and path.BaseShape
# Consider implementing a mix-in class if it is needed again.
def clone(self, *args, **overrides):
settings = dict(self.get_param_values(), **overrides)
if len(args) == 1 and isinstance(args[0], tuple):
args = args[0]
arg_names = ['x', 'y', 'text', 'direction', 'points', 'arrowstyle']
settings = {k: v for k, v in dict(self.get_param_values(), **overrides).items()
if k not in arg_names[:len(args)]}
return self.__class__(*args, **settings)

def dimension_values(self, dimension, expanded=True, flat=True):
Expand All @@ -165,7 +182,7 @@ def dimension_values(self, dimension, expanded=True, flat=True):
elif index == 1:
return np.array([self.y])
else:
return super(Text, self).dimension_values(dimension)
return super(Arrow, self).dimension_values(dimension)



Expand Down Expand Up @@ -205,5 +222,9 @@ def __init__(self, x,y, text, fontsize=12,
halign=halign, valign=valign, **params)

def clone(self, *args, **overrides):
settings = dict(self.get_param_values(), **overrides)
if len(args) == 1 and isinstance(args[0], tuple):
args = args[0]
arg_names = ['x', 'y', 'text', 'fontsize', 'halign', 'valign', 'rotation']
settings = {k: v for k, v in dict(self.get_param_values(), **overrides).items()
if k not in arg_names[:len(args)]}
return self.__class__(*args, **settings)
5 changes: 3 additions & 2 deletions holoviews/plotting/mpl/annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,16 @@ class ArrowPlot(AnnotationPlot):
style_opts = sorted(set(_arrow_style_opts + _text_style_opts))

def draw_annotation(self, axis, data, opts):
direction, text, xy, points, arrowstyle = data
x, y, text, direction, points, arrowstyle = data
direction = direction.lower()
arrowprops = dict({'arrowstyle':arrowstyle},
**{k: opts[k] for k in self._arrow_style_opts if k in opts})
textopts = {k: opts[k] for k in self._text_style_opts if k in opts}
if direction in ['v', '^']:
xytext = (0, points if direction=='v' else -points)
elif direction in ['>', '<']:
xytext = (points if direction=='<' else -points, 0)
return [axis.annotate(text, xy=xy, textcoords='offset points',
return [axis.annotate(text, xy=(x, y), textcoords='offset points',
xytext=xytext, ha="center", va="center",
arrowprops=arrowprops, **textopts)]

Expand Down

0 comments on commit 16a6aea

Please sign in to comment.