-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
81 lines (67 loc) · 2.23 KB
/
script.js
File metadata and controls
81 lines (67 loc) · 2.23 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
function getTokenFromCookies(cookieName) {
const cookies = document.cookie.split(';');
for (const cookie of cookies) {
const [name, value] = cookie.trim().split('=');
if (name === cookieName) {
return value;
}
}
return null;
}
var employeeLabel = [], employeeSalaryData = [], empHonorDivision = [];
async function dummyChart() {
await getDummyData();
const ctx = document.getElementById('myChart').getContext('2d');
const chart = new Chart(ctx, {
type: 'bar',
data: {
labels: employeeLabel,
datasets: [{
label: 'Employee Basic salary',
backgroundColor: 'blue',
borderColor: 'rgb(255, 99, 132)',
data: employeeSalaryData
},
{
label: 'Employee Honor Division',
backgroundColor: 'pink',
borderColor: 'rgb(255, 99, 132)',
data: empHonorDivision
}
]
},
options: {
tooltips: {
mode: 'index'
}
}
});
}
dummyChart();
async function getDummyData() {
const apiUrl = "https://asia-southeast2-gis-project-401902.cloudfunctions.net/get-all-employee";
const token = getTokenFromCookies('Login');
const myHeaders = new Headers();
myHeaders.append('Login', token);
const requestOptions = {
method: 'GET',
headers: myHeaders,
redirect: 'follow',
};
try {
const response = await fetch(apiUrl, requestOptions);
const barChatData = await response.json();
if (barChatData.data && Array.isArray(barChatData.data)) {
const salary = barChatData.data.map((x) => x.salary && x.salary['basic-salary'] ? x.salary['basic-salary'] : 0);
const honor_division = barChatData.data.map((x) => x.salary && x.salary['honor-division'] ? x.salary['honor-division'] : 0);
const name = barChatData.data.map((x) => x.username || '');
employeeSalaryData = salary;
empHonorDivision = honor_division;
employeeLabel = name;
} else {
console.error("Data is null or not an array:", barChatData);
}
} catch (error) {
console.error("Error fetching data:", error);
}
}