charts.html 5.2 KB

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