feat(chart): add toggle for percentage metric calculation mode in Table chart (#33656)

This commit is contained in:
Levis Mbote
2025-06-13 21:00:58 +03:00
committed by GitHub
parent 0d3eebd221
commit 7deca8f2cd
6 changed files with 216 additions and 13 deletions

View File

@@ -37,6 +37,7 @@ def contribution(
columns: list[str] | None = None,
time_shifts: list[str] | None = None,
rename_columns: list[str] | None = None,
contribution_totals: dict[str, float] | None = None,
) -> DataFrame:
"""
Calculate cell contribution to row/column total for numeric columns.
@@ -82,10 +83,19 @@ def contribution(
numeric_df_view = numeric_df[actual_columns]
if orientation == PostProcessingContributionOrientation.COLUMN:
numeric_df_view = numeric_df_view / numeric_df_view.values.sum(
axis=0, keepdims=True
)
contribution_df[rename_columns] = numeric_df_view
if contribution_totals:
for i, col in enumerate(numeric_df_view.columns):
total = contribution_totals.get(col)
rename_col = rename_columns[i]
if total is None or total == 0:
contribution_df[rename_col] = 0
else:
contribution_df[rename_col] = numeric_df_view[col] / total
else:
numeric_df_view = numeric_df_view / numeric_df_view.values.sum(
axis=0, keepdims=True
)
contribution_df[rename_columns] = numeric_df_view
return contribution_df
result = get_column_groups(numeric_df_view, time_shifts, rename_columns)