Skip to content

Commit 206bc90

Browse files
committed
Update to use xarray-plotly helpers
1 parent 379fe95 commit 206bc90

2 files changed

Lines changed: 20 additions & 20 deletions

File tree

flixopt/comparison.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from .config import CONFIG
1313
from .plot_result import PlotResult
1414
from .statistics_accessor import (
15+
_SLOT_DEFAULTS,
1516
ColorType,
1617
SelectType,
1718
_build_color_kwargs,
@@ -615,15 +616,10 @@ def storage(
615616

616617
# Add charge state as line overlay on secondary y-axis
617618
if 'charge_state' in ds:
618-
# Build line figure with same faceting kwargs, colored by case
619-
line_kwargs = {
620-
k: v for k, v in plotly_kwargs.items() if k in ('x', 'facet_col', 'facet_row', 'animation_frame')
621-
}
622-
line_fig = ds['charge_state'].plotly.line(color='case', **line_kwargs)
623-
# Update legend group for charge_state traces
624-
for trace in line_fig.data:
625-
trace.legendgroup = f'charge_state_{trace.name}'
626-
# Combine using xarray_plotly's add_secondary_y which handles facets correctly
619+
# Filter out bar-only kwargs, apply line defaults, override color for comparison
620+
line_kwargs = {k: v for k, v in plotly_kwargs.items() if k not in ('pattern_shape', 'color')}
621+
_apply_slot_defaults(line_kwargs, {**_SLOT_DEFAULTS['storage_line'], 'color': 'case'})
622+
line_fig = ds['charge_state'].plotly.line(**line_kwargs)
627623
fig = add_secondary_y(fig, line_fig, secondary_y_title='Charge State')
628624

629625
return self._finalize(ds, fig, show)

flixopt/statistics_accessor.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
'flows': {'x': 'time', 'color': 'variable', 'symbol': None},
6464
'charge_states': {'x': 'time', 'color': 'variable', 'symbol': None},
6565
'storage': {'x': 'time', 'color': 'variable', 'pattern_shape': None},
66+
'storage_line': {'x': 'time', 'color': None, 'line_dash': None, 'symbol': None},
6667
'sizes': {'x': 'variable', 'color': 'variable'},
6768
'duration_curve': {'symbol': None}, # x is computed dynamically
6869
'effects': {}, # x is computed dynamically
@@ -2393,17 +2394,20 @@ def storage(
23932394
_apply_unified_hover(fig, unit=unit_label)
23942395

23952396
# Add charge state as line on secondary y-axis
2396-
# Build line figure with same faceting kwargs
2397-
line_kwargs = {
2398-
k: v for k, v in plotly_kwargs.items() if k in ('x', 'facet_col', 'facet_row', 'animation_frame')
2399-
}
2400-
line_fig = charge_da.plotly.line(color=None, **line_kwargs)
2401-
# Style the line traces
2402-
for trace in line_fig.data:
2403-
trace.name = 'charge_state'
2404-
trace.line = dict(color=charge_state_color, width=2)
2405-
trace.showlegend = trace == line_fig.data[0] # Only first trace in legend
2406-
trace.legendgroup = 'charge_state'
2397+
# Filter out bar-only kwargs, then apply line-specific defaults
2398+
line_kwargs = {k: v for k, v in plotly_kwargs.items() if k not in ('pattern_shape', 'color')}
2399+
_apply_slot_defaults(line_kwargs, 'storage_line')
2400+
line_fig = charge_da.plotly.line(**line_kwargs)
2401+
# Style all traces including animation frames
2402+
update_traces(
2403+
line_fig,
2404+
line=dict(color=charge_state_color, width=2),
2405+
name='charge_state',
2406+
legendgroup='charge_state',
2407+
showlegend=False,
2408+
)
2409+
if line_fig.data:
2410+
line_fig.data[0].showlegend = True
24072411
# Combine using xarray_plotly's add_secondary_y which handles facets correctly
24082412
fig = add_secondary_y(fig, line_fig, secondary_y_title='Charge State')
24092413

0 commit comments

Comments
 (0)