This repository has been archived by the owner on Nov 20, 2021. It is now read-only.
/
js_events.py
62 lines (53 loc) · 2.2 KB
/
js_events.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
from .js_utils import JSCode
from cytoolz import valmap
import jinja2
def d3_identity_func():
return JSCode('function(__val__) { return __val__; }')
def tooltip_events(click_event, tooltip_content):
"""
Helper functions that, given a click event name, displays a tooltip with the specified content.
:param click_event: string - name of the click event. Example: 'area_click'
:param tooltip_content: dict {column_name -> column_format} - names of a datum properties to be included in the tooltip.
:return: a dictionary with JSCode instances, to be used as the 'events' parameter of a visualization module.
"""
events = {}
# figure is defined in the base.js template
code = jinja2.Template('''
function() {
auxiliary.tip = datagramas.tip()
.attr('class', 'd3-tip')
.parent(figure.node().parentNode)
.html(function(d) {
console.log('tip', d);
var tooltip_content = '<dl>';
{% for key, f in tooltip_content.items() %}
tooltip_content += '<dt>{{ key }}</dt><dd>' + {{ f }}(datagramas.get(d, '{{ key }}')) + '</dd>';
{% endfor %}
tooltip_content += '</dl>';
return tooltip_content;
});
console.log('this', this);
d3.select(this).call(auxiliary.tip);
auxiliary.current_tip = null;
}
''').render(tooltip_content=tooltip_content)
events['datagram_start'] = JSCode(code)
# this event is the same in all cases.
events[click_event] = JSCode('''
function(d) {
if (auxiliary.current_tip !== null) {
if (auxiliary.current_tip !== d) {
auxiliary.tip.hide(auxiliary.current_tip);
auxiliary.current_tip = null;
} else {
console.log('repeated tip');
auxiliary.current_tip = null;
auxiliary.tip.hide(d);
return;
}
}
auxiliary.tip.show(d, container)
auxiliary.current_tip = d;
}
''')
return events