|
import plotly.express as px |
|
import plotly.graph_objects as go |
|
import pandas as pd |
|
from typing import Dict, Optional, List |
|
|
|
|
|
FIXED_ICF_COMPONENT_LABELS: List[str] = [ |
|
'Funções Corporais (b)', |
|
'Atividades e Participação (d)', |
|
'Ambiente (e)', |
|
'Estruturas Corporais (s)', |
|
'Outros' |
|
] |
|
|
|
|
|
ICF_COMPONENT_COLOR_MAP: Dict[str, str] = { |
|
'Funções Corporais (b)': '#FFC145', |
|
'Atividades e Participação (d)': '#B7F242', |
|
'Ambiente (e)': '#4369C0', |
|
'Estruturas Corporais (s)': '#DA3B95', |
|
'Outros': '#AAAAAA' |
|
} |
|
|
|
def create_pie_chart( |
|
input_df: pd.DataFrame, |
|
title: str = "Distribuição da Classificação" |
|
) -> Optional[go.Figure]: |
|
""" |
|
Generates a pie chart from a DataFrame, using consistent colors for ICF categories |
|
present in the input data. |
|
|
|
Args: |
|
input_df (pd.DataFrame): DataFrame with 'Componente CIF' (labels) |
|
and 'Frequencia' (values) columns. |
|
title (str): The title of the chart. |
|
|
|
Returns: |
|
Optional[go.Figure]: The Plotly Figure object containing the pie chart, |
|
or None if there is no valid data to plot (e.g., all frequencies are zero or negative). |
|
""" |
|
|
|
|
|
if input_df.empty: |
|
print(f"Aviso: DataFrame de entrada para o gráfico de pizza '{title}' está vazio. Retornando None.") |
|
return None |
|
|
|
|
|
|
|
plot_df = input_df[input_df['Frequencia'] > 0].copy() |
|
|
|
|
|
|
|
if plot_df.empty: |
|
print(f"Aviso: Nenhum dado com frequência positiva para gerar o gráfico de pizza: '{title}'. Retornando None.") |
|
return None |
|
|
|
|
|
|
|
category_order_map = {'Componente CIF': FIXED_ICF_COMPONENT_LABELS} |
|
|
|
figure = px.pie( |
|
plot_df, |
|
names='Componente CIF', |
|
values='Frequencia', |
|
title=title, |
|
color='Componente CIF', |
|
color_discrete_map=ICF_COMPONENT_COLOR_MAP, |
|
category_orders=category_order_map |
|
) |
|
|
|
figure.update_layout(legend_title_text='Componentes') |
|
figure.update_traces( |
|
direction='clockwise', |
|
rotation=-30, |
|
textinfo="label+value+percent", |
|
textposition='outside', |
|
textfont_size=16, |
|
pull=0.05, |
|
hovertemplate="<b>%{label}</b><br>Frequência: %{value}<br>Porcentagem: %{percent}<extra></extra>" |
|
) |
|
return figure |
|
|
|
def create_bar_chart( |
|
input_df: pd.DataFrame, |
|
title: str = "Frequência da Classificação" |
|
) -> Optional[go.Figure]: |
|
""" |
|
Generates a bar chart from a DataFrame, using consistent colors for ICF categories |
|
present in the input data. |
|
|
|
Args: |
|
input_df (pd.DataFrame): DataFrame with 'Componente CIF' (X-axis) |
|
and 'Frequencia' (Y-axis) columns. |
|
title (str): The title of the chart. |
|
|
|
Returns: |
|
Optional[go.Figure]: The Plotly Figure object containing the bar chart, |
|
or None if there is no valid data to plot (e.g., all frequencies are zero). |
|
""" |
|
|
|
|
|
if input_df.empty: |
|
print(f"Aviso: DataFrame de entrada para o gráfico de barras '{title}' está vazio. Retornando None.") |
|
return None |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if input_df['Frequencia'].sum() == 0: |
|
print(f"Aviso: Todos os dados em input_df têm frequência 0 para o gráfico de barras: '{title}'. Retornando None.") |
|
return None |
|
|
|
|
|
|
|
category_order_map = {'Componente CIF': FIXED_ICF_COMPONENT_LABELS} |
|
|
|
figure = px.bar( |
|
input_df, |
|
x='Componente CIF', |
|
y='Frequencia', |
|
title=title, |
|
labels={'Componente CIF': 'Componentes CIF', 'Frequencia': 'Frequência'}, |
|
color='Componente CIF', |
|
color_discrete_map=ICF_COMPONENT_COLOR_MAP, |
|
category_orders=category_order_map, |
|
text_auto=True |
|
) |
|
|
|
figure.update_layout( |
|
legend_title_text='Componentes', |
|
xaxis_title="Componentes CIF", |
|
yaxis_title="Frequência", |
|
showlegend=True |
|
) |
|
figure.update_traces( |
|
textfont_size=14, |
|
textangle=0, |
|
textposition="inside", |
|
hovertemplate="<b>%{x}</b><br>Frequência: %{y}<extra></extra>" |
|
) |
|
return figure |
|
|
|
def create_tree_map_chart( |
|
tree_map_df: pd.DataFrame, |
|
title: str = "Treemap de Frequências por Hierarquia de Códigos" |
|
) -> Optional[go.Figure]: |
|
""" |
|
Generates a Treemap chart from a DataFrame. |
|
|
|
Args: |
|
tree_map_df (pd.DataFrame): DataFrame with 'Parent', 'Subparent', |
|
'Filho' (Child), and 'Frequencia' (Frequency) columns. |
|
title (str): The title of the chart. |
|
|
|
Returns: |
|
Optional[go.Figure]: The Plotly Figure object containing the Treemap chart, |
|
or None if the DataFrame is empty. |
|
""" |
|
if tree_map_df.empty: |
|
print(f"Aviso: DataFrame vazio para gerar o Treemap: '{title}'.") |
|
return None |
|
|
|
figure = px.treemap( |
|
tree_map_df, |
|
path=['Parent', 'Subparent', 'Filho'], |
|
values='Frequencia', |
|
title=title, |
|
color='Frequencia', |
|
color_continuous_scale='spectral_r', |
|
height=700, |
|
) |
|
|
|
figure.update_traces( |
|
textinfo="label+value+percent entry", |
|
hovertemplate='<b>%{label}</b><br>Frequência: %{value}<br>Porcentagem: %{percentEntry:.1%}<extra></extra>', |
|
) |
|
|
|
return figure |
|
|