Skip to content

Commit

Permalink
Fixes for changes to categorical handling in bokeh 0.12.7 (#1849)
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr committed Sep 12, 2017
1 parent 3b5e55d commit 08bc330
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 13 deletions.
59 changes: 48 additions & 11 deletions holoviews/plotting/bokeh/chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,24 @@ def get_extents(self, element, ranges):
x1 = xdim.pprint_value(extents[2])
return (x0, y0, x1, y1)


def _get_factors(self, element):
"""
Get factors for categorical axes.
"""
xdim, ydim = element.dimensions()[:2]
gdim = element.get_dimension(self.group_index)
xvals = element.dimension_values(0, False)
xvals = [x if xvals.dtype.kind in 'SU' else xdim.pprint_value(x) for x in xvals]
if bokeh_version >= '0.12.7' and gdim:
gvals = element.dimension_values(gdim, False)
gvals = [g if gvals.dtype.kind in 'SU' else gdim.pprint_value(g) for g in gvals]
coords = ([(x, g) for x in xvals for g in gvals], [])
else:
coords = ([x.replace(':', ';') for x in xvals], [])
if self.invert_axes: coords = coords[::-1]
return coords

def _get_axis_labels(self, *args, **kwargs):
"""
Override axis mapping by setting the first key and value
Expand All @@ -663,15 +681,20 @@ def _get_axis_labels(self, *args, **kwargs):
element = self.current_frame
if self.batched:
element = element.last
return (dim_axis_label(element.kdims[0]),
dim_axis_label(element.vdims[0]), None)
xlabel = dim_axis_label(element.kdims[0])
gdim = element.get_dimension(self.group_index)
if bokeh_version >= '0.12.7' and gdim:
xlabel = ', '.join([xlabel, dim_axis_label(gdim)])
return (xlabel, dim_axis_label(element.vdims[0]), None)

def get_group(self, xvals, nshift, ngroups, width, xdim):
"""
Adjust x-value positions on categorical axes to stop
x-axis overlapping. Currently bokeh uses a suffix
of the format ':%f' with a floating value to set up
offsets within a single category.
Needed for bokeh version <0.12.7
"""
adjusted_xvals = []
gwidth = float(width)/ngroups
Expand Down Expand Up @@ -742,7 +765,7 @@ def get_data(self, element, ranges, empty):
mapping = {'x': xdim.name, 'top': 'top',
'bottom': 'bottom', 'width': width}
elif grouping == 'grouped':
if len(grouped):
if len(grouped) and bokeh_version < '0.12.7':
gwidth = width / float(len(grouped))
else:
gwidth = width
Expand Down Expand Up @@ -775,6 +798,11 @@ def get_data(self, element, ranges, empty):
for i, (k, ds) in enumerate(grouped.items()):
xs = ds.dimension_values(xdim)
ys = ds.dimension_values(ydim)
k = k[0] if isinstance(k, tuple) else k
if group_dim:
gval = k if isinstance(k, basestring) else group_dim.pprint_value(k)
if bokeh_version < '0.12.7':
gval = gval.replace(':', ';')

# Apply stacking or grouping
if grouping == 'stacked':
Expand All @@ -784,7 +812,11 @@ def get_data(self, element, ranges, empty):
data[xdim.name].append(xs)
if hover: data[ydim.name].append(ys)
elif grouping == 'grouped':
xoffsets = self.get_group(xs, i, len(grouped), width, xdim)
if bokeh_version >= '0.12.7':
xoffsets = [(x if xs.dtype.kind in 'SU' else xdim.pprint_value(x), gval)
for x in xs]
else:
xoffsets = self.get_group(xs, i, len(grouped), width, xdim)
data['xoffsets'].append(xoffsets)
data[ydim.name].append(ys)
if hover: data[xdim.name].append(xs)
Expand All @@ -794,8 +826,6 @@ def get_data(self, element, ranges, empty):

# Add group dimension to data
if group_dim and group_dim not in ds.dimensions():
k = k[0] if isinstance(k, tuple) else k
gval = group_dim.pprint_value(k).replace(':', ';')
ds = ds.add_dimension(group_dim.name, ds.ndims, gval)

# Get colormapper
Expand Down Expand Up @@ -922,9 +952,13 @@ def _get_factors(self, element):
if not element.kdims:
return [element.label], []
else:
factors = [', '.join([d.pprint_value(v).replace(':', ';')
for d, v in zip(element.kdims, key)])
for key in element.groupby(element.kdims).data.keys()]
if bokeh_version < '0.12.7':
factors = [', '.join([d.pprint_value(v).replace(':', ';')
for d, v in zip(element.kdims, key)])
for key in element.groupby(element.kdims).data.keys()]
else:
factors = [tuple(d.pprint_value(v) for d, v in zip(element.kdims, key))
for key in element.groupby(element.kdims).data.keys()]
if self.invert_axes:
return [], factors
else:
Expand Down Expand Up @@ -969,8 +1003,11 @@ def get_data(self, element, ranges=None, empty=False):
for key, g in groups.items():
# Compute group label
if element.kdims:
label = ', '.join([d.pprint_value(v).replace(':', ';')
for d, v in zip(element.kdims, key)])
if bokeh_version < '0.12.7':
label = ', '.join([d.pprint_value(v).replace(':', ';')
for d, v in zip(element.kdims, key)])
else:
label = tuple(d.pprint_value(v) for d, v in zip(element.kdims, key))
else:
label = key

Expand Down
6 changes: 4 additions & 2 deletions holoviews/plotting/bokeh/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -614,8 +614,10 @@ def _get_factors(self, element):
xdim, ydim = element.dimensions()[:2]
xvals, yvals = [element.dimension_values(i, False)
for i in range(2)]
coords = ([x if xvals.dtype.kind in 'SU' else xdim.pprint_value(x).replace(':', ';') for x in xvals],
[y if yvals.dtype.kind in 'SU' else ydim.pprint_value(y).replace(':', ';') for y in yvals])
coords = ([v if vals.dtype.kind in 'SU' else dim.pprint_value(v) for v in vals]
for dim, vals in [(xdim, xvals), (ydim, yvals)])
if bokeh_version < '0.12.7':
coords = tuple([v.replace(':', ';') for v in vals] for vals in coords)
if self.invert_axes: coords = coords[::-1]
return coords

Expand Down

0 comments on commit 08bc330

Please sign in to comment.