Skip to content

Commit

Permalink
Fixed Contours and Polygons hover (#3314)
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr committed Dec 18, 2018
1 parent 8efcc3b commit bdcc61a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
14 changes: 10 additions & 4 deletions holoviews/plotting/bokeh/path.py
Expand Up @@ -166,10 +166,13 @@ def _get_hover_data(self, data, element):
if 'hover' not in self.handles or self.static_source:
return

npath = len([vs for vs in data.values()][0])
for d in element.vdims:
dim = util.dimension_sanitizer(d.name)
if dim not in data:
if element.interface.isscalar(element, d):
if element.level is not None:
data[dim] = np.full(npath, element.level)
elif element.interface.isscalar(element, d):
data[dim] = element.dimension_values(d, expanded=False)
else:
data[dim] = element.split(datatype='array', dimensions=[d])
Expand Down Expand Up @@ -197,14 +200,18 @@ def get_data(self, element, ranges, style):
xs, ys = ys, xs
data = dict(xs=xs, ys=ys)
mapping = dict(self._mapping)
color = style.get('color')
if (isinstance(color, dim) and color.applies(element)) or color in element:
self._get_hover_data(data, element)

color, fill_color = style.get('color'), style.get('fill_color')
if (((isinstance(color, dim) and color.applies(element)) or color in element) or
(isinstance(fill_color, dim) and fill_color.applies(element)) or fill_color in element):
cdim = None
elif None not in [element.level, self.color_index] and element.vdims:
cdim = element.vdims[0]
else:
cidx = self.color_index+2 if isinstance(self.color_index, int) else self.color_index
cdim = element.get_dimension(cidx)

if cdim is None:
return data, mapping, style

Expand All @@ -221,7 +228,6 @@ def get_data(self, element, ranges, style):
factors = util.unique_array(values) if values.dtype.kind in 'SUO' else None
cmapper = self._get_colormapper(cdim, element, ranges, style, factors)
mapping[self._color_style] = {'field': dim_name, 'transform': cmapper}
self._get_hover_data(data, element)
if self.show_legend:
mapping['legend'] = dim_name
return data, mapping, style
Expand Down
13 changes: 13 additions & 0 deletions holoviews/tests/plotting/bokeh/testpathplot.py
Expand Up @@ -251,6 +251,19 @@ def test_multi_polygon_hole_plot(self):
self.assertEqual(source.data['ys'], [[[np.array([2, 0, 7]), np.array([2, 3, 1.6]),
np.array([4.5, 5, 3.5])], [np.array([7, 5, 2])]]])

def test_polygons_hover_color_op(self):
polygons = Polygons([
{('x', 'y'): [(0, 0), (0, 1), (1, 0)], 'color': 'green'},
{('x', 'y'): [(1, 0), (1, 1), (0, 1)], 'color': 'red'}
], vdims='color').options(fill_color='color', tools=['hover'])
plot = bokeh_renderer.get_plot(polygons)
cds = plot.handles['source']
glyph = plot.handles['glyph']
self.assertEqual(glyph.line_color, 'black')
self.assertEqual(glyph.fill_color, {'field': 'fill_color'})
self.assertEqual(cds.data['color'], np.array(['green', 'red']))
self.assertEqual(cds.data['fill_color'], np.array(['green', 'red']))

def test_polygons_color_op(self):
polygons = Polygons([
{('x', 'y'): [(0, 0), (0, 1), (1, 0)], 'color': 'green'},
Expand Down

0 comments on commit bdcc61a

Please sign in to comment.