[BugFix] Charts: Fix Legend & Title Crowding in Jupyter (#6316)

* better jupyter legend

* add that to the commit
This commit is contained in:
Danglewood
2024-04-17 07:53:45 -07:00
committed by GitHub
parent 326f239d01
commit 779708a3a4
6 changed files with 71 additions and 49 deletions

View File

@@ -264,6 +264,7 @@ class Charting:
orientation: Literal["h", "v"] = "v",
colors: Optional[List[str]] = None,
layout_kwargs: Optional[Dict[str, Any]] = None,
bar_kwargs: Optional[Dict[str, Any]] = None,
render: bool = True,
**kwargs,
) -> Union[OpenBBFigure, Figure, None]:
@@ -281,19 +282,18 @@ class Charting:
The bar mode, by default "group".
xtype : Literal["category", "multicategory", "date", "log", "linear"], optional
The x-axis type, by default "category".
title : Optional[str], optional
title : str, optional
The title of the chart, by default None.
xtitle : Optional[str], optional
xtitle : str, optional
The x-axis title, by default None.
ytitle : Optional[str], optional
ytitle : str, optional
The y-axis title, by default None.
orientation : Literal["h", "v"], optional
The orientation of the chart, by default "v".
colors: Optional[List[str]], optional
colors: List[str], optional
Manually set the colors to cycle through for each column in 'y', by default None.
layout_kwargs : Optional[Dict[str, Any]], optional
bar_kwargs : Dict[str, Any], optional
Additional keyword arguments to apply with figure.add_bar(), by default None.
layout_kwargs : Dict[str, Any], optional
Additional keyword arguments to apply with figure.update_layout(), by default None.
Returns
-------
OpenBBFigure
@@ -311,6 +311,7 @@ class Charting:
ytitle=ytitle,
orientation=orientation,
colors=colors,
bar_kwargs=bar_kwargs,
layout_kwargs=layout_kwargs,
)

View File

