charts.html 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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="200" height="120" class="mt-2"></canvas>');
  19. $('#pieChart').remove();
  20. $('#pie-chart-container').append('<canvas id="pieChart" width="100" height="80" class="mt-2"></canvas>');
  21. $('#lineChart').remove();
  22. $('#line-chart-container').append('<canvas id="lineChart" width="400" height="120" class="mt-2"></canvas>');
  23. var ctx = document.getElementById('lineChart').getContext('2d');
  24. var ctxBar = document.getElementById('barChart').getContext('2d');
  25. var ctxPie = document.getElementById('pieChart').getContext('2d');
  26. var location_sel = document.getElementById("location")
  27. var location_name = location_sel.options[location_sel.selectedIndex].text;
  28. var myLineChart = new Chart(ctx, {
  29. type: 'line',
  30. data: {
  31. labels: data.label,
  32. datasets: [
  33. {
  34. label: '5 Star',
  35. data: data.five_star,
  36. borderColor: 'rgb(75, 192, 192)',
  37. backgroundColor: 'rgb(75, 192, 192)',
  38. borderWidth: 3,
  39. pointRadius: 3,
  40. pointStyle: 'rect',
  41. fill: false
  42. },
  43. {
  44. label: '4 Star',
  45. data: data.four_star,
  46. borderColor: 'rgb(54, 162, 235)',
  47. backgroundColor: 'rgb(54, 162, 235)',
  48. borderWidth: 3,
  49. pointRadius: 2,
  50. fill: false
  51. },
  52. {
  53. label: '3 Star',
  54. data: data.three_star,
  55. borderColor: 'rgb(153, 102, 255)',
  56. backgroundColor: 'rgb(153, 102, 255)',
  57. borderWidth: 3,
  58. pointRadius: 2,
  59. fill: false
  60. },
  61. {
  62. label: '2 Star',
  63. data: data.two_star,
  64. borderColor: 'rgb(255, 159, 64)',
  65. backgroundColor: 'rgb(255, 159, 64)',
  66. borderWidth: 3,
  67. pointRadius: 3,
  68. pointStyle: 'rectRot',
  69. fill: false
  70. },
  71. {
  72. label: '1 Star',
  73. data: data.one_star,
  74. borderColor: 'rgb(255, 99, 132)',
  75. backgroundColor: 'rgb(255, 99, 132)',
  76. borderWidth: 3,
  77. pointRadius: 3,
  78. pointStyle: 'rect',
  79. fill: false
  80. },
  81. ]
  82. },
  83. options: {
  84. scales: {
  85. xAxes: [{
  86. gridLines: {
  87. drawOnChartArea:false
  88. }
  89. }],
  90. yAxes: [{
  91. ticks: {
  92. beginAtZero: true
  93. }
  94. }]
  95. },
  96. title: {
  97. display: true,
  98. fontSize: 20,
  99. text: "One Year's Total Star Rating - " + ' ' + location_name
  100. }
  101. }
  102. });
  103. var myBarChart = new Chart(ctxBar, {
  104. type: 'bar',
  105. data: {
  106. labels: data.label,
  107. datasets: [
  108. {
  109. label: 'All Reviews',
  110. data: data.total_reviews,
  111. borderColor: 'rgb(45, 63, 94)',
  112. backgroundColor: 'rgb(158, 181, 222)',
  113. borderWidth: 1,
  114. fill: false
  115. }]
  116. },
  117. options: {
  118. scales: {
  119. yAxes: [{
  120. ticks: {
  121. beginAtZero: true
  122. }
  123. }],
  124. xAxes: [{
  125. gridLines: {
  126. drawOnChartArea:false
  127. }
  128. }]
  129. },
  130. title: {
  131. display: true,
  132. fontSize: 16,
  133. text: "One Year's Total Review - " + ' ' +location_name
  134. }
  135. }
  136. });
  137. var myPieChart = new Chart(ctxPie, {
  138. type: 'pie',
  139. data: {
  140. labels: ['1*', '2*', '3*', '4*', '5*'],
  141. datasets: [{
  142. data: data.this,
  143. backgroundColor: [
  144. 'rgb(255, 99, 132)',
  145. 'rgb(255, 159, 64)',
  146. 'rgb(153, 102, 255)',
  147. 'rgb(54, 162, 235)',
  148. 'rgb(75, 192, 192)'
  149. ]
  150. }]
  151. },
  152. options: {
  153. title: {
  154. display: true,
  155. fontSize: 16,
  156. text: 'This '+time_interval+' - ' + ' ' + location_name
  157. }
  158. }
  159. });
  160. },
  161. error: function(error_data) {
  162. console.log(error_data);
  163. }
  164. });
  165. {% endblock jquery %}
  166. </script>