forked from poplarShift/python-data-science-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
style_link.py
65 lines (54 loc) · 2.08 KB
/
style_link.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import holoviews as hv
class StyleLink():
"""
Class that fixes style options across holoviews elements.
Init e.g. like so:
sl = StyleLink(
[
[styling_group_A_element_1, styling_group_A_element_2],
[styling_group_B_element_1, styling_group_B_element_2],
],
{
'line_color': hv.Cycle(['blue', 'orange', 'salmon']),
'line_dash': hv.Cycle(['dotted', 'solid'])
}
)
Elements are given as a list of lists. The hv.Cycle instances will then cycle
through the outer level of the nested list, giving the same styling options to
every item of the inner list nesting level.
License
-------
GNU-GPLv3, (C) A. R.
(https://github.com/poplarShift/python-data-science-utils)
"""
def __init__(self, elements, link_options):
self.default_options = hv.Store.options(hv.Store.current_backend)
self.elements = elements
self.link_options = link_options
self.linked = list(link_options.keys())
self.link()
def link(self):
elements, link_options = self.elements, self.link_options
for k, es in enumerate(elements):
if not isinstance(es, list):
es = [es]
for m, e in enumerate(es):
etype = type(e).__name__
opt_dict = {}
for opt, cyc in link_options.items():
if cyc is None:
cyc = getattr(self.default_options, etype)['style'].kwargs[opt]
N = len(cyc.values)
opt_dict[opt] = cyc.values[k%N]
elements[k][m].opts(**opt_dict)
def unlink(self):
elements, linked = self.elements, self.linked
for k, es in enumerate(elements):
if not isinstance(es, list):
es = [es]
for e in es:
etype = type(e).__name__
opt_dict = {}
for opt in linked:
opt_dict[opt] = getattr(self.default_options, etype)['style'].kwargs[opt]
e.opts(**opt_dict)