parthsinha commited on
Commit
5af4193
·
verified ·
1 Parent(s): 16af6e4

Update visualizations.py

Browse files
Files changed (1) hide show
  1. visualizations.py +38 -48
visualizations.py CHANGED
@@ -9,14 +9,13 @@ from data_processor import DataProcessor
9
  def create_visualizations(data_processor: DataProcessor) -> Dict[str, Any]:
10
  """
11
  Create all visualizations for the Fetii dashboard.
12
- Compatible with both Streamlit and Gradio interfaces.
13
  """
14
  insights = data_processor.get_quick_insights()
15
  df = data_processor.df
16
 
17
  visualizations = {}
18
 
19
- # Core visualizations - optimized for Gradio display
20
  visualizations['hourly_distribution'] = create_hourly_chart(insights['hourly_distribution'])
21
  visualizations['group_size_distribution'] = create_group_size_chart(insights['group_size_distribution'])
22
  visualizations['popular_locations'] = create_locations_chart(insights['top_pickups'])
@@ -49,21 +48,21 @@ def create_hourly_chart(hourly_data: Dict[int, int]) -> go.Figure:
49
 
50
  fig = go.Figure()
51
 
52
- # Create modern gradient colors based on intensity
53
  max_count = max(counts)
54
  colors = []
55
  for count in counts:
56
  intensity = count / max_count
57
  if intensity > 0.8:
58
- colors.append('#667eea') # Primary gradient start
59
  elif intensity > 0.6:
60
- colors.append('#764ba2') # Primary gradient end
61
  elif intensity > 0.4:
62
- colors.append('#f093fb') # Secondary gradient start
63
  elif intensity > 0.2:
64
- colors.append('#4facfe') # Success gradient
65
  else:
66
- colors.append('#9ca3af') # Gray for low activity
67
 
68
  fig.add_trace(go.Bar(
69
  x=hour_labels,
@@ -83,15 +82,15 @@ def create_hourly_chart(hourly_data: Dict[int, int]) -> go.Figure:
83
  title={
84
  'text': 'Trip Distribution by Hour',
85
  'x': 0.5,
86
- 'font': {'size': 16, 'color': '#1f2937', 'family': 'Inter'}
87
  },
88
  xaxis_title='Hour of Day',
89
  yaxis_title='Number of Trips',
90
  plot_bgcolor='rgba(0,0,0,0)',
91
  paper_bgcolor='rgba(0,0,0,0)',
92
  font={'color': '#374151', 'family': 'Inter'},
93
- height=280,
94
- margin=dict(t=50, b=40, l=40, r=40),
95
  xaxis=dict(
96
  showgrid=True,
97
  gridwidth=1,
@@ -115,11 +114,11 @@ def create_group_size_chart(group_data: Dict[int, int]) -> go.Figure:
115
  sizes = list(group_data.keys())
116
  counts = list(group_data.values())
117
 
118
- # Enhanced modern color palette with gradients
119
  colors = [
120
- '#667eea', '#764ba2', '#f093fb', '#f5576c',
121
- '#4facfe', '#00f2fe', '#43e97b', '#38f9d7',
122
- '#fa709a', '#fee140', '#a8edea', '#fed6e3'
123
  ]
124
 
125
  fig = go.Figure()
@@ -134,22 +133,30 @@ def create_group_size_chart(group_data: Dict[int, int]) -> go.Figure:
134
  hovertemplate='<b>%{label}</b><br>Trips: %{value}<br>Percentage: %{percent}<extra></extra>',
135
  textinfo='label+percent',
136
  textposition='auto',
137
- textfont=dict(color='white', size=11, family='Inter'),
138
- hole=0.4
139
  ))
140
 
141
  fig.update_layout(
142
  title={
143
  'text': 'Group Size Distribution',
144
  'x': 0.5,
145
- 'font': {'size': 16, 'color': '#1f2937', 'family': 'Inter'}
146
  },
147
  plot_bgcolor='rgba(0,0,0,0)',
148
  paper_bgcolor='rgba(0,0,0,0)',
149
  font={'color': '#374151', 'family': 'Inter'},
150
- height=280,
151
- margin=dict(t=50, b=40, l=40, r=40),
152
- showlegend=False
 
 
 
 
 
 
 
 
153
  )
154
 
155
  return fig
@@ -169,17 +176,12 @@ def create_locations_chart(pickup_data: list) -> go.Figure:
169
 
170
  fig = go.Figure()
171
 
172
- # Enhanced gradient colors with modern palette
173
  max_count = max(counts)
174
- base_colors = ['#667eea', '#764ba2', '#f093fb', '#f5576c', '#4facfe', '#00f2fe', '#43e97b', '#38f9d7']
175
  colors = []
176
- for i, count in enumerate(counts):
177
- base_color = base_colors[i % len(base_colors)]
178
- # Convert hex to rgba with opacity based on intensity
179
- hex_color = base_color.lstrip('#')
180
- rgb = tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4))
181
  intensity = count / max_count
182
- colors.append(f'rgba({rgb[0]}, {rgb[1]}, {rgb[2]}, {0.6 + intensity * 0.4})')
183
 
184
  fig.add_trace(go.Bar(
185
  x=counts,
@@ -201,15 +203,15 @@ def create_locations_chart(pickup_data: list) -> go.Figure:
201
  title={
202
  'text': 'Top Pickup Locations',
203
  'x': 0.5,
204
- 'font': {'size': 16, 'color': '#1f2937', 'family': 'Inter'}
205
  },
206
  xaxis_title='Number of Pickups',
207
  yaxis_title='',
208
  plot_bgcolor='rgba(0,0,0,0)',
209
  paper_bgcolor='rgba(0,0,0,0)',
210
  font={'color': '#374151', 'family': 'Inter'},
211
- height=280,
212
- margin=dict(t=50, b=40, l=120, r=40),
213
  yaxis=dict(
214
  autorange="reversed",
215
  showline=True,
@@ -273,27 +275,15 @@ def create_time_heatmap(df: pd.DataFrame) -> go.Figure:
273
  title={
274
  'text': 'Trip Patterns by Day & Hour',
275
  'x': 0.5,
276
- 'font': {'size': 16, 'color': '#1f2937', 'family': 'Inter', 'weight': 700}
277
  },
278
  xaxis_title='Hour of Day',
279
  yaxis_title='Day of Week',
280
- plot_bgcolor='rgba(248, 250, 252, 0.5)',
281
  paper_bgcolor='rgba(0,0,0,0)',
282
  font={'color': '#374151', 'family': 'Inter'},
283
- height=350,
284
- margin=dict(t=50, b=40, l=100, r=40),
285
- xaxis=dict(
286
- showgrid=True,
287
- gridwidth=1,
288
- gridcolor='rgba(156, 163, 175, 0.3)',
289
- tickfont=dict(size=11)
290
- ),
291
- yaxis=dict(
292
- showgrid=True,
293
- gridwidth=1,
294
- gridcolor='rgba(156, 163, 175, 0.3)',
295
- tickfont=dict(size=11)
296
- )
297
  )
298
 
299
  return fig
 
9
  def create_visualizations(data_processor: DataProcessor) -> Dict[str, Any]:
10
  """
11
  Create all visualizations for the Fetii dashboard.
 
12
  """
13
  insights = data_processor.get_quick_insights()
14
  df = data_processor.df
15
 
16
  visualizations = {}
17
 
18
+ # Core visualizations
19
  visualizations['hourly_distribution'] = create_hourly_chart(insights['hourly_distribution'])
20
  visualizations['group_size_distribution'] = create_group_size_chart(insights['group_size_distribution'])
21
  visualizations['popular_locations'] = create_locations_chart(insights['top_pickups'])
 
48
 
49
  fig = go.Figure()
50
 
51
+ # Create gradient colors based on intensity
52
  max_count = max(counts)
53
  colors = []
54
  for count in counts:
55
  intensity = count / max_count
56
  if intensity > 0.8:
57
+ colors.append('#dc2626') # Red for peak
58
  elif intensity > 0.6:
59
+ colors.append('#ea580c') # Orange-red
60
  elif intensity > 0.4:
61
+ colors.append('#d97706') # Orange
62
  elif intensity > 0.2:
63
+ colors.append('#3b82f6') # Blue
64
  else:
65
+ colors.append('#6b7280') # Gray for low activity
66
 
67
  fig.add_trace(go.Bar(
68
  x=hour_labels,
 
82
  title={
83
  'text': 'Trip Distribution by Hour',
84
  'x': 0.5,
85
+ 'font': {'size': 18, 'color': '#1f2937', 'family': 'Inter'}
86
  },
87
  xaxis_title='Hour of Day',
88
  yaxis_title='Number of Trips',
89
  plot_bgcolor='rgba(0,0,0,0)',
90
  paper_bgcolor='rgba(0,0,0,0)',
91
  font={'color': '#374151', 'family': 'Inter'},
92
+ height=320,
93
+ margin=dict(t=60, b=50, l=50, r=50),
94
  xaxis=dict(
95
  showgrid=True,
96
  gridwidth=1,
 
114
  sizes = list(group_data.keys())
115
  counts = list(group_data.values())
116
 
117
+ # Modern color palette
118
  colors = [
119
+ '#3b82f6', '#10b981', '#f59e0b', '#ef4444',
120
+ '#8b5cf6', '#06b6d4', '#84cc16', '#f97316',
121
+ '#ec4899', '#6366f1', '#14b8a6', '#eab308'
122
  ]
123
 
124
  fig = go.Figure()
 
133
  hovertemplate='<b>%{label}</b><br>Trips: %{value}<br>Percentage: %{percent}<extra></extra>',
134
  textinfo='label+percent',
135
  textposition='auto',
136
+ textfont=dict(color='white', size=14, family='Inter'),
137
+ hole=0.3 # Reduced hole size to make chart appear larger
138
  ))
139
 
140
  fig.update_layout(
141
  title={
142
  'text': 'Group Size Distribution',
143
  'x': 0.5,
144
+ 'font': {'size': 18, 'color': '#1f2937', 'family': 'Inter'}
145
  },
146
  plot_bgcolor='rgba(0,0,0,0)',
147
  paper_bgcolor='rgba(0,0,0,0)',
148
  font={'color': '#374151', 'family': 'Inter'},
149
+ height=500, # Increased height from 320 to 500
150
+ width=500, # Added explicit width
151
+ margin=dict(t=60, b=50, l=50, r=50),
152
+ showlegend=True, # Show legend to make it more informative
153
+ legend=dict(
154
+ orientation="v",
155
+ yanchor="middle",
156
+ y=0.5,
157
+ xanchor="left",
158
+ x=1.05
159
+ )
160
  )
161
 
162
  return fig
 
176
 
177
  fig = go.Figure()
178
 
179
+ # Gradient colors
180
  max_count = max(counts)
 
181
  colors = []
182
+ for count in counts:
 
 
 
 
183
  intensity = count / max_count
184
+ colors.append(f'rgba(59, 130, 246, {0.4 + intensity * 0.6})')
185
 
186
  fig.add_trace(go.Bar(
187
  x=counts,
 
203
  title={
204
  'text': 'Top Pickup Locations',
205
  'x': 0.5,
206
+ 'font': {'size': 18, 'color': '#1f2937', 'family': 'Inter'}
207
  },
208
  xaxis_title='Number of Pickups',
209
  yaxis_title='',
210
  plot_bgcolor='rgba(0,0,0,0)',
211
  paper_bgcolor='rgba(0,0,0,0)',
212
  font={'color': '#374151', 'family': 'Inter'},
213
+ height=320,
214
+ margin=dict(t=60, b=50, l=140, r=50),
215
  yaxis=dict(
216
  autorange="reversed",
217
  showline=True,
 
275
  title={
276
  'text': 'Trip Patterns by Day & Hour',
277
  'x': 0.5,
278
+ 'font': {'size': 18, 'color': '#1f2937', 'family': 'Inter'}
279
  },
280
  xaxis_title='Hour of Day',
281
  yaxis_title='Day of Week',
282
+ plot_bgcolor='rgba(0,0,0,0)',
283
  paper_bgcolor='rgba(0,0,0,0)',
284
  font={'color': '#374151', 'family': 'Inter'},
285
+ height=400,
286
+ margin=dict(t=60, b=50, l=100, r=50)
 
 
 
 
 
 
 
 
 
 
 
 
287
  )
288
 
289
  return fig