-
Notifications
You must be signed in to change notification settings - Fork 26
/
graph.js
230 lines (208 loc) · 7.9 KB
/
graph.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
220
221
222
223
224
225
226
227
228
229
230
// Customise chart background colour
var backgroundColor = 'white';
Chart.plugins.register({
beforeDraw: function(c) {
var ctx = c.chart.ctx;
ctx.fillStyle = backgroundColor;
ctx.fillRect(0, 0, c.chart.width, c.chart.height);
}
});
url_string = window.location.href;
var url = new URL(url_string);
var project = url.searchParams.get("project");
var user = url.searchParams.get("user");
var url_param = url.searchParams.toString();
// Hide top projects section when filtering on it
if (project!=="" && project!==null){
$('#top_projects').hide();
}
// Else make an API call and render chart
else{
$.ajax({
url: '/api/top_project_stats/?' + url_param,
dataType: 'json',
success: function(data){
var ctx = document.getElementById('projectsChart');
var projectsChart = new Chart(ctx, {
type: 'bar',
data: {
labels: data['projects'],
datasets: [{
label: 'Number of edits',
data: data['edits_per_project'],
backgroundColor: 'rgba(255, 99, 132, 0.2)',
borderColor: 'rgba(255, 99, 132, 1)',
borderWidth: 1
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true,
callback: function (value) { if (Number.isInteger(value)) { return value; } }
}
}]
},
// When chart is rendered, change download and href atribute of anchor 'Download JPG' to allow users to download charts as image
// toDataURL function gets url of chart in type of jpeg image
animation: {
onComplete: function(){
let projects_link = document.getElementById("projectsChart").toDataURL("image/jpeg");
document.getElementById("projects_url").href = projects_link;
document.getElementById("projects_url").setAttribute('download', 'top_projects.jpg');
}
}
}
});
},
complete: function(){
$('#loader1').hide();
}
})
}
// Hide top users section when filtering on it
if (user!=="" && user!==null){
$('#top_users').hide();
}
// Else make an API call and render chart
else{
$.ajax({
url: '/api/top_user_stats/?' + url_param,
dataType: 'json',
success: function(data){
var ctx = document.getElementById('usersChart');
var projectsChart = new Chart(ctx, {
type: 'bar',
data: {
labels: data['usernames'],
datasets: [{
label: 'Number of edits',
data: data['edits_per_user'],
backgroundColor: 'rgba(255, 99, 132, 0.2)',
borderColor: 'rgba(255, 99, 132, 1)',
borderWidth: 1
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true,
callback: function (value) { if (Number.isInteger(value)) { return value; } }
}
}]
},
// When chart is rendered, change download and href atribute of anchor 'Download JPG' to allow users to download charts as image
// toDataURL function gets url of chart in type of jpeg image
animation: {
onComplete: function(){
let users_link = document.getElementById("usersChart").toDataURL("image/jpeg");
document.getElementById("users_url").href = users_link;
document.getElementById("users_url").setAttribute('download', 'top_users.jpg');
}
}
}
});
},
complete: function(){
$('#loader2').hide();
}
})
}
// calling the edits_over_time API when page loads gives default view_type
$.ajax({
url: '/api/time_stats/?' + url_param,
dataType: 'json',
success: function(data) {
show_chart(data['view_type']);
draw_chart(data);
},
complete: function() {
$('#loader3').hide();
}
})
// when user clicks on button to change default view_type
$(".view-type").click(function() {
var chart_name = $(this).attr("name");
var status = $('#' + chart_name).attr("status");
// if chart is loaded previously
if (status == "loaded") {
show_chart(chart_name);
} else {
//adding view_type parameter to url
extra_url_param = url_param + '&view_type=' + chart_name
$.ajax({
url: '/api/time_stats/?' + extra_url_param,
dataType: 'json',
success: function(data) {
show_chart(data['view_type']);
draw_chart(data);
},
complete: function() {
$('#loader3').hide();
}
})
}
});
// shows the required chart and hides oher
function show_chart(view_type) {
$(".timeChart").hide();
$('#' + view_type).show();
$(".downloadAnchor").hide();
$('#'+view_type+'-jpg').show();
$('#'+view_type+'-csv').show();
for (var i = 0; i < 3; i++) {
($(".view-type")[i]).style.borderStyle = "hidden";
}
document.getElementsByName(view_type)[0].style.borderStyle = "solid";
var status = $('#' + view_type).attr("status");
if (status != 'loaded') {
$('#' + view_type).attr("status", "loaded");
}
}
// draw the chart from response data
function draw_chart(data) {
var ctx = document.getElementById(data['view_type']);
var timeChart = new Chart(ctx, {
type: 'line',
data: {
labels: data['time_array'],
datasets: [{
label: 'Number of edits',
data: data['edits_array'],
backgroundColor: 'rgba(255, 99, 132, 0.2)',
borderColor: 'rgba(255, 99, 132, 1)',
borderWidth: 1,
fill: false
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true,
callback: function(value) {
if (Number.isInteger(value)) {
return value;
}
}
}
}]
},
maintainAspectRatio: false,
// When chart is rendered, change download and href atribute of anchor 'Download JPG' to allow users to download charts as image
// toDataURL function gets url of chart in type of jpeg image
animation: {
onComplete: function() {
// sets download jpg button
let time_link = document.getElementById(data['view_type']).toDataURL("image/jpeg");
document.getElementById(data['view_type']+'-jpg').href = time_link;
document.getElementById(data['view_type']+'-jpg').setAttribute('download', 'edits_over_time.jpg');
// sets download csv button
document.getElementById(data['view_type']+'-csv').href = '/time_csv/?' + url_param + '&view_type=' + data['view_type'] ;
}
}
}
});
}