Graph - PHP Online
Form of PHP Sandbox
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
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.
Result of php executing
Full code of Graph.php
- <!DOCTYPE html>
- <html>
- <head>
- <title>Représentation Graphique des Statistiques</title>
- <!-- Inclure la bibliothèque Chart.js -->
- <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.min.js"></script>
- </head>
- <body>
- <div style="width: 800px; height: 400px;">
- <canvas id="myChart"></canvas>
- </div>
- <?php
- // Lire les données à partir du fichier CSV
- $fichier = fopen('C:\Users\NNO\Downloads\Statistike Auswertung\Dokumentenstatistik\donnees_statistiques.csv', 'r');
- $donnees = array();
- while (($ligne = fgetcsv($fichier)) !== FALSE) {
- $donnees[] = $ligne;
- }
- fclose($fichier);
- // Prétraiter les données pour les représenter graphiquement
- $labels = array();
- $datasets = array();
- $parametres = array("cree", "lu", "change", "efface");
- foreach ($donnees as $ligne) {
- $date = $ligne[0];
- $nom_dossier = $ligne[5];
- // Vérifier si le nom du dossier est déjà dans les labels
- if (!in_array($nom_dossier, $labels)) {
- $labels[] = $nom_dossier;
- }
- // Initialiser les tableaux pour chaque paramètre
- foreach ($parametres as $parametre) {
- if (!isset($datasets[$parametre])) {
- $datasets[$parametre] = array();
- }
- if (!isset($datasets[$parametre][$date])) {
- $datasets[$parametre][$date] = array();
- }
- }
- // Ajouter les données à chaque paramètre
- foreach ($parametres as $parametre) {
- $index_parametre = array_search($parametre, $ligne);
- $valeur_parametre = intval($ligne[$index_parametre + 1]); // +1 pour sauter la colonne "Date"
- $datasets[$parametre][$date][$nom_dossier] = $valeur_parametre;
- }
- }
- ?>
- <script>
- // Convertir les données PHP en format JavaScript
- var labels = <?php echo json_encode($labels); ?>;
- var datasets = <?php echo json_encode($datasets); ?>;
- // Préparer les données pour le graphique
- var data = [];
- var colors = ['red', 'blue', 'green', 'orange']; // Couleurs pour chaque paramètre
- // Créer les ensembles de données pour chaque paramètre
- <?php foreach ($parametres as $index => $parametre): ?>
- var dataset_<?php echo $parametre; ?> = {
- label: '<?php echo ucfirst($parametre); ?>',
- backgroundColor: colors[<?php echo $index; ?>],
- data: []
- };
- // Remplir les données pour chaque dossier
- labels.forEach(function(label) {
- var total = 0;
- Object.keys(datasets['<?php echo $parametre; ?>'][label]).forEach(function(dossier) {
- total += datasets['<?php echo $parametre; ?>'][label][dossier];
- });
- dataset_<?php echo $parametre; ?>.data.push(total);
- });
- data.push(dataset_<?php echo $parametre; ?>);
- <?php endforeach; ?>
- // Créer le graphique
- var ctx = document.getElementById('myChart').getContext('2d');
- var myChart = new Chart(ctx, {
- type: 'bar',
- data: {
- labels: labels,
- datasets: data
- },
- options: {
- scales: {
- xAxes: [{ stacked: true }],
- yAxes: [{ stacked: true }]
- }
- }
- });
- </script>
- </body>
- </html>
File Description
- Graph
- PHP Code
- 25 Mar-2024
- 3.45 Kb
You can Share it:
Latest PHP Pastes