File size: 7,129 Bytes
f1b19d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
"""

Data Visualizer Tool - Create charts from data

"""
import logging
from typing import Dict, Any
import io
import base64
import sys
import os

# Add parent directory to path for imports
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from utils.helpers import parse_json_safe

logger = logging.getLogger(__name__)


def visualize_data(

    data: str, 

    chart_type: str = "bar", 

    x_column: str = None, 

    y_column: str = None,

    title: str = "Data Visualization"

) -> Dict[str, Any]:
    """

    Create a chart visualization from data.

    

    Args:

        data: JSON or CSV string data

        chart_type: Type of chart - 'bar', 'line', 'pie', 'scatter'

        x_column: X-axis column name

        y_column: Y-axis column name

        title: Chart title

        

    Returns:

        Dictionary with base64 encoded image and metadata

    """
    try:
        import matplotlib.pyplot as plt
        import pandas as pd
        import json
        
        # Parse data
        try:
            # Try JSON first
            data_dict = json.loads(data)
            df = pd.DataFrame(data_dict)
        except json.JSONDecodeError:
            # Try CSV
            from io import StringIO
            df = pd.read_csv(StringIO(data))
        
        if df.empty:
            raise ValueError("Data is empty")
        
        # Auto-select columns if not specified
        if x_column is None and len(df.columns) > 0:
            x_column = df.columns[0]
        if y_column is None and len(df.columns) > 1:
            y_column = df.columns[1]
        elif y_column is None:
            y_column = df.columns[0]
        
        # Validate columns exist
        if x_column not in df.columns:
            raise ValueError(f"Column '{x_column}' not found in data")
        if y_column not in df.columns:
            raise ValueError(f"Column '{y_column}' not found in data")
        
        # Create figure
        plt.figure(figsize=(10, 6))
        
        # Generate chart based on type
        if chart_type == "bar":
            plt.bar(df[x_column], df[y_column])
            plt.xlabel(x_column)
            plt.ylabel(y_column)
            
        elif chart_type == "line":
            plt.plot(df[x_column], df[y_column], marker='o')
            plt.xlabel(x_column)
            plt.ylabel(y_column)
            plt.grid(True, alpha=0.3)
            
        elif chart_type == "pie":
            plt.pie(df[y_column], labels=df[x_column], autopct='%1.1f%%')
            
        elif chart_type == "scatter":
            plt.scatter(df[x_column], df[y_column], alpha=0.6)
            plt.xlabel(x_column)
            plt.ylabel(y_column)
            plt.grid(True, alpha=0.3)
            
        else:
            raise ValueError(f"Unknown chart type: {chart_type}")
        
        plt.title(title)
        plt.tight_layout()
        
        # Convert to base64
        buffer = io.BytesIO()
        plt.savefig(buffer, format='png', dpi=100, bbox_inches='tight')
        buffer.seek(0)
        image_base64 = base64.b64encode(buffer.read()).decode('utf-8')
        plt.close()
        
        return {
            "image_base64": image_base64,
            "dimensions": {"width": 1000, "height": 600},
            "chart_type": chart_type,
            "title": title,
            "columns_used": {"x": x_column, "y": y_column}
        }
        
    except Exception as e:
        logger.error(f"Error creating visualization: {e}")
        raise


def create_multi_chart(data: str, chart_configs: list) -> Dict[str, Any]:
    """

    Create multiple charts from the same dataset.

    

    Args:

        data: JSON or CSV string data

        chart_configs: List of chart configuration dictionaries

        

    Returns:

        Dictionary with multiple chart images

    """
    try:
        import matplotlib.pyplot as plt
        import pandas as pd
        import json
        
        # Parse data once
        try:
            data_dict = json.loads(data)
            df = pd.DataFrame(data_dict)
        except json.JSONDecodeError:
            from io import StringIO
            df = pd.read_csv(StringIO(data))
        
        charts = []
        for idx, config in enumerate(chart_configs):
            try:
                result = visualize_data(
                    data,
                    chart_type=config.get("chart_type", "bar"),
                    x_column=config.get("x_column"),
                    y_column=config.get("y_column"),
                    title=config.get("title", f"Chart {idx+1}")
                )
                charts.append(result)
            except Exception as e:
                logger.error(f"Error creating chart {idx+1}: {e}")
                charts.append({"error": str(e)})
        
        return {
            "total_charts": len(charts),
            "charts": charts
        }
        
    except Exception as e:
        logger.error(f"Error creating multi-chart: {e}")
        raise


def generate_statistics_chart(data: str) -> Dict[str, Any]:
    """

    Generate a statistical summary chart from numeric data.

    

    Args:

        data: JSON or CSV string with numeric data

        

    Returns:

        Dictionary with statistics chart

    """
    try:
        import matplotlib.pyplot as plt
        import pandas as pd
        import json
        
        # Parse data
        try:
            data_dict = json.loads(data)
            df = pd.DataFrame(data_dict)
        except json.JSONDecodeError:
            from io import StringIO
            df = pd.read_csv(StringIO(data))
        
        # Get numeric columns
        numeric_cols = df.select_dtypes(include=['number']).columns
        
        if len(numeric_cols) == 0:
            raise ValueError("No numeric columns found in data")
        
        # Create statistics summary
        fig, axes = plt.subplots(1, 2, figsize=(14, 6))
        
        # Box plot
        df[numeric_cols].boxplot(ax=axes[0])
        axes[0].set_title("Distribution (Box Plot)")
        axes[0].set_ylabel("Values")
        
        # Histogram
        df[numeric_cols].hist(ax=axes[1], bins=20, alpha=0.7)
        axes[1].set_title("Distribution (Histogram)")
        
        plt.tight_layout()
        
        # Convert to base64
        buffer = io.BytesIO()
        plt.savefig(buffer, format='png', dpi=100, bbox_inches='tight')
        buffer.seek(0)
        image_base64 = base64.b64encode(buffer.read()).decode('utf-8')
        plt.close()
        
        # Calculate statistics
        stats = df[numeric_cols].describe().to_dict()
        
        return {
            "image_base64": image_base64,
            "statistics": stats,
            "numeric_columns": list(numeric_cols)
        }
        
    except Exception as e:
        logger.error(f"Error generating statistics chart: {e}")
        raise