drop voice46 from scales 1-10. fix plots breakline in title
This commit is contained in:
36
plots.py
36
plots.py
@@ -13,6 +13,12 @@ import hashlib
|
||||
class JPMCPlotsMixin:
|
||||
"""Mixin class for plotting functions in JPMCSurvey."""
|
||||
|
||||
def _process_title(self, title: str) -> str | list[str]:
|
||||
"""Process title to handle <br> tags for Altair."""
|
||||
if isinstance(title, str) and '<br>' in title:
|
||||
return title.split('<br>')
|
||||
return title
|
||||
|
||||
def _sanitize_filename(self, title: str) -> str:
|
||||
"""Convert plot title to a safe filename."""
|
||||
# Remove HTML tags
|
||||
@@ -156,8 +162,8 @@ class JPMCPlotsMixin:
|
||||
chart_spec = chart.to_dict()
|
||||
existing_title = chart_spec.get('title', '')
|
||||
|
||||
# Handle different title formats (string vs dict)
|
||||
if isinstance(existing_title, str):
|
||||
# Handle different title formats (string vs dict vs list)
|
||||
if isinstance(existing_title, (str, list)):
|
||||
title_config = {
|
||||
'text': existing_title,
|
||||
'subtitle': lines,
|
||||
@@ -260,6 +266,7 @@ class JPMCPlotsMixin:
|
||||
color: str = ColorPalette.PRIMARY,
|
||||
height: int | None = None,
|
||||
width: int | str | None = None,
|
||||
domain: list[float] | None = None,
|
||||
) -> alt.Chart:
|
||||
"""Create a bar plot showing average scores and count of non-null values for each column."""
|
||||
df = self._ensure_dataframe(data)
|
||||
@@ -278,11 +285,14 @@ class JPMCPlotsMixin:
|
||||
|
||||
# Convert to pandas for Altair (sort by average descending)
|
||||
stats_df = pl.DataFrame(stats).sort('average', descending=True).to_pandas()
|
||||
|
||||
if domain is None:
|
||||
domain = [stats_df['average'].min(), stats_df['average'].max()]
|
||||
|
||||
# Base bar chart
|
||||
bars = alt.Chart(stats_df).mark_bar(color=color).encode(
|
||||
x=alt.X('voice:N', title=x_label, sort='-y'),
|
||||
y=alt.Y('average:Q', title=y_label, scale=alt.Scale(domain=[0, 10])),
|
||||
y=alt.Y('average:Q', title=y_label, scale=alt.Scale(domain=domain)),
|
||||
tooltip=[
|
||||
alt.Tooltip('voice:N', title='Voice'),
|
||||
alt.Tooltip('average:Q', title='Average', format='.2f'),
|
||||
@@ -303,7 +313,7 @@ class JPMCPlotsMixin:
|
||||
|
||||
# Combine layers
|
||||
chart = (bars + text).properties(
|
||||
title=title,
|
||||
title=self._process_title(title),
|
||||
width=width or 800,
|
||||
height=height or getattr(self, 'plot_height', 400)
|
||||
)
|
||||
@@ -360,7 +370,7 @@ class JPMCPlotsMixin:
|
||||
alt.Tooltip('count:Q', title='Count')
|
||||
]
|
||||
).add_params(selection).properties(
|
||||
title=title,
|
||||
title=self._process_title(title),
|
||||
width=width or 800,
|
||||
height=height or getattr(self, 'plot_height', 400)
|
||||
)
|
||||
@@ -420,7 +430,7 @@ class JPMCPlotsMixin:
|
||||
alt.Tooltip('count:Q', title='Count')
|
||||
]
|
||||
).add_params(selection).properties(
|
||||
title=title,
|
||||
title=self._process_title(title),
|
||||
width=width or 800,
|
||||
height=height or getattr(self, 'plot_height', 400)
|
||||
)
|
||||
@@ -473,7 +483,7 @@ class JPMCPlotsMixin:
|
||||
alt.Tooltip('count:Q', title='1st Place Votes')
|
||||
]
|
||||
).properties(
|
||||
title=title,
|
||||
title=self._process_title(title),
|
||||
width=width or 800,
|
||||
height=height or getattr(self, 'plot_height', 400)
|
||||
)
|
||||
@@ -514,7 +524,7 @@ class JPMCPlotsMixin:
|
||||
)
|
||||
|
||||
chart = (bars + text).properties(
|
||||
title=title,
|
||||
title=self._process_title(title),
|
||||
width=width or 800,
|
||||
height=height or getattr(self, 'plot_height', 400)
|
||||
)
|
||||
@@ -571,7 +581,7 @@ class JPMCPlotsMixin:
|
||||
alt.Tooltip('count:Q', title='Selections')
|
||||
]
|
||||
).properties(
|
||||
title=title,
|
||||
title=self._process_title(title),
|
||||
width=width or 800,
|
||||
height=height or getattr(self, 'plot_height', 400)
|
||||
)
|
||||
@@ -627,7 +637,7 @@ class JPMCPlotsMixin:
|
||||
alt.Tooltip('count:Q', title='In Top 3')
|
||||
]
|
||||
).properties(
|
||||
title=title,
|
||||
title=self._process_title(title),
|
||||
width=width or 800,
|
||||
height=height or getattr(self, 'plot_height', 400)
|
||||
)
|
||||
@@ -713,7 +723,7 @@ class JPMCPlotsMixin:
|
||||
# Combine layers
|
||||
chart = (bars + text).properties(
|
||||
title={
|
||||
"text": title,
|
||||
"text": self._process_title(title),
|
||||
"subtitle": [trait_description, "(Numbers on bars indicate respondent count)"]
|
||||
},
|
||||
width=width or 800,
|
||||
@@ -776,7 +786,7 @@ class JPMCPlotsMixin:
|
||||
alt.Tooltip('correlation:Q', format='.2f')
|
||||
]
|
||||
).properties(
|
||||
title=title,
|
||||
title=self._process_title(title),
|
||||
width=width or 800,
|
||||
height=height or 350
|
||||
)
|
||||
@@ -832,7 +842,7 @@ class JPMCPlotsMixin:
|
||||
alt.Tooltip('correlation:Q', format='.2f')
|
||||
]
|
||||
).properties(
|
||||
title=title,
|
||||
title=self._process_title(title),
|
||||
width=width or 800,
|
||||
height=height or 350
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user