2024-07-24 16:56:08 +01:00
|
|
|
const limit_ratio = 1.3
|
2024-07-24 18:11:37 +01:00
|
|
|
const colors = ['#b6b8fc', '#b6f4fc', '#fcb6cc', '#e0fcb6', '#fcdcb6', '#b6fcc8', '#f0b6fc']
|
2024-07-23 21:51:42 +01:00
|
|
|
async function main() {
|
2024-08-01 22:42:07 +01:00
|
|
|
let current_url = new URL(document.URL)
|
|
|
|
current_url.hash = '' // Strip the local part of the URL, for example if we select a Truth
|
|
|
|
const vote_response = await fetch(current_url.toString()+"/votes");
|
2024-07-23 21:51:42 +01:00
|
|
|
if (!vote_response.ok) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-07-26 19:25:41 +01:00
|
|
|
let keys = []
|
2024-07-23 21:51:42 +01:00
|
|
|
let datasets = []
|
|
|
|
try {
|
2024-07-26 19:25:41 +01:00
|
|
|
const vote_data = (await vote_response.json());
|
|
|
|
for (let player in vote_data.votes) {
|
2024-07-23 21:51:42 +01:00
|
|
|
datasets.push({
|
|
|
|
parsing: true,
|
|
|
|
label: player,
|
2024-07-26 19:25:41 +01:00
|
|
|
data: vote_data.votes[player],
|
2024-07-23 21:51:42 +01:00
|
|
|
})
|
|
|
|
}
|
2024-07-26 19:25:41 +01:00
|
|
|
for (let i = 1; i <= vote_data.truth_count; i++) {
|
|
|
|
keys.push("Vérité " + i)
|
|
|
|
}
|
2024-07-23 21:51:42 +01:00
|
|
|
} catch (error) {
|
|
|
|
console.error("Failed to parse vote data : \n\t" + error.message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-07-24 18:11:37 +01:00
|
|
|
|
2024-07-24 15:23:52 +01:00
|
|
|
// Sort by label to maintain the same graph order, as it goes through a hash map in the backend.
|
|
|
|
datasets.sort((a, b) => a.label > b.label)
|
2024-07-24 18:11:37 +01:00
|
|
|
for (let i = 0; i < datasets.length; i++) {
|
|
|
|
datasets[i].backgroundColor = colors[i % colors.length]
|
|
|
|
}
|
2024-07-24 15:23:52 +01:00
|
|
|
|
2024-07-23 21:51:42 +01:00
|
|
|
const chart_canvas = document.getElementById("vote_chart")
|
|
|
|
let chart
|
2024-07-24 16:56:08 +01:00
|
|
|
let previous_orientation
|
2024-07-23 21:51:42 +01:00
|
|
|
function create_chart(keys, data) {
|
|
|
|
let main_axis;
|
|
|
|
let aspect_ratio;
|
2024-07-24 16:56:08 +01:00
|
|
|
let orientation
|
|
|
|
|
|
|
|
if (window.innerWidth / window.innerHeight > limit_ratio) {
|
|
|
|
orientation = "landscape"
|
2024-07-23 21:51:42 +01:00
|
|
|
main_axis = 'x'
|
|
|
|
aspect_ratio = 2
|
|
|
|
} else {
|
2024-07-24 16:56:08 +01:00
|
|
|
orientation = "portrait"
|
2024-07-23 21:51:42 +01:00
|
|
|
main_axis = 'y'
|
|
|
|
aspect_ratio = 0.5
|
|
|
|
}
|
|
|
|
|
2024-07-24 16:56:08 +01:00
|
|
|
// Don't re-create the chart for no reason.
|
|
|
|
if (orientation === previous_orientation) {
|
|
|
|
console.log("bijour")
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
console.log("badour")
|
|
|
|
}
|
|
|
|
|
|
|
|
previous_orientation = orientation
|
|
|
|
|
2024-07-23 21:51:42 +01:00
|
|
|
if ( chart ) {
|
|
|
|
chart.destroy()
|
|
|
|
}
|
|
|
|
|
|
|
|
chart = new Chart(chart_canvas, {
|
|
|
|
type: 'bar',
|
|
|
|
data: {
|
|
|
|
labels: keys,
|
|
|
|
datasets: data,
|
|
|
|
},
|
|
|
|
options: {
|
|
|
|
aspectRatio: aspect_ratio,
|
|
|
|
indexAxis: main_axis,
|
|
|
|
grouped: true,
|
|
|
|
scales: {
|
|
|
|
x: {
|
|
|
|
stacked: true
|
|
|
|
},
|
|
|
|
y: {
|
|
|
|
stacked: true
|
|
|
|
}
|
2024-07-24 18:08:13 +01:00
|
|
|
},
|
|
|
|
plugins: {
|
|
|
|
title: {
|
|
|
|
display: true,
|
|
|
|
position: 'bottom',
|
|
|
|
text: 'Répartition des suppositions'
|
|
|
|
}
|
2024-07-23 21:51:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function update_chart_ratio(_) {
|
|
|
|
create_chart(keys, datasets)
|
|
|
|
}
|
|
|
|
|
2024-07-24 16:56:08 +01:00
|
|
|
const orientation_query = matchMedia(`(aspect-ratio < ${limit_ratio})`);
|
2024-07-23 21:51:42 +01:00
|
|
|
orientation_query.onchange = update_chart_ratio
|
|
|
|
|
|
|
|
create_chart(keys, datasets)
|
|
|
|
}
|
|
|
|
|
|
|
|
main().then()
|