WebAdminPanel/static/js/top-users.js

56 lines
1.7 KiB
JavaScript
Raw Normal View History

2022-12-14 09:12:57 -05:00
fetch("/top_users?number=5", {
// method: 'POST',
2022-12-11 08:51:44 -05:00
headers: {'Content-Type': 'application/json'}
2022-12-14 09:12:57 -05:00
// body: JSON.stringify({number:5})
})
2022-12-11 08:51:44 -05:00
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error: ${response.status}`);
}
return response.json();
}).then((data) => {
const ctx = document.getElementById('top-users');
new Chart(ctx, {
type: 'pie',
data: {
labels: data["members"],
datasets: [{
data: data["counts"]
}]
},
plugins: [ChartDataLabels],
options: {
maintainAspectRatio: false,
plugins :{
legend: false,
datalabels: {
color: '#f4f6fc',
formatter: function(value, context) {
return context.chart.data.labels[context.dataIndex];
}
},
tooltip: {
callbacks: {
label: (context) => {
let sum = 0;
let value = context.parsed;
let dataArray = context.dataset.data;
dataArray.map(data => {
sum += data;
});
let percentage = Math.round(value*100 / sum);
return `${percentage}%`
}
}
}
}
},
});
})