###____________ Chart selection logic and dashboard generation___________ import plotly.express as px import plotly.graph_objects as go from plotly.subplots import make_subplots import pandas as pd class DashboardGenerator: def __init__(self, df, schema): self.df = df self.schema = schema self.charts = [] def generate_all_charts(self): """ Generate appropriate charts for each column type """ print(" Generating charts...") #numeric columns - Histograms for col in self.schema['numeric'][:5]: # Limit to 5 charts fig = self.create_histogram(col) self.charts.append({ 'title': f'Distribution of {col}', 'figure': fig, 'type': 'histogram' }) # categorical columns - Bar charts (top 10) for col in self.schema['categorical'][:3]: fig = self.create_bar_chart(col) self.charts.append({ 'title': f'Top values in {col}', 'figure': fig, 'type': 'bar' }) # time series - Line charts for date_col in self.schema['datetime']: for num_col in self.schema['numeric'][:2]: fig = self.create_time_series(date_col, num_col) self.charts.append({ 'title': f'{num_col} over time', 'figure': fig, 'type': 'line' }) # correlation heatmap if len(self.schema['numeric']) >= 2: fig = self.create_correlation_heatmap() self.charts.append({ 'title': 'Correlation Heatmap', 'figure': fig, 'type': 'heatmap' }) return self.charts def create_histogram(self, column): """Create histogram for numeric column""" fig = px.histogram( self.df, x=column, title=f'Distribution of {column}', color_discrete_sequence=['#2E86AB'], nbins=30 ) fig.update_layout( showlegend=False, height=400, template='plotly_white' ) return fig def create_bar_chart(self, column): """Create bar chart for categorical column""" value_counts = self.df[column].value_counts().head(10) fig = px.bar( x=value_counts.values, y=value_counts.index, orientation='h', title=f'Top 10 values in {column}', color=value_counts.values, color_continuous_scale='Blues' ) fig.update_layout( xaxis_title='Count', yaxis_title=column, height=400, template='plotly_white' ) return fig def create_time_series(self, date_col, value_col): """Create time series line chart""" # group by date time_data = self.df.groupby(pd.Grouper(key=date_col, freq='D'))[value_col].mean().reset_index() fig = px.line( time_data, x=date_col, y=value_col, title=f'{value_col} over time', markers=True ) fig.update_layout( xaxis_title='Date', yaxis_title=value_col, height=400, template='plotly_white' ) return fig def create_correlation_heatmap(self): """Create correlation heatmap""" corr_matrix = self.df[self.schema['numeric']].corr() fig = px.imshow( corr_matrix, text_auto='.2f', aspect='auto', color_continuous_scale='RdBu', title='Correlation Heatmap' ) fig.update_layout( height=500, template='plotly_white' ) return fig def create_key_metrics(self): """Create KPI cards for important metrics""" metrics = [] for col in self.schema['numeric'][:4]: # Top 4 numeric columns mean_val = self.df[col].mean() std_val = self.df[col].std() min_val = self.df[col].min() max_val = self.df[col].max() metrics.append({ 'name': col.upper(), 'value': f"{mean_val:,.0f}", 'change': f"±{std_val:,.0f}", 'min': f"{min_val:,.0f}", 'max': f"{max_val:,.0f}", 'type': 'average' }) return metrics def create_summary_table(self): """Create summary statistics table""" summary = [] for col in self.schema['numeric']: summary.append({ 'Column': col, 'Mean': round(self.df[col].mean(), 2), 'Median': round(self.df[col].median(), 2), 'Std Dev': round(self.df[col].std(), 2), 'Min': round(self.df[col].min(), 2), 'Max': round(self.df[col].max(), 2) }) return pd.DataFrame(summary)