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.

83 lines
2.8 KiB

6 years ago
  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. <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
  9. </head>
  10. <body>
  11. <div class="row">
  12. <div class="col-md-6">
  13. <canvas id="voteResults"></canvas>
  14. </div>
  15. <div class="col-md-6" id="res"></div>
  16. </div>
  17. <br>
  18. <script>
  19. var ctx = document.getElementById('voteResults').getContext('2d');
  20. var config = {
  21. type: 'pie',
  22. data: {
  23. datasets: [{
  24. data: [
  25. ],
  26. backgroundColor: [
  27. ],
  28. label: ''
  29. }],
  30. labels: []
  31. },
  32. options: {
  33. responsive: true
  34. }
  35. };
  36. window.myPie = new Chart(ctx, config);
  37. function getRandomColor() {
  38. var letters = '0123456789ABCDEF';
  39. var color = '#';
  40. for (var i = 0; i < 6; i++) {
  41. color += letters[Math.floor(Math.random() * 16)];
  42. }
  43. return color;
  44. }
  45. $.get("https://127.0.0.1:5000/votings", function(dataStr, status){
  46. data = $.parseJSON(dataStr)
  47. if (status == "success") {
  48. data.forEach(function(element) {
  49. var a = document.createElement('a');
  50. var linkText = document.createTextNode(element["name"]);
  51. a.appendChild(linkText);
  52. a.title = element["name"];
  53. a.href = "javascript:graph(" + element["id"] + ");";
  54. document.getElementById("res").appendChild(a);
  55. document.getElementById("res").appendChild(document.createElement("br"));
  56. });
  57. }
  58. });
  59. function graph(id) {
  60. $.get("https://127.0.0.1:5000/votings/" + id, function(dataStr, status){
  61. data = $.parseJSON(dataStr)
  62. if (status == "success") {
  63. config["data"]["datasets"][0]["label"] = data["name"]
  64. config["data"]["datasets"][0]["data"] = []
  65. config["data"]["datasets"][0]["backgroundColor"] = []
  66. config["data"]["labels"] = []
  67. for (var key in data["votes"]) {
  68. config["data"]["datasets"][0]["data"].push(data["votes"][key]["votes"]);
  69. config["data"]["datasets"][0]["backgroundColor"].push(getRandomColor());
  70. config["data"]["labels"].push(data["votes"][key]["name"]);
  71. }
  72. window.myPie.update();
  73. }
  74. });
  75. }
  76. </script>
  77. </body>
  78. </html>