forked from sachiikrish/AttendanceTracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchart.php
More file actions
121 lines (115 loc) · 4.15 KB
/
Copy pathchart.php
File metadata and controls
121 lines (115 loc) · 4.15 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
<?php
require "includes/database_connect.php";
if(!isset($_SESSION['user_id'])){
header("location: home.php");
}
$user_id = $_SESSION['user_id'];
$sql_3 = "SELECT * FROM subjects where user_id = '$user_id'";
$result_3 = mysqli_query($conn, $sql_3);
$data_3 = mysqli_fetch_all($result_3, MYSQLI_ASSOC);
//prepare the subject names and classes count arrays
$subjects_array = [];
$classes_array = [];
$attendance = [];
foreach($data_3 as $subject){
$subject_id = $subject['id'];
$sql = "SELECT COUNT(*) as present_classes FROM attendance where user_id = '$user_id' AND STATUS = 'Present' AND subject_id = '$subject_id'";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
$classes = $row['present_classes'];
$classes_array[$subject_id] = $classes;
$subjects_array[$subject_id] = $subject['subject_name'];
$sql_4 = "SELECT * FROM classes where user_id = '$user_id' AND subject_id = '$subject_id'";
$result_4 = mysqli_query($conn, $sql_4);
$classes_data = mysqli_fetch_assoc($result_4);
$total_classes = $classes_data['total_classes'];
$attendance[$subject_id] = round((($classes / $total_classes)*100), 1);
}
$classes_json = json_encode(array_values($classes_array));
$subjects_json = json_encode(array_values($subjects_array));
$attendance_json = json_encode(array_values($attendance));
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div>
<canvas id="myChart"></canvas>
</div>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script>
var ctx = document.getElementById('myChart').getContext('2d');
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: <?php echo $subjects_json ?>,
datasets: [{
label: 'Number of classes attended',
data: <?php echo $classes_json ?>,
borderWidth: 1,
backgroundColor: 'rgba(20,184,166,0.2)',
borderColor: 'rgba(20,184,166,1)',
yAxisID: 'y'
}, {
label: 'Attendance %',
data: <?php echo $attendance_json ?>,
borderWidth: 1,
backgroundColor: 'rgba(249,115,22,0.2)',
borderColor: 'rgba(249,115,22,1)',
yAxisID: 'y1'
}]
},
options: {
scales: {
y: {
beginAtZero: true,
position: 'left',
title: {
display: true,
text: 'Number of Classes Attended'
}
},
y1: {
beginAtZero: true,
position: 'right',
title: {
display: true,
text: 'Attendance %'
},
type: 'linear',
grid: {
drawOnChartArea: false
}
}
},
plugins: {
legend: {
display: true,
position: 'top',
},
tooltip: {
callbacks: {
label: function(context) {
let label = context.dataset.label || '';
if (label) {
label += ': ';
}
if (context.parsed.y !== null) {
label += context.parsed.y;
}
return label;
}
}
}
}
}
});
</script>
<?php
include "includes/script.php";
?>
</body>
</html>