Skip to content

Commit 0be74fd

Browse files
committed
Changed line to bar charts
1 parent 79dfab1 commit 0be74fd

1 file changed

Lines changed: 144 additions & 57 deletions

File tree

src/app.py

Lines changed: 144 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
from typing import Any
2+
3+
import plotly.graph_objects as go
24
import polars as pl
35
import streamlit as st
46
import streamlit.components.v1 as components
5-
import plotly.graph_objects as go
7+
68
from data_validation import get_validated_stock_data
79

810

@@ -176,7 +178,6 @@ def display_table(data: dict[str, Any], header_name: str):
176178
}
177179

178180
rating_data = {
179-
# "Symbol": f"{latest_ratings_data['symbol']}",
180181
"Date": f"{latest_ratings_data['date']}",
181182
"Rating": f"{latest_ratings_data['rating']}",
182183
"Score": f"{latest_ratings_data['score']}",
@@ -218,78 +219,130 @@ def display_table(data: dict[str, Any], header_name: str):
218219
def display_metrics_charts(
219220
metrics_data: list[dict[str, Any]], key_metrics_ttm_data: dict[str, Any]
220221
):
221-
def create_compact_line_chart(data: pl.DataFrame, x_col, y_col, title, ttm_value):
222+
def create_compact_bar_chart(data: pl.DataFrame, x_col, y_col, title, ttm_value):
222223
fig = go.Figure()
223224

224-
# Historical data line
225+
# Historical data bars
225226
fig.add_trace(
226-
go.Scatter(
227+
go.Bar(
227228
x=data[x_col],
228229
y=data[y_col],
229-
mode="lines+markers",
230-
line=dict(width=2, color="royalblue"), # Professional blue color
230+
name="Historical",
231231
marker=dict(
232-
size=6, color="royalblue", line=dict(width=1, color="darkblue")
232+
color="#4C78A8", # Modern blue
233+
line=dict(width=1.5, color="#2E2E2E"), # Dark outline
233234
),
234-
name="Historical",
235+
width=0.75,
236+
opacity=0.9,
237+
hovertemplate="%{x}: %{y:.2f}",
235238
)
236239
)
237240

238-
# TTM value point
241+
# TTM value with high-contrast marker
239242
latest_date = data[x_col].dt.max()
240243
fig.add_trace(
241244
go.Scatter(
242245
x=[latest_date],
243246
y=[ttm_value],
244247
mode="markers+text",
245248
marker=dict(
246-
size=6,
247-
color="firebrick",
249+
size=14,
250+
color="#F28C38", # Vibrant orange
248251
symbol="diamond",
249-
line=dict(width=2, color="darkred"),
252+
line=dict(width=2, color="#D76F1E"), # Darker orange outline
250253
),
251254
name="TTM",
252-
text=[f"{ttm_value:.2f}"], # Display TTM value as text
255+
text=[f"{ttm_value:.2f}"],
253256
textposition="top center",
257+
textfont=dict(
258+
size=13, color="#2E2E2E", weight="bold"
259+
), # Dark gray for visibility
260+
hovertemplate="TTM: %{y:.2f}",
254261
)
255262
)
256263

257-
# Horizontal line for TTM value
264+
# TTM reference line
258265
fig.add_shape(
259266
type="line",
260267
x0=data[x_col].dt.min(),
261268
y0=ttm_value,
262269
x1=latest_date,
263270
y1=ttm_value,
264-
line=dict(color="firebrick", width=2, dash="dash"),
271+
line=dict(
272+
color="#F28C38",
273+
width=2,
274+
dash="dash",
275+
),
265276
)
266277

267-
# Update layout for a more professional appearance
278+
# Adaptive layout
268279
fig.update_layout(
269-
title="",
270-
title_font=dict(size=16, family="Arial", color="black"),
280+
title=dict(
281+
text=title,
282+
font=dict(size=16, color="#2E2E2E"), # Darker gray for contrast
283+
x=0.5,
284+
xanchor="center",
285+
y=0.95,
286+
yanchor="top",
287+
),
271288
xaxis_title="Year",
272289
yaxis_title=title,
273-
plot_bgcolor="white", # Clean background
274-
height=400,
275-
margin=dict(l=40, r=40, t=40, b=40),
276-
font=dict(family="Arial", size=12),
290+
plot_bgcolor="rgba(0,0,0,0)", # Transparent plot
291+
paper_bgcolor="rgba(0,0,0,0)", # Transparent paper
292+
height=450,
293+
margin=dict(l=60, r=40, t=80, b=60),
294+
font=dict(
295+
family="Inter, Arial, sans-serif",
296+
size=13,
297+
color="#2E2E2E", # Dark gray text
298+
),
277299
showlegend=True,
278300
legend=dict(
279-
orientation="h", yanchor="bottom", y=1.02, xanchor="right", x=1
301+
orientation="h",
302+
yanchor="bottom",
303+
y=1.02,
304+
xanchor="right",
305+
x=1,
306+
font=dict(size=12),
307+
bgcolor="rgba(255,255,255,0.8)", # Light background for legend on white
280308
),
281-
hovermode="x unified", # Unified hover for better readability
309+
hovermode="x unified",
310+
transition_duration=500,
282311
)
283312

284-
# Customize axes
313+
# Enhanced axes
285314
fig.update_xaxes(
286315
tickmode="array",
287316
tickvals=data[x_col],
288-
ticktext=data[x_col],
289-
gridcolor="lightgrey",
317+
ticktext=data[x_col].dt.strftime("%Y"),
318+
gridcolor="rgba(0,0,0,0.2)", # Darker gridlines for white background
319+
linecolor="#666666",
320+
linewidth=1.5,
321+
ticks="outside",
322+
tickfont=dict(size=12),
323+
title_font=dict(size=14),
324+
zeroline=False,
290325
)
291-
fig.update_yaxes(gridcolor="lightgrey")
292326

327+
fig.update_yaxes(
328+
gridcolor="rgba(0,0,0,0.2)", # Darker gridlines
329+
linecolor="#666666",
330+
linewidth=1.5,
331+
tickfont=dict(size=12),
332+
title_font=dict(size=14),
333+
zeroline=False,
334+
showline=True,
335+
)
336+
337+
# Enhanced hover
338+
fig.update_traces(
339+
hoverlabel=dict(
340+
bgcolor="#FFFFFF", # White hover background
341+
font_size=12,
342+
font_color="#2E2E2E", # Dark gray text
343+
bordercolor="#666666",
344+
),
345+
)
293346
return fig
294347

295348
# Create charts
@@ -304,7 +357,7 @@ def create_compact_line_chart(data: pl.DataFrame, x_col, y_col, title, ttm_value
304357
.drop("date")
305358
)
306359

307-
if df is None:
360+
if df is None or df.is_empty():
308361
print("Dataframe is empty")
309362
return None
310363

@@ -314,7 +367,7 @@ def plot_chart(metrics: list[tuple[str, str]]):
314367
dfx = df.select("FYDateEnding", f"{metric}")
315368

316369
st.plotly_chart(
317-
create_compact_line_chart(
370+
create_compact_bar_chart(
318371
dfx,
319372
x_col="FYDateEnding",
320373
y_col=f"{metric}",
@@ -361,48 +414,82 @@ def plot_chart(metrics: list[tuple[str, str]]):
361414

362415

363416
def display_growth_charts(growth_data: list[dict[str, Any]]):
364-
def create_compact_line_chart(data: pl.DataFrame, x_col, y_col, title):
417+
def create_compact_bar_chart(data: pl.DataFrame, x_col, y_col, title):
365418
fig = go.Figure()
366419

367-
# Historical data line
368420
fig.add_trace(
369-
go.Scatter(
421+
go.Bar(
370422
x=data[x_col],
371423
y=data[y_col],
372-
mode="lines+markers",
373-
line=dict(width=2, color="royalblue"), # Professional blue color
424+
name="",
374425
marker=dict(
375-
size=6, color="royalblue", line=dict(width=1, color="darkblue")
426+
color="#54A24B", # Modern green
427+
line=dict(width=1.5, color="#2E2E2E"), # Dark outline
376428
),
377-
name="", # f"Historical {title}",
429+
width=0.7,
430+
hovertemplate="%{x}: %{y:.2f}%",
378431
)
379432
)
380433

381-
# Update layout for a more professional appearance
382434
fig.update_layout(
383-
title="",
384-
title_font=dict(size=16, family="Arial", color="black"),
435+
title=dict(
436+
text=title,
437+
font=dict(size=18, color="#2E2E2E"), # Darker gray
438+
x=0.5,
439+
xanchor="center",
440+
y=0.95,
441+
yanchor="top",
442+
),
385443
xaxis_title="Year",
386-
yaxis_title=title,
387-
plot_bgcolor="white", # Clean background
388-
height=400,
389-
margin=dict(l=40, r=40, t=40, b=40),
390-
font=dict(family="Arial", size=12),
391-
showlegend=True,
392-
legend=dict(
393-
orientation="h", yanchor="bottom", y=1.02, xanchor="right", x=1
444+
yaxis_title="Growth Rate (%)",
445+
plot_bgcolor="rgba(0,0,0,0)",
446+
paper_bgcolor="rgba(0,0,0,0)",
447+
height=450,
448+
margin=dict(l=50, r=50, t=80, b=60),
449+
font=dict(
450+
family="Inter, Arial, sans-serif",
451+
size=13,
452+
color="#2E2E2E", # Dark gray text
394453
),
395-
hovermode="x unified", # Unified hover for better readability
454+
showlegend=False,
455+
hovermode="x unified",
456+
barmode="group",
457+
transition_duration=400,
396458
)
397459

398-
# Customize axes
399460
fig.update_xaxes(
400461
tickmode="array",
401462
tickvals=data[x_col],
402-
ticktext=data[x_col],
403-
gridcolor="lightgrey",
463+
ticktext=data[x_col].dt.strftime("%Y"),
464+
gridcolor="rgba(0,0,0,0.2)", # Darker gridlines
465+
linecolor="#666666",
466+
linewidth=1,
467+
ticks="outside",
468+
tickfont=dict(size=12),
469+
title_font=dict(size=14),
470+
zeroline=False,
471+
)
472+
473+
fig.update_yaxes(
474+
gridcolor="rgba(0,0,0,0.2)",
475+
linecolor="#666666",
476+
linewidth=1,
477+
ticksuffix="%",
478+
tickfont=dict(size=12),
479+
title_font=dict(size=14),
480+
zeroline=False,
481+
showline=True,
482+
)
483+
484+
fig.update_traces(
485+
opacity=0.95,
486+
hoverlabel=dict(
487+
bgcolor="#FFFFFF",
488+
font_size=12,
489+
font_color="#2E2E2E",
490+
bordercolor="#666666",
491+
),
404492
)
405-
fig.update_yaxes(gridcolor="lightgrey")
406493

407494
return fig
408495

@@ -417,9 +504,9 @@ def create_compact_line_chart(data: pl.DataFrame, x_col, y_col, title):
417504
)
418505
.sort("date")
419506
.drop("date")
420-
) # pl.col("col_name").list.eval(pl.element().sqrt()).
507+
)
421508

422-
if df is None:
509+
if df is None or df.is_empty():
423510
print("Dataframe is empty")
424511
return None
425512

@@ -428,7 +515,7 @@ def plot_chart(metrics: list[tuple[str, str]]):
428515
dfx = df.select("Year", f"{metric}")
429516

430517
st.plotly_chart(
431-
create_compact_line_chart(
518+
create_compact_bar_chart(
432519
dfx,
433520
x_col="Year",
434521
y_col=f"{metric}",

0 commit comments

Comments
 (0)