File size: 4,721 Bytes
79c6522
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import re
from llm_utils import extract_chart_suggestion

# Set the style for the visualizations
sns.set_style("whitegrid")
plt.rcParams.update({'font.size': 12})

def generate_chart(response, df):
    """
    Generate a chart based on the LLM response
    Returns a matplotlib figure or None if no chart is needed
    """
    # Extract chart suggestion from response
    chart_info = extract_chart_suggestion(response)
    
    # If no chart type detected, return None
    if not chart_info["type"]:
        return None
    
    # Create figure
    fig, ax = plt.subplots(figsize=(10, 6))
    
    # Check if suggested columns exist in the dataframe
    valid_columns = [col for col in chart_info["columns"] if col in df.columns]
    
    # If no valid columns found, try to infer from dataframe
    if not valid_columns:
        # For numerical charts, select numeric columns
        if chart_info["type"] in ["histogram", "line chart", "scatter plot", "box plot"]:
            valid_columns = df.select_dtypes(include=['number']).columns.tolist()[:2]
        else:  # For categorical charts, include at least one categorical
            num_cols = df.select_dtypes(include=['number']).columns.tolist()[:1]
            cat_cols = df.select_dtypes(include=['object']).columns.tolist()[:1]
            valid_columns = cat_cols + num_cols
    
    # If still no valid columns, return None
    if not valid_columns:
        return None
    
    # Generate the appropriate chart based on chart_info["type"]
    try:
        if chart_info["type"] == "bar chart":
            if len(valid_columns) >= 2:
                # If we have at least 2 columns, assume first is category and second is value
                sns.barplot(x=valid_columns[0], y=valid_columns[1], data=df, ax=ax)
            else:
                # Single column - show value counts
                df[valid_columns[0]].value_counts().plot(kind='bar', ax=ax)
            
            ax.set_title(f"Bar Chart of {valid_columns[0]}")
            ax.set_xlabel(valid_columns[0])
            ax.tick_params(axis='x', rotation=45)
        
        elif chart_info["type"] == "histogram":
            sns.histplot(data=df, x=valid_columns[0], kde=True, ax=ax)
            ax.set_title(f"Histogram of {valid_columns[0]}")
            ax.set_xlabel(valid_columns[0])
        
        elif chart_info["type"] == "line chart":
            if len(valid_columns) >= 2:
                # Sort by the x column for better line charts
                sorted_df = df.sort_values(by=valid_columns[0])
                ax.plot(sorted_df[valid_columns[0]], sorted_df[valid_columns[1]])
                ax.set_title(f"Line Chart of {valid_columns[1]} by {valid_columns[0]}")
                ax.set_xlabel(valid_columns[0])
                ax.set_ylabel(valid_columns[1])
            else:
                # Single column - show values over index
                df[valid_columns[0]].plot(ax=ax)
                ax.set_title(f"Line Chart of {valid_columns[0]}")
        
        elif chart_info["type"] == "scatter plot":
            if len(valid_columns) >= 2:
                sns.scatterplot(x=valid_columns[0], y=valid_columns[1], data=df, ax=ax)
                ax.set_title(f"Scatter Plot of {valid_columns[1]} vs {valid_columns[0]}")
                ax.set_xlabel(valid_columns[0])
                ax.set_ylabel(valid_columns[1])
        
        elif chart_info["type"] == "pie chart":
            # Get value counts for the first column
            counts = df[valid_columns[0]].value_counts()
            # Limit to top 10 categories for readability
            if len(counts) > 10:
                counts = counts.head(10)
            counts.plot(kind='pie', ax=ax, autopct='%1.1f%%')
            ax.set_title(f"Pie Chart of {valid_columns[0]} Distribution")
            ax.set_ylabel('')
        
        elif chart_info["type"] == "box plot":
            if len(valid_columns) >= 2:
                # If we have 2 columns, use the first as category and second as value
                sns.boxplot(x=valid_columns[0], y=valid_columns[1], data=df, ax=ax)
                ax.set_title(f"Box Plot of {valid_columns[1]} by {valid_columns[0]}")
                ax.tick_params(axis='x', rotation=45)
            else:
                # Single column - simple box plot
                sns.boxplot(y=valid_columns[0], data=df, ax=ax)
                ax.set_title(f"Box Plot of {valid_columns[0]}")
        
        # Adjust layout and return the figure
        plt.tight_layout()
        return fig
    
    except Exception as e:
        print(f"Error generating chart: {str(e)}")
        return None