Spaces:
Sleeping
Sleeping
File size: 6,174 Bytes
d7d3dff | 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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 | import streamlit as st
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import pandas as pd
from datetime import datetime, timedelta
class DashboardComponents:
def __init__(self, colors):
self.colors = colors
def render_metric_card(self, title, value, subtitle=None, trend=None, trend_value=None):
"""Render a metric card with optional trend indicator"""
trend_html = ""
if trend and trend_value:
trend_color = self.colors['success'] if trend == 'up' else self.colors['danger']
trend_arrow = '↑' if trend == 'up' else '↓'
trend_html = f"""
<div style="color: {trend_color}; font-size: 0.9rem; margin-top: 5px;">
{trend_arrow} {trend_value}%
</div>
"""
st.markdown(f"""
<div class="metric-card">
<div style="color: {self.colors['subtext']}; font-size: 0.9rem;">{title}</div>
<div style="color: {self.colors['text']}; font-size: 2rem; font-weight: bold; margin: 10px 0;">
{value}
</div>
{f'<div style="color: {self.colors["subtext"]}; font-size: 0.8rem;">{subtitle}</div>' if subtitle else ''}
{trend_html}
</div>
""", unsafe_allow_html=True)
def create_gauge_chart(self, value, title):
"""Create a gauge chart for metrics like ATS score"""
fig = go.Figure(go.Indicator(
mode="gauge+number",
value=value,
title={'text': title, 'font': {'size': 24, 'color': self.colors['text']}},
gauge={
'axis': {'range': [0, 100], 'tickwidth': 1, 'tickcolor': self.colors['text']},
'bar': {'color': self.colors['primary']},
'bgcolor': "white",
'borderwidth': 2,
'bordercolor': "gray",
'steps': [
{'range': [0, 40], 'color': self.colors['danger']},
{'range': [40, 70], 'color': self.colors['warning']},
{'range': [70, 100], 'color': self.colors['success']}
],
}
))
fig.update_layout(
paper_bgcolor=self.colors['card'],
plot_bgcolor=self.colors['card'],
font={'color': self.colors['text']},
height=300,
margin=dict(l=20, r=20, t=50, b=20)
)
return fig
def create_trend_chart(self, dates, values, title):
"""Create a trend line chart"""
fig = go.Figure()
fig.add_trace(go.Scatter(
x=dates,
y=values,
mode='lines+markers',
line=dict(color=self.colors['info'], width=3),
marker=dict(size=8, color=self.colors['info'])
))
fig.update_layout(
title=title,
paper_bgcolor=self.colors['card'],
plot_bgcolor=self.colors['card'],
font={'color': self.colors['text']},
height=300,
margin=dict(l=20, r=20, t=50, b=20),
xaxis=dict(
showgrid=True,
gridwidth=1,
gridcolor=self.colors['background']
),
yaxis=dict(
showgrid=True,
gridwidth=1,
gridcolor=self.colors['background']
)
)
return fig
def create_bar_chart(self, categories, values, title):
"""Create a bar chart"""
fig = go.Figure(go.Bar(
x=categories,
y=values,
marker_color=self.colors['primary'],
text=values,
textposition='auto',
))
fig.update_layout(
title=title,
paper_bgcolor=self.colors['card'],
plot_bgcolor=self.colors['card'],
font={'color': self.colors['text']},
height=300,
margin=dict(l=20, r=20, t=50, b=20),
xaxis=dict(
showgrid=False,
title_text="Categories",
color=self.colors['text']
),
yaxis=dict(
showgrid=True,
gridwidth=1,
gridcolor=self.colors['background'],
title_text="Values",
color=self.colors['text']
)
)
return fig
def create_dual_axis_chart(self, categories, values1, values2, title):
"""Create a chart with dual y-axes"""
fig = make_subplots(specs=[[{"secondary_y": True}]])
fig.add_trace(
go.Bar(
x=categories,
y=values1,
name="Count",
marker_color=self.colors['secondary']
),
secondary_y=False
)
fig.add_trace(
go.Scatter(
x=categories,
y=values2,
name="Score",
line=dict(color=self.colors['warning'], width=3),
mode='lines+markers'
),
secondary_y=True
)
fig.update_layout(
title=title,
paper_bgcolor=self.colors['card'],
plot_bgcolor=self.colors['card'],
font={'color': self.colors['text']},
height=300,
margin=dict(l=20, r=20, t=50, b=20),
showlegend=True,
legend=dict(
orientation="h",
yanchor="bottom",
y=1.02,
xanchor="right",
x=1
)
)
fig.update_xaxes(title_text="Categories", color=self.colors['text'])
fig.update_yaxes(title_text="Count", color=self.colors['text'], secondary_y=False)
fig.update_yaxes(title_text="Score", color=self.colors['text'], secondary_y=True)
return fig |