File size: 995 Bytes
f58c9c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import altair as alt 
import pandas as pd
import matplotlib.cm as cm

def altair_gauge(score, max_score, title):
    source = pd.DataFrame({"category": [1,2], "value": [4,3], "tt": ['hello!',None]})
    gauge_theta2 = -1 * 2 * 3.14 * 0.25 + 3.14 * score / float(max_score)
    c = alt.layer(
        alt.Chart(source).mark_arc(innerRadius=100, theta=0, thetaOffset=(-1*2*3.14*0.25), theta2=(-1*2*3.14*0.25 + 3.14), color='lightgray', tooltip=None),
        alt.Chart(source).mark_arc(innerRadius=100, theta=0, thetaOffset=(-1*2*3.14*0.25), theta2=gauge_theta2, tooltip=f'{title}: {int(score)}', color=get_color(score, max_score)),
        alt.Chart(source).mark_text(text='%.1d' % score, size=80, font='Calibri', dy=-30)
    ).properties(title=title)
    return c

def get_color(score, max_score):
    cmap = cm.get_cmap('RdYlGn')
    color = cmap(score / float(max_score))
    color = f'rgba({int(color[0]*256)}, {int(color[1]*256)}, {int(color[2]*256)}, {int(color[3]*256)})'
    return color