Graph - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of Graph.php

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <title>Représentation Graphique des Statistiques</title>
  5.     <!-- Inclure la bibliothèque Chart.js -->
  6.     <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.min.js"></script>
  7. </head>
  8. <body>
  9.     <div style="width: 800px; height: 400px;">
  10.         <canvas id="myChart"></canvas>
  11.     </div>
  12.  
  13.     <?php
  14.     // Lire les données à partir du fichier CSV
  15.     $fichier = fopen('C:\Users\NNO\Downloads\Statistike Auswertung\Dokumentenstatistik\donnees_statistiques.csv', 'r');
  16.     $donnees = array();
  17.     while (($ligne = fgetcsv($fichier)) !== FALSE) {
  18.         $donnees[] = $ligne;
  19.     }
  20.     fclose($fichier);
  21.  
  22.     // Prétraiter les données pour les représenter graphiquement
  23.     $labels = array();
  24.     $datasets = array();
  25.     $parametres = array("cree", "lu", "change", "efface");
  26.  
  27.     foreach ($donnees as $ligne) {
  28.         $date = $ligne[0];
  29.         $nom_dossier = $ligne[5];
  30.  
  31.         // Vérifier si le nom du dossier est déjà dans les labels
  32.         if (!in_array($nom_dossier, $labels)) {
  33.             $labels[] = $nom_dossier;
  34.         }
  35.  
  36.         // Initialiser les tableaux pour chaque paramètre
  37.         foreach ($parametres as $parametre) {
  38.             if (!isset($datasets[$parametre])) {
  39.                 $datasets[$parametre] = array();
  40.             }
  41.             if (!isset($datasets[$parametre][$date])) {
  42.                 $datasets[$parametre][$date] = array();
  43.             }
  44.         }
  45.  
  46.         // Ajouter les données à chaque paramètre
  47.         foreach ($parametres as $parametre) {
  48.             $index_parametre = array_search($parametre, $ligne);
  49.             $valeur_parametre = intval($ligne[$index_parametre + 1]); // +1 pour sauter la colonne "Date"
  50.             $datasets[$parametre][$date][$nom_dossier] = $valeur_parametre;
  51.         }
  52.     }
  53.  
  54.     ?>
  55.  
  56.     <script>
  57.         // Convertir les données PHP en format JavaScript
  58.         var labels = <?php echo json_encode($labels); ?>;
  59.         var datasets = <?php echo json_encode($datasets); ?>;
  60.  
  61.         // Préparer les données pour le graphique
  62.         var data = [];
  63.         var colors = ['red', 'blue', 'green', 'orange']; // Couleurs pour chaque paramètre
  64.  
  65.         // Créer les ensembles de données pour chaque paramètre
  66.         <?php foreach ($parametres as $index => $parametre): ?>
  67.             var dataset_<?php echo $parametre; ?> = {
  68.                 label: '<?php echo ucfirst($parametre); ?>',
  69.                 backgroundColor: colors[<?php echo $index; ?>],
  70.                 data: []
  71.             };
  72.  
  73.             // Remplir les données pour chaque dossier
  74.             labels.forEach(function(label) {
  75.                 var total = 0;
  76.                 Object.keys(datasets['<?php echo $parametre; ?>'][label]).forEach(function(dossier) {
  77.                     total += datasets['<?php echo $parametre; ?>'][label][dossier];
  78.                 });
  79.                 dataset_<?php echo $parametre; ?>.data.push(total);
  80.             });
  81.  
  82.             data.push(dataset_<?php echo $parametre; ?>);
  83.         <?php endforeach; ?>
  84.  
  85.         // Créer le graphique
  86.         var ctx = document.getElementById('myChart').getContext('2d');
  87.         var myChart = new Chart(ctx, {
  88.             type: 'bar',
  89.             data: {
  90.                 labels: labels,
  91.                 datasets: data
  92.             },
  93.             options: {
  94.                 scales: {
  95.                     xAxes: [{ stacked: true }],
  96.                     yAxes: [{ stacked: true }]
  97.                 }
  98.             }
  99.         });
  100.     </script>
  101. </body>
  102. </html>
File Description
  • Graph
  • PHP Code
  • 25 Mar-2024
  • 3.45 Kb
You can Share it: