From 023531fac7fd71623e4154d8d5c83f8a908f696d Mon Sep 17 00:00:00 2001 From: Danglewood <85772166+deeleeramone@users.noreply.github.com> Date: Sun, 19 Oct 2025 08:43:09 -0700 Subject: [PATCH] [BugFix] `openbb-charting`: Get `ChartStyle.plt_style` Default Value From UserSettings (#7243) * set ChartStyle.plt_style from UserSettings * fix discrepencies between raw figure and pywry for light/dark --- .../plotly/src/components/Chart.tsx | 4 + .../openbb_derivatives/derivatives_views.py | 6 -- .../openbb_econometrics/econometrics_views.py | 2 +- .../economy/openbb_economy/economy_views.py | 8 +- .../equity/openbb_equity/equity_views.py | 6 +- .../extensions/etf/openbb_etf/etf_views.py | 2 +- .../openbb_fixedincome/fixedincome_views.py | 6 -- .../openbb_technical/technical_views.py | 15 ---- .../charting/openbb_charting/charting.py | 8 +- .../charts/correlation_matrix.py | 9 --- .../openbb_charting/charts/generic_charts.py | 25 ------ .../charts/price_historical.py | 12 --- .../openbb_charting/core/chart_style.py | 7 ++ .../charting/openbb_charting/core/plotly.html | 78 +++++++++---------- 14 files changed, 63 insertions(+), 125 deletions(-) diff --git a/frontend-components/plotly/src/components/Chart.tsx b/frontend-components/plotly/src/components/Chart.tsx index 0b1e2473fa7..6442055e2f5 100644 --- a/frontend-components/plotly/src/components/Chart.tsx +++ b/frontend-components/plotly/src/components/Chart.tsx @@ -530,6 +530,10 @@ function Chart({ ...(originalData.layout.font || {}), color: darkmode ? "#fff" : "#000", }; + originalData.layout.plot_bgcolor = { + ...(originalData.layout.plot_bgcolor || {}), + color: darkmode ? "#000" : "#fff", + }; const changeIcon = darkmode ? ICONS.sunIcon : ICONS.moonIcon; diff --git a/openbb_platform/extensions/derivatives/openbb_derivatives/derivatives_views.py b/openbb_platform/extensions/derivatives/openbb_derivatives/derivatives_views.py index 0ef903f42a3..b50837fbedc 100644 --- a/openbb_platform/extensions/derivatives/openbb_derivatives/derivatives_views.py +++ b/openbb_platform/extensions/derivatives/openbb_derivatives/derivatives_views.py @@ -194,12 +194,6 @@ class DerivativesViews: # Update the layout of the figure. figure.update_layout( title=dict(text=title, x=0.5, font=dict(size=20)), - 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, diff --git a/openbb_platform/extensions/econometrics/openbb_econometrics/econometrics_views.py b/openbb_platform/extensions/econometrics/openbb_econometrics/econometrics_views.py index 61c935a56b8..3406b88885e 100644 --- a/openbb_platform/extensions/econometrics/openbb_econometrics/econometrics_views.py +++ b/openbb_platform/extensions/econometrics/openbb_econometrics/econometrics_views.py @@ -36,4 +36,4 @@ class EconometricsViews: # pylint: disable=import-outside-toplevel from openbb_charting.charts.correlation_matrix import correlation_matrix - return correlation_matrix(**kwargs) + return correlation_matrix(**kwargs) # type: ignore diff --git a/openbb_platform/extensions/economy/openbb_economy/economy_views.py b/openbb_platform/extensions/economy/openbb_economy/economy_views.py index c08b2e00c02..dba17319e34 100644 --- a/openbb_platform/extensions/economy/openbb_economy/economy_views.py +++ b/openbb_platform/extensions/economy/openbb_economy/economy_views.py @@ -224,12 +224,6 @@ class EconomyViews: # 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)" 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", @@ -519,7 +513,7 @@ class EconomyViews: ) content = fig.to_plotly_json() - return fig, content + return fig, content # type: ignore @staticmethod def economy_shipping_chokepoint_info( diff --git a/openbb_platform/extensions/equity/openbb_equity/equity_views.py b/openbb_platform/extensions/equity/openbb_equity/equity_views.py index 4a16071bdde..4cab4705576 100644 --- a/openbb_platform/extensions/equity/openbb_equity/equity_views.py +++ b/openbb_platform/extensions/equity/openbb_equity/equity_views.py @@ -29,7 +29,7 @@ class EquityViews: # pylint: disable=import-outside-toplevel from openbb_charting.charts.price_performance import price_performance - return price_performance(**kwargs) + return price_performance(**kwargs) # type: ignore @staticmethod def equity_historical_market_cap( # noqa: PLR0912 @@ -80,6 +80,6 @@ class EquityViews: scatter_kwargs=scatter_kwargs, **kwargs, ) - content = fig.show(external=True).to_plotly_json() + content = fig.show(external=True).to_plotly_json() # type: ignore - return fig, content + return fig, content # type: ignore diff --git a/openbb_platform/extensions/etf/openbb_etf/etf_views.py b/openbb_platform/extensions/etf/openbb_etf/etf_views.py index c5aa066a157..f0b2ff06940 100644 --- a/openbb_platform/extensions/etf/openbb_etf/etf_views.py +++ b/openbb_platform/extensions/etf/openbb_etf/etf_views.py @@ -33,7 +33,7 @@ class EtfViews: # pylint: disable=import-outside-toplevel from openbb_charting.charts.price_performance import price_performance - return price_performance(**kwargs) + return price_performance(**kwargs) # type: ignore @staticmethod def etf_holdings( diff --git a/openbb_platform/extensions/fixedincome/openbb_fixedincome/fixedincome_views.py b/openbb_platform/extensions/fixedincome/openbb_fixedincome/fixedincome_views.py index 50c1718bc8d..833333f439a 100644 --- a/openbb_platform/extensions/fixedincome/openbb_fixedincome/fixedincome_views.py +++ b/openbb_platform/extensions/fixedincome/openbb_fixedincome/fixedincome_views.py @@ -170,12 +170,6 @@ class FixedIncomeViews: # Update the layout of the figure. figure.update_layout( title=dict(text=title, x=0.5, font=dict(size=20)), - plot_bgcolor=( - "rgba(21,21,21,0)" if text_color == "white" else "rgba(230,230,230,0)" - ), - paper_bgcolor=( - "rgba(21,21,21,1)" if text_color == "white" else "rgba(230,230,230,1)" - ), xaxis=dict( title="Maturity", ticklen=10, diff --git a/openbb_platform/extensions/technical/openbb_technical/technical_views.py b/openbb_platform/extensions/technical/openbb_technical/technical_views.py index 90eebcfc5da..cc610017d35 100644 --- a/openbb_platform/extensions/technical/openbb_technical/technical_views.py +++ b/openbb_platform/extensions/technical/openbb_technical/technical_views.py @@ -289,12 +289,6 @@ class TechnicalViews: fig.set_title(title) fig.update_layout( - 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", @@ -381,9 +375,6 @@ class TechnicalViews: 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)" if font_color == "white" else "rgba(255,255,255,255)" - ), plot_bgcolor="rgba(255,255,255,1)", font=dict(color=font_color), yaxis=dict( @@ -527,12 +518,6 @@ 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)" 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", diff --git a/openbb_platform/obbject_extensions/charting/openbb_charting/charting.py b/openbb_platform/obbject_extensions/charting/openbb_charting/charting.py index 6163bde2ad6..6ee3ed5bd95 100644 --- a/openbb_platform/obbject_extensions/charting/openbb_charting/charting.py +++ b/openbb_platform/obbject_extensions/charting/openbb_charting/charting.py @@ -487,6 +487,7 @@ class Charting: kwargs["provider"] = self._obbject.provider kwargs["extra"] = self._obbject.extra fig, content = charting_function(**kwargs) + content = fig.show(external=True, **kwargs).to_plotly_json() # type: ignore self._obbject.chart = Chart(fig=fig, content=content, format=self._format) if render: fig.show(**kwargs) @@ -629,8 +630,13 @@ class Charting: style = self._charting_settings.chart_style font_color = "black" if style == "light" else "white" paper_bgcolor = "white" if style == "light" else "black" + plot_bgcolor = "white" if style == "light" else "black" figure = figure.update_layout( - dict(font_color=font_color, paper_bgcolor=paper_bgcolor) + dict( + font_color=font_color, + paper_bgcolor=paper_bgcolor, + plot_bgcolor=plot_bgcolor, + ) ) # pylint: disable=R1735 return figure diff --git a/openbb_platform/obbject_extensions/charting/openbb_charting/charts/correlation_matrix.py b/openbb_platform/obbject_extensions/charting/openbb_charting/charts/correlation_matrix.py index b1531958eb9..f35df015940 100644 --- a/openbb_platform/obbject_extensions/charting/openbb_charting/charts/correlation_matrix.py +++ b/openbb_platform/obbject_extensions/charting/openbb_charting/charts/correlation_matrix.py @@ -101,15 +101,6 @@ def correlation_matrix( # noqa: PLR0912 ) fig = Figure(data=[heatmap], layout=layout) figure = OpenBBFigure(fig=fig) - figure.update_layout( - 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)" - ), - ) layout_kwargs = kwargs.get("layout_kwargs", {}) if layout_kwargs: diff --git a/openbb_platform/obbject_extensions/charting/openbb_charting/charts/generic_charts.py b/openbb_platform/obbject_extensions/charting/openbb_charting/charts/generic_charts.py index e1bd2967d32..93f26d052e1 100644 --- a/openbb_platform/obbject_extensions/charting/openbb_charting/charts/generic_charts.py +++ b/openbb_platform/obbject_extensions/charting/openbb_charting/charts/generic_charts.py @@ -246,12 +246,6 @@ def line_chart( # noqa: PLR0912 fig.update_layout( title=dict(text=title if title else None, x=0.5, font=dict(size=16)), 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="v", yanchor="top", @@ -456,12 +450,6 @@ 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)" 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="v", yanchor="top", @@ -637,12 +625,6 @@ 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), ) @@ -802,14 +784,7 @@ def surface3d( center=dict(x=-0.01, y=0, z=-0.3), eye=dict(x=1.75, y=1.75, z=0.69), ), - paper_bgcolor=( - "rgba(21,21,21,1)" if text_color == "white" else "rgba(255,255,255,1)" - ), - plot_bgcolor=( - "rgba(21,21,21,1)" if text_color == "white" else "rgba(255,255,255,1)" - ), font=dict(color=text_color), - # margin=dict(t=100, b=10, r=10, l=10), ) fig.update_scenes( diff --git a/openbb_platform/obbject_extensions/charting/openbb_charting/charts/price_historical.py b/openbb_platform/obbject_extensions/charting/openbb_charting/charts/price_historical.py index 0160e64c30b..a7f81ee7852 100644 --- a/openbb_platform/obbject_extensions/charting/openbb_charting/charts/price_historical.py +++ b/openbb_platform/obbject_extensions/charting/openbb_charting/charts/price_historical.py @@ -120,12 +120,6 @@ def price_historical( # noqa: PLR0912 if _volume is True and "atr" in indicators: # type: ignore fig.add_inchart_volume(data) fig.update_layout( - 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), showlegend=True, legend=dict( @@ -254,12 +248,6 @@ def price_historical( # noqa: PLR0912 y2title = None # type: ignore fig.update_layout( - 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="v", diff --git a/openbb_platform/obbject_extensions/charting/openbb_charting/core/chart_style.py b/openbb_platform/obbject_extensions/charting/openbb_charting/core/chart_style.py index 9b23615c05c..99402cd5e33 100644 --- a/openbb_platform/obbject_extensions/charting/openbb_charting/core/chart_style.py +++ b/openbb_platform/obbject_extensions/charting/openbb_charting/core/chart_style.py @@ -67,9 +67,16 @@ class ChartStyle: console_style : `str`, optional The name of the Rich style to use, by default "" """ + # pylint: disable=import-outside-toplevel + from openbb_core.app.service.user_service import UserService + if self.initialized: return + user_settings = UserService().read_from_file() + pref_style = getattr(user_settings.preferences, "chart_style", None) + plt_style = plt_style or pref_style + self.initialized = True self.user_styles_directory = user_styles_directory or self.user_styles_directory self.plt_style = plt_style or self.plt_style diff --git a/openbb_platform/obbject_extensions/charting/openbb_charting/core/plotly.html b/openbb_platform/obbject_extensions/charting/openbb_charting/core/plotly.html index 27167c2d256..ecb094ea0de 100644 --- a/openbb_platform/obbject_extensions/charting/openbb_charting/core/plotly.html +++ b/openbb_platform/obbject_extensions/charting/openbb_charting/core/plotly.html @@ -33,7 +33,7 @@ }