File size: 17,462 Bytes
2d2c483
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
# Chart Generator Module
# Contains parse_embedded_chart_data() and create_chart_from_data() functions

import re
import io
import os
import sys
import base64
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from typing import List, Dict, Any, Optional

# Add the parent directory to sys.path to import from models.py
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
from models import ExportSection

def parse_embedded_chart_data(sections: List[ExportSection]) -> List[Dict[str, Any]]:
    """Parse chart data embedded in section content using CHARTDATA markers"""
    charts = []
    
    for section in sections:
        if section.content:
            # Look for chart data markers - handle both formats
            chart_matches = re.findall(r'<!--CHART_DATA_START-->(.*?)<!--CHART_DATA_END-->', section.content, re.DOTALL)
            
            # Also check for alternative markers as fallback
            alt_chart_matches = re.findall(r'<!--CHARTDATASTART-->(.*?)<!--CHARTDATAEND-->', section.content, re.DOTALL)
            chart_matches.extend(alt_chart_matches)
            
            # logging.info(f"Section '{section.title}': Found {len(chart_matches)} chart data markers")
            
            for chart_data in chart_matches:
                try:
                    # Parse the chart data array
                    chart_array = eval(chart_data.strip())
                    # logging.info(f"Parsed chart array with {len(chart_array)} items")
                    # logging.info(f"Chart array type: {type(chart_array)}")
                    
                    # Each chart is an array with: [Section, ChartType, Title, Period, Data]
                    for chart_item in chart_array:
                        if len(chart_item) >= 5:
                            section_name = chart_item[0]
                            chart_type = chart_item[1]  # DC=Doughnut, BG=Bar Graph, etc.
                            chart_title = chart_item[2]
                            period = chart_item[3]
                            data = chart_item[4]
                            
                            chart_info = {
                                "section": section_name,
                                "type": chart_type,
                                "title": chart_title,
                                "period": period,
                                "data": data,
                                "source_section": section.title
                            }
                            
                            charts.append(chart_info)
                            # logging.info(f"Added chart: {chart_type} - {chart_title} for section {section_name}")
                        else:
                            # logging.warning(f"Chart item has insufficient data: {len(chart_item)} items, expected 5")
                            pass
                
                except Exception as e:
                    # logging.error(f"Failed to parse chart data for section '{section.title}': {str(e)}")
                    continue
            
            # Additional debugging: Check for any chart-related content
            if not chart_matches:
                # Look for any chart-related markers or text
                chart_indicators = re.findall(r'<!--.*?CHART.*?-->', section.content, re.DOTALL)
                if chart_indicators:
                    # logging.info(f"Section '{section.title}': Found chart indicators: {chart_indicators}")
                    pass
                
                # Check for general chart text
                if 'CHART' in section.content.upper() or 'chart' in section.content.lower():
                    # logging.info(f"Section '{section.title}': Contains chart-related text")
                    pass
    
    # logging.info(f"Total charts parsed: {len(charts)}")
    return charts

