You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
2.4 KiB

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Voting Results</title>
  6. <script src="https://cdn.jsdelivr.net/npm/chart.js@2.8.0"></script>
  7. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  8. </head>
  9. <body>
  10. <div id="canvas-holder" style="width:40%">
  11. <canvas id="voteResults"></canvas>
  12. </div>
  13. <br>
  14. <script>
  15. var ctx = document.getElementById('voteResults').getContext('2d');
  16. var config = {
  17. type: 'pie',
  18. data: {
  19. datasets: [{
  20. data: [
  21. ],
  22. backgroundColor: [
  23. ],
  24. label: ''
  25. }],
  26. labels: []
  27. },
  28. options: {
  29. responsive: true
  30. }
  31. };
  32. window.myPie = new Chart(ctx, config);
  33. function getRandomColor() {
  34. var letters = '0123456789ABCDEF';
  35. var color = '#';
  36. for (var i = 0; i < 6; i++) {
  37. color += letters[Math.floor(Math.random() * 16)];
  38. }
  39. return color;
  40. }
  41. $.get("https://127.0.0.1:5000/votings", function(data, status){
  42. if (status == "success") {
  43. data.forEach(function(element) {
  44. var a = document.createElement('a');
  45. var linkText = document.createTextNode(element["name"]);
  46. a.appendChild(linkText);
  47. a.title = element["name"];
  48. a.href = "javascript:graph(" + element["id"] + ");";
  49. document.body.appendChild(a);
  50. document.body.appendChild(document.createElement("br"));
  51. });
  52. }
  53. });
  54. function graph(id) {
  55. $.get("https://127.0.0.1:5000/votings/" + id, function(data, status){
  56. if (status == "success") {
  57. config["data"]["datasets"][0]["label"] = data["name"]
  58. config["data"]["datasets"][0]["data"] = []
  59. config["data"]["datasets"][0]["backgroundColor"] = []
  60. config["data"]["labels"] = []
  61. for (var key in data["votes"]) {
  62. config["data"]["datasets"][0]["data"].push(data["votes"][key]["votes"]);
  63. config["data"]["datasets"][0]["backgroundColor"].push(getRandomColor());
  64. config["data"]["labels"].push(data["votes"][key]["name"]);
  65. }
  66. window.myPie.update();
  67. }
  68. });
  69. }
  70. </script>
  71. </body>
  72. </html>