-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreport_visualizer.py
More file actions
497 lines (412 loc) · 14.9 KB
/
report_visualizer.py
File metadata and controls
497 lines (412 loc) · 14.9 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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
from pathlib import Path
from collections import defaultdict
from datetime import datetime
import csv
import html
import re
# --------------------------------------------------
# OPTIONAL PDF SUPPORT
# --------------------------------------------------
try:
from reportlab.lib.pagesizes import A4
from reportlab.lib.units import mm
from reportlab.pdfgen import canvas
REPORTLAB_AVAILABLE = True
except ImportError:
REPORTLAB_AVAILABLE = False
# --------------------------------------------------
# HELPERS
# --------------------------------------------------
def ensure_folder(folder_path: Path) -> None:
folder_path.mkdir(parents=True, exist_ok=True)
def clean_text(value) -> str:
if value is None:
return ""
return str(value).strip()
def parse_float(value) -> float:
text = clean_text(value).replace(",", "")
if text == "":
return 0.0
return float(text)
def safe_filename(name: str) -> str:
return re.sub(r"[^a-zA-Z0-9_-]+", "_", name).strip("_").lower()
# --------------------------------------------------
# LOAD CLEANED CSV
# --------------------------------------------------
def load_cleaned_sales_data(csv_file: Path) -> list[dict]:
if not csv_file.exists():
raise FileNotFoundError(
f"Cleaned CSV not found: {csv_file}\n"
"Run automation.py first."
)
rows = []
with csv_file.open("r", encoding="utf-8-sig", newline="") as f:
reader = csv.DictReader(f)
for raw_row in reader:
try:
row = {
"order_id": clean_text(raw_row.get("order_id")),
"order_date": clean_text(raw_row.get("order_date")),
"customer_name": clean_text(raw_row.get("customer_name")),
"product": clean_text(raw_row.get("product")),
"category": clean_text(raw_row.get("category")),
"quantity": parse_float(raw_row.get("quantity")),
"unit_price": parse_float(raw_row.get("unit_price")),
"city": clean_text(raw_row.get("city")),
"revenue": parse_float(raw_row.get("revenue")),
"month": clean_text(raw_row.get("month")),
}
rows.append(row)
except ValueError:
continue
if not rows:
raise ValueError("Cleaned CSV exists but contains no valid rows.")
return rows
# --------------------------------------------------
# SUMMARY
# --------------------------------------------------
def generate_summary(rows: list[dict]) -> dict:
total_orders = len({row["order_id"] for row in rows})
total_units_sold = sum(row["quantity"] for row in rows)
total_revenue = sum(row["revenue"] for row in rows)
revenue_by_order = defaultdict(float)
revenue_by_product = defaultdict(float)
revenue_by_category = defaultdict(float)
revenue_by_city = defaultdict(float)
revenue_by_month = defaultdict(float)
for row in rows:
revenue_by_order[row["order_id"]] += row["revenue"]
revenue_by_product[row["product"]] += row["revenue"]
revenue_by_category[row["category"]] += row["revenue"]
revenue_by_city[row["city"]] += row["revenue"]
revenue_by_month[row["month"]] += row["revenue"]
average_order_value = (
sum(revenue_by_order.values()) / len(revenue_by_order)
if revenue_by_order else 0.0
)
top_products = sorted(
revenue_by_product.items(),
key=lambda item: item[1],
reverse=True
)[:5]
top_categories = sorted(
revenue_by_category.items(),
key=lambda item: item[1],
reverse=True
)
sales_by_city = sorted(
revenue_by_city.items(),
key=lambda item: item[1],
reverse=True
)
monthly_sales = sorted(
revenue_by_month.items(),
key=lambda item: item[0]
)
return {
"total_orders": total_orders,
"total_units_sold": total_units_sold,
"total_revenue": total_revenue,
"average_order_value": average_order_value,
"top_products": top_products,
"top_categories": top_categories,
"sales_by_city": sales_by_city,
"monthly_sales": monthly_sales,
}
# --------------------------------------------------
# SIMPLE SVG BAR CHART GENERATOR
# --------------------------------------------------
def create_svg_bar_chart(
title: str,
items: list[tuple[str, float]],
output_file: Path,
width: int = 900,
bar_height: int = 38,
left_margin: int = 220,
right_margin: int = 120,
top_margin: int = 70,
bottom_margin: int = 40,
) -> None:
if not items:
output_file.write_text("<svg xmlns='http://www.w3.org/2000/svg'></svg>", encoding="utf-8")
return
max_value = max(value for _, value in items) if items else 1
chart_width = width - left_margin - right_margin
height = top_margin + bottom_margin + len(items) * (bar_height + 12)
svg_lines = [
f"<svg xmlns='http://www.w3.org/2000/svg' width='{width}' height='{height}' viewBox='0 0 {width} {height}'>",
"<rect width='100%' height='100%' fill='white'/>",
f"<text x='{left_margin}' y='35' font-size='24' font-family='Arial' font-weight='bold'>{html.escape(title)}</text>",
]
y = top_margin
for label, value in items:
bar_width = 0
if max_value > 0:
bar_width = (value / max_value) * chart_width
safe_label = html.escape(str(label))
safe_value = html.escape(f"{value:,.2f}")
svg_lines.append(
f"<text x='20' y='{y + 24}' font-size='15' font-family='Arial'>{safe_label}</text>"
)
svg_lines.append(
f"<rect x='{left_margin}' y='{y}' width='{bar_width:.2f}' height='{bar_height}' rx='6' ry='6' fill='#4F81BD'/>"
)
svg_lines.append(
f"<text x='{left_margin + bar_width + 10:.2f}' y='{y + 24}' font-size='15' font-family='Arial'>{safe_value}</text>"
)
y += bar_height + 12
svg_lines.append("</svg>")
output_file.write_text("\n".join(svg_lines), encoding="utf-8")
# --------------------------------------------------
# HTML REPORT
# --------------------------------------------------
def build_html_report(summary: dict, output_file: Path) -> None:
top_products_html = "\n".join(
f"<li><strong>{html.escape(name)}</strong>: {value:,.2f}</li>"
for name, value in summary["top_products"]
)
category_html = "\n".join(
f"<li><strong>{html.escape(name)}</strong>: {value:,.2f}</li>"
for name, value in summary["top_categories"]
)
city_html = "\n".join(
f"<li><strong>{html.escape(name)}</strong>: {value:,.2f}</li>"
for name, value in summary["sales_by_city"]
)
month_html = "\n".join(
f"<li><strong>{html.escape(name)}</strong>: {value:,.2f}</li>"
for name, value in summary["monthly_sales"]
)
html_content = f"""<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Sales Visual Report</title>
<style>
body {{
font-family: Arial, sans-serif;
margin: 40px;
color: #222;
background: #fafafa;
}}
.container {{
max-width: 1100px;
margin: auto;
background: white;
padding: 32px;
border-radius: 14px;
box-shadow: 0 4px 18px rgba(0,0,0,0.08);
}}
h1 {{
margin-bottom: 10px;
}}
h2 {{
margin-top: 36px;
border-bottom: 2px solid #e5e7eb;
padding-bottom: 8px;
}}
.metrics {{
display: grid;
grid-template-columns: repeat(2, minmax(220px, 1fr));
gap: 16px;
margin: 24px 0 20px 0;
}}
.card {{
background: #f4f7fb;
padding: 18px;
border-radius: 12px;
}}
.label {{
font-size: 13px;
color: #555;
margin-bottom: 6px;
}}
.value {{
font-size: 24px;
font-weight: bold;
}}
img {{
max-width: 100%;
border: 1px solid #ddd;
border-radius: 10px;
background: white;
margin-top: 12px;
}}
ul {{
line-height: 1.8;
}}
.footer {{
margin-top: 40px;
color: #666;
font-size: 13px;
}}
@media print {{
body {{
background: white;
margin: 0;
}}
.container {{
box-shadow: none;
border-radius: 0;
max-width: none;
padding: 20px;
}}
}}
</style>
</head>
<body>
<div class="container">
<h1>Sales Visual Report</h1>
<p>Generated from <code>cleaned_sales_data.csv</code></p>
<div class="metrics">
<div class="card">
<div class="label">Total Orders</div>
<div class="value">{summary['total_orders']}</div>
</div>
<div class="card">
<div class="label">Total Units Sold</div>
<div class="value">{summary['total_units_sold']:.2f}</div>
</div>
<div class="card">
<div class="label">Total Revenue</div>
<div class="value">{summary['total_revenue']:,.2f}</div>
</div>
<div class="card">
<div class="label">Average Order Value</div>
<div class="value">{summary['average_order_value']:,.2f}</div>
</div>
</div>
<h2>Top Products by Revenue</h2>
<img src="top_products_chart.svg" alt="Top products chart">
<ul>
{top_products_html}
</ul>
<h2>Category Sales</h2>
<img src="category_sales_chart.svg" alt="Category sales chart">
<ul>
{category_html}
</ul>
<h2>City / Location Sales</h2>
<img src="city_sales_chart.svg" alt="City sales chart">
<ul>
{city_html}
</ul>
<h2>Monthly Sales</h2>
<img src="monthly_sales_chart.svg" alt="Monthly sales chart">
<ul>
{month_html}
</ul>
<div class="footer">
This report was generated automatically by report_visualizer.py
</div>
</div>
</body>
</html>
"""
output_file.write_text(html_content, encoding="utf-8")
# --------------------------------------------------
# OPTIONAL PDF
# --------------------------------------------------
def create_pdf_report(summary: dict, output_file: Path) -> bool:
if not REPORTLAB_AVAILABLE:
return False
c = canvas.Canvas(str(output_file), pagesize=A4)
width, height = A4
y = height - 20 * mm
def draw_line(text: str, size: int = 11, gap: float = 7):
nonlocal y
c.setFont("Helvetica", size)
c.drawString(20 * mm, y, text[:110])
y -= gap * mm
if y < 20 * mm:
c.showPage()
y = height - 20 * mm
c.setFont("Helvetica-Bold", 18)
c.drawString(20 * mm, y, "Sales PDF Summary")
y -= 12 * mm
draw_line(f"Total Orders: {summary['total_orders']}", 12, 6)
draw_line(f"Total Units Sold: {summary['total_units_sold']:.2f}", 12, 6)
draw_line(f"Total Revenue: {summary['total_revenue']:,.2f}", 12, 6)
draw_line(f"Average Order Value: {summary['average_order_value']:,.2f}", 12, 8)
draw_line("Top Products by Revenue", 14, 6)
for name, value in summary["top_products"]:
draw_line(f"- {name}: {value:,.2f}", 11, 5)
y -= 4 * mm
draw_line("Category Sales", 14, 6)
for name, value in summary["top_categories"]:
draw_line(f"- {name}: {value:,.2f}", 11, 5)
y -= 4 * mm
draw_line("City / Location Sales", 14, 6)
for name, value in summary["sales_by_city"]:
draw_line(f"- {name}: {value:,.2f}", 11, 5)
y -= 4 * mm
draw_line("Monthly Sales", 14, 6)
for name, value in summary["monthly_sales"]:
draw_line(f"- {name}: {value:,.2f}", 11, 5)
c.save()
return True
# --------------------------------------------------
# MAIN
# --------------------------------------------------
def main() -> None:
project_root = Path(__file__).parent
output_folder = project_root / "output"
ensure_folder(output_folder)
cleaned_csv = output_folder / "cleaned_sales_data.csv"
top_products_chart = output_folder / "top_products_chart.svg"
category_sales_chart = output_folder / "category_sales_chart.svg"
city_sales_chart = output_folder / "city_sales_chart.svg"
monthly_sales_chart = output_folder / "monthly_sales_chart.svg"
html_report = output_folder / "sales_visual_report.html"
pdf_report = output_folder / "sales_visual_report.pdf"
print("📈 Charts + Visual Report Generator")
print("-" * 60)
try:
rows = load_cleaned_sales_data(cleaned_csv)
summary = generate_summary(rows)
create_svg_bar_chart(
"Top Products by Revenue",
summary["top_products"],
top_products_chart
)
create_svg_bar_chart(
"Category Sales",
summary["top_categories"],
category_sales_chart
)
create_svg_bar_chart(
"City / Location Sales",
summary["sales_by_city"],
city_sales_chart
)
create_svg_bar_chart(
"Monthly Sales",
summary["monthly_sales"],
monthly_sales_chart
)
build_html_report(summary, html_report)
print("Charts created:")
print(f"1. {top_products_chart}")
print(f"2. {category_sales_chart}")
print(f"3. {city_sales_chart}")
print(f"4. {monthly_sales_chart}")
print("")
print(f"HTML report created: {html_report}")
pdf_created = create_pdf_report(summary, pdf_report)
if pdf_created:
print(f"PDF report created : {pdf_report}")
else:
print("PDF report skipped : reportlab is not installed")
print("")
print("✅ Visualization complete.")
except FileNotFoundError as e:
print("File error:")
print(e)
except ValueError as e:
print("Data error:")
print(e)
except Exception as e:
print("Unexpected error:")
print(str(e))
if __name__ == "__main__":
main()