# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/00_utils.ipynb.

# %% auto 0
__all__ = ['bar_plot']

# %% ../nbs/00_utils.ipynb 3
import pkgutil
import pandas as pd
from typing import Optional, List

def rawdata(
        all: bool = True, # If True, returns all dataframes. If False, returns only the employee_details dataframe
        tables: Optional[List] = None # If not None, returns only the dataframes in the list
        ) -> dict: # Returns a dictionary of dataframes
    """
    Returns a dictionary of dataframes containing the data for the project
    
    Internal export - pkgutil component can only run in the context of a module
    """
    if all and tables is None:
        return {
            'employee_details': pd.read_excel(pkgutil.get_data(__name__, '_data/Employee_details.xlsx')),
            'glossary': pd.read_excel(pkgutil.get_data(__name__, '_data/Glossary.xlsx')),
            'manager_details': pd.read_excel(pkgutil.get_data(__name__, '_data/Manager_details.xlsx')),
            'manager_mapping': pd.read_excel(pkgutil.get_data(__name__, '_data/Manager_mapping.xlsx')),
            'promo_meta_data': pd.read_excel(pkgutil.get_data(__name__, '_data/promo_meta_data.xlsx'))
        }
    elif tables is not None:
        return {table: pd.read_excel(pkgutil.get_data(__name__, f'_data/{table}.xlsx')) for table in tables}
    else:
        raise ValueError('Either all or tables must be specified')

# %% ../nbs/00_utils.ipynb 4
import matplotlib.ticker as mtick
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns

def bar_plot(
        x_values: np.ndarray, # The x-axis values
        y_values: np.ndarray, # The y-axis values
        type: str = 'Job Family' # The category/type of the x-axis values
    ) -> None:
    "Helper function to plot a bar plot with the attrition rate"
    plt.figure(figsize=(7, 3))
    custom_params = {"axes.spines.right": False, "axes.spines.top": False}
    sns.set_theme(style="ticks", rc=custom_params)

    normalized_values = (y_values - np.min(y_values)) / (np.max(y_values) - np.min(y_values))
    ax = sns.barplot(x=x_values, y=y_values, palette=sns.color_palette("coolwarm", as_cmap=True)(normalized_values))

    # Add the values on top of the bars
    for p in ax.patches:
        height = p.get_height()
        ax.text(p.get_x()+p.get_width()/2.,
                height + 0.01,
                '{:1.1f}%'.format(height*100),
                ha="center", fontsize=11)
    
    plt.title(f'Attrition Rate by {type}')
    plt.xlabel('')
    plt.ylabel('Attrition Rate')
    plt.xticks(rotation=45, ha='right')

    # Change y-axis to percentage format
    ax.yaxis.set_major_formatter(mtick.PercentFormatter(1.0))


    plt.show()    
