-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchart-snippet.html
More file actions
105 lines (94 loc) · 3.14 KB
/
Copy pathchart-snippet.html
File metadata and controls
105 lines (94 loc) · 3.14 KB
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
<!--
Blog Analytics Charts
Add this snippet to your Jekyll about page (e.g. about.md or about.html).
Chart.js is loaded from cdnjs (no cookies, no tracking).
-->
<h2>Statistiques du site</h2>
<p><small id="analytics-date"></small></p>
<div class="mb-4">
<canvas id="visitorsChart" height="120"></canvas>
</div>
<div class="mb-4">
<canvas id="pagesChart" height="250"></canvas>
</div>
<script src="/javascript/chart.umd.min.js"></script>
<script type="module">
const BASE = '/about/data';
try {
const [vRes, pRes] = await Promise.all([
fetch(`${BASE}/visitors.json`),
fetch(`${BASE}/pages.json`)
]);
if (!vRes.ok || !pRes.ok) throw new Error('Failed to load data');
const visitors = await vRes.json();
const pages = await pRes.json();
// Show generation date
const d = new Date(visitors.generated);
document.getElementById('analytics-date').textContent =
`Dernière mise à jour : ${d.toLocaleDateString('fr-FR')} à ${d.toLocaleTimeString('fr-FR', {hour:'2-digit', minute:'2-digit'})}`;
// --- Daily visitors (vertical bar) ---
new Chart(document.getElementById('visitorsChart'), {
type: 'bar',
data: {
labels: visitors.labels.map(function(d) {
return new Date(d + 'T00:00:00').toLocaleDateString('fr-FR', { day:'numeric', month:'short' });
}),
datasets: [{
label: 'Visiteurs uniques',
data: visitors.values,
backgroundColor: 'rgba(75, 192, 192, 0.7)',
borderColor: 'rgba(75, 192, 192, 1)',
borderWidth: 1,
borderRadius: 3
}]
},
options: {
responsive: true,
plugins: {
title: { display: true, text: 'Visiteurs par jour (30 derniers jours)', font: { size: 14 } },
legend: { display: false }
},
scales: {
y: { beginAtZero: true, ticks: { precision: 0 } },
x: { ticks: { maxRotation: 45, font: { size: 10 } } }
}
}
});
// --- Top pages (horizontal bar) ---
const mobile = window.innerWidth < 480;
const maxLen = mobile ? 22 : 40;
new Chart(document.getElementById('pagesChart'), {
type: 'bar',
data: {
labels: pages.labels.map(function(p) {
return p.length > maxLen ? '…' + p.slice(-(maxLen - 1)) : p;
}),
datasets: [{
label: 'Pages vues',
data: pages.values,
backgroundColor: 'rgba(54, 162, 235, 0.7)',
borderColor: 'rgba(54, 162, 235, 1)',
borderWidth: 1,
borderRadius: 3
}]
},
options: {
indexAxis: 'y',
responsive: true,
plugins: {
title: { display: true, text: 'Pages les plus visitées (30 derniers jours)', font: { size: 14 } },
legend: { display: false }
},
scales: {
x: { beginAtZero: true, ticks: { precision: 0 } },
y: {
afterFit(scale) { scale.width = Math.min(scale.width, mobile ? 130 : 220); },
ticks: { font: { size: mobile ? 10 : 11 } }
}
}
}
});
} catch (e) {
console.error('Analytics charts failed:', e);
}
</script>