Skip to content

Commit

Permalink
Allow defining CompositeElementPlot glyph order (#2169)
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr authored Dec 2, 2017
1 parent b6ce41d commit b7ec70f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
15 changes: 6 additions & 9 deletions holoviews/plotting/bokeh/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from ..util import dynamic_update, process_cmap
from .plot import BokehPlot, TOOLS
from .util import (mpl_to_bokeh, get_tab_title, py2js_tickformatter,
rgba_tuple, recursive_model_update)
rgba_tuple, recursive_model_update, glyph_order)

property_prefixes = ['selection', 'nonselection', 'muted', 'hover']

Expand Down Expand Up @@ -936,10 +936,12 @@ def _init_glyphs(self, plot, element, ranges, source, data=None, mapping=None, s
style = self.style[self.cyclic_index]
data, mapping, style = self.get_data(element, ranges, style)

keys = glyph_order(dict(data, **mapping), self._draw_order)

source_cache = {}
current_id = element._plot_id
self.handles['previous_id'] = current_id
for key in dict(mapping, **data):
for key in keys:
ds_data = data.get(key, {})
if id(ds_data) in source_cache:
source = source_cache[id(ds_data)]
Expand Down Expand Up @@ -993,13 +995,8 @@ def _update_glyphs(self, element, ranges):
style = self.style[self.cyclic_index]
data, mapping, style = self.get_data(element, ranges, style)

# Order glyphs by supplied draw order
keys = sorted(dict(mapping, **data))
def order_fn(glyph):
matches = [item for item in self._draw_order if glyph.startswith(item)]
if matches: return self._draw_order.index(matches[0])
return 1e6+keys.index(glyph)
for key in sorted(keys, key=order_fn):
keys = glyph_order(dict(data, **mapping), self._draw_order)
for key in keys:
gdata = data.get(key)
source = self.handles[key+'_source']
glyph = self.handles.get(key+'_glyph')
Expand Down
20 changes: 20 additions & 0 deletions holoviews/plotting/bokeh/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,3 +602,23 @@ def date_to_integer(date):
else:
raise ValueError('Datetime type not recognized')
return dt_int


def glyph_order(keys, draw_order=[]):
"""
Orders a set of glyph handles using regular sort and an explicit
sort order. The explicit draw order must take the form of a list
of glyph names while the keys should be glyph names with a custom
suffix. The draw order may only match subset of the keys and any
matched items will take precedence over other entries.
>>> glyph_order(['scatter_1', 'patch_1', 'rect_1'], \
['scatter', 'patch'])
['scatter_1', 'patch_1', 'rect_1']
"""
keys = sorted(keys)
def order_fn(glyph):
matches = [item for item in draw_order if glyph.startswith(item)]
return ((draw_order.index(matches[0]), glyph) if matches else
(1e9+keys.index(glyph), glyph))
return sorted(keys, key=order_fn)

0 comments on commit b7ec70f

Please sign in to comment.