File size: 2,049 Bytes
1d769a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 streamlit as st
import pandas as pd
import altair as alt

# Load hospital dataset
hospitals_df = pd.read_csv("https://data.medicare.gov/api/views/xubh-q36u/rows.csv?accessType=DOWNLOAD")

# Filter for the largest hospital in each state
largest_hospitals_df = hospitals_df.loc[hospitals_df.groupby("State")["Hospital overall rating"].idxmax()]

# Select columns to display
cols_to_display = ["State", "Hospital Name", "City", "Zip Code", "lat", "lng"]

# Create a Streamlit table to display the largest hospitals
st.table(largest_hospitals_df[cols_to_display])

# Define chart functions
def stacked_bar_chart():
    chart = alt.Chart(largest_hospitals_df).mark_bar().encode(
        x=alt.X('State:N'),
        y=alt.Y('count():Q', stack="normalize"),
        color=alt.Color('Hospital Type:N'),
        tooltip=['Hospital Name', 'City', 'Hospital overall rating']
    ).properties(
        width=700,
        height=400,
        title='Number of Hospitals by State and Type'
    )
    text = chart.mark_text(
        align='center',
        baseline='middle',
        dx=0,
        dy=5,
        color='white'
    ).encode(
        text=alt.Text('count():Q', format='.1f')
    )
    st.altair_chart(chart + text)

def bump_chart():
    chart = alt.Chart(largest_hospitals_df).transform_rank(
        'Hospital overall rating',
        groupby=['State']
    ).mark_line().encode(
        x='rank:Q',
        y=alt.Y('Hospital Name:N', sort='-x'),
        color=alt.Color('State:N'),
        tooltip=['City', 'Zip Code', 'Hospital overall rating']
    ).properties(
        width=700,
        height=400,
        title='Hospital Rankings by State'
    )
    st.altair_chart(chart)

# Define chart buttons
st.sidebar.header('Select a Chart')
chart_options = ['Stacked Bar Chart with Text Overlay', 'Bump Chart']
chart_choice = st.sidebar.selectbox('', chart_options)

# Call chart functions based on user input
if chart_choice == 'Stacked Bar Chart with Text Overlay':
    stacked_bar_chart()
elif chart_choice == 'Bump Chart':
    bump_chart()