generated from app-generator/django-argon-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 5
/
dashboard.js
219 lines (191 loc) · 6.79 KB
/
dashboard.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
$(document).ready(function () {
// to update orders and sales charts
GraghQLAjax();
// to update traffic and visit tables
RESTAjax();
});
// Ajax functions to update chart and tables
function GraghQLAjax(params) {
var query = JSON.stringify({
query: `query {
orders_month_report {
month
total
}
sales_month_report {
month
total_amount
}
}`
});
$.ajax({
method: 'POST',
url: '/graphql/',
data: query,
contentType: 'application/json',
success: function (data) {
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
// sales chart
var sales_chart_data = [];
$.each(data.data.sales_month_report, function (index, obj) {
sales_chart_data.push(obj.total_amount);
});
SalesChart(sales_chart_data, months);
// orders chart
var orders_chart_data = [];
$.each(data.data.orders_month_report, function (index, obj) {
orders_chart_data.push(obj.total);
});
OrderChart(orders_chart_data, months);
},
error: function () {
alert('Error occurred');
}
});
}
function RESTAjax() {
$.ajax({
method: 'GET',
url: '/api/v1/visits/',
success: function (data) {
var html_template = '';
$.each(data.results, function (index, obj) {
html_template += '<tr>';
html_template += '<th scope="row">' + obj.page_name + '</th>';
html_template += '<td>' + obj.visitors + '</td>';
html_template += '<td>' + obj.unique_users + '</td>';
if (obj.bounce_rate_type === 1)
html_template += '<td><i class="fas fa-arrow-up text-success mr-3"></i>' + obj.bounce_rate + '%</td>';
else
html_template += '<td><i class="fas fa-arrow-down text-warning mr-3"></i>' + obj.bounce_rate + '%</td>';
html_template += '</tr>';
$('#visitsInfo').find('tbody').html(html_template);
});
},
error: function () {
alert('Error occurred');
}
});
$.ajax({
method: 'GET',
url: '/api/v1/traffics/',
success: function (data) {
var html_template = '';
$.each(data.results, function (index, obj) {
var bg_gradient = obj.rate_type === 1 ? "bg-gradient-success" : "bg-gradient-danger";
html_template += '<tr>';
html_template += '<th scope="row">' + obj.referral + '</th>';
html_template += '<td>' + obj.visitors + '</td>';
html_template += '<td><div class="d-flex align-items-center"><span class="mr-2">' + obj.rate + '%</span>';
html_template += '<div><div class="progress">';
html_template += '<div class="progress-bar ' + bg_gradient + '" role="progressbar" ' +
'aria-valuenow="' + obj.rate + '" aria-valuemin="0" aria-valuemax="100" ' +
'style="width: ' + obj.rate + '%;"></div>';
html_template += '</div></div>';
html_template += '</div></td>';
html_template += '</tr>';
$('#trafficsInfo').find('tbody').html(html_template);
});
},
error: function () {
alert('Error occurred');
}
});
}
// charts function
function SalesChart(data, labels) {
// Variables
var $chart = $('#chartSales');
var salesChart = new Chart($chart, {
type: 'line',
options: {
scales: {
yAxes: [{
gridLines: {
lineWidth: 1,
color: Charts.colors.gray[900],
zeroLineColor: Charts.colors.gray[900]
},
ticks: {
callback: function (value) {
if (!(value % 10)) {
return '$' + value;
}
}
}
}]
},
tooltips: {
callbacks: {
label: function (item, data) {
var label = data.datasets[item.datasetIndex].label || '';
var yLabel = item.yLabel;
var content = '';
if (data.datasets.length > 1) {
content += '<span class="popover-body-label mr-auto">' + label + '</span>';
}
content += '<span class="popover-body-value">$' + yLabel + '</span>';
return content;
}
}
}
},
data: {
labels: labels,
datasets: [{
label: 'Performance',
data: data
}]
}
});
// Save to jQuery object
$chart.data('chart', salesChart);
}
function OrderChart(data, labels) {
var $chart = $('#chartOrders');
var ordersChart = new Chart($chart, {
type: 'bar',
options: {
scales: {
yAxes: [{
gridLines: {
lineWidth: 1,
color: '#dfe2e6',
zeroLineColor: '#dfe2e6'
},
ticks: {
callback: function (value) {
if (!(value % 10)) {
//return '$' + value + 'k'
return value
}
}
}
}]
},
tooltips: {
callbacks: {
label: function (item, data) {
var label = data.datasets[item.datasetIndex].label || '';
var yLabel = item.yLabel;
var content = '';
if (data.datasets.length > 1) {
content += '<span class="popover-body-label mr-auto">' + label + '</span>';
}
content += '<span class="popover-body-value">' + yLabel + '</span>';
return content;
}
}
}
},
data: {
labels: labels,
datasets: [{
label: 'Sales',
data: data
}]
}
});
// Save to jQuery object
$chart.data('chart', ordersChart);
}