charts.html 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. {% extends 'analytics.html' %}
  2. <script>
  3. {% block jquery %}
  4. var location_id = document.getElementById("location").value;
  5. var time_interval = document.getElementById("timeInterval").value;
  6. var endpoint = '/analytics/analytic-data';
  7. params = {
  8. "location_id": location_id,
  9. "time_interval": time_interval
  10. }
  11. $.ajax({
  12. type: "get",
  13. url: endpoint,
  14. data: params,
  15. dataType: 'json',
  16. success: function(data) {
  17. $('#barChart').remove();
  18. $('#bar-chart-container').append('<canvas id="barChart" width="400" height="100" class="mt-2"></canvas>');
  19. $('#lineChart').remove();
  20. $('#line-chart-container').append('<canvas id="lineChart" width="400" height="100" class="mt-2"></canvas>');
  21. var ctx = document.getElementById('lineChart').getContext('2d');
  22. var ctxBar = document.getElementById('barChart').getContext('2d');
  23. var myLineChart = new Chart(ctx, {
  24. type: 'line',
  25. data: {
  26. labels: data.label,
  27. datasets: [
  28. {
  29. label: '5 Star',
  30. data: data.five_star,
  31. borderColor: 'rgb(75, 192, 192)',
  32. backgroundColor: 'rgb(75, 192, 192)',
  33. borderWidth: 3,
  34. fill: false
  35. },
  36. {
  37. label: '4 Star',
  38. data: data.four_star,
  39. borderColor: 'rgb(54, 162, 235)',
  40. backgroundColor: 'rgb(54, 162, 235)',
  41. borderWidth: 3,
  42. fill: false
  43. },
  44. {
  45. label: '3 Star',
  46. data: data.three_star,
  47. borderColor: 'rgb(153, 102, 255)',
  48. backgroundColor: 'rgb(153, 102, 255)',
  49. borderWidth: 3,
  50. fill: false
  51. },
  52. {
  53. label: '2 Star',
  54. data: data.two_star,
  55. borderColor: 'rgb(255, 159, 64)',
  56. backgroundColor: 'rgb(255, 159, 64)',
  57. borderWidth: 3,
  58. fill: false
  59. },
  60. {
  61. label: '1 Star',
  62. data: data.one_star,
  63. borderColor: 'rgb(255, 99, 132)',
  64. backgroundColor: 'rgb(255, 99, 132)',
  65. borderWidth: 3,
  66. fill: false
  67. },
  68. ]
  69. },
  70. options: {
  71. scales: {
  72. xAxes: [{
  73. gridLines: {
  74. drawOnChartArea:false
  75. }
  76. }],
  77. yAxes: [{
  78. ticks: {
  79. beginAtZero: true
  80. }
  81. }]
  82. }
  83. }
  84. });
  85. var myBarChart = new Chart(ctxBar, {
  86. type: 'bar',
  87. data: {
  88. labels: data.label,
  89. datasets: [
  90. {
  91. label: 'All Reviews',
  92. data: data.total_reviews,
  93. borderColor: 'rgb(75, 192, 192)',
  94. backgroundColor: 'rgb(75, 192, 192)',
  95. borderWidth: 3,
  96. fill: false
  97. }]
  98. },
  99. options: {
  100. scales: {
  101. yAxes: [{
  102. ticks: {
  103. beginAtZero: true
  104. }
  105. }],
  106. xAxes: [{
  107. gridLines: {
  108. drawOnChartArea:false
  109. }
  110. }]
  111. }
  112. }
  113. });
  114. },
  115. error: function(error_data) {
  116. console.log(error_data);
  117. }
  118. });
  119. {% endblock jquery %}
  120. </script>