import plotly.graph_objects as go


def load_data():
    return [
        ("Car", 42),
        ("Bus", 31),
        ("Walking", 26),
        ("Bicycle", 19),
        ("Train", 14),
    ]


def value_table(data):
    return {"columns": ["Commute mode", "Respondents (%)"], "rows": [list(row) for row in data]}


def make_after(data) -> go.Figure:
    modes, percentages = zip(*sorted(data, key=lambda row: -row[1]), strict=True)
    fig = go.Figure(
        go.Bar(
            x=percentages,
            y=modes,
            orientation="h",
            marker_color="#356890",
            text=[f"{value}%" for value in percentages],
            textposition="outside",
            hovertemplate="%{y}: %{x}%<extra></extra>",
        )
    )
    fig.update_layout(
        template="simple_white",
        font={"family": "Inter, system-ui, -apple-system, sans-serif", "color": "#1f2937"},
        title={
            "text": "Commute modes selected",
            "subtitle": {"text": "Respondents could select multiple categories."},
        },
        xaxis={"title": "Respondents (%)", "range": [0, 55], "dtick": 10},
        yaxis_autorange="reversed",
        margin={"l": 80, "r": 28, "t": 78, "b": 60},
    )
    return fig


if __name__ == "__main__":
    print(make_after(load_data()).to_json())
