-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.js
271 lines (224 loc) · 8.45 KB
/
scripts.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
jQuery(document).ready(function($) {
let chartTypes = ['line', 'bar', 'radar'];
let currentChartTypeIndex = 0;
function getCookie(name) {
let cookieName = name + "=";
let decodedCookie = decodeURIComponent(document.cookie);
let cookieArray = decodedCookie.split(';');
for (let i = 0; i < cookieArray.length; i++) {
let cookie = cookieArray[i];
while (cookie.charAt(0) === ' ') {
cookie = cookie.substring(1);
}
if (cookie.indexOf(cookieName) === 0) {
return cookie.substring(cookieName.length, cookie.length);
}
}
return null;
}
function setCookie(name, value, days) {
let expirationDate = new Date();
expirationDate.setDate(expirationDate.getDate() + days);
let cookieValue = name + "=" + value + ";expires=" + expirationDate.toUTCString() + ";path=/";
document.cookie = cookieValue;
}
let storedChartType = getCookie('currentChartType');
if (storedChartType) {
let storedChartTypeIndex = chartTypes.indexOf(storedChartType);
if (storedChartTypeIndex !== -1) {
currentChartTypeIndex = storedChartTypeIndex;
}
}
function toggleChartType() {
currentChartTypeIndex = (currentChartTypeIndex + 1) % chartTypes.length;
let newChartType = chartTypes[currentChartTypeIndex];
setCookie('currentChartType', newChartType, 365);
if (window.myChart) {
if (window.myChart.config.type === 'radar') {
window.myChart.config.options.scales = null;
}
let chartData = window.myChart.config.data;
let chartOptions = window.myChart.config.options;
window.myChart.destroy();
let ctx = document.getElementById('canvas').getContext('2d');
window.myChart = new Chart(ctx, {
type: newChartType,
data: chartData,
options: chartOptions
});
}
if (window.myChart.config.type === 'radar') { window.document.location.reload(); }
}
function prepareChartData(data, period) {
const labels = [];
const pageviewsData = [];
const visitorsData = [];
let periodData = null;
if (data[period] && data[period].pageviews && data[period].pageviews.data && data[period].visitors && data[period].visitors.data) {
periodData = data[period];
periodData.pageviews.data.sort((a, b) => new Date(a.value).getTime() - new Date(b.value).getTime());
periodData.visitors.data.sort((a, b) => new Date(a.value).getTime() - new Date(b.value).getTime());
for (const item of periodData.pageviews.data) {
const date = new Date(item.value);
const dayOfMonth = date.getUTCDate();
const monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
const monthName = monthNames[date.getUTCMonth()];
labels.push(`${dayOfMonth}/${monthName}`);
pageviewsData.push(parseInt(item.count));
}
for (const item of periodData.visitors.data) {
visitorsData.push(parseInt(item.count));
}
} else {
console.error(`Datos insuficientes para el periodo ${period}.`);
}
return { labels, pageviewsData, visitorsData };
}
dataChart.pageService = (days) => {
const dataLast = {
prepareLastData: function (data, period) {
const pageData = this.preparePageStats(data); // Obtenemos las páginas para cada período
// Verificamos si el período seleccionado existe en el objeto de datos
if (!pageData.hasOwnProperty(period)) {
console.error("El período seleccionado no es válido.");
return null;
}
// Devolvemos la página correspondiente al período seleccionado
return pageData[period].data;
},
preparePageStats: function(statsData) {
const pageStats = {
"7_days": statsData["7_days"].page,
"15_days": statsData["15_days"].page,
"30_days": statsData["30_days"].page
};
return pageStats;
},
pageService: function(days) {
let lastData = null;
const lastDataFor7Days = this.prepareLastData(statsData, "7_days");
const lastDataFor15Days = this.prepareLastData(statsData, "15_days");
const lastDataFor30Days = this.prepareLastData(statsData, "30_days");
if (days === 7) {
lastData = lastDataFor7Days;
} else if (days === 15) {
lastData = lastDataFor15Days;
} else if (days === 30) {
lastData = lastDataFor30Days;
} else {
console.error("Número de días no válido.");
return;
}
const container = document.getElementById('page-rating');
container.innerHTML = ""; // Limpiar el contenido anterior
lastData.forEach(function(item) {
const row = document.createElement('tr'); // Crear una fila
const pageCell = document.createElement('td'); // Crear una celda para la página
const visitsCell = document.createElement('td'); // Crear una celda para las visitas
pageCell.textContent = item.value.substring(0, 64); // Establecer el valor de la página
visitsCell.textContent = item.count; // Establecer el valor de las visitas
row.appendChild(pageCell); // Añadir la celda de página a la fila
row.appendChild(visitsCell); // Añadir la celda de visitas a la fila
container.appendChild(row); // Añadir la fila al contenedor de la tabla
});
}
};
if (days === 7) {
dataLast.pageService(7);
} else if (days === 15) {
dataLast.pageService(15);
} else if (days === 30) {
dataLast.pageService(30);
} else {
console.error("Número de días no válido.");
return;
}
};
dataChart.chartService = (days) => {
let chartData = null;
const chartDataFor7Days = prepareChartData(statsData, "7_days");
const chartDataFor15Days = prepareChartData(statsData, "15_days");
const chartDataFor30Days = prepareChartData(statsData, "30_days");
if (days === 7) {
chartData = chartDataFor7Days;
} else if (days === 15) {
chartData = chartDataFor15Days;
} else if (days === 30) {
chartData = chartDataFor30Days;
} else {
console.error("Número de días no válido.");
return;
}
const ctx = document.getElementById('canvas').getContext('2d');
if (window.myChart) {
window.myChart.destroy();
}
if (storedChartType) {
let storedChartTypeIndex = chartTypes.indexOf(storedChartType);
if (storedChartTypeIndex !== -1) {
currentChartTypeIndex = storedChartTypeIndex;
}
}
let initialChartType = chartTypes[currentChartTypeIndex];
if (initialChartType === 'radar') {
window.chartOptions = {
plugins: {
title: {
display: true,
text: 'Pageviews and Visitors for the Last ' + days + ' Days'
}
},
scales: {
r: {
grid: {
circular: true
}
}
}
};
} else {
window.chartOptions = {
plugins: {
title: {
display: true,
text: 'Pageviews and Visitors for the Last ' + days + ' Days'
}
},
scales: {
y: {
stacked: false
}
}
};
}
const chartOptions = window.chartOptions;
window.myChart = new Chart(ctx, {
type: initialChartType,
data: {
labels: chartData.labels,
datasets: [
{
label: 'Visitors',
data: chartData.visitorsData,
borderColor: 'rgba(0, 135, 218, 1)',
backgroundColor: 'rgba(82, 187, 252, 0.9)',
borderWidth: 1
},
{
label: 'Pageviews',
data: chartData.pageviewsData,
borderColor: 'rgba(255, 177, 160, 1)',
backgroundColor: 'rgba(255, 87, 51, 0.9)',
borderWidth: 1
}
]
},
options: chartOptions
});
};
dataChart.pageService(7)
dataChart.chartService(7);
$('#changeChartTypeButton').on('click', function() {
toggleChartType();
});
});