File size: 3,934 Bytes
a5eb38c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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