Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BugFix] Enforce User Preference For Dark/Light Mode In Chart Output To API #6664

Merged
merged 2 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ def derivatives_futures_curve( # noqa: PLR0912

figure = OpenBBFigure().create_subplots(shared_xaxes=True)
figure.update_layout(ChartStyle().plotly_template.get("layout", {}))
text_color = "white" if ChartStyle().plt_style == "dark" else "black"

def create_fig(figure, df, dates, color_count):
"""Create a scatter for each date in the data."""
Expand Down Expand Up @@ -184,7 +185,12 @@ def create_fig(figure, df, dates, color_count):
# Update the layout of the figure.
figure.update_layout(
title=dict(text=title, x=0.5, font=dict(size=20)),
plot_bgcolor="rgba(255,255,255,0)",
plot_bgcolor=(
"rgba(0,0,0,0)" if text_color == "white" else "rgba(255,255,255,0)"
),
paper_bgcolor=(
"rgba(0,0,0,0)" if text_color == "white" else "rgba(255,255,255,0)"
),
xaxis=dict(
title="",
ticklen=0,
Expand All @@ -207,7 +213,9 @@ def create_fig(figure, df, dates, color_count):
x=0,
xref="paper",
font=dict(size=12),
bgcolor="rgba(0,0,0,0)",
bgcolor=(
"rgba(0,0,0,0)" if text_color == "white" else "rgba(255,255,255,0)"
),
),
margin=dict(
b=10,
Expand Down
18 changes: 14 additions & 4 deletions openbb_platform/extensions/economy/openbb_economy/economy_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def economy_fred_series(
from openbb_charting.charts.helpers import (
z_score_standardization,
)
from openbb_charting.core.chart_style import ChartStyle
from openbb_charting.core.openbb_figure import OpenBBFigure
from openbb_charting.styles.colors import LARGE_CYCLER
from openbb_core.app.utils import basemodel_to_df
Expand Down Expand Up @@ -177,7 +178,8 @@ def economy_fred_series(
fig = OpenBBFigure().create_subplots(
rows=1, cols=1, shared_xaxes=True, shared_yaxes=False
)

fig.update_layout(ChartStyle().plotly_template.get("layout", {}))
text_color = "white" if ChartStyle().plt_style == "dark" else "black"
# For each series in the DataFrame, add a scatter plot.
for i, col in enumerate(df_ta.columns):

Expand Down Expand Up @@ -227,15 +229,21 @@ def economy_fred_series(
# Now update the layout of the complete figure.
fig.update_layout(
title=dict(text=title, x=0.5, font=dict(size=16)),
paper_bgcolor="rgba(0,0,0,0)",
plot_bgcolor="rgba(0,0,0,0)",
paper_bgcolor=(
"rgba(0,0,0,0)" if text_color == "white" else "rgba(255,255,255,0)"
),
plot_bgcolor=(
"rgba(0,0,0,0)" if text_color == "white" else "rgba(255,255,255,0)"
),
legend=dict(
orientation="h",
yanchor="bottom",
xanchor="right",
y=1.02,
x=0.95,
bgcolor="rgba(0,0,0,0)",
bgcolor=(
"rgba(0,0,0,0)" if text_color == "white" else "rgba(255,255,255,0)"
),
font=dict(size=12),
),
yaxis=(
Expand Down Expand Up @@ -304,13 +312,15 @@ def economy_fred_series(
margin=(
dict(r=25, l=25, b=75 if xtitle else 30) if normalize is False else None
),
font=dict(color=text_color),
autosize=True,
dragmode="pan",
)
if kwargs.get("layout_kwargs"):
fig.update_layout(kwargs.get("layout_kwargs"))
if kwargs.get("title"):
fig.set_title(str(kwargs.get("title")))

content = fig.to_plotly_json()

return fig, content
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def fixedincome_government_yield_curve( # noqa: PLR0912

figure = OpenBBFigure().create_subplots(shared_xaxes=True)
figure.update_layout(ChartStyle().plotly_template.get("layout", {}))
text_color = "white" if ChartStyle().plt_style == "dark" else "black"

def create_fig(figure, df, dates, color_count, country: Optional[str] = None):
"""Create a scatter for each date in the data."""
Expand All @@ -90,7 +91,6 @@ def create_fig(figure, df, dates, color_count, country: Optional[str] = None):
figure.add_scatter(
x=plot_df["Maturity"],
y=plot_df["Yield"],
# fill=fill,
mode="lines+markers",
name=f"{country} - {date}" if country else date,
line=dict(width=3, color=color),
Expand Down Expand Up @@ -142,7 +142,12 @@ def create_fig(figure, df, dates, color_count, country: Optional[str] = None):
# Update the layout of the figure.
figure.update_layout(
title=dict(text=title, x=0.5, font=dict(size=20)),
plot_bgcolor="rgba(255,255,255,0)",
plot_bgcolor=(
"rgba(0,0,0,0)" if text_color == "white" else "rgba(255,255,255,0)"
),
paper_bgcolor=(
"rgba(0,0,0,0)" if text_color == "white" else "rgba(255,255,255,0)"
),
xaxis=dict(
title="Maturity",
ticklen=0,
Expand Down Expand Up @@ -174,7 +179,9 @@ def create_fig(figure, df, dates, color_count, country: Optional[str] = None):
x=0,
xref="paper",
font=dict(size=12),
bgcolor="rgba(0,0,0,0)",
bgcolor=(
"rgba(0,0,0,0)" if text_color == "white" else "rgba(255,255,255,0)"
),
),
margin=dict(
b=25,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,16 +289,22 @@ def technical_cones(**kwargs) -> Tuple["OpenBBFigure", Dict[str, Any]]:
fig.set_title(title)

fig.update_layout(
paper_bgcolor="rgba(0,0,0,0)",
plot_bgcolor="rgba(0,0,0,0)",
paper_bgcolor=(
"rgba(0,0,0,0)" if text_color == "white" else "rgba(255,255,255,0)"
),
plot_bgcolor=(
"rgba(0,0,0,0)" if text_color == "white" else "rgba(255,255,255,0)"
),
font=dict(color=text_color),
legend=dict(
orientation="h",
yanchor="bottom",
xanchor="right",
y=1.02,
x=1,
bgcolor="rgba(0,0,0,0)",
bgcolor=(
"rgba(0,0,0,0)" if text_color == "white" else "rgba(255,255,255,0)"
),
),
yaxis=dict(
ticklen=0,
Expand Down Expand Up @@ -366,7 +372,9 @@ def technical_relative_rotation(
figure = OpenBBFigure(fig) # pylint: disable=E0606
font_color = "black" if ChartStyle().plt_style == "light" else "white"
figure.update_layout(
paper_bgcolor="rgba(0,0,0,0)",
paper_bgcolor=(
"rgba(0,0,0,0)" if font_color == "white" else "rgba(255,255,255,255)"
),
plot_bgcolor="rgba(255,255,255,1)",
font=dict(color=font_color),
yaxis=dict(
Expand Down Expand Up @@ -473,7 +481,7 @@ def _ta_ma(**kwargs):
specs=[[{"secondary_y": True}]],
)
fig.update_layout(ChartStyle().plotly_template.get("layout", {}))

font_color = "black" if ChartStyle().plt_style == "light" else "white"
ma_df = DataFrame()
window = [window] if isinstance(window, int) else window
for w in window:
Expand Down Expand Up @@ -511,16 +519,20 @@ def _ta_ma(**kwargs):

fig.update_layout(
title=dict(text=title, x=0.5, font=dict(size=16)),
paper_bgcolor="rgba(0,0,0,0)",
plot_bgcolor="rgba(0,0,0,0)",
paper_bgcolor=(
"rgba(0,0,0,0)" if font_color == "white" else "rgba(255,255,255,255)"
),
plot_bgcolor=(
"rgba(0,0,0,0)" if font_color == "white" else "rgba(255,255,255,0)"
),
showlegend=True,
legend=dict(
orientation="h",
yanchor="bottom",
xanchor="right",
y=1.02,
x=0.95,
bgcolor="rgba(0,0,0,0)",
bgcolor="rgba(0,0,0,0)" if font_color == "white" else "rgba(255,255,255,0)",
),
xaxis=dict(
ticklen=0,
Expand All @@ -537,6 +549,7 @@ def _ta_ma(**kwargs):
mirror=True,
autorange=True,
),
font=dict(color=font_color),
)

content = fig.show(external=True).to_plotly_json()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def line_chart( # noqa: PLR0912
"""Create a line chart."""
# pylint: disable=import-outside-toplevel
from pandas import DataFrame, Series, to_datetime # noqa
from openbb_charting.core.openbb_figure import OpenBBFigure # noqa
from openbb_charting.core.openbb_figure import OpenBBFigure

if data is None:
raise ValueError("Error: Data is a required field.")
Expand Down Expand Up @@ -122,6 +122,8 @@ def line_chart( # noqa: PLR0912
except Exception as _:
fig = OpenBBFigure(create_backend=True)

fig.update_layout(ChartStyle().plotly_template.get("layout", {}))
text_color = "white" if ChartStyle().plt_style == "dark" else "black"
title = f"{title}" if title else ""
xtitle = xtitle if xtitle else ""
y1title = ytitle if ytitle else ""
Expand Down Expand Up @@ -242,15 +244,24 @@ def line_chart( # noqa: PLR0912

fig.update_layout(
title=dict(text=title if title else None, x=0.5, font=dict(size=16)),
paper_bgcolor="rgba(0,0,0,0)",
plot_bgcolor="rgba(0,0,0,0)",
font=dict(color=text_color),
paper_bgcolor=(
"rgba(0,0,0,0)" if text_color == "white" else "rgba(255,255,255,0)"
),
plot_bgcolor=(
"rgba(0,0,0,0)" if text_color == "white" else "rgba(255,255,255,0)"
),
legend=dict(
orientation="h",
yanchor="bottom",
orientation="v",
yanchor="top",
xanchor="right",
y=1.02,
x=0.95,
bgcolor="rgba(0,0,0,0)",
y=0.95,
x=-0.01,
xref="paper",
font=dict(size=12),
bgcolor=(
"rgba(0,0,0,0)" if text_color == "white" else "rgba(255,255,255,0)"
),
),
yaxis=(
dict(
Expand Down Expand Up @@ -405,6 +416,7 @@ def bar_chart( # noqa: PLR0912
)

figure.update_layout(ChartStyle().plotly_template.get("layout", {}))
text_color = "white" if ChartStyle().plt_style == "dark" else "black"
if colors is not None:
figure.update_layout(colorway=colors)
if bar_kwargs is None:
Expand Down Expand Up @@ -434,15 +446,23 @@ def bar_chart( # noqa: PLR0912

figure.update_layout(
title=dict(text=title if title else None, x=0.5, font=dict(size=16)),
paper_bgcolor="rgba(0,0,0,0)",
plot_bgcolor="rgba(0,0,0,0)",
paper_bgcolor=(
"rgba(0,0,0,0)" if text_color == "white" else "rgba(255,255,255,0)"
),
plot_bgcolor=(
"rgba(0,0,0,0)" if text_color == "white" else "rbga(255,255,255,0)"
),
legend=dict(
orientation="h",
yanchor="bottom",
orientation="v",
yanchor="top",
xanchor="right",
y=1.02,
x=0.98,
bgcolor="rgba(0,0,0,0)",
y=0.95,
x=-0.01 if orientation == "v" else 1.01,
xref="paper",
font=dict(size=12),
bgcolor=(
"rgba(0,0,0,0)" if text_color == "white" else "rgba(255,255,255,0)"
),
),
xaxis=dict(
type=xtype,
Expand All @@ -469,6 +489,7 @@ def bar_chart( # noqa: PLR0912
),
margin=dict(pad=5),
barmode=barmode,
font=dict(color=text_color),
)
if orientation == "h":
figure.update_layout(
Expand All @@ -483,6 +504,7 @@ def bar_chart( # noqa: PLR0912
hoverlabel=dict(
font=dict(size=12),
),
hovermode="y unified",
)
if layout_kwargs:
figure.update_layout(
Expand Down Expand Up @@ -532,7 +554,7 @@ def bar_increasing_decreasing( # pylint: disable=W0102
"""
# pylint: disable=import-outside-toplevel
from openbb_charting.core.openbb_figure import OpenBBFigure # noqa
from pandas import Series # noqa
from pandas import Series

try:
figure = OpenBBFigure()
Expand All @@ -548,7 +570,8 @@ def bar_increasing_decreasing( # pylint: disable=W0102
row_width=[1],
specs=[[{"secondary_y": True}]],
)
# figure.update_layout(ChartStyle().plotly_template.get("layout", {}))
figure.update_layout(ChartStyle().plotly_template.get("layout", {}))
text_color = "white" if ChartStyle().plt_style == "dark" else "black"

try:
data = Series(data=values, index=keys)
Expand Down Expand Up @@ -605,6 +628,13 @@ def bar_increasing_decreasing( # pylint: disable=W0102
categoryorder="array" if orientation == "v" else None,
categoryarray=keys if orientation == "v" else None,
),
paper_bgcolor=(
"rgba(0,0,0,0)" if text_color == "white" else "rgba(255,255,255,0)"
),
plot_bgcolor=(
"rgba(0,0,0,0)" if text_color == "white" else "rgba(255,255,255,0)"
),
font=dict(color="white" if text_color == "white" else "black"),
margin=dict(pad=5),
)

Expand Down
Loading
Loading