results.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. "use strict";
  2. var connection = new signalR.HubConnectionBuilder().withUrl("/resultsHub").build();
  3. connection.on("UpdateResults", function (results) {
  4. document.body.style.opacity=1;
  5. var a = parseInt(results.optionA || 0);
  6. var b = parseInt(results.optionB || 0);
  7. var percentages = getPercentages(a, b);
  8. document.getElementById("optionA").innerText = percentages.a + "%";
  9. document.getElementById("optionB").innerText = percentages.b + "%";
  10. var totalVotes = 'No votes yet';
  11. if (results.voteCount > 0) {
  12. totalVotes = results.voteCount + (results.voteCount > 1 ? " votes" : " vote");
  13. }
  14. document.getElementById("totalVotes").innerText = totalVotes;
  15. var bg1 = document.getElementById('background-stats-1');
  16. var bg2 = document.getElementById('background-stats-2');
  17. bg1.style.width = (percentages.a-0.2) + "%";
  18. bg2.style.width = (percentages.b-0.2) + "%";
  19. });
  20. connection.start().catch(function (err) {
  21. return console.error(err.toString());
  22. });
  23. function getPercentages(a, b) {
  24. var result = {};
  25. if (a + b > 0) {
  26. result.a = Math.round(a / (a + b) * 100);
  27. result.b = 100 - result.a;
  28. } else {
  29. result.a = result.b = 50;
  30. }
  31. return result;
  32. }