def create_enhanced_chart_style(chart_type, data, title, business_idea):
    """Create charts with enhanced professional styling"""
    # Set modern style
    plt.style.use('default')
    
    # Design palette from image
    colors = ['#6A2EAB', '#5A2E9B', '#C0C0C0', '#FF8C00', '#FFD700', '#696969', '#FF4500']
    
    if chart_type == "DC":  # Doughnut Chart
        fig, ax = plt.subplots(figsize=(14, 10))
        
        # Extract labels and values from data
        labels = [item[0] for item in data]
        values = [item[1] for item in data]
        
        # Create doughnut chart with enhanced styling
        wedges, texts, autotexts = ax.pie(values, labels=labels, colors=colors[:len(values)],
                                         autopct='%1.1f%%', startangle=90,
                                         wedgeprops=dict(width=0.6, edgecolor='white', linewidth=2),
                                         textprops={'fontsize': 18})
        
        # Enhanced title styling
        ax.set_title(title, fontsize=24, fontweight='bold', pad=20, color='#374151')
        
        # Add legend with enhanced styling
        legend = ax.legend(wedges, labels, title="Categories", 
                          loc="center left", bbox_to_anchor=(1, 0, 0.5, 1),
                          fontsize=16, title_fontsize=18)
        legend.get_title().set_color('#FF6B6B')
        
        # Add business name watermark
        fig.text(0.5, 0.02, f"Generated for: {business_idea}", 
                ha='center', va='bottom', fontsize=8, color='#9CA3AF', style='italic')
        
        plt.tight_layout()
        return fig
        
    elif chart_type == "BG":  # Bar Graph
        fig, ax = plt.subplots(figsize=(16, 10))
        
        # Extract labels and values
        labels = [item[0] for item in data]
        values = [item[1] for item in data]
        
        # Create bar chart with enhanced styling
        bars = ax.bar(labels, values, color=colors[:len(values)], 
                     edgecolor='white', linewidth=1, alpha=0.8)
        
        # Add value labels on bars
        for bar, value in zip(bars, values):
            height = bar.get_height()
            ax.text(bar.get_x() + bar.get_width()/2., height + max(values)*0.01,
                   f'{value}', ha='center', va='bottom', fontweight='bold', fontsize=18)
        
        # Enhanced styling
        ax.set_title(title, fontsize=24, fontweight='bold', pad=20, color='#374151')
        ax.set_xlabel('Categories', fontsize=18, color='#6B7280')
        ax.set_ylabel('Values', fontsize=18, color='#6B7280')
        
        # Rotate x-axis labels for better readability
        plt.setp(ax.get_xticklabels(), rotation=45, ha='right', fontsize=16)
        plt.setp(ax.get_yticklabels(), fontsize=16)
        
        # Add grid for better readability
        ax.yaxis.grid(True, alpha=0.3)
        ax.set_axisbelow(True)
        
        # Add business name watermark
        fig.text(0.5, 0.02, f"Generated for: {business_idea}", 
                ha='center', va='bottom', fontsize=8, color='#9CA3AF', style='italic')
        
        plt.tight_layout()
        return fig
        
    elif chart_type == "LG":  # Line Graph
        fig, ax = plt.subplots(figsize=(16, 10))
        
        # Extract data
        x_values = [item[0] for item in data]
        y_values = [item[1] for item in data]
        
        # Create line chart with enhanced styling
        ax.plot(x_values, y_values, color='#FF6B6B', linewidth=3, marker='o', 
               markersize=8, markerfacecolor='white', markeredgecolor='#FF6B6B')
        
        # Fill area under line
        ax.fill_between(x_values, y_values, alpha=0.3, color='#4ECDC4')
        
        # Enhanced styling
        ax.set_title(title, fontsize=24, fontweight='bold', pad=20, color='#374151')
        ax.set_xlabel('Time Period', fontsize=18, color='#6B7280')
        ax.set_ylabel('Values', fontsize=18, color='#6B7280')
        
        # Add grid
        ax.grid(True, alpha=0.3)
        ax.set_axisbelow(True)
        
        # Increase tick label font sizes
        plt.setp(ax.get_xticklabels(), fontsize=16)
        plt.setp(ax.get_yticklabels(), fontsize=16)
        
        # Add business name watermark
        fig.text(0.5, 0.02, f"Generated for: {business_idea}", 
                ha='center', va='bottom', fontsize=8, color='#9CA3AF', style='italic')
        
        plt.tight_layout()
        return fig
        
    elif chart_type == "PG":  # Pie Graph
        fig, ax = plt.subplots(figsize=(14, 10))
        
        # Extract labels and values
        labels = [item[0] for item in data]
        values = [item[1] for item in data]
        
        # Create pie chart with enhanced styling
        wedges, texts, autotexts = ax.pie(values, labels=labels, colors=colors[:len(values)],
                                         autopct='%1.1f%%', startangle=90,
                                         wedgeprops=dict(edgecolor='white', linewidth=2),
                                         textprops={'fontsize': 18})
        
        # Enhanced title styling
        ax.set_title(title, fontsize=24, fontweight='bold', pad=20, color='#374151')
        
        # Add business name watermark
        fig.text(0.5, 0.02, f"Generated for: {business_idea}", 
                ha='center', va='bottom', fontsize=8, color='#9CA3AF', style='italic')
        
        plt.tight_layout()
        return fig
    
    return None

