Skip to content

Commit e3fc133

Browse files
committed
Adding sort function to Total metrics bar charts
1 parent 2aa7cae commit e3fc133

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

solarforecastarbiter/reports/figures/plotly_figures.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,44 @@ def gen_grays(num_colors):
6262
'font': {'size': 14}
6363
}
6464

65+
SORT_UPDATEMENU_DROPDOWN = [{
66+
"buttons": [
67+
dict(
68+
method="restyle",
69+
label="Original Order",
70+
args = [{'visible': [True, False, False, False, False]}],
71+
),
72+
dict(
73+
method="restyle",
74+
label="ᐁ Value",
75+
args = [{'visible': [False, True, False, False, False]}],
76+
),
77+
dict(
78+
method="restyle",
79+
label="ᐃ Value",
80+
args = [{'visible': [False, False, True, False, False]}],
81+
),
82+
dict(
83+
method="restyle",
84+
label="ᐁ Name",
85+
args = [{'visible': [False, False, False, True, False]}],
86+
),
87+
dict(
88+
method="restyle",
89+
label="ᐃ Name",
90+
args = [{'visible': [False, False, False, False, True]}],
91+
)
92+
],
93+
"direction": "down",
94+
"showactive": True,
95+
"xanchor": 'center',
96+
"x": 0.025,
97+
"yanchor": 'bottom',
98+
"pad": {'b': 5},
99+
"active": 0,
100+
}
101+
]
102+
65103
# Used to adjust plot height when many x axis labels or long labels are
66104
# present. The length of the longest label of the plot will be multiplies by
67105
# this value and added o the height of PLOT_LAYOUT_DEFAULTS to determine the
@@ -900,6 +938,7 @@ def bar(df, metric):
900938
x_values = pd.Series(x_values, name='abbrev')
901939
palette = cycle(PALETTE)
902940
palette = [next(palette) for _ in x_values]
941+
data = data.assign(palette=palette)
903942
metric_name = datamodel.ALLOWED_METRICS[metric]
904943

905944
# remove height limit when long abbreviations are used or there are more
@@ -921,14 +960,45 @@ def bar(df, metric):
921960
elif longest_x_label > 30:
922961
x_axis_kwargs.update({'tickangle': 45})
923962

963+
# Create dataframes for each sort (name, value)
964+
data_val_asc = data.sort_values(by=['value', 'name'], ascending=True)
965+
data_val_desc = data.sort_values(by=['value', 'name'], ascending=False)
966+
data_name_asc = data.sort_values(by=['name'], ascending=True)
967+
data_name_desc = data.sort_values(by=['name'], ascending=False)
968+
924969
fig = go.Figure()
925970
fig.add_trace(go.Bar(x=x_values, y=data['value'],
926971
text=data['name'],
972+
visible=True,
927973
marker=go.bar.Marker(color=palette),
928974
hovertemplate='(%{text}, %{y})<extra></extra>'))
975+
fig.add_trace(go.Bar(x=data_val_asc['name'], y=data_val_asc['value'],
976+
text=data_val_asc['abbrev'],
977+
visible=False,
978+
marker=go.bar.Marker(color=data_val_asc['palette']),
979+
hovertemplate='(%{text}, %{y})<extra></extra>'))
980+
fig.add_trace(go.Bar(x=data_val_desc['name'], y=data_val_desc['value'],
981+
text=data_val_desc['abbrev'],
982+
visible=False,
983+
marker=go.bar.Marker(color=data_val_desc['palette']),
984+
hovertemplate='(%{text}, %{y})<extra></extra>'))
985+
fig.add_trace(go.Bar(x=data_name_asc['name'], y=data_name_asc['value'],
986+
text=data_name_asc['abbrev'],
987+
visible=False,
988+
marker=go.bar.Marker(color=data_name_asc['palette']),
989+
hovertemplate='(%{text}, %{y})<extra></extra>'))
990+
fig.add_trace(go.Bar(x=data_name_desc['name'], y=data_name_desc['value'],
991+
text=data_name_desc['abbrev'],
992+
visible=False,
993+
marker=go.bar.Marker(color=data_name_desc['palette']),
994+
hovertemplate='(%{text}, %{y})<extra></extra>'))
995+
updatemenus = SORT_UPDATEMENU_DROPDOWN
996+
if len(x_values) <= 1:
997+
updatemenus = None
929998
fig.update_layout(
930999
title=f'<b>{metric_name}</b>',
9311000
xaxis_title=metric_name,
1001+
updatemenus=updatemenus,
9321002
**plot_layout_args)
9331003
configure_axes(fig, x_axis_kwargs, y_range)
9341004
return fig

0 commit comments

Comments
 (0)