summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDanglewood <85772166+deeleeramone@users.noreply.github.com>2024-05-07 05:04:46 -0700
committerGitHub <noreply@github.com>2024-05-07 12:04:46 +0000
commitd3b9ce6d37ddcc4a5867d71ccfce15622c2d5615 (patch)
tree806234cc317650cc42fce626acf2f6e63be1a057
parent63723f7b2cb01e91a6dace483ac8071772944110 (diff)
[BugFix] Set Chart Style Before Output (#6367)
* set chart style before output * docstring * black --------- Co-authored-by: Igor Radovanovic <74266147+IgorWounds@users.noreply.github.com>
-rw-r--r--openbb_platform/obbject_extensions/charting/openbb_charting/__init__.py47
1 files changed, 40 insertions, 7 deletions
diff --git a/openbb_platform/obbject_extensions/charting/openbb_charting/__init__.py b/openbb_platform/obbject_extensions/charting/openbb_charting/__init__.py
index c79dc270784..bc1f07409bb 100644
--- a/openbb_platform/obbject_extensions/charting/openbb_charting/__init__.py
+++ b/openbb_platform/obbject_extensions/charting/openbb_charting/__init__.py
@@ -61,6 +61,8 @@ class Charting:
Create a line chart from external data.
create_bar_chart
Create a bar chart, on a single x-axis with one or more values for the y-axis, from external data.
+ toggle_chart_style
+ Toggle the chart style, of an existing chart, between light and dark mode.
"""
def __init__(self, obbject):
@@ -234,6 +236,7 @@ class Charting:
same_axis=same_axis,
**kwargs,
)
+ fig = self._set_chart_style(fig)
if render:
return fig.show(**kwargs)
@@ -298,7 +301,6 @@ class Charting:
OpenBBFigure
The OpenBBFigure object.
"""
-
fig = bar_chart(
data=data,
x=x,
@@ -313,7 +315,7 @@ class Charting:
bar_kwargs=bar_kwargs,
layout_kwargs=layout_kwargs,
)
-
+ fig = self._set_chart_style(fig)
if render:
return fig.show(**kwargs)
@@ -322,7 +324,6 @@ class Charting:
# pylint: disable=inconsistent-return-statements
def show(self, render: bool = True, **kwargs):
"""Display chart and save it to the OBBject."""
-
try:
charting_function = self._get_chart_function(
self._obbject._route # pylint: disable=protected-access
@@ -345,14 +346,17 @@ class Charting:
_kwargs = kwargs.pop("kwargs")
kwargs.update(_kwargs.get("chart_params", {}))
fig, content = charting_function(**kwargs)
+ fig = self._set_chart_style(fig)
+ content = fig.show(external=True, **kwargs).to_plotly_json()
self._obbject.chart = Chart(
fig=fig, content=content, format=charting_router.CHART_FORMAT
)
if render:
fig.show(**kwargs)
- except Exception:
+ except Exception: # pylint: disable=W0718
try:
fig = self.create_line_chart(data=self._obbject.results, render=False, **kwargs) # type: ignore
+ fig = self._set_chart_style(fig)
content = fig.show(external=True, **kwargs).to_plotly_json() # type: ignore
self._obbject.chart = Chart(
fig=fig, content=content, format=charting_router.CHART_FORMAT
@@ -467,20 +471,49 @@ class Charting:
self.show(data=data_as_df, render=render, **kwargs)
else:
self.show(**kwargs, render=render)
- except Exception:
+ except Exception: # pylint: disable=W0718
try:
fig = self.create_line_chart(data=data_as_df, render=False, **kwargs)
+ fig = self._set_chart_style(fig)
content = fig.show(external=True, **kwargs).to_plotly_json() # type: ignore
self._obbject.chart = Chart(
fig=fig, content=content, format=charting_router.CHART_FORMAT
)
if render:
return fig.show(**kwargs) # type: ignore
- except Exception as e:
+ except Exception as e: # pylint: disable=W0718
raise RuntimeError(
"Failed to automatically create a generic chart with the data provided."
) from e
+ def _set_chart_style(self, figure: Figure):
+ """Set the user preference for light or dark mode."""
+ style = self._charting_settings.chart_style # pylint: disable=protected-access
+ font_color = "black" if style == "light" else "white"
+ paper_bgcolor = "white" if style == "light" else "black"
+ figure = figure.update_layout(
+ dict( # pylint: disable=R1735
+ font_color=font_color, paper_bgcolor=paper_bgcolor
+ )
+ )
+ return figure
+
+ def toggle_chart_style(self): # pylint: disable=protected-access
+ """Toggle the chart style between light and dark mode."""
+ if not hasattr(self._obbject.chart, "fig"):
+ raise ValueError(
+ "Error: No chart has been created. Please create a chart first."
+ )
+ current = self._charting_settings.chart_style
+ new = "light" if current == "dark" else "dark"
+ self._charting_settings.chart_style = new
+ figure = self._obbject.chart.fig
+ updated_figure = self._set_chart_style(figure)
+ self._obbject.chart.fig = updated_figure
+ self._obbject.chart.content = updated_figure.show(
+ external=True
+ ).to_plotly_json()
+
def table(
self,
data: Optional[Union[pd.DataFrame, pd.Series]] = None,
@@ -510,7 +543,7 @@ class Charting:
or self._obbject._route, # pylint: disable=protected-access
theme=self._charting_settings.table_style,
)
- except Exception as e:
+ except Exception as e: # pylint: disable=W0718
warn(f"Failed to show figure with backend. {e}")
else: