forked from Kishore0-0-7/Hospital-Management-System
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch_analytics.php
More file actions
178 lines (160 loc) · 5.39 KB
/
Copy pathfetch_analytics.php
File metadata and controls
178 lines (160 loc) · 5.39 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
<?php
header('Content-Type: application/json');
require_once('connection.php');
$type = $_GET['type'] ?? '';
$response = [];
switch($type) {
case 'appointment_status':
$query = "SELECT
CASE
WHEN userStatus=1 AND doctorStatus=1 THEN 'Active'
WHEN userStatus=0 AND doctorStatus=1 THEN 'Cancelled by Patient'
WHEN userStatus=1 AND doctorStatus=0 THEN 'Cancelled by Doctor'
ELSE 'Other'
END as status,
COUNT(*) as count
FROM appointmenttb
GROUP BY
CASE
WHEN userStatus=1 AND doctorStatus=1 THEN 'Active'
WHEN userStatus=0 AND doctorStatus=1 THEN 'Cancelled by Patient'
WHEN userStatus=1 AND doctorStatus=0 THEN 'Cancelled by Doctor'
ELSE 'Other'
END";
$result = mysqli_query($con, $query);
$labels = [];
$data = [];
$colors = [
'Active' => '#28a745',
'Cancelled by Patient' => '#dc3545',
'Cancelled by Doctor' => '#ffc107',
'Other' => '#6c757d'
];
$backgroundColor = [];
while($row = mysqli_fetch_assoc($result)) {
$labels[] = $row['status'];
$data[] = $row['count'];
$backgroundColor[] = $colors[$row['status']] ?? '#6c757d';
}
$response = [
'labels' => $labels,
'datasets' => [[
'data' => $data,
'backgroundColor' => $backgroundColor
]]
];
break;
case 'gender_distribution':
$query = "SELECT gender, COUNT(*) as count FROM patreg GROUP BY gender";
$result = mysqli_query($con, $query);
$labels = [];
$data = [];
$colors = [
'Male' => '#007bff',
'Female' => '#e83e8c',
'Other' => '#6c757d'
];
$backgroundColor = [];
while($row = mysqli_fetch_assoc($result)) {
$labels[] = $row['gender'];
$data[] = $row['count'];
$backgroundColor[] = $colors[$row['gender']] ?? '#6c757d';
}
$response = [
'labels' => $labels,
'datasets' => [[
'data' => $data,
'backgroundColor' => $backgroundColor
]]
];
break;
case 'revenue':
$query = "SELECT
DATE_FORMAT(appdate, '%b %Y') as month,
SUM(docFees) as revenue,
COUNT(*) as appointments
FROM appointmenttb
WHERE appdate >= DATE_SUB(CURDATE(), INTERVAL 12 MONTH)
GROUP BY YEAR(appdate), MONTH(appdate)
ORDER BY appdate ASC";
$result = mysqli_query($con, $query);
$labels = [];
$revenue = [];
$appointments = [];
while($row = mysqli_fetch_assoc($result)) {
$labels[] = $row['month'];
$revenue[] = $row['revenue'];
$appointments[] = $row['appointments'];
}
$response = [
'labels' => $labels,
'datasets' => [
[
'label' => 'Revenue',
'data' => $revenue,
'borderColor' => '#28a745',
'backgroundColor' => 'rgba(40, 167, 69, 0.1)',
'yAxisID' => 'y'
],
[
'label' => 'Appointments',
'data' => $appointments,
'borderColor' => '#007bff',
'backgroundColor' => 'rgba(0, 123, 255, 0.1)',
'yAxisID' => 'y1'
]
]
];
break;
case 'age_distribution':
$query = "SELECT
CASE
WHEN age < 18 THEN 'Under 18'
WHEN age BETWEEN 18 AND 30 THEN '18-30'
WHEN age BETWEEN 31 AND 50 THEN '31-50'
ELSE 'Over 50'
END as age_group,
COUNT(*) as count
FROM patient_health_details
GROUP BY
CASE
WHEN age < 18 THEN 'Under 18'
WHEN age BETWEEN 18 AND 30 THEN '18-30'
WHEN age BETWEEN 31 AND 50 THEN '31-50'
ELSE 'Over 50'
END
ORDER BY
CASE age_group
WHEN 'Under 18' THEN 1
WHEN '18-30' THEN 2
WHEN '31-50' THEN 3
ELSE 4
END";
$result = mysqli_query($con, $query);
$labels = [];
$data = [];
$backgroundColor = [
'rgba(54, 162, 235, 0.8)',
'rgba(75, 192, 192, 0.8)',
'rgba(255, 206, 86, 0.8)',
'rgba(255, 99, 132, 0.8)'
];
while($row = mysqli_fetch_assoc($result)) {
$labels[] = $row['age_group'];
$data[] = $row['count'];
}
$response = [
'labels' => $labels,
'datasets' => [[
'data' => $data,
'backgroundColor' => array_slice($backgroundColor, 0, count($data)),
'borderColor' => 'rgba(255, 255, 255, 1)',
'borderWidth' => 1
]]
];
break;
default:
http_response_code(400);
$response = ['error' => 'Invalid type specified'];
}
echo json_encode($response);