| 
									
										
										
										
											2025-06-30 15:03:37 +02:00
										 |  |  | {% extends 'base_embed.html.twig' %}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-30 15:26:32 +02:00
										 |  |  | {% block title %}Graphe thématique : {{ label }} - {{ stats.name }}{% endblock %}
 | 
					
						
							| 
									
										
										
										
											2025-06-30 15:03:37 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | {% block body %}
 | 
					
						
							|  |  |  | <div class="container mt-4">
 | 
					
						
							| 
									
										
										
										
											2025-06-30 15:26:32 +02:00
										 |  |  |     <h1><i class="bi {{ icon }} fs-2"></i> {{ label }} <small class="text-muted">- {{ stats.name }}</small></h1>
 | 
					
						
							| 
									
										
										
										
											2025-07-05 12:37:01 +02:00
										 |  |  |     <canvas id="embedThemeChart" width="600" height="400"></canvas>
 | 
					
						
							| 
									
										
										
										
											2025-06-30 15:03:37 +02:00
										 |  |  |     <div class="mb-3 mt-2">
 | 
					
						
							|  |  |  |         <a href="{{ path('app_admin_stats', {'insee_code': stats.zone}) }}" class="btn btn-info me-2">
 | 
					
						
							|  |  |  |             <i class="bi bi-bar-chart"></i> Voir la page de la ville
 | 
					
						
							|  |  |  |         </a>
 | 
					
						
							| 
									
										
										
										
											2025-07-05 14:31:50 +02:00
										 |  |  |         <a href="{{ path('admin_followup_theme_graph', {'insee_code': stats.zone, 'theme': theme}) }}" class="btn btn-primary me-2">
 | 
					
						
							|  |  |  |             <i class="bi bi-graph-up"></i> Graphe détaillé
 | 
					
						
							|  |  |  |         </a>
 | 
					
						
							| 
									
										
										
										
											2025-07-05 16:15:56 +02:00
										 |  |  |         <a href="{{ path('app_public_index') }}" target="_blank" class="btn btn-success me-2">
 | 
					
						
							| 
									
										
										
										
											2025-06-30 15:03:37 +02:00
										 |  |  |             <i class="bi bi-globe"></i> OSM Mon Commerce
 | 
					
						
							|  |  |  |         </a>
 | 
					
						
							|  |  |  |     </div>
 | 
					
						
							|  |  |  | </div>
 | 
					
						
							|  |  |  | {% endblock %}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | {% block javascripts %}
 | 
					
						
							|  |  |  | {{ parent() }}
 | 
					
						
							|  |  |  | <script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.0/dist/chart.umd.min.js"></script>
 | 
					
						
							|  |  |  | <script src="https://cdn.jsdelivr.net/npm/chartjs-adapter-date-fns@3.0.0"></script>
 | 
					
						
							| 
									
										
										
										
											2025-06-30 15:51:51 +02:00
										 |  |  | <script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-datalabels@2"></script>
 | 
					
						
							| 
									
										
										
										
											2025-06-30 15:03:37 +02:00
										 |  |  | <script>
 | 
					
						
							|  |  |  | const series = {{ series|json_encode|raw }};
 | 
					
						
							|  |  |  | const theme = {{ theme|json_encode|raw }};
 | 
					
						
							|  |  |  | const label = {{ label|json_encode|raw }};
 | 
					
						
							|  |  |  | const countData = (series[theme + '_count'] || []).map(e => ({x: e.date, y: e.value}));
 | 
					
						
							|  |  |  | const completionData = (series[theme + '_completion'] || []).map(e => ({x: e.date, y: e.value}));
 | 
					
						
							|  |  |  | const canvas = document.getElementById('embedThemeChart');
 | 
					
						
							|  |  |  | if (canvas) {
 | 
					
						
							|  |  |  |     new Chart(canvas, {
 | 
					
						
							|  |  |  |         type: 'line',
 | 
					
						
							|  |  |  |         data: {
 | 
					
						
							|  |  |  |             datasets: [
 | 
					
						
							|  |  |  |                 {
 | 
					
						
							|  |  |  |                     label: 'Nombre',
 | 
					
						
							|  |  |  |                     data: countData,
 | 
					
						
							|  |  |  |                     borderColor: 'blue',
 | 
					
						
							|  |  |  |                     backgroundColor: 'rgba(0,0,255,0.1)',
 | 
					
						
							|  |  |  |                     fill: false,
 | 
					
						
							|  |  |  |                     yAxisID: 'y',
 | 
					
						
							| 
									
										
										
										
											2025-06-30 15:51:51 +02:00
										 |  |  |                     datalabels: {
 | 
					
						
							|  |  |  |                         align: 'top',
 | 
					
						
							|  |  |  |                         anchor: 'end',
 | 
					
						
							|  |  |  |                         display: true,
 | 
					
						
							|  |  |  |                         formatter: function(value) { return value.y; },
 | 
					
						
							|  |  |  |                         font: { weight: 'bold' }
 | 
					
						
							|  |  |  |                     }
 | 
					
						
							| 
									
										
										
										
											2025-06-30 15:03:37 +02:00
										 |  |  |                 },
 | 
					
						
							|  |  |  |                 {
 | 
					
						
							|  |  |  |                     label: 'Complétion (%)',
 | 
					
						
							|  |  |  |                     data: completionData,
 | 
					
						
							|  |  |  |                     borderColor: 'green',
 | 
					
						
							|  |  |  |                     backgroundColor: 'rgba(0,255,0,0.1)',
 | 
					
						
							|  |  |  |                     fill: false,
 | 
					
						
							|  |  |  |                     yAxisID: 'y1',
 | 
					
						
							| 
									
										
										
										
											2025-06-30 15:51:51 +02:00
										 |  |  |                     datalabels: {
 | 
					
						
							|  |  |  |                         align: 'bottom',
 | 
					
						
							|  |  |  |                         anchor: 'end',
 | 
					
						
							|  |  |  |                         display: true,
 | 
					
						
							|  |  |  |                         formatter: function(value) { return value.y + '%'; },
 | 
					
						
							|  |  |  |                         font: { weight: 'bold' }
 | 
					
						
							|  |  |  |                     }
 | 
					
						
							| 
									
										
										
										
											2025-06-30 15:03:37 +02:00
										 |  |  |                 }
 | 
					
						
							|  |  |  |             ]
 | 
					
						
							|  |  |  |         },
 | 
					
						
							|  |  |  |         options: {
 | 
					
						
							|  |  |  |             parsing: true,
 | 
					
						
							|  |  |  |             responsive: true,
 | 
					
						
							|  |  |  |             plugins: {
 | 
					
						
							|  |  |  |                 title: {
 | 
					
						
							|  |  |  |                     display: true,
 | 
					
						
							|  |  |  |                     text: label
 | 
					
						
							| 
									
										
										
										
											2025-06-30 15:51:51 +02:00
										 |  |  |                 },
 | 
					
						
							|  |  |  |                 datalabels: {
 | 
					
						
							|  |  |  |                     display: true
 | 
					
						
							|  |  |  |                 },
 | 
					
						
							|  |  |  |                 tooltip: {
 | 
					
						
							|  |  |  |                     callbacks: {
 | 
					
						
							|  |  |  |                         title: function(context) {
 | 
					
						
							|  |  |  |                             // Affiche la date au survol
 | 
					
						
							|  |  |  |                             return context[0].parsed.x ? new Date(context[0].parsed.x).toLocaleString() : '';
 | 
					
						
							|  |  |  |                         },
 | 
					
						
							|  |  |  |                         label: function(context) {
 | 
					
						
							|  |  |  |                             return context.dataset.label + ': ' + context.parsed.y;
 | 
					
						
							|  |  |  |                         }
 | 
					
						
							|  |  |  |                     }
 | 
					
						
							| 
									
										
										
										
											2025-06-30 15:03:37 +02:00
										 |  |  |                 }
 | 
					
						
							|  |  |  |             },
 | 
					
						
							|  |  |  |             scales: {
 | 
					
						
							|  |  |  |                 x: { type: 'time', time: { unit: 'day' }, title: { display: true, text: 'Date' } },
 | 
					
						
							|  |  |  |                 y: { beginAtZero: true, title: { display: true, text: 'Nombre' } },
 | 
					
						
							| 
									
										
										
										
											2025-06-30 15:51:51 +02:00
										 |  |  |                 y1: { beginAtZero: true, position: 'right', title: { display: true, text: 'Complétion (%)' }, grid: { drawOnChartArea: false }, min: 0, max: 100 }
 | 
					
						
							| 
									
										
										
										
											2025-06-30 15:03:37 +02:00
										 |  |  |             }
 | 
					
						
							| 
									
										
										
										
											2025-06-30 15:51:51 +02:00
										 |  |  |         },
 | 
					
						
							|  |  |  |         plugins: [ChartDataLabels]
 | 
					
						
							| 
									
										
										
										
											2025-06-30 15:03:37 +02:00
										 |  |  |     });
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | // Affichage de la progression sur une semaine
 | 
					
						
							|  |  |  | function getDelta(data, days) {
 | 
					
						
							|  |  |  |     if (!data.length) return null;
 | 
					
						
							| 
									
										
										
										
											2025-07-05 10:29:53 +02:00
										 |  |  |     
 | 
					
						
							| 
									
										
										
										
											2025-06-30 15:03:37 +02:00
										 |  |  |     const now = new Date(data[data.length - 1].x);
 | 
					
						
							|  |  |  |     const refDate = new Date(now.getTime() - days * 24 * 60 * 60 * 1000);
 | 
					
						
							| 
									
										
										
										
											2025-07-05 10:29:53 +02:00
										 |  |  |     const last = data[data.length - 1].y;
 | 
					
						
							|  |  |  |     
 | 
					
						
							|  |  |  |     // Chercher la mesure exacte à la date de référence
 | 
					
						
							|  |  |  |     let exactRef = null;
 | 
					
						
							| 
									
										
										
										
											2025-06-30 15:03:37 +02:00
										 |  |  |     for (let i = data.length - 1; i >= 0; i--) {
 | 
					
						
							|  |  |  |         const d = new Date(data[i].x);
 | 
					
						
							|  |  |  |         if (d <= refDate) {
 | 
					
						
							| 
									
										
										
										
											2025-07-05 10:29:53 +02:00
										 |  |  |             exactRef = data[i].y;
 | 
					
						
							| 
									
										
										
										
											2025-06-30 15:03:37 +02:00
										 |  |  |             break;
 | 
					
						
							|  |  |  |         }
 | 
					
						
							|  |  |  |     }
 | 
					
						
							| 
									
										
										
										
											2025-07-05 10:29:53 +02:00
										 |  |  |     
 | 
					
						
							|  |  |  |     // Si on a trouvé une mesure exacte, l'utiliser
 | 
					
						
							|  |  |  |     if (exactRef !== null) {
 | 
					
						
							|  |  |  |         return last - exactRef;
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  |     
 | 
					
						
							|  |  |  |     // Sinon, chercher les deux mesures les plus proches pour faire une interpolation
 | 
					
						
							|  |  |  |     let beforeRef = null;
 | 
					
						
							|  |  |  |     let afterRef = null;
 | 
					
						
							|  |  |  |     let beforeDate = null;
 | 
					
						
							|  |  |  |     let afterDate = null;
 | 
					
						
							|  |  |  |     
 | 
					
						
							|  |  |  |     // Chercher la mesure juste avant la date de référence
 | 
					
						
							|  |  |  |     for (let i = data.length - 1; i >= 0; i--) {
 | 
					
						
							|  |  |  |         const d = new Date(data[i].x);
 | 
					
						
							|  |  |  |         if (d < refDate) {
 | 
					
						
							|  |  |  |             beforeRef = data[i].y;
 | 
					
						
							|  |  |  |             beforeDate = d;
 | 
					
						
							|  |  |  |             break;
 | 
					
						
							|  |  |  |         }
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  |     
 | 
					
						
							|  |  |  |     // Chercher la mesure juste après la date de référence
 | 
					
						
							|  |  |  |     for (let i = 0; i < data.length; i++) {
 | 
					
						
							|  |  |  |         const d = new Date(data[i].x);
 | 
					
						
							|  |  |  |         if (d > refDate) {
 | 
					
						
							|  |  |  |             afterRef = data[i].y;
 | 
					
						
							|  |  |  |             afterDate = d;
 | 
					
						
							|  |  |  |             break;
 | 
					
						
							|  |  |  |         }
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  |     
 | 
					
						
							|  |  |  |     // Si on a les deux mesures, faire une interpolation linéaire
 | 
					
						
							|  |  |  |     if (beforeRef !== null && afterRef !== null && beforeDate !== null && afterDate !== null) {
 | 
					
						
							|  |  |  |         const timeDiff = afterDate.getTime() - beforeDate.getTime();
 | 
					
						
							|  |  |  |         const refTimeDiff = refDate.getTime() - beforeDate.getTime();
 | 
					
						
							|  |  |  |         const ratio = refTimeDiff / timeDiff;
 | 
					
						
							|  |  |  |         const interpolatedRef = beforeRef + (afterRef - beforeRef) * ratio;
 | 
					
						
							|  |  |  |         return last - interpolatedRef;
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  |     
 | 
					
						
							|  |  |  |     // Si on n'a qu'une mesure avant, l'utiliser
 | 
					
						
							|  |  |  |     if (beforeRef !== null) {
 | 
					
						
							|  |  |  |         return last - beforeRef;
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  |     
 | 
					
						
							|  |  |  |     // Si on n'a qu'une mesure après, l'utiliser
 | 
					
						
							|  |  |  |     if (afterRef !== null) {
 | 
					
						
							|  |  |  |         return last - afterRef;
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  |     
 | 
					
						
							|  |  |  |     // Si aucune mesure n'est disponible, retourner null
 | 
					
						
							|  |  |  |     return null;
 | 
					
						
							| 
									
										
										
										
											2025-06-30 15:03:37 +02:00
										 |  |  | }
 | 
					
						
							| 
									
										
										
										
											2025-07-05 10:29:53 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-30 15:03:37 +02:00
										 |  |  | function formatDelta(val) {
 | 
					
						
							| 
									
										
										
										
											2025-07-03 10:28:49 +02:00
										 |  |  |     if (val === null) return 'Pas de données';
 | 
					
						
							| 
									
										
										
										
											2025-06-30 15:03:37 +02:00
										 |  |  |     if (val === 0) return '0';
 | 
					
						
							|  |  |  |     return (val > 0 ? '+' : '') + val;
 | 
					
						
							|  |  |  | }
 | 
					
						
							| 
									
										
										
										
											2025-07-05 10:29:53 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-03 10:28:49 +02:00
										 |  |  | const delta7dCount = getDelta(countData, 7);
 | 
					
						
							| 
									
										
										
										
											2025-07-05 10:29:53 +02:00
										 |  |  | const delta7dCompletion = getDelta(completionData, 7);
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-30 15:03:37 +02:00
										 |  |  | const infoDiv = document.createElement('div');
 | 
					
						
							| 
									
										
										
										
											2025-07-03 10:28:49 +02:00
										 |  |  | infoDiv.className = 'mt-3 alert ' + (delta7dCount === null ? 'alert-secondary' : 'alert-info');
 | 
					
						
							| 
									
										
										
										
											2025-07-05 10:29:53 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | let progressionText = '';
 | 
					
						
							|  |  |  | if (delta7dCount === null) {
 | 
					
						
							|  |  |  |     progressionText = '<span title="Données insuffisantes pour calculer la progression">Aucune donnée</span>';
 | 
					
						
							|  |  |  | } else {
 | 
					
						
							|  |  |  |     const countText = delta7dCount > 0 ? '+' + delta7dCount : delta7dCount === 0 ? '0' : delta7dCount;
 | 
					
						
							|  |  |  |     const completionText = delta7dCompletion !== null ? 
 | 
					
						
							|  |  |  |         (delta7dCompletion > 0 ? '+' + delta7dCompletion.toFixed(1) : delta7dCompletion === 0 ? '0' : delta7dCompletion.toFixed(1)) + '%' : 
 | 
					
						
							|  |  |  |         'N/A';
 | 
					
						
							|  |  |  |     progressionText = `${countText} objets, ${completionText} complétion`;
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | infoDiv.innerHTML = `<strong>Progression sur 7 jours :</strong> ${progressionText}`;
 | 
					
						
							| 
									
										
										
										
											2025-06-30 15:03:37 +02:00
										 |  |  | canvas.parentNode.appendChild(infoDiv);
 | 
					
						
							|  |  |  | </script>
 | 
					
						
							|  |  |  | {% endblock %} 
 |