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.

79 lines
2.5 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(dataStr, status){
  42. data = $.parseJSON(dataStr)
  43. if (status == "success") {
  44. data.forEach(function(element) {
  45. var a = document.createElement('a');
  46. var linkText = document.createTextNode(element["name"]);
  47. a.appendChild(linkText);
  48. a.title = element["name"];
  49. a.href = "javascript:graph(" + element["id"] + ");";
  50. document.body.appendChild(a);
  51. document.body.appendChild(document.createElement("br"));
  52. });
  53. }
  54. });
  55. function graph(id) {
  56. $.get("https://127.0.0.1:5000/votings/" + id, function(dataStr, status){
  57. data = $.parseJSON(dataStr)
  58. if (status == "success") {
  59. config["data"]["datasets"][0]["label"] = data["name"]
  60. config["data"]["datasets"][0]["data"] = []
  61. config["data"]["datasets"][0]["backgroundColor"] = []
  62. config["data"]["labels"] = []
  63. for (var key in data["votes"]) {
  64. config["data"]["datasets"][0]["data"].push(data["votes"][key]["votes"]);
  65. config["data"]["datasets"][0]["backgroundColor"].push(getRandomColor());
  66. config["data"]["labels"].push(data["votes"][key]["name"]);
  67. }
  68. window.myPie.update();
  69. }
  70. });
  71. }
  72. </script>
  73. </body>
  74. </html>