<!-- fréquentation par division par an -->
<?php
$i=1;
foreach($an as $eachyear) {
	if(substr($_SESSION['nb_year'],1,1)=='p'&&$eachyear[$i]==0) { $i++; }
	else {
		$tabNbPerDiv[$i] = array();
		$tabPercentPerDiv = array();
		$liste_division = mysqli_query($bdd,"SELECT division FROM ".$prefix."_freq GROUP BY division ORDER BY division ASC");
		$div=0;
		foreach($liste_division as $listediv) {
			if($listediv['division']!="") {
				$nbeleves_div = mysqli_query($bdd,"SELECT freq.freq_id FROM ".$prefix."_freq freq 
				WHERE date_freq >= '".$firstday[$i]."' AND date_freq <= '".$lastday[$i]."' AND division = '".$listediv['division']."' ".$filtre_activs."");
				$nbeleves = mysqli_num_rows($nbeleves_div);
				if($grappes_check==1) {
					$elegrap_nb = mysqli_query($bdd,"SELECT nombre FROM ".$prefix."_freqgrappes WHERE date_freq >= '".$firstday[$i]."' AND date_freq <= '".$lastday[$i]."' AND division = '".$listediv['division']."' AND passage = '0' ".$filtre_activs."");
					while($elegrap = mysqli_fetch_assoc($elegrap_nb)) {
						$nbeleves += $elegrap['nombre'];
					}
				}
				array_push($tabNbPerDiv[$i], $nbeleves);
				$percent = round(($nbeleves * 100 / $totalfreq_nb),2);
				array_push($tabPercentPerDiv, $percent);
			}
			$div++;
		}
		$i++;
	}
}
$bctab2 = '<table style="text-align:right;">
<thead><tr><td colspan="'.($div+2).'" style="text-align:center;">'.FDIR_STATS_EVOL2.'</td></tr><tr><th>'.YEAR.'</th>';
$nivi=0;
foreach($liste_division as $listediv) {
	$bctab2 .= '<th>'.$listediv['division'].'</th>';
	$nivi++;
}
$bctab2 .= '</tr></thead><tbody>';
$w=1;
foreach($an as $eachyear) {
	$bctab2 .= '<tr><td>'.$an[$w].'</td>';
	$m=0;$tot=0;
	foreach($liste_division as $listediv) {
		$bctab2 .= '<td>'.$tabNbPerDiv[$w][$m].'</td>';
		$m++;
	}
	$bctab2 .= '</tr>';
	$w++;
}	
$bctab2 .= '</tbody></table>';
?>
<div id="wrapper" class="statcadre">
<canvas id="chart_nbperyearperdiv" class="charts" width="600" height="300"></canvas>
</div>
<script type="text/javascript">
(async function() {
    
    Chart.register(ChartDataLabels);
    const labels = <?php echo json_encode($tabDivisionsFreq); ?>;
    const data = {
      labels: labels,
      datasets: [
		<?php
		if(isset($an[5])) { $i=5; }
		else if(isset($an[4])) { $i=4; }
		else if(isset($an[3])) { $i=3; }
		else { $i=2; }
		$j=1;
		if(substr($_SESSION['nb_year'],1,1)=='a') { $nb_sans=0; } else { $nb_sans=1; $j=2; }
		for($i;$i>$nb_sans;$i--) { ?>
		{
			label: <?php echo json_encode($an[$i]); ?>,
			data: <?php echo json_encode($tabNbPerDiv[$i]); ?>,
			backgroundColor: <?php echo json_encode($tabDivColorsFreq); ?>,
			borderColor: <?php echo json_encode($tabDivColorsFreq); ?>,
			borderWidth: 1
		} <?php if($j<=count($an)) { echo ','; } $j++;} ?> ]
    };
new Chart(
    document.getElementById('chart_nbperyearperdiv'),
    {
    type: 'bar',
    data: data,
    options: {
        plugins: {
            title: {
                display: true,
                text: "<?php echo FDIR_STATS_EVOL2; ?> (<?php if(isset($an[5])) { $i=5; } else if(isset($an[4])) { $i=4; } else if(isset($an[3])) { $i=3; } else { $i=2; } $j=1; if(substr($_SESSION['nb_year'],1,1)=='a') { $nb_sans=0; } else { $nb_sans=1; $j=2; } for($i;$i>$nb_sans;$i--) { echo $an[$i]; if($j<count($an)) { echo ' - '; } $j++; } ?>)"
            },
            legend: {
    			display:false
    		},
            tooltip : {
                mode: 'point',
                intersect: false,
              }  ,    
            datalabels: {
              display: true, // Affiche les étiquettes de données
              color: '#878787', // Couleur du texte
              align: 'center', // Alignement horizontal
              anchor: 'center', // Alignement vertical
              rotation: -90,
              font: {
                weight: 'bold', // Style de police
              },
              display: function(context) {
                  return context.dataset.data[context.dataIndex] !== 0; // N'affiche pas si la valeur est égale à 0
                },
            },
        },
        responsive: true,
        scales: {
            yAxes: [{
                ticks: {
                    beginAtZero:true
                }
            }]
        },
      }
    }
);
})();
</script>
<?php
echo '<br> <br>'.$bctab2;
$_SESSION['bctabs'] .= '<table></table>'.$bctab2;
?>