def create_chart_from_data(chart_info: Dict[str, Any], business_idea: str) -> bytes:
    """Create a matplotlib chart from parsed chart data with enhanced styling"""
    try:
        chart_type = chart_info["type"]
        chart_title = chart_info["title"]
        data = chart_info["data"]
        
        # logging.info(f"Creating chart: {chart_type} - {chart_title} with {len(data)} data points")
        
        # Use enhanced styling function
        fig = create_enhanced_chart_style(chart_type, data, chart_title, business_idea)
        
        if fig is None:
            # Fallback to original method if enhanced styling fails
            if chart_type == "DC":  # Doughnut Chart
                fig, ax = plt.subplots(figsize=(14, 10))  # Larger size for better visibility
                
                # Extract labels and values from data
                labels = [item[0] for item in data]
                values = [item[1] for item in data]
                
                # High contrast color scheme
                colors = ['#6A2EAB', '#5A2E9B', '#C0C0C0', '#FF8C00', '#FFD700', '#696969', '#FF4500']
                
                # Create doughnut chart
                wedges, texts, autotexts = ax.pie(values, labels=labels, colors=colors[:len(values)],
                                                 autopct='%1.1f%%', startangle=90,
                                                 wedgeprops=dict(width=0.6, edgecolor='white', linewidth=2),
                                                 textprops={'fontsize': 18})
                
                # Enhanced title
                ax.set_title(chart_title, fontsize=22, fontweight='bold', pad=15, color='#374151')
                
                # Add legend
                ax.legend(wedges, labels, title="Categories", loc="center left", bbox_to_anchor=(1, 0, 0.5, 1), fontsize=16)
                
                plt.tight_layout()
            elif chart_type == "BG":  # Bar Graph
                fig, ax = plt.subplots(figsize=(16, 10))
                
                # Extract labels and values
                labels = [item[0] for item in data]
                values = [item[1] for item in data]
                
                # High contrast color scheme
                colors = ['#6A2EAB', '#5A2E9B', '#C0C0C0', '#FF8C00', '#FFD700', '#696969', '#FF4500']
                
                # Create bar chart
                bars = ax.bar(labels, values, color=colors[:len(values)], 
                             edgecolor='white', linewidth=1, alpha=0.8)
                
                # Add value labels on bars
                for bar, value in zip(bars, values):
                    height = bar.get_height()
                    ax.text(bar.get_x() + bar.get_width()/2., height + max(values)*0.01,
                           f'{value}', ha='center', va='bottom', fontweight='bold', fontsize=18)
                
                ax.set_title(chart_title, fontsize=22, fontweight='bold', pad=15, color='#374151')
                ax.set_xlabel('Categories', fontsize=18, color='#6B7280')
                ax.set_ylabel('Values', fontsize=18, color='#6B7280')
                
                # Rotate x-axis labels
                plt.setp(ax.get_xticklabels(), rotation=45, ha='right', fontsize=16)
                plt.setp(ax.get_yticklabels(), fontsize=16)
                
                # Add grid
                ax.yaxis.grid(True, alpha=0.3)
                ax.set_axisbelow(True)
                
                plt.tight_layout()
            elif chart_type == "LG":  # Line Graph
                fig, ax = plt.subplots(figsize=(16, 10))
                
                # Extract data
                x_values = [item[0] for item in data]
                y_values = [item[1] for item in data]
                
                # Create line chart
                ax.plot(x_values, y_values, color='#FF6B6B', linewidth=3, marker='o', 
                       markersize=8, markerfacecolor='white', markeredgecolor='#FF6B6B')
                
                # Fill area under line
                ax.fill_between(x_values, y_values, alpha=0.3, color='#4ECDC4')
                
                ax.set_title(chart_title, fontsize=22, fontweight='bold', pad=15, color='#374151')
                ax.set_xlabel('Time Period', fontsize=18, color='#6B7280')
                ax.set_ylabel('Values', fontsize=18, color='#6B7280')
                
                # Add grid
                ax.grid(True, alpha=0.3)
                ax.set_axisbelow(True)
                
                # Increase tick label font sizes
                plt.setp(ax.get_xticklabels(), fontsize=16)
                plt.setp(ax.get_yticklabels(), fontsize=16)
                
                plt.tight_layout()
            elif chart_type == "PG":  # Pie Graph
                fig, ax = plt.subplots(figsize=(14, 10))
                
                # Extract labels and values
                labels = [item[0] for item in data]
                values = [item[1] for item in data]
                
                # High contrast color scheme
                colors = ['#6A2EAB', '#5A2E9B', '#C0C0C0', '#FF8C00', '#FFD700', '#696969', '#FF4500']
                
                # Create pie chart
                wedges, texts, autotexts = ax.pie(values, labels=labels, colors=colors[:len(values)],
                                                 autopct='%1.1f%%', startangle=90,
                                                 wedgeprops=dict(edgecolor='white', linewidth=2),
                                                 textprops={'fontsize': 18})
                
                ax.set_title(chart_title, fontsize=22, fontweight='bold', pad=15, color='#374151')
                
                plt.tight_layout()
            else:
                # logging.warning(f"Unknown chart type: {chart_type}")
                return None
        
        # Save to bytes
        buffer = io.BytesIO()
        fig.savefig(buffer, format='png', dpi=300, bbox_inches='tight', 
                   facecolor='white', edgecolor='none')
        buffer.seek(0)
        chart_bytes = buffer.getvalue()
        buffer.close()
        
        # Close the figure to free memory
        plt.close(fig)
        
        return chart_bytes
        
    except Exception as e:
        # logging.error(f"Failed to create chart: {str(e)}")
        return None

def fix_base64_padding(base64_string: str) -> str:
    """Fix common base64 padding issues"""
    # Remove any whitespace or newlines
    base64_string = base64_string.strip()
    
    # Add padding if needed
    missing_padding = len(base64_string) % 4
    if missing_padding:
        base64_string += '=' * (4 - missing_padding)
    
    return base64_string

def robust_base64_decode(base64_string: str) -> Optional[bytes]:
    """Robustly decode base64 string with error handling and padding correction"""
    try:
        # First try direct decoding
        return base64.b64decode(base64_string)
    except Exception as e1:
        try:
            # Try with padding correction
            fixed_string = fix_base64_padding(base64_string)
            return base64.b64decode(fixed_string)
        except Exception as e2:
            try:
                # Try with URL-safe base64
                return base64.urlsafe_b64decode(fix_base64_padding(base64_string))
            except Exception as e3:
                try:
                    # Try with standard base64 ignoring padding
                    return base64.b64decode(base64_string + '==', validate=False)
                except Exception as e4:
                    return None