Skip to content

Commit

Permalink
fix: autoscale graph axis for better view on pnl values (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaume-sig authored Dec 19, 2024
1 parent 73161bf commit 98f421a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
27 changes: 25 additions & 2 deletions portfolio_analytics/dashboard/app/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def create_pnl_figure(df_plot):
x=df["Date"],
y=[0] * len(df),
mode="lines",
line=dict(color="rgba(0,0,0,0)"),
line=dict(color="rgba(0,0,0,0.2)"),
showlegend=False,
hoverinfo="skip",
)
Expand All @@ -60,8 +60,31 @@ def create_pnl_figure(df_plot):
)
)

# Add y-axis scaling configuration
y_values = df["PnL"].values
y_min, y_max = min(y_values), max(y_values)
y_range_padding = (y_max - y_min) * 0.1 # 10% padding

# Set range based purely on data values plus padding
y_range_min = y_min - y_range_padding
y_range_max = y_max + y_range_padding

# Only include 0 if the data actually crosses it
if y_min <= 0 <= y_max:
y_range_min = min(y_range_min, 0)
y_range_max = max(y_range_max, 0)

fig.update_layout(
xaxis_title="", yaxis_title="", showlegend=False, hovermode="x unified"
xaxis_title="",
yaxis_title="",
showlegend=False,
hovermode="x unified",
yaxis=dict(
range=[y_range_min, y_range_max],
zeroline=True,
zerolinecolor="rgba(0,0,0,0.2)",
zerolinewidth=1,
),
)

return fig
Expand Down
4 changes: 3 additions & 1 deletion tests/test_dashboard/test_core/test_pnl.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ def test_daily_pnl_aggregation(self, monkeypatch, tmp_path, sample_df: pd.DataFr
assert isinstance(daily_pnl, pd.DataFrame)
assert len(daily_pnl) == len(sample_df["Date"].unique())

def test_calculation_error_handling(self, monkeypatch, tmp_path, sample_df: pd.DataFrame):
def test_calculation_error_handling(
self, monkeypatch, tmp_path, sample_df: pd.DataFrame
):
"""Tests error handling in daily PnL calculation."""
# Given
monkeypatch.setattr(
Expand Down

0 comments on commit 98f421a

Please sign in to comment.