Skip to content

Commit a60c2fb

Browse files
9 - Using Ajax to Render Charts
1 parent bd9c886 commit a60c2fb

File tree

3 files changed

+80
-31
lines changed

3 files changed

+80
-31
lines changed

src/analytics/templates/analytics/sales.html

Lines changed: 61 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,63 @@
44
{% block javascript %}
55

66
<script>
7-
var ctx = document.getElementById("myChart").getContext('2d');
8-
var myChart = new Chart(ctx, {
9-
type: 'line',
10-
data: {
11-
labels: ["Monday", "Tues", "Wed", "Thurs", "Friday", "Sat", "Sun"],
12-
datasets: [{
13-
label: 'Sales Per Day',
14-
data: [12, 19, 3, 5, 2, 3, 49],
15-
backgroundColor: 'rgba(0, 158, 29, 0.45)',
16-
borderColor:'rgba(0, 158, 29, 1)',
17-
}]
18-
},
19-
options: {
20-
scales: {
21-
yAxes: [{
22-
ticks: {
23-
beginAtZero:true
7+
8+
$(document).ready(function(){
9+
function renderChart(id, data, labels){
10+
// var ctx = document.getElementById("myChart").getContext('2d');
11+
var ctx = $('#' + id)
12+
var myChart = new Chart(ctx, {
13+
type: 'line',
14+
data: {
15+
labels: labels,
16+
datasets: [{
17+
label: 'Sales',
18+
data: data,
19+
backgroundColor: 'rgba(0, 158, 29, 0.45)',
20+
borderColor:'rgba(0, 158, 29, 1)',
21+
}]
22+
},
23+
options: {
24+
scales: {
25+
yAxes: [{
26+
ticks: {
27+
beginAtZero:true
28+
}
29+
}]
30+
},
31+
backgroundColor: 'rgba(75, 192, 192, 1)'
2432
}
25-
}]
26-
},
27-
backgroundColor: 'rgba(75, 192, 192, 1)'
33+
});
34+
}
35+
36+
function getSalesData(id, type){
37+
var url = '/analytics/sales/data/'
38+
var method = 'GET'
39+
var data = {"type": type}
40+
$.ajax({
41+
url: url,
42+
method: method,
43+
data: data,
44+
success: function(responseData){
45+
renderChart(id, responseData.data, responseData.labels)
46+
}, error: function(error){
47+
$.alert("An error occurred")
48+
}
49+
})
2850
}
29-
});
51+
var chartsToRender = $('.cfe-render-chart')
52+
$.each(chartsToRender, function(index, html){
53+
var $this = $(this)
54+
if ( $this.attr('id') && $this.attr('data-type')){
55+
getSalesData($this.attr('id'), $this.attr('data-type'))
56+
}
57+
58+
})
59+
60+
})
61+
62+
63+
3064
</script>
3165
{% endblock %}
3266

@@ -69,7 +103,7 @@ <h3>This week's sales</h3>
69103
</div>
70104

71105
<div class='col'>
72-
<canvas id="myChart" width="400" height="400"></canvas>
106+
<canvas class='cfe-render-chart' id="thisWeekSales" data-type='week' width="400" height="400"></canvas>
73107
</div>
74108
</div>
75109

@@ -83,13 +117,12 @@ <h1>Previous 4 weeks</h1>
83117
</div>
84118
<div class='col'>
85119
<p>Orders Total: ${{ last_four_weeks.recent_data.total__sum }}</p>
86-
</div>
87-
<div class='col'>
88-
<p>Shipped Total: {% if last_four_weeks.shipped_data.total__sum %}
89-
${{ last_four_weeks.shipped_data.total__sum }} {% endif %}</p>
90-
</div>
91-
<div class='col'>
120+
<p>Shipped Total: {% if last_four_weeks.shipped_data.total__sum %}${{ last_four_weeks.shipped_data.total__sum }} {% endif %}</p>
92121
<p>Paid Totals: ${{ last_four_weeks.paid_data.total__sum }}</p>
122+
123+
</div>
124+
<div class='col'>
125+
<canvas class='cfe-render-chart' id="fourWeekSales" data-type='4weeks' width="400" height="400"></canvas>
93126
</div>
94127
</div>
95128

src/analytics/views.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,30 @@
11
import datetime
22
from django.contrib.auth.mixins import LoginRequiredMixin
33
from django.db.models import Count, Sum, Avg
4-
from django.http import HttpResponse
5-
from django.views.generic import TemplateView
4+
from django.http import HttpResponse, JsonResponse
5+
from django.views.generic import TemplateView, View
66
from django.shortcuts import render
77

88
from django.utils import timezone
99

1010

1111
from orders.models import Order
1212

13+
class SalesAjaxView(View):
14+
def get(self, request, *args, **kwargs):
15+
data = {}
16+
print(request.user)
17+
if request.user.is_staff:
18+
if request.GET.get('type') == 'week':
19+
data['labels'] = ["Mon", "Tues", "Weds", "Thurs", "Fri","Sat", "Sun"]
20+
data['data'] = [123, 131, 232, 12, 323,313, 3193]
21+
if request.GET.get('type') == '4weeks':
22+
data['labels'] = ["Last Week", "Two Weeks Ago", "Three Weeks Ago", "Four Weeks Ago"]
23+
data['data'] = [123, 131, 343, 13231]
24+
return JsonResponse(data)
25+
26+
27+
1328
class SalesView(LoginRequiredMixin, TemplateView):
1429
template_name = 'analytics/sales.html'
1530

src/ecommerce/urls.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
checkout_address_create_view,
3232
checkout_address_reuse_view
3333
)
34-
from analytics.views import SalesView
34+
from analytics.views import SalesView, SalesAjaxView
3535
from billing.views import payment_method_view, payment_method_createview
3636
from carts.views import cart_detail_api_view
3737
from marketing.views import MarketingPreferenceUpdateView, MailchimpWebhookView
@@ -51,6 +51,7 @@
5151
url(r'^addresses/create/$', AddressCreateView.as_view(), name='address-create'),
5252
url(r'^addresses/(?P<pk>\d+)/$', AddressUpdateView.as_view(), name='address-update'),
5353
url(r'^analytics/sales/$', SalesView.as_view(), name='sales-analytics'),
54+
url(r'^analytics/sales/data/$', SalesAjaxView.as_view(), name='sales-analytics-data'),
5455
url(r'^contact/$', contact_page, name='contact'),
5556
url(r'^login/$', LoginView.as_view(), name='login'),
5657
url(r'^checkout/address/create/$', checkout_address_create_view, name='checkout_address_create'),

0 commit comments

Comments
 (0)