import plotly.graph_objects as go
from vuecore.schemas.basic.scatter import ScatterConfig
from vuecore.schemas.basic.line import LineConfig
from vuecore.schemas.basic.bar import BarConfig
from vuecore.schemas.basic.box import BoxConfig
[docs]
def apply_scatter_theme(fig: go.Figure, config: ScatterConfig) -> go.Figure:
"""
Applies a consistent layout and theme to a Plotly scatter plot.
This function handles all styling and layout adjustments, such as titles,
dimensions, templates, and trace properties, separating these concerns
from the initial data mapping.
Parameters
----------
fig : go.Figure
The Plotly figure object to be styled.
config : ScatterConfig
The configuration object containing all styling and layout info.
Returns
-------
go.Figure
The styled Plotly figure object.
"""
# Apply trace-specific updates
fig.update_traces(
marker=dict(
opacity=config.opacity,
line=dict(width=config.marker_line_width, color=config.marker_line_color),
),
selector=dict(mode="markers"),
)
# Use the labels dictionary to set axis titles, falling back to defaults
x_title = config.x_title or (
config.labels.get(config.x) if config.labels else None or config.x.title()
)
y_title = config.y_title or (
config.labels.get(config.y) if config.labels else None or config.y.title()
)
# Apply layout updates for scatter plot
fig.update_layout(
title_text=config.title,
title_subtitle_text=config.subtitle,
xaxis_title=x_title,
yaxis_title=y_title,
height=config.height,
width=config.width,
template=config.template,
xaxis_type="log" if config.log_x else None,
yaxis_type="log" if config.log_y else None,
xaxis_range=config.range_x,
yaxis_range=config.range_y,
)
return fig
[docs]
def apply_line_theme(fig: go.Figure, config: LineConfig) -> go.Figure:
"""
Applies a consistent layout and theme to a Plotly line plot.
This function handles all styling and layout adjustments, such as titles,
dimensions, templates, and trace properties, separating these concerns
from the initial data mapping.
Parameters
----------
fig : go.Figure
The Plotly figure object to be styled.
config : LineConfig
The configuration object containing all styling and layout info.
Returns
-------
go.Figure
The styled Plotly figure object.
"""
# Apply trace-specific updates
fig.update_traces(
mode="lines+markers" if config.markers else "lines",
line_shape=config.line_shape,
)
# Use the labels dictionary to set axis titles, falling back to defaults
x_title = config.x_title or (
config.labels.get(config.x) if config.labels else None or config.x.title()
)
y_title = config.y_title or (
config.labels.get(config.y) if config.labels else None or config.y.title()
)
# Apply layout updates for line plot
fig.update_layout(
title_text=config.title,
title_subtitle_text=config.subtitle,
xaxis_title=x_title,
yaxis_title=y_title,
height=config.height,
width=config.width,
template=config.template,
xaxis_type="log" if config.log_x else None,
yaxis_type="log" if config.log_y else None,
xaxis_range=config.range_x,
yaxis_range=config.range_y,
)
return fig
[docs]
def apply_bar_theme(fig: go.Figure, config: BarConfig) -> go.Figure:
"""
Applies a consistent layout and theme to a Plotly bar plot.
This function handles all styling and layout adjustments, such as titles,
dimensions, templates, and trace properties, separating these concerns
from the initial data mapping.
Parameters
----------
fig : go.Figure
The Plotly figure object to be styled.
config : BarConfig
The configuration object containing all styling and layout info.
Returns
-------
go.Figure
The styled Plotly figure object.
"""
# Apply trace-specific updates for bar plots
fig.update_traces(opacity=config.opacity, selector=dict(type="bar"))
# Use the labels dictionary to set axis titles, falling back to defaults
x_title = config.x_title or (
config.labels.get(config.x) if config.labels else None or config.x.title()
)
y_title = config.y_title or (
config.labels.get(config.y) if config.labels else None or config.y.title()
)
# Apply layout updates for bar plot
fig.update_layout(
title_text=config.title,
title_subtitle_text=config.subtitle,
xaxis_title=x_title,
yaxis_title=y_title,
height=config.height,
width=config.width,
template=config.template,
xaxis_type="log" if config.log_x else None,
yaxis_type="log" if config.log_y else None,
xaxis_range=config.range_x,
yaxis_range=config.range_y,
barmode=config.barmode,
)
return fig
[docs]
def apply_box_theme(fig: go.Figure, config: BoxConfig) -> go.Figure:
"""
Applies a consistent layout and theme to a Plotly box plot.
This function handles all styling and layout adjustments, such as titles,
dimensions, templates, and trace properties, separating these concerns
from the initial data mapping.
Parameters
----------
fig : go.Figure
The Plotly figure object to be styled.
config : BoxConfig
The configuration object containing all styling and layout info.
Returns
-------
go.Figure
The styled Plotly figure object.
"""
# Apply trace-specific updates for box plots
fig.update_traces(
boxpoints=config.points, notched=config.notched, selector=dict(type="box")
)
# Use the labels dictionary to set axis titles, falling back to defaults
x_title = config.x_title or (
config.labels.get(config.x)
if config.x and config.labels
else None or (config.x.title() if config.x else None)
)
y_title = config.y_title or (
config.labels.get(config.y)
if config.y and config.labels
else None or (config.y.title() if config.y else None)
)
# Apply layout updates for box plot
fig.update_layout(
title_text=config.title,
title_subtitle_text=config.subtitle,
xaxis_title=x_title,
yaxis_title=y_title,
height=config.height,
width=config.width,
template=config.template,
xaxis_type="log" if config.log_x else None,
yaxis_type="log" if config.log_y else None,
xaxis_range=config.range_x,
yaxis_range=config.range_y,
boxmode=config.boxmode,
)
return fig