Sfoglia il codice sorgente

Add a pie chart for last week and last month all data

Mohidul Islam 5 anni fa
parent
commit
716b38c7fb
3 ha cambiato i file con 58 aggiunte e 11 eliminazioni
  1. 5 3
      analytics/templates/analytics.html
  2. 31 4
      analytics/templates/charts.html
  3. 22 4
      analytics/utils.py

+ 5 - 3
analytics/templates/analytics.html

@@ -1,6 +1,5 @@
 {% extends "base.html" %}
 {% block report %}
-
     <div class="mt-2">
         <select id="location" class="select-style">
             {% for loc in location_list %}
@@ -14,8 +13,11 @@
         <button id="applyBtn" style="display: none;">Apply</button>
     </div>
 <br>
-    <div id="bar-chart-container">
-<!--        <canvas id="barChart" width="400" height="150" class="mt-2"></canvas>-->
+    <div style="display: flex">
+        <div id="bar-chart-container" style="position: relative; width: 60%;">
+        </div>
+        <div id="pie-chart-container" style="position: relative; width: 40%;">
+        </div>
     </div>
 <br>
 <br>

+ 31 - 4
analytics/templates/charts.html

@@ -16,13 +16,17 @@
         dataType: 'json',
         success: function(data) {
             $('#barChart').remove();
-            $('#bar-chart-container').append('<canvas id="barChart" width="400" height="120" class="mt-2"></canvas>');
+            $('#bar-chart-container').append('<canvas id="barChart" width="200" height="120" class="mt-2"></canvas>');
+
+            $('#pieChart').remove();
+            $('#pie-chart-container').append('<canvas id="pieChart" width="100" height="80" class="mt-2"></canvas>');
 
             $('#lineChart').remove();
             $('#line-chart-container').append('<canvas id="lineChart" width="400" height="120" class="mt-2"></canvas>');
 
             var ctx = document.getElementById('lineChart').getContext('2d');
             var ctxBar = document.getElementById('barChart').getContext('2d');
+            var ctxPie = document.getElementById('pieChart').getContext('2d');
             var location_sel = document.getElementById("location")
             var location_name = location_sel.options[location_sel.selectedIndex].text;
             var myLineChart = new Chart(ctx, {
@@ -96,7 +100,7 @@
                     title: {
                         display: true,
                         fontSize: 20,
-                        text: 'Total Star Rating  - ' + location_name
+                        text: 'Total Star Rating  - ' + ' ' + location_name
                     }
                 }
             });
@@ -129,8 +133,31 @@
                     },
                     title: {
                         display: true,
-                        fontSize: 20,
-                        text: 'Total Review  - ' + location_name
+                        fontSize: 16,
+                        text: 'Total Review  - ' + ' ' +location_name
+                    }
+                }
+            });
+            var myPieChart = new Chart(ctxPie, {
+                type: 'pie',
+                data: {
+                    labels: ['1*', '2*', '3*', '4*', '5*'],
+                    datasets: [{
+                        data: data.this,
+                        backgroundColor: [
+                            'rgb(255, 99, 132)',
+                            'rgb(255, 159, 64)',
+                            'rgb(153, 102, 255)',
+                            'rgb(54, 162, 235)',
+                            'rgb(75, 192, 192)'
+                        ]
+                     }]
+                },
+                options: {
+                    title: {
+                        display: true,
+                        fontSize: 16,
+                        text: 'Last '+time_interval+'  - ' + ' ' + location_name
                     }
                 }
             });

+ 22 - 4
analytics/utils.py

@@ -1,9 +1,21 @@
 from django.utils import timezone
 from review.models import Review
+from django.db.models import Count
+
+
+def last_data(date, location_id):
+    res = Review.objects.filter(create_time__gte=date, location_id=location_id).values('star_rating').annotate(total=Count('star_rating')).order_by('star_rating')
+    ratings = [0] * 5
+    for r in res:
+        ratings[r.get('star_rating')-1] = r.get('total')
+    return ratings
 
 
 def get_review_count_by_month(location_id):
-    day = 31 - timezone.now().day
+    now = timezone.now()
+    day = 31 - now.day
+    first_day_month = now - timezone.timedelta(days=now.day)
+    this_month = last_data(first_day_month, location_id)
     sql = f'''
     SELECT review_id, MONTHNAME(create_time) as month, COUNT(review_id) as total_review
     FROM review_review
@@ -26,6 +38,7 @@ def get_review_count_by_month(location_id):
             '''
         qs = Review.objects.raw(sql)
         ratings = [0]*len(label)
+
         for q in qs:
             ratings[label.index(q.month)] = q.total_ratings
         star_ratings.append(ratings)
@@ -36,13 +49,17 @@ def get_review_count_by_month(location_id):
         'two_star': star_ratings[1],
         'three_star': star_ratings[2],
         'four_star': star_ratings[3],
-        'five_star': star_ratings[4]
+        'five_star': star_ratings[4],
+        'this': this_month
     }
     return response
 
 
 def get_review_count_by_week(location_id):
-    day = 6 - timezone.now().weekday()
+    now = timezone.now()
+    day = 6 - now.weekday()
+    first_day_week = now - timezone.timedelta(days=now.weekday()+1)
+    this_week = last_data(first_day_week, location_id)
     sql = f'''
     SELECT review_id, WEEK(create_time) as week, COUNT(review_id) as total_review
     FROM review_review
@@ -75,6 +92,7 @@ def get_review_count_by_week(location_id):
         'two_star': star_ratings[1],
         'three_star': star_ratings[2],
         'four_star': star_ratings[3],
-        'five_star': star_ratings[4]
+        'five_star': star_ratings[4],
+        'this': this_week
     }
     return response