From 39652bd9c908247139d177b02f1126cb5e07c119 Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Tue, 26 Jan 2021 13:12:41 +0100 Subject: Some minor changes lol --- admin/public/index.html | 1 + admin/public/questions.html | 31 ++++++++++++++++++++++++ admin/public/questions.js | 57 +++++++++++++++++++++++++++++++++++++++++++++ admin/public/votes.js | 2 +- 4 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 admin/public/questions.html create mode 100644 admin/public/questions.js (limited to 'admin/public') diff --git a/admin/public/index.html b/admin/public/index.html index d88d38d..55bb54b 100644 --- a/admin/public/index.html +++ b/admin/public/index.html @@ -23,6 +23,7 @@
diff --git a/admin/public/questions.html b/admin/public/questions.html new file mode 100644 index 0000000..23332b8 --- /dev/null +++ b/admin/public/questions.html @@ -0,0 +1,31 @@ + + + + + + + + + Fragen + + +
+ Home + Logout +
+
+ +
+

+ +
+ + + + + diff --git a/admin/public/questions.js b/admin/public/questions.js new file mode 100644 index 0000000..32e48bf --- /dev/null +++ b/admin/public/questions.js @@ -0,0 +1,57 @@ +let chart; +const data = []; +let question_index = 0; + +const label = document.getElementById("question"); + +fetch("/admin/api/percentages") + .then((response) => response.json()) + .then((response) => { + response.forEach((e) => { + if (!data[e.id - 1]) data[e.id - 1] = []; + data[e.id - 1].push(e); + }); + render(question_index); + }); + +function render(index) { + const ctx = document.getElementById("questions").getContext("2d"); + const q = data[question_index]; + label.innerText = q[0].question; + chart = new Chart(ctx, { + type: "pie", + data: { + labels: [...new Set(q.map((a) => a.option))], + datasets: [ + { + label: "# of Votes", + data: q.map((a) => a.count || 0), + backgroundColor: () => "#" + (Math.random().toString(16) + "0000000").slice(2, 8), + borderWidth: 1, + }, + ], + }, + options: { + legend: { + display: false, + }, + scales: { + yAxes: [ + { + ticks: { + beginAtZero: true, + precision: 0, + }, + }, + ], + }, + }, + }); +} + +document.getElementById("switch").addEventListener("click", () => { + chart.destroy(); + render(++question_index); + if (question_index + 1 < data.length) question_index++; + else question_index = 0; +}); diff --git a/admin/public/votes.js b/admin/public/votes.js index 1bde08b..39a379f 100644 --- a/admin/public/votes.js +++ b/admin/public/votes.js @@ -1,4 +1,4 @@ -let date; +let data; let chart; fetch("/admin/api/votes") -- cgit v1.2.3