-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalytics.php
More file actions
236 lines (210 loc) Β· 7.6 KB
/
analytics.php
File metadata and controls
236 lines (210 loc) Β· 7.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
<?php
session_start();
include 'db.php'; // Database connection
// Ensure only admin can access
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'admin') {
header("Location: login.php");
exit();
}
// Fetch Top 5 Customers
$customerQuery = "SELECT user.username AS name, SUM(orders.total_price) AS total_spent
FROM orders
JOIN user ON orders.user_id = user.id
GROUP BY user.id
ORDER BY total_spent DESC LIMIT 5";
$customerResult = $conn->query($customerQuery);
$top_customers = $customerResult->fetch_all(MYSQLI_ASSOC);
// Fetch Sales by Product Category
$categoryQuery = "SELECT ANY_VALUE(products.category) AS name, SUM(order_items.quantity * order_items.price) AS revenue
FROM order_items
JOIN products ON order_items.product_id = products.id
GROUP BY products.category";
$categoryResult = $conn->query($categoryQuery);
$category_sales = $categoryResult->fetch_all(MYSQLI_ASSOC);
// Fetch Sales Trends (Daily Sales)
$salesTrendsQuery = "SELECT DATE(order_date) AS date, SUM(total_price) AS revenue
FROM orders
GROUP BY DATE(order_date)
ORDER BY date ASC";
$salesTrendsResult = $conn->query($salesTrendsQuery);
$sales_trends = $salesTrendsResult->fetch_all(MYSQLI_ASSOC);
// Fetch Payment Method Distribution
$paymentQuery = "SELECT payment_method, COUNT(*) AS count
FROM payments
GROUP BY payment_method";
$paymentResult = $conn->query($paymentQuery);
$payment_methods = $paymentResult->fetch_all(MYSQLI_ASSOC);
// Fetch Stock Availability for Top 5 Products
$stockQuery = "SELECT name, quantity FROM products ORDER BY quantity DESC LIMIT 5";
$stockResult = $conn->query($stockQuery);
$stock_availability = $stockResult->fetch_all(MYSQLI_ASSOC);
// Fetch Top 5 Best Selling Products
$topSellingQuery = "SELECT products.name, SUM(order_items.quantity) AS total_sold
FROM order_items
JOIN products ON order_items.product_id = products.id
GROUP BY products.id
ORDER BY total_sold DESC
LIMIT 5";
$topSellingResult = $conn->query($topSellingQuery);
$top_selling_products = $topSellingResult->fetch_all(MYSQLI_ASSOC);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Advanced Analytics</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.3.0/css/bootstrap.min.css">
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<style>
body {
background-color: #f8f9fa;
}
.card {
border-radius: 10px;
box-shadow: 2px 2px 10px rgba(0, 0, 0, 0.1);
}
.dark-mode {
background-color: #121212;
color: white;
}
.dark-mode .card {
background-color: #1e1e1e;
color: white;
border-color: #333;
}
</style>
</head>
<body>
<div class="container mt-4">
<h2 class="text-center mb-4">π Advanced Analytics Dashboard</h2>
<button class="btn btn-dark mb-3" onclick="toggleDarkMode()">π Toggle Dark Mode</button>
<!-- Top Customers -->
<div class="card mb-4">
<div class="card-header bg-success text-white">π Top 5 Customers</div>
<div class="card-body">
<canvas id="topCustomersChart"></canvas>
</div>
</div>
<!-- Sales by Category -->
<div class="card mb-4">
<div class="card-header bg-info text-white">π¦ Sales by Product Category</div>
<div class="card-body">
<canvas id="categorySalesChart"></canvas>
</div>
</div>
<!-- Sales Trends -->
<div class="card mb-4">
<div class="card-header bg-warning text-dark">π Sales Trends Over Time</div>
<div class="card-body">
<canvas id="salesTrendsChart"></canvas>
</div>
</div>
<!-- Payment Method Distribution -->
<div class="card mb-4">
<div class="card-header bg-primary text-white">π³ Payment Methods Used</div>
<div class="card-body">
<canvas id="paymentChart"></canvas>
</div>
</div>
<!-- Stock Availability -->
<div class="card mb-4">
<div class="card-header bg-danger text-white">π Stock Availability (Top Products)</div>
<div class="card-body">
<canvas id="stockChart"></canvas>
</div>
</div>
<!-- Top Selling Products (Pie Chart) -->
<div class="card mb-4">
<div class="card-header bg-secondary text-white">π₯ Top 5 Selling Products</div>
<div class="card-body">
<canvas id="topSellingChart"></canvas>
</div>
</div>
</div>
<script>
function getRandomColors(length) {
const colors = [];
for (let i = 0; i < length; i++) {
colors.push(`hsl(${Math.floor(Math.random() * 360)}, 70%, 60%)`);
}
return colors;
}
// Top Customers Chart
new Chart(document.getElementById('topCustomersChart'), {
type: 'bar',
data: {
labels: <?php echo json_encode(array_column($top_customers, 'name')); ?>,
datasets: [{
label: 'Total Spent (βΉ)',
data: <?php echo json_encode(array_column($top_customers, 'total_spent')); ?>,
backgroundColor: getRandomColors(5)
}]
}
});
// Sales by Category Chart
new Chart(document.getElementById('categorySalesChart'), {
type: 'pie',
data: {
labels: <?php echo json_encode(array_column($category_sales, 'name')); ?>,
datasets: [{
data: <?php echo json_encode(array_column($category_sales, 'revenue')); ?>,
backgroundColor: getRandomColors(5)
}]
}
});
// Sales Trends Chart
new Chart(document.getElementById('salesTrendsChart'), {
type: 'line',
data: {
labels: <?php echo json_encode(array_column($sales_trends, 'date')); ?>,
datasets: [{
label: 'Revenue (βΉ)',
data: <?php echo json_encode(array_column($sales_trends, 'revenue')); ?>,
backgroundColor: 'rgba(255, 99, 132, 0.2)',
borderColor: 'rgba(255, 99, 132, 1)',
fill: true
}]
}
});
// Payment Method Distribution Chart
new Chart(document.getElementById('paymentChart'), {
type: 'doughnut',
data: {
labels: <?php echo json_encode(array_column($payment_methods, 'payment_method')); ?>,
datasets: [{
data: <?php echo json_encode(array_column($payment_methods, 'count')); ?>,
backgroundColor: getRandomColors(3)
}]
}
});
// Stock Availability Chart
new Chart(document.getElementById('stockChart'), {
type: 'bar',
data: {
labels: <?php echo json_encode(array_column($stock_availability, 'name')); ?>,
datasets: [{
label: 'Stock Available',
data: <?php echo json_encode(array_column($stock_availability, 'quantity')); ?>,
backgroundColor: getRandomColors(5)
}]
}
});
// Top Selling Products Chart (Pie Chart)
new Chart(document.getElementById('topSellingChart'), {
type: 'pie',
data: {
labels: <?php echo json_encode(array_column($top_selling_products, 'name')); ?>,
datasets: [{
data: <?php echo json_encode(array_column($top_selling_products, 'total_sold')); ?>,
backgroundColor: getRandomColors(5)
}]
}
});
// Dark Mode Toggle
function toggleDarkMode() {
document.body.classList.toggle("dark-mode");
}
</script>
</body>
</html>