import plotly.graph_objects as go
import plotly.express as px

# Data from the provided JSON
participants = ["User 4", "User 1", "User 8", "User 15", "User 19", "User 2", "User 5", "User 9", "User 12", "User 13", "User 14", "User 17", "User 18", "User 10", "User 11", "User 16", "User 3", "User 6", "User 7", "User 20"]
completion_rates = [100.0, 85.71, 85.71, 85.71, 85.71, 71.43, 71.43, 71.43, 71.43, 71.43, 71.43, 71.43, 71.43, 57.14, 57.14, 57.14, 42.86, 42.86, 42.86, 42.86]

# Create horizontal bar chart
fig = go.Figure(data=go.Bar(
    x=completion_rates,
    y=participants,
    orientation='h',
    marker=dict(
        color=completion_rates,
        colorscale='Blues',
        cmin=min(completion_rates),  # Use actual minimum
        cmax=max(completion_rates)   # Use actual maximum
    ),
    cliponaxis=False
))

# Update layout
fig.update_layout(
    title="Habit Completion Rate by Participant",
    xaxis_title="Completion %",
    yaxis_title="Participants"
)

# Update axes and remove grid lines
fig.update_xaxes(range=[0, 100], showgrid=False)
fig.update_yaxes(categoryorder='array', categoryarray=participants, showgrid=False)

# Save the chart
fig.write_image("habit_completion_chart.png")