sammy786 commited on
Commit
681a703
Β·
verified Β·
1 Parent(s): 282fb81

Update utils/formatters.py

Browse files
Files changed (1) hide show
  1. utils/formatters.py +71 -25
utils/formatters.py CHANGED
@@ -488,55 +488,101 @@ def create_trend_line_chart(data: Dict) -> go.Figure:
488
  def create_card_performance_chart(data: Dict) -> go.Figure:
489
  """Create card performance comparison chart"""
490
  try:
491
- rewards_by_card = data.get('rewards_by_card', {})
 
 
 
 
 
 
492
 
493
- if not rewards_by_card:
494
- # Create sample data
495
- rewards_by_card = {
496
- 'Amex Gold': 95.50,
497
- 'Chase Sapphire Reserve': 62.00,
498
- 'Citi Double Cash': 30.00
499
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
500
 
501
- # Sort by rewards
502
- sorted_cards = sorted(rewards_by_card.items(), key=lambda x: x[0], reverse=True)
503
- cards = [c[0] for c in sorted_cards]
504
- rewards = [c[0] for c in sorted_cards]
505
 
506
- # Color gradient
507
- colors = ['#667eea', '#8b9cf7', '#b0bbff'][:len(cards)]
 
 
 
 
508
 
509
  fig = go.Figure(data=[go.Bar(
510
- x=cards,
511
- y=rewards,
 
512
  marker=dict(
513
  color=colors,
514
  line=dict(color='white', width=2)
515
  ),
516
  text=[f'${r:.2f}' for r in rewards],
517
  textposition='outside',
518
- hovertemplate='<b>%{x}</b><br>Rewards: $%{y:.2f}<extra></extra>'
519
  )])
520
 
521
  fig.update_layout(
522
- title='Top Performing Cards',
523
- xaxis_title='Credit Card',
524
- yaxis_title='Total Rewards ($)',
 
 
 
 
 
525
  template='plotly_white',
526
  height=400,
527
- showlegend=False
 
 
 
 
 
 
 
 
528
  )
529
 
530
  return fig
531
 
532
  except Exception as e:
533
- print(f"Error creating performance chart: {e}")
 
 
 
534
  fig = go.Figure()
535
  fig.add_annotation(
536
- text="Chart data unavailable",
537
  xref="paper", yref="paper",
538
  x=0.5, y=0.5, showarrow=False,
539
  font=dict(size=14, color="#666")
540
  )
541
- fig.update_layout(height=400, template='plotly_white')
542
- return fig
 
 
 
 
 
488
  def create_card_performance_chart(data: Dict) -> go.Figure:
489
  """Create card performance comparison chart"""
490
  try:
491
+ # Try multiple possible data keys
492
+ rewards_by_card = (
493
+ data.get('rewards_by_card') or
494
+ data.get('card_performance') or
495
+ data.get('top_cards') or
496
+ {}
497
+ )
498
 
499
+ print(f"πŸ” DEBUG - rewards_by_card type: {type(rewards_by_card)}")
500
+ print(f"πŸ” DEBUG - rewards_by_card content: {rewards_by_card}")
501
+
502
+ # Handle different data formats
503
+ if isinstance(rewards_by_card, list):
504
+ # If it's a list of dicts like [{"card": "Amex", "rewards": 95.50}, ...]
505
+ cards = [item.get('card', item.get('card_name', 'Unknown')) for item in rewards_by_card]
506
+ rewards = [float(item.get('rewards', item.get('reward_amount', 0))) for item in rewards_by_card]
507
+ elif isinstance(rewards_by_card, dict):
508
+ # If it's a dict like {"Amex Gold": 95.50, "Chase": 62.00}
509
+ cards = list(rewards_by_card.keys())
510
+ rewards = [float(v) for v in rewards_by_card.values()]
511
+ else:
512
+ # Fallback to empty
513
+ cards = []
514
+ rewards = []
515
+
516
+ # If still no data, create sample data
517
+ if not cards or sum(rewards) == 0:
518
+ print("⚠️ No card performance data, using sample data")
519
+ cards = ['Amex Gold', 'Chase Sapphire Reserve', 'Citi Double Cash']
520
+ rewards = [95.50, 62.00, 30.00]
521
 
522
+ # Sort by rewards (highest first)
523
+ sorted_pairs = sorted(zip(cards, rewards), key=lambda x: x[0], reverse=True)
524
+ cards = [p[0] for p in sorted_pairs]
525
+ rewards = [p[0] for p in sorted_pairs]
526
 
527
+ # Take top 5 only
528
+ cards = cards[:5]
529
+ rewards = rewards[:5]
530
+
531
+ # Create color gradient (darker = better performance)
532
+ colors = ['#667eea', '#7d8ef2', '#949ef9', '#abaeff', '#c2beff'][:len(cards)]
533
 
534
  fig = go.Figure(data=[go.Bar(
535
+ y=cards, # Horizontal bar chart
536
+ x=rewards,
537
+ orientation='h',
538
  marker=dict(
539
  color=colors,
540
  line=dict(color='white', width=2)
541
  ),
542
  text=[f'${r:.2f}' for r in rewards],
543
  textposition='outside',
544
+ hovertemplate='<b>%{y}</b><br>Total Rewards: $%{x:.2f}<extra></extra>'
545
  )])
546
 
547
  fig.update_layout(
548
+ title={
549
+ 'text': 'Top Performing Cards',
550
+ 'x': 0.5,
551
+ 'xanchor': 'center',
552
+ 'font': {'size': 18, 'color': '#333'}
553
+ },
554
+ xaxis_title='Total Rewards Earned ($)',
555
+ yaxis_title='',
556
  template='plotly_white',
557
  height=400,
558
+ showlegend=False,
559
+ margin=dict(l=150, r=50, t=60, b=50), # More space for card names
560
+ xaxis=dict(
561
+ gridcolor='#f0f0f0',
562
+ showgrid=True
563
+ ),
564
+ yaxis=dict(
565
+ autorange='reversed' # Best card at top
566
+ )
567
  )
568
 
569
  return fig
570
 
571
  except Exception as e:
572
+ print(f"❌ Error creating card performance chart: {e}")
573
+ print(f"πŸ“‹ Traceback: {traceback.format_exc()}")
574
+
575
+ # Return empty chart with error message
576
  fig = go.Figure()
577
  fig.add_annotation(
578
+ text=f"Chart data unavailable<br><sub>{str(e)}</sub>",
579
  xref="paper", yref="paper",
580
  x=0.5, y=0.5, showarrow=False,
581
  font=dict(size=14, color="#666")
582
  )
583
+ fig.update_layout(
584
+ height=400,
585
+ template='plotly_white',
586
+ title='Top Performing Cards'
587
+ )
588
+ return fig