FinancialPlatform / app /charts.py
Dmitry Beresnev
app refactoring
50e9fe0
"""Chart creation utilities for the financial dashboard."""
import plotly.graph_objects as go
import pandas as pd
def get_dark_theme_layout():
"""Get common dark theme layout settings for all charts."""
return dict(
plot_bgcolor="#0d1117",
paper_bgcolor="#0e1117",
font=dict(color="#e6edf3", size=12, family="Arial, sans-serif"),
xaxis=dict(
gridcolor="#30363d",
showgrid=True,
zeroline=False,
color="#8b949e"
),
yaxis=dict(
gridcolor="#30363d",
showgrid=True,
zeroline=False,
color="#8b949e"
),
legend=dict(
bgcolor="rgba(13, 17, 23, 0.8)",
bordercolor="#30363d",
borderwidth=1,
font=dict(color="#e6edf3")
),
hoverlabel=dict(
bgcolor="#0d1117",
bordercolor="#30363d",
font=dict(color="#e6edf3")
)
)
def create_price_chart(df: pd.DataFrame, symbol: str, period: int) -> go.Figure:
"""Create price chart with SMA and EMA indicators."""
fig = go.Figure()
fig.add_trace(go.Scatter(
x=df.index, y=df["close"],
name="Close Price",
line=dict(color="#0066ff", width=2.5)
))
fig.add_trace(go.Scatter(
x=df.index, y=df["SMA"],
name=f"SMA {period}",
line=dict(color="#00d084", width=2, dash="dash")
))
fig.add_trace(go.Scatter(
x=df.index, y=df["EMA"],
name=f"EMA {period}",
line=dict(color="#ffa500", width=2, dash="dot")
))
layout = get_dark_theme_layout()
fig.update_layout(
title=f"{symbol} - Price with Moving Averages",
xaxis_title="Date",
yaxis_title="Price ($)",
hovermode="x unified",
template="plotly_dark",
height=500,
margin=dict(l=0, r=0, t=40, b=0),
**layout
)
return fig
def create_rsi_chart(df: pd.DataFrame, symbol: str) -> go.Figure:
"""Create RSI (Relative Strength Index) chart."""
fig = go.Figure()
fig.add_trace(go.Scatter(
x=df.index, y=df["RSI"],
name="RSI",
line=dict(color="#ff3838", width=2.5),
fill="tozeroy",
fillcolor="rgba(255, 56, 56, 0.15)"
))
fig.add_hline(y=70, line_dash="dash", line_color="rgba(255, 165, 0, 0.6)",
annotation_text="Overbought (70)")
fig.add_hline(y=30, line_dash="dash", line_color="rgba(0, 208, 132, 0.6)",
annotation_text="Oversold (30)")
fig.add_hline(y=50, line_dash="dot", line_color="rgba(139, 148, 158, 0.3)")
layout = get_dark_theme_layout()
layout["yaxis"]["range"] = [0, 100]
fig.update_layout(
title=f"{symbol} - Relative Strength Index (RSI)",
xaxis_title="Date",
yaxis_title="RSI",
hovermode="x unified",
template="plotly_dark",
height=500,
margin=dict(l=0, r=0, t=40, b=0),
**layout
)
return fig
def create_financial_chart(income_data: pd.DataFrame) -> go.Figure:
"""Create financial revenue and net income chart."""
fig = go.Figure()
fig.add_trace(go.Bar(
x=income_data['period_ending'],
y=income_data['total_revenue'],
name="Total Revenue",
marker=dict(color='#0066ff', opacity=0.9),
yaxis='y1'
))
fig.add_trace(go.Bar(
x=income_data['period_ending'],
y=income_data['net_income'],
name="Net Income",
marker=dict(color='#00d084', opacity=0.9),
yaxis='y1'
))
layout = get_dark_theme_layout()
fig.update_layout(
title="Revenue & Net Income (Annual)",
xaxis_title="Period",
yaxis_title="Amount ($)",
hovermode="x unified",
template="plotly_dark",
height=400,
barmode='group',
margin=dict(l=0, r=0, t=40, b=0),
**layout
)
return fig