<?php
$i=1;
foreach($tabDebniveau as $divdeb) {
	$tab[$i] = array();
	$nivcolor = mysqli_query($bdd,"SELECT bgcolor FROM ".$prefix."_edtnivcolor WHERE niveau = '".$divdeb."'");
	$nivcolor_info = mysqli_fetch_assoc($nivcolor);
	$tabColor[$i] = $nivcolor_info['bgcolor'];
	foreach (new DatePeriod($jourP1date, new DateInterval('P7D') /* pas de sept jours */, $jourP2date) as $dt) {
		$date = $dt->format('Y-m-d');
		$week = $dt->format('oW');
		$datetoend = strtotime(date("Y-m-d", strtotime($date)) . " +6 day"); // ajouter 6 jours
		$dateend = date('Y-m-d',$datetoend);
		$nbeleves = 0;
		$elegrap_nb = mysqli_query($bdd,"SELECT nombre FROM ".$prefix."_freqgrappes WHERE date_freq >= '".$dt->format('Y-m-d')."' AND date_freq <= '".$dateend."' AND niveau = '".$divdeb."' AND passage = '1' ".$filtre_activs."");
		while($elegrap = mysqli_fetch_assoc($elegrap_nb)) {
			$nbeleves += $elegrap['nombre'];
		}
		array_push($tab[$i], $nbeleves);
	}
	$i++;
}
?>
<div id="wrapper" class="statcadre">
<canvas id="chart_passperweekperniv" class="charts"></canvas>
</div>
<script type="text/javascript">
(async function() {
    
    Chart.register(ChartDataLabels);
    const labels = <?php echo json_encode($tabWeeks); ?>;
    const data = {
      labels: labels,
      datasets: [ 
        <?php $i=1;$j=1;
        foreach($tabDebniveau as $divdeb) { ?>
        {
            label: <?php echo json_encode($tabNiveau[$i-1]); ?>,
            data: <?php echo json_encode($tab[$i]); ?>,
            backgroundColor: '<?php echo $tabColor[$i] ?>',
            borderColor: '<?php echo $tabColor[$i] ?>',
            borderWidth: 1
        } <?php if($j<count($tabDebniveau)) { echo ','; } $i++; $j++;} ?> ]
    };
new Chart(
    document.getElementById('chart_passperweekperniv'),
    {
    type: 'bar',
    data: data,
    options: {
        plugins: {
            title: {
                display: true,
                text: "<?php echo FDIR_STATS_INF27; ?>"
            },
            legend: {
                display:false
            },
            tooltip : {
                mode: 'point',
                intersect: false,
              }  ,    
            datalabels: {
              display: true, // Affiche les étiquettes de données
              color: '#fff', // Couleur du texte
              align: 'center', // Alignement horizontal
              anchor: 'center', // Alignement vertical
              font: {
                weight: 'bold', // Style de police
              },
              display: function(context) {
                    return false; // Ne pas afficher les étiquettes de données
                },
            },
        },
        responsive: true,
        maintainAspectRatio: false,
        scales: {
            yAxes: [{
                ticks: {
                    beginAtZero:true
                }
            }]
        },
      }
    }
);
})();
</script>