Skip to content

Commit

Permalink
avoid stacking in total metric plots (#599)
Browse files Browse the repository at this point in the history
* avoid stacking in total metric plots

* update whatsnew

* add fewer null chars

* Apply suggestions from code review

Co-authored-by: Will Holmgren <william.holmgren@gmail.com>

* flake8

Co-authored-by: Will Holmgren <william.holmgren@gmail.com>
  • Loading branch information
alorenzo175 and wholmgren committed Oct 23, 2020
1 parent 1062925 commit 1007556
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
2 changes: 2 additions & 0 deletions docs/source/whatsnew/1.0.0rc4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ Bug fixes
(:issue:`343`) (:pull:`594`)
* Stop errors generated in the report process from being sent to
sentry (:issue:`329`) (:pull:`597`)
* Avoid stacking of forecasts errors in the Total plots in reports
and show the full forecast name on hover (:issue:`463`) (:pull:`599`)


Contributors
Expand Down
19 changes: 13 additions & 6 deletions solarforecastarbiter/reports/figures/plotly_figures.py
Original file line number Diff line number Diff line change
Expand Up @@ -835,23 +835,28 @@ def bar(df, metric):
data = df[(df['category'] == 'total') & (df['metric'] == metric)]
y_range = None
x_axis_kwargs = {}
x_values = data['abbrev']
x_values = []
# to avoid stacking, add null characters to fx with
# same abbreviated name. GH463
for val, ser in data[['abbrev']].groupby('abbrev'):
x_values += [val + ('\0' * i) for i in range(len(ser))]
x_values = pd.Series(x_values, name='abbrev')
palette = cycle(PALETTE)
palette = [next(palette) for _ in x_values]
metric_name = datamodel.ALLOWED_METRICS[metric]

# remove height limit when long abbreviations are used or there are more
# than 5 pairs to problems with labels being cutt off.
# than 5 pairs to problems with labels being cut off.
plot_layout_args = deepcopy(PLOT_LAYOUT_DEFAULTS)
longest_x_label = x_values.map(len).max()
# ok to cut off null characters at the end of the labels
longest_x_label = x_values.map(lambda x: len(x.rstrip('\0'))).max()
if longest_x_label > 15 or x_values.size > 6:
# Set explicit height and set automargin on x axis to allow for dynamic
# sizing to accomodate long x axis labels. Height is set based on
# length of longest x axis label, due to a failure that can occur when
# plotly determines there is not enough space for automargins to work.
max_name_length = x_values.map(len).max()
plot_height = plot_layout_args['height'] + (
max_name_length * X_LABEL_HEIGHT_FACTOR)
longest_x_label * X_LABEL_HEIGHT_FACTOR)
plot_layout_args['height'] = plot_height
x_axis_kwargs = {'automargin': True}
if longest_x_label > 60:
Expand All @@ -861,7 +866,9 @@ def bar(df, metric):

fig = go.Figure()
fig.add_trace(go.Bar(x=x_values, y=data['value'],
marker=go.bar.Marker(color=palette)))
text=data['name'],
marker=go.bar.Marker(color=palette),
hovertemplate='(%{text}, %{y})<extra></extra>'))
fig.update_layout(
title=f'<b>{metric_name}</b>',
xaxis_title=metric_name,
Expand Down

0 comments on commit 1007556

Please sign in to comment.