PurchaseAgent2 / utils /charts.py
PD03's picture
Create utils/charts.py
a5eb38c verified
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:
@staticmethod
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
@staticmethod
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
@staticmethod
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
@staticmethod
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
@staticmethod
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