@@ -289,21 +289,20 @@ def equity_price_historical( # noqa: PLR0912
)
if _volume is True and "atr" in indicators: # type: ignore
fig.add_inchart_volume(data)
fig.set_title(title)
fig.update_layout(
paper_bgcolor="rgba(0,0,0,0)",
plot_bgcolor="rgba(0,0,0,0)",
font=dict(color=text_color),
showlegend=True,
legend=dict(
orientation="h",
yanchor="bottom",
xanchor="auto",
y=1.02,
x=0.95,
orientation="v",
yanchor="top",
xanchor="right",
y=0.95,
x=-0.01,
xref="paper",
bgcolor="rgba(0,0,0,0)",
font=dict(size=12),
bgcolor="rgba(0,0,0,0)",
),
xaxis=dict(
ticklen=0,
@@ -328,6 +327,7 @@ def equity_price_historical( # noqa: PLR0912
zeroline=True,
mirror=True,
showline=True,
tickfont=dict(size=14),
),
yaxis2=dict(
ticklen=0,
@@ -341,6 +341,10 @@ def equity_price_historical( # noqa: PLR0912
hovermode="x",
)
if kwargs.get("title"):
title = kwargs["title"]
fig.update_layout(title=dict(text=title, x=0.5))
content = fig.to_plotly_json()
return fig, content
@@ -353,7 +357,7 @@ def equity_price_historical( # noqa: PLR0912
if "symbol" in data.columns:
data = data.pivot(columns="symbol", values=target)
title: str = kwargs.get("title") if "title" in kwargs else title if title else "Historical Prices" # type: ignore
title: str = kwargs.get("title", "Historical Prices") # type: ignore
y1title = data.iloc[:, 0].name
y2title = ""
@@ -374,8 +378,6 @@ def equity_price_historical( # noqa: PLR0912
fig = OpenBBFigure()
fig.update_layout(ChartStyle().plotly_template.get("layout", {}))
for i, col in enumerate(data.columns):
hovertemplate = f"{data[col].name}: %{{y}}<extra></extra>"
@@ -416,23 +418,34 @@ def equity_price_historical( # noqa: PLR0912
y2title = None # type: ignore
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)",
font=dict(color=text_color),
legend=dict(
orientation="h",
yanchor="bottom",
xanchor="right",
y=1.02,
x=1,
bgcolor="rgba(0,0,0,0)",
legend=(
dict(
orientation="v",
yanchor="top",
xanchor="right",
y=0.95,
x=-0.01,
bgcolor="rgba(0,0,0,0)",
)
if len(data.columns) > 2
else dict(
orientation="h",
yanchor="bottom",
xanchor="right",
y=1.02,
x=0.98,
bgcolor="rgba(0,0,0,0)",
)
),
yaxis1=(
dict(
side="right",
ticklen=0,
showgrid=True,
showline=True,
gridcolor="rgba(128,128,128,0.3)",
title=dict(
text=y1title if y1title else None, standoff=20, font=dict(size=20)
),
@@ -458,11 +471,16 @@ def equity_price_historical( # noqa: PLR0912
xaxis=dict(
ticklen=0,
showgrid=True,
showline=True,
gridcolor="rgba(128,128,128,0.3)",
),
margin=dict(l=20, r=20, b=20),
margin=dict(l=20, r=20, b=20, t=20),
dragmode="pan",
hovermode="x",
)
if kwargs.get("title"):
title = kwargs["title"]
fig.update_layout(title=dict(text=title, x=0.5))
content = fig.show(external=True).to_plotly_json()

View File

@@ -42,6 +42,8 @@ if TYPE_CHECKING:
TimeSeriesT = TypeVar("TimeSeriesT", bound="TimeSeries")
pio.default_renderers = "notebook"
class OpenBBFigure(go.Figure):
"""Custom Figure class for OpenBB Terminal.

File diff suppressed because one or more lines are too long

View File

@@ -601,13 +601,14 @@ class PlotlyTA(PltTA):
item.hoveron = "fills"
item.legendgroup = "SR Lines"
item.pop("hovertemplate", None)
item.opacity = 0.5
if not sr_legend_shown:
item.showlegend = True
sr_legend_shown = True
if "annotations" in figure.layout:
for item in figure.layout.annotations: # type: ignore
item["font"]["size"] = 12
item["font"]["size"] = 14
figure.update_layout(margin=dict(l=50, r=10, b=10, t=20))
return figure

View File

@@ -117,8 +117,6 @@ def line_chart( # noqa: PLR0912
except Exception as _:
fig = OpenBBFigure(create_backend=True)
fig.update_layout(ChartStyle().plotly_template.get("layout", {}))
title = f"{title}" if title else ""
xtitle = xtitle if xtitle else ""
y1title = ytitle if ytitle else ""
@@ -345,6 +343,7 @@ def bar_chart( # noqa: PLR0912
ytitle: Optional[str] = None,
orientation: Literal["h", "v"] = "v",
colors: Optional[List[str]] = None,
bar_kwargs: Optional[Dict[str, Any]] = None,
layout_kwargs: Optional[Dict[str, Any]] = None,
**kwargs,
) -> Union[OpenBBFigure, Figure]:
@@ -364,15 +363,17 @@ def bar_chart( # noqa: PLR0912
The bar mode, by default "group".
xtype : Literal["category", "multicategory", "date", "log", "linear"], optional
The x-axis type, by default "category".
title : Optional[str], optional
title : str, optional
The title of the chart, by default None.
xtitle : Optional[str], optional
xtitle : str, optional
The x-axis title, by default None.
ytitle : Optional[str], optional
ytitle : str, optional
The y-axis title, by default None.
colors: Optional[List[str]], optional
colors: List[str], optional
Manually set the colors to cycle through for each column in 'y', by default None.
layout_kwargs : Optional[Dict[str, Any]], optional
bar_kwargs : Dict[str, Any], optional
Additional keyword arguments to apply with figure.add_bar(), by default None.
layout_kwargs : Dict[str, Any], optional
Additional keyword arguments to apply with figure.update_layout(), by default None.
Returns
@@ -399,7 +400,8 @@ def bar_chart( # noqa: PLR0912
figure.update_layout(ChartStyle().plotly_template.get("layout", {}))
if colors is not None:
figure.update_layout(colorway=colors)
if bar_kwargs is None:
bar_kwargs = {}
if isinstance(data, (Data, list, dict)):
data = basemodel_to_df(convert_to_basemodel(data), index=None)
@@ -420,20 +422,20 @@ def bar_chart( # noqa: PLR0912
else "%{fullData.name}:%{x}<extra></extra>"
),
width=0.95 / len(y) * 0.75 if barmode == "group" and len(y) > 1 else 0.95,
**bar_kwargs,
)
figure.update_layout(
title=dict(text=title if title else None, x=0.5, font=dict(size=20)),
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)",
legend=dict(
orientation="h",
yanchor="bottom",
y=1.02,
xanchor="right",
x=0.94,
y=1.02,
x=0.98,
bgcolor="rgba(0,0,0,0)",
font=dict(size=12),
),
xaxis=dict(
type=xtype,
@@ -475,12 +477,10 @@ def bar_chart( # noqa: PLR0912
font=dict(size=12),
),
)
if layout_kwargs:
figure.update_layout(
**layout_kwargs,
)
return figure