File size: 2,587 Bytes
1af09ab
864a52e
1af09ab
864a52e
 
 
 
 
 
1af09ab
864a52e
1af09ab
864a52e
1af09ab
864a52e
 
 
 
 
 
1af09ab
864a52e
 
 
 
1af09ab
 
864a52e
 
 
 
 
1af09ab
864a52e
 
1af09ab
864a52e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import panel as pn
import vega_datasets

# Enable Panel extensions
pn.extension(design='bootstrap')
pn.extension('vega')
template = pn.template.BootstrapTemplate(
    title='Nan-Hsin Lin',
)

# Define a function to create and return a plot

def create_plot(subgroup, date_range, moving_av_window):

    # Apply any required transformations to the data in pandas
    df3 = df2[df2['choice'] == 'approve'].copy()
    df3 = df3[df3['subgroup'] == subgroup]
    df3['smoothed_rate'] = df3['rate'].rolling(moving_av_window).mean().shift(-int(moving_av_window/2))
    start_date, end_date = date_range
    df3 = df3[(df3['timestamp'] >= np.datetime64(start_date)) & (df3['timestamp'] <= np.datetime64(end_date))]

    # Line chart
    rate_line = alt.Chart(df3).mark_line(strokeWidth=2, color='red').encode(
        x=alt.X('timestamp:T', axis=alt.Axis(title=None)),
        y=alt.Y('average(smoothed_rate):Q', axis=alt.Axis(title='move_avg'), scale=alt.Scale(domain=[30, 60]))
    )

    # Scatter plot with individual polls
    rate_scatter = alt.Chart(df3).mark_point(color='grey', size=2, opacity=0.7).encode(
        x=alt.X('timestamp:T', axis=alt.Axis(title=None)),
        y=alt.Y('average(rate):Q', axis=alt.Axis(title='approve'), scale=alt.Scale(domain=[30, 60])), 
    )

    # Put them together
    plot = alt.layer(rate_line, rate_scatter).configure_view(strokeWidth=0)
    
    # Return the combined chart
    return plot

# Create the selection widget
select = pn.widgets.Select(name='Select', options=df2['subgroup'].unique().tolist())

# Create the slider for the date range
dateSlider = pn.widgets.DateRangeSlider(name='Date Range Slider', 
                                    start=df2['timestamp'].min(), 
                                    end=df2['timestamp'].max(),
                                    value=(df2['timestamp'].min(), df2['timestamp'].max()))

# Create the slider for the moving average window
avgSlider = pn.widgets.IntSlider(name='Moving average window', start=1, end=100, value=1)

# Bind the widgets to the create_plot function
plot_widgets = pn.Row(pn.bind(create_plot, select, dateSlider, avgSlider))

# Combine everything in a Panel Column to create an app
maincol = pn.Column()
maincol.append("# SI649 Lab07")
maincol.append("Hello! This is **Nan**. I love information visualization! Email me: [[email protected]](mailto:[email protected])")
maincol.append(plot_widgets)
maincol.append(select)
maincol.append(dateSlider)
maincol.append(avgSlider)
template.main.append(maincol)

# set the app to be servable
template.servable(title="Nan-Hsin Lin")