/
utils.py
113 lines (95 loc) · 2.44 KB
/
utils.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
from collections import namedtuple
from plotly.offline import plot
from plotly.graph_objs.layout import Margin
from whotracksme.website.plotting.colors import (
tracker_category_colors, cliqz_colors
)
FontSet = namedtuple("FontSet", 'mono regular')
CliqzFonts = FontSet(
mono="monospace",
regular="sans-serif"
)
def set_category_colors(tracker_labels):
colors = []
for l in tracker_labels:
colors.append(tracker_category_colors[l])
return colors
def set_margins(l=60, r=60, b=40, t=0, pad=5):
return Margin(
l=l,
r=r,
b=b,
t=t,
pad=pad
)
def set_line_style(color, width=3, line_style="solid"):
"""
Defines default styling of scatter graphs with some smoothing.
Args:
color: line color
width: line width
line_style: line style (solid, dashed, ...)
Returns: line_style dict parameters
"""
style = dict(
color=color,
width=width,
# shape='spline',
# smoothing=1.3,
dash=line_style
)
return style
def annotation(text, x, y, background_color, shift_x=-1, text_size=12, color="white"):
if shift_x == -1:
shift_x = 4 * len(text) + 10
return dict(
x=x,
y=y,
xref='x',
yref='y',
text=text,
font=dict(
family=CliqzFonts.regular,
size=text_size,
color=color
),
width=len(text) * 10 if len(text) > 30 else 100,
showarrow=True,
ax=shift_x,
ay=0,
align='center',
bordercolor=background_color,
borderpad=5,
borderwidth=1,
bgcolor=background_color,
)
def overview_label(text, x, y, text_size=12, shift_x=-1, color=cliqz_colors["black"]):
if shift_x == -1:
shift_x = 4 * len(text) + 10
return dict(
x=x,
y=y,
xref='x',
yref='y',
text=text,
font=dict(
family=CliqzFonts.regular,
size=text_size,
color=color
),
width=len(text) * 10 if len(text) > 30 else 100,
showarrow=False,
ax=shift_x,
ay=0,
align='center',
borderpad=5,
borderwidth=1,
)
def div_output(fig, display_mode_bar=False):
return plot(
figure_or_data=fig,
output_type='div',
show_link=False,
include_plotlyjs=False,
config={"displayModeBar": display_mode_bar}
)