Spaces:
Sleeping
Sleeping
import plotly.express as px | |
import plotly.graph_objects as go | |
from plotly.subplots import make_subplots | |
import pandas as pd | |
import streamlit as st | |
class ProcurementCharts: | |
def create_spend_trend_chart(df): | |
"""Create spending trend line chart""" | |
df['PO_Date'] = pd.to_datetime(df['PO_Date']) | |
monthly_spend = df.groupby(df['PO_Date'].dt.to_period('M'))['Total_Value'].sum().reset_index() | |
monthly_spend['PO_Date'] = monthly_spend['PO_Date'].astype(str) | |
fig = px.line(monthly_spend, x='PO_Date', y='Total_Value', | |
title='π Monthly Spending Trend', | |
labels={'Total_Value': 'Spend ($)', 'PO_Date': 'Month'}) | |
fig.update_layout( | |
plot_bgcolor='rgba(0,0,0,0)', | |
paper_bgcolor='rgba(0,0,0,0)', | |
font=dict(color='white'), | |
title_font_size=18, | |
height=400 | |
) | |
fig.update_traces(line_color='#00D4AA', line_width=3) | |
return fig | |
def create_category_pie_chart(df): | |
"""Create category spending pie chart""" | |
category_spend = df.groupby('Category')['Total_Value'].sum().reset_index() | |
fig = px.pie(category_spend, values='Total_Value', names='Category', | |
title='π° Spend by Category', | |
color_discrete_sequence=px.colors.qualitative.Set3) | |
fig.update_layout( | |
plot_bgcolor='rgba(0,0,0,0)', | |
paper_bgcolor='rgba(0,0,0,0)', | |
font=dict(color='white'), | |
title_font_size=18, | |
height=400 | |
) | |
return fig | |
def create_supplier_performance_chart(df): | |
"""Create supplier performance scatter plot""" | |
supplier_metrics = df.groupby('Supplier').agg({ | |
'Total_Value': 'sum', | |
'Delivery_Performance': 'mean', | |
'PO_Number': 'count' | |
}).reset_index() | |
fig = px.scatter(supplier_metrics, x='Total_Value', y='Delivery_Performance', | |
size='PO_Number', hover_name='Supplier', | |
title='π― Supplier Performance vs Spend', | |
labels={'Total_Value': 'Total Spend ($)', | |
'Delivery_Performance': 'Delivery Performance (%)'}) | |
fig.update_layout( | |
plot_bgcolor='rgba(0,0,0,0)', | |
paper_bgcolor='rgba(0,0,0,0)', | |
font=dict(color='white'), | |
title_font_size=18, | |
height=400 | |
) | |
return fig | |
def create_kpi_cards(total_spend, total_pos, avg_delivery, top_supplier): | |
"""Create KPI cards using Plotly""" | |
kpis = [ | |
{"title": "Total Spend", "value": f"${total_spend:,.0f}", "color": "#FF6B6B"}, | |
{"title": "Purchase Orders", "value": f"{total_pos:,}", "color": "#4ECDC4"}, | |
{"title": "Avg Delivery %", "value": f"{avg_delivery:.1f}%", "color": "#45B7D1"}, | |
{"title": "Top Supplier", "value": top_supplier, "color": "#96CEB4"} | |
] | |
return kpis | |
def create_status_donut_chart(df): | |
"""Create PO status donut chart""" | |
status_counts = df['Status'].value_counts().reset_index() | |
fig = go.Figure(data=[go.Pie(labels=status_counts['Status'], | |
values=status_counts['count'], | |
hole=.5)]) | |
fig.update_layout( | |
title="π Purchase Order Status", | |
plot_bgcolor='rgba(0,0,0,0)', | |
paper_bgcolor='rgba(0,0,0,0)', | |
font=dict(color='white'), | |
title_font_size=18, | |
height=400, | |
annotations=[dict(text='PO Status', x=0.5, y=0.5, font_size=16, showarrow=False)] | |
) | |
return fig | |