Spaces:
Sleeping
Sleeping
Create utils/charts.py
Browse files- utils/charts.py +105 -0
utils/charts.py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import plotly.express as px
|
| 2 |
+
import plotly.graph_objects as go
|
| 3 |
+
from plotly.subplots import make_subplots
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import streamlit as st
|
| 6 |
+
|
| 7 |
+
class ProcurementCharts:
|
| 8 |
+
@staticmethod
|
| 9 |
+
def create_spend_trend_chart(df):
|
| 10 |
+
"""Create spending trend line chart"""
|
| 11 |
+
df['PO_Date'] = pd.to_datetime(df['PO_Date'])
|
| 12 |
+
monthly_spend = df.groupby(df['PO_Date'].dt.to_period('M'))['Total_Value'].sum().reset_index()
|
| 13 |
+
monthly_spend['PO_Date'] = monthly_spend['PO_Date'].astype(str)
|
| 14 |
+
|
| 15 |
+
fig = px.line(monthly_spend, x='PO_Date', y='Total_Value',
|
| 16 |
+
title='π Monthly Spending Trend',
|
| 17 |
+
labels={'Total_Value': 'Spend ($)', 'PO_Date': 'Month'})
|
| 18 |
+
|
| 19 |
+
fig.update_layout(
|
| 20 |
+
plot_bgcolor='rgba(0,0,0,0)',
|
| 21 |
+
paper_bgcolor='rgba(0,0,0,0)',
|
| 22 |
+
font=dict(color='white'),
|
| 23 |
+
title_font_size=18,
|
| 24 |
+
height=400
|
| 25 |
+
)
|
| 26 |
+
fig.update_traces(line_color='#00D4AA', line_width=3)
|
| 27 |
+
|
| 28 |
+
return fig
|
| 29 |
+
|
| 30 |
+
@staticmethod
|
| 31 |
+
def create_category_pie_chart(df):
|
| 32 |
+
"""Create category spending pie chart"""
|
| 33 |
+
category_spend = df.groupby('Category')['Total_Value'].sum().reset_index()
|
| 34 |
+
|
| 35 |
+
fig = px.pie(category_spend, values='Total_Value', names='Category',
|
| 36 |
+
title='π° Spend by Category',
|
| 37 |
+
color_discrete_sequence=px.colors.qualitative.Set3)
|
| 38 |
+
|
| 39 |
+
fig.update_layout(
|
| 40 |
+
plot_bgcolor='rgba(0,0,0,0)',
|
| 41 |
+
paper_bgcolor='rgba(0,0,0,0)',
|
| 42 |
+
font=dict(color='white'),
|
| 43 |
+
title_font_size=18,
|
| 44 |
+
height=400
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
return fig
|
| 48 |
+
|
| 49 |
+
@staticmethod
|
| 50 |
+
def create_supplier_performance_chart(df):
|
| 51 |
+
"""Create supplier performance scatter plot"""
|
| 52 |
+
supplier_metrics = df.groupby('Supplier').agg({
|
| 53 |
+
'Total_Value': 'sum',
|
| 54 |
+
'Delivery_Performance': 'mean',
|
| 55 |
+
'PO_Number': 'count'
|
| 56 |
+
}).reset_index()
|
| 57 |
+
|
| 58 |
+
fig = px.scatter(supplier_metrics, x='Total_Value', y='Delivery_Performance',
|
| 59 |
+
size='PO_Number', hover_name='Supplier',
|
| 60 |
+
title='π― Supplier Performance vs Spend',
|
| 61 |
+
labels={'Total_Value': 'Total Spend ($)',
|
| 62 |
+
'Delivery_Performance': 'Delivery Performance (%)'})
|
| 63 |
+
|
| 64 |
+
fig.update_layout(
|
| 65 |
+
plot_bgcolor='rgba(0,0,0,0)',
|
| 66 |
+
paper_bgcolor='rgba(0,0,0,0)',
|
| 67 |
+
font=dict(color='white'),
|
| 68 |
+
title_font_size=18,
|
| 69 |
+
height=400
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
return fig
|
| 73 |
+
|
| 74 |
+
@staticmethod
|
| 75 |
+
def create_kpi_cards(total_spend, total_pos, avg_delivery, top_supplier):
|
| 76 |
+
"""Create KPI cards using Plotly"""
|
| 77 |
+
kpis = [
|
| 78 |
+
{"title": "Total Spend", "value": f"${total_spend:,.0f}", "color": "#FF6B6B"},
|
| 79 |
+
{"title": "Purchase Orders", "value": f"{total_pos:,}", "color": "#4ECDC4"},
|
| 80 |
+
{"title": "Avg Delivery %", "value": f"{avg_delivery:.1f}%", "color": "#45B7D1"},
|
| 81 |
+
{"title": "Top Supplier", "value": top_supplier, "color": "#96CEB4"}
|
| 82 |
+
]
|
| 83 |
+
|
| 84 |
+
return kpis
|
| 85 |
+
|
| 86 |
+
@staticmethod
|
| 87 |
+
def create_status_donut_chart(df):
|
| 88 |
+
"""Create PO status donut chart"""
|
| 89 |
+
status_counts = df['Status'].value_counts().reset_index()
|
| 90 |
+
|
| 91 |
+
fig = go.Figure(data=[go.Pie(labels=status_counts['Status'],
|
| 92 |
+
values=status_counts['count'],
|
| 93 |
+
hole=.5)])
|
| 94 |
+
|
| 95 |
+
fig.update_layout(
|
| 96 |
+
title="π Purchase Order Status",
|
| 97 |
+
plot_bgcolor='rgba(0,0,0,0)',
|
| 98 |
+
paper_bgcolor='rgba(0,0,0,0)',
|
| 99 |
+
font=dict(color='white'),
|
| 100 |
+
title_font_size=18,
|
| 101 |
+
height=400,
|
| 102 |
+
annotations=[dict(text='PO Status', x=0.5, y=0.5, font_size=16, showarrow=False)]
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
return fig
|