# Vizro is an open-source toolkit for creating modular data visualization applications.
# check out https://github.com/mckinsey/vizro for more info about Vizro
# and checkout https://vizro.readthedocs.io/en/stable/ for documentation.
import pandas as pd
import plotly.express as px
import vizro.models as vm
import vizro.plotly.express as px
from vizro import Vizro
from vizro.models.types import capture
# Load the data
df = pd.read_csv("https://raw.githubusercontent.com/plotly/Figure-Friday/main/2024/week-29/ewf_standings.csv")
df = df[df["team_name"] == "Manchester City Women"]
@capture("graph")
def stacked_bar(data_frame, **kwargs):
    """Create a stacked 100% bar chart for the selected team."""
    fig = px.histogram(data_frame, **kwargs)
    fig.update_layout(
        xaxis=dict(
            title=None,
            tickmode="array",
            tickvals=[0, 0.2, 0.4, 0.6, 0.8, 1],
            ticktext=["0%", "20%", "40%", "60%", "80%", "100%"],
        ),
        yaxis_title="Season",
        hovermode=False,
        margin={"l": 0, "r": 0},
        legend=dict(
            title=None,
            yanchor="top",
            y=1.05,
            xanchor="right",
            x=0.95
        )
    )
    return fig
page = vm.Page(
    title="Figure Friday Week 29 - English Women's Football",
    components=[
        vm.Graph(
            header="""
            ### A decade of success: The evolution of **Manchester City Women's** win rate ⚽
            
            #### In 2024, Manchester City Women's win rate soared to **82%, nearly doubling from 43%** in the 2014 season.
            """,
            figure=stacked_bar(
                data_frame=df,
                y="season",
                x=["wins", "losses", "draws"],
                barnorm="fraction",
                text_auto=".0%",
                color_discrete_map={"wins": "#625AD8", "losses": "#adb5bd", "draws": "#dee2e6"},
            ),
            footer="""
            **Data source:** The English Women’s Football (EWF) Database, May 2024, https://github.com/probjects/ewf-database. 
            Since the competition's start, games were played within a single year as a summer league. This changed in 
            the 2017-2018 season to a winter league format, aligning with the traditional English football calendar. 
            Learn more about the Plotly Figure Friday - Week 29 initiative [here](https://community.plotly.com/t/figure-friday-2024-week-29/85928).
            """,
        ),
    ],
)
dashboard = vm.Dashboard(pages=[page], theme="vizro_light")
Vizro().build(dashboard).run()