-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathvis.py
More file actions
442 lines (387 loc) · 11.6 KB
/
vis.py
File metadata and controls
442 lines (387 loc) · 11.6 KB
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
''' Visulization Functions '''
import plotly.graph_objects as go
def bar_chart(data, granularity, feature):
''' Displays Sales Trends over Time '''
fig = go.Figure(
data=[
go.Bar(
x=data.index.start_time.astype(str),
y=data.values,
text=data.values,
textposition='auto'
)
]
)
if feature == 'total_amount':
yaxis_title = 'Total Sales Amount'
elif feature == 'transaction_id':
yaxis_title = 'Number of Transactions'
elif feature == 'transaction_qty':
yaxis_title = 'Total Quantity Sold'
else:
yaxis_title = 'Total Amount/Qty'
fig.update_layout(
title_text = f'{yaxis_title}: {granularity}',
xaxis_title = f'{granularity}',
yaxis_title = yaxis_title,
template = 'plotly_dark' ,
height = 700
)
return fig
def line_chart(data, granularity, feature):
''' Displays Moving Average '''
if granularity == 'Daily':
window = 7
frequency = 'Days'
elif granularity == 'Weekly':
window = 4
frequency = 'Weeks'
elif granularity == 'Monthly':
window = 2
frequency = 'Months'
else:
window = 2
frequency = 'Months'
moving_average = data.rolling(window=window).mean()
if feature == 'total_amount':
yaxis_title = 'Total Sales Amount'
elif feature == 'transaction_id':
yaxis_title = 'Number of Transactions'
elif feature == 'transaction_qty':
yaxis_title = 'Total Quantity Sold'
else:
yaxis_title = 'Total Amount/Qty'
fig = go.Figure()
fig.add_trace(
go.Scatter(
x = data.index.start_time.astype(str),
y = data.values,
mode='lines',
name='Actual Data'
)
)
fig.add_trace(
go.Scatter(
x = moving_average.index.start_time.astype(str),
y = moving_average.values,
mode='lines',
name=f'{window}-{frequency} Moving Average'
)
)
fig.update_layout(
title_text = f'Growth of {yaxis_title} Over {window}-{frequency}',
xaxis_title = f'{granularity}',
yaxis_title = f'{yaxis_title}',
template = 'plotly_dark',
legend=dict(
orientation='h',
yanchor='bottom',
y=1.02,
xanchor='right',
x=1
),
height = 350
)
return fig
def growth_chart(data, granularity, feature):
''' Displays Growth Rate by Granularity '''
growth = data.pct_change() * 100
growth = growth.round(2)
if granularity == 'Daily':
frequency = 'Day'
elif granularity == 'Weekly':
frequency = 'Week'
elif granularity == 'Monthly':
frequency = 'Month'
else:
frequency = 'Month'
fig = go.Figure(
data=[
go.Bar(
x=growth.index.start_time.astype(str),
y=growth.values,
text=growth.values,
textposition='auto'
)
]
)
if feature == 'total_amount':
yaxis_title = 'Total Sales Amount'
elif feature == 'transaction_id':
yaxis_title = 'Number of Transactions'
elif feature == 'transaction_qty':
yaxis_title = 'Total Quantity Sold'
else:
yaxis_title = "Total Amount/Qty"
fig.update_layout(
title_text = f'Growth Rate of {yaxis_title} per {frequency}',
xaxis_title = f'{granularity}',
yaxis_title = f'{yaxis_title}',
height = 350
)
return fig
def avg_sales_by_week(data, feature):
''' Displays Average Sales by Week '''
if feature == 'Sales':
graph_title = 'Average Sales by Weekday',
graph_yaxis = 'Revenue ($)'
elif feature == 'Transactions':
graph_title = 'Average Number of Transaction by Weekday',
graph_yaxis = 'No. of Transactions'
else:
graph_title = 'Average Sales/Transactions by Weekday',
graph_yaxis = 'Transactions/Revenue'
fig = go.Figure()
fig.add_trace(
go.Bar(
x=data.index,
y=data.values
)
)
fig.update_layout(
title=str(graph_title[0]),
xaxis_title='Weekday',
yaxis_title=graph_yaxis,
height=700
)
return fig
def avg_sales_by_hourofday(data, feature):
''' Displays Averages Sales by Hour of the Day '''
if feature == 'Sales':
graph_title = 'Average Sales by Hour of Day'
graph_yaxis = 'Average Sales'
elif feature == 'Transactions':
graph_title = 'Average Transactions by Hour of Day'
graph_yaxis = 'Number of Transactions'
else:
graph_title = 'Average Sales/Transactions'
graph_yaxis = 'Transactions/Revenue'
fig = go.Figure()
fig.add_trace(
go.Scatter(
x=data.index,
y=data.values,
mode='lines+markers',
fill = 'tozeroy',
fillcolor='rgba(0, 176, 246, 0.2)'
)
)
fig.update_layout(
title=str(graph_title),
xaxis_title='Hour of Day',
yaxis_title=graph_yaxis,
height=300
)
return fig
def avg_sales_by_hourofday_product(data, feature):
''' Displays Average Sales by Hour of the Day (Product-wise) '''
fig = go.Figure()
if feature == 'Sales':
graph_title = 'Average Sales by Hour of Day (Product Category)'
graph_yaxis = 'Revenue ($)'
for category in data['product_category'].unique():
cat_df = data[data['product_category'] == category]
hourly_sales = cat_df.groupby(
[cat_df['transaction_date'].dt.date, 'transaction_hour']
)['total_amount'].sum().reset_index()
average_hourly_sales = hourly_sales.groupby('transaction_hour')['total_amount'].mean()
fig.add_trace(
go.Scatter(
x=average_hourly_sales.index,
y=average_hourly_sales.values,
mode='lines',
name=category
)
)
elif feature == 'Transactions':
graph_title = 'Average Number of Transactions by Hour of Day (Product Category)'
graph_yaxis = 'Number of Transactions'
for category in data['product_category'].unique():
cat_df = data[data['product_category'] == category]
hourly_transactions = cat_df.groupby(
[cat_df['transaction_date'].dt.date, 'transaction_hour']
).size().reset_index(name='total_transactions')
average_hourly_transactions = hourly_transactions.groupby(
'transaction_hour'
)['total_transactions'].mean()
fig.add_trace(
go.Scatter(
x=average_hourly_transactions.index,
y=average_hourly_transactions.values,
mode='lines',
name=category
)
)
else:
graph_title = 'Average Sales/Transactions by Hour of Day'
graph_yaxis = 'Sales/Transactions'
fig.update_layout(
title=graph_title,
xaxis_title='Hour of Day',
yaxis_title=graph_yaxis,
showlegend=True,
height=400
)
return fig
def top_selling_product(data):
''' Displays Top Selling Product '''
fig = go.Figure()
fig.add_trace(
go.Bar(
x=data.index.astype(str),
y=data.values
)
)
fig.update_layout(
title='Product Category Distribution',
xaxis_title='Product Category',
yaxis_title='Total Sales',
height=800
)
return fig
def category_breakdown(data, category):
''' Displays Product Category Breakdown '''
fig = go.Figure()
fig.add_trace(
go.Pie(
values=data[category].values,
labels=data[category].index.astype(str).tolist(),
textinfo='percent+label',
hole=0.3
)
)
fig.update_layout(
title="Product Type Sales Distribution",
legend=dict(
orientation="h",
yanchor="bottom",
y=-0.4,
xanchor="center",
x=0.5
),
height=400
)
return fig
def store_sales_trend(store1_sale, store2_sale, store3_sale, feature):
''' Displays Store Sales Trend '''
if feature == 'Sales':
graph_title = 'Store Sales Comparsion (Revenue)'
graph_yaxis = 'Total Sales'
elif feature == 'Transactions':
graph_title = 'Store Sales Comparsion (Transactions)'
graph_yaxis = 'Total Transactions'
else:
graph_title = 'Store Sales Comparsion'
graph_yaxis = 'Total Sales/Transactions'
fig = go.Figure()
fig.add_trace(
go.Scatter(
x=store1_sale.index.start_time.astype(str),
y=store1_sale.values,
mode='lines',
name='Lower Manhattan'
)
)
fig.add_trace(
go.Scatter(
x=store2_sale.index.start_time.astype(str),
y=store2_sale.values,
mode='lines',
name="Hell's Kitchen"
)
)
fig.add_trace(
go.Scatter(
x=store3_sale.index.start_time.astype(str),
y=store3_sale.values,
mode='lines',
name='Astoria'
)
)
fig.update_layout(
title=graph_title,
xaxis_title='Week',
yaxis_title=graph_yaxis,
legend=dict(
x=0.5,
y=1.1,
orientation='h',
xanchor='center'
),
height=475
)
return fig
def store_sale_distribution(sale_data, label, feature):
''' Displays Store Sales Distribution '''
if feature == 'Sales':
graph_title = 'Store Sales Distribution (Revenue)'
elif feature == 'Transactions':
graph_title = 'Store Sales Distribution (Transactions)'
else:
graph_title = 'Store Sales Distribution'
fig = go.Figure()
fig.add_trace(
go.Pie(
labels=label,
values=sale_data,
hole=0.3,
textinfo='percent+label'
)
)
fig.update_layout(
title=graph_title,
legend=dict(
orientation="h",
yanchor="bottom",
y=-0.2,
xanchor="center",
x=0.5
),
height=475
)
return fig
def growth_rate(store1_growth, store2_growth, store3_growth, feature):
''' Displays Grown Rate of 3 Stores '''
if feature == 'Sales':
graph_title = 'Weekly Growth Rates by Store Location (Revenue)'
elif feature == 'Transactions':
graph_title = 'Weekly Growth Rates by Store Location (Number of Transactions)'
else:
graph_title = 'Weekly Growth Rates by Store Location'
fig = go.Figure()
fig.add_trace(
go.Bar(
x=store1_growth.index.start_time.astype(str),
y=store1_growth.values,
name="Lower Manhattan"
)
)
fig.add_trace(
go.Bar(
x=store2_growth.index.start_time.astype(str),
y=store2_growth.values,
name="Hell's Kitchen"
)
)
fig.add_trace(
go.Bar(
x=store3_growth.index.start_time.astype(str),
y=store3_growth.values,
name="Astoria"
)
)
fig.update_layout(
title=graph_title,
xaxis_title="Week",
yaxis_title="Growth Rate (%)",
barmode="group",
legend=dict(
title="Store Location",
orientation="h",
yanchor="bottom",
y=1.02,
xanchor="center",
x=0.5
)
)
return fig