4
4
{% block javascript %}
5
5
6
6
< 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)'
24
32
}
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
+ } )
28
50
}
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
+
30
64
</ script >
31
65
{% endblock %}
32
66
@@ -69,7 +103,7 @@ <h3>This week's sales</h3>
69
103
</ div >
70
104
71
105
< 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 >
73
107
</ div >
74
108
</ div >
75
109
@@ -83,13 +117,12 @@ <h1>Previous 4 weeks</h1>
83
117
</ div >
84
118
< div class ='col '>
85
119
< 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 >
92
121
< 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 >
93
126
</ div >
94
127
</ div >
95
128
0 commit comments