begin process at 2010 02 10 15:33:04
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

Graphique

 > HISTOGRAMME HORIZONTAL

HISTOGRAMME HORIZONTAL


 Information sur la source

Note :
8,17 / 10 - par 6 personnes
8,17 / 10

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10
Catégorie :Graphique Niveau :Débutant Date de création :25/11/2004 Vu / téléchargé :6 881 / 926

Auteur : secteur

Ecrire un message privé
Commentaire sur cette source (7)
Ajouter un commentaire et/ou une note

 Description

Cliquez pour voir la capture en taille normale
Affiche un histogramme pour des statistiques. J'ai choisi de faire un tableau HTML avec la repetition
d'un gradient (de largeur 1px) pour les barres et non pas une image (comme dans le cas de camemberts par exemple). La fonction 'echo' directement le tableau.
Parametres expliques au debut (j'espere de facon assez claire)
Les couleurs correspondent en fait au nom des fichiers .png dans le dossier Images. Ce sont les gradients.
Ah oui, c'est tout en anglais...desole, une habitude.

Source

  • <?php
  • // Values and corresponding captions (array) => $values (see example below)
  • // Order => $order (0 for no order, 1 for ascending, -1 for descending)
  • // Show the value next to the bars? (boolean) => $showValue
  • // Length of the longest bar => $maxLength (shouldn't exceed 4/5 of $tableWidth)
  • // Colors (array) => $colors
  • // Height of each bar => $barHeight (12px for best rendering)
  • // Spacing between bars => $spacing
  • // Gap between caption and bars => $gap
  • // Captions (array) => $captions
  • // Width of table => $tableWidth
  • //This is an example:
  • $values=array('Vingt' => 20, 'Cinquante' => 50, 'Trente'=>30);
  • $colors=array('blue','purple');
  • histogram($values,1,true,200,12,10,6,300,$colors);
  • //This is the function that echoes (not returns!) the table in HTML
  • function histogram($values,$order, $showValue, $maxLength, $barHeight, $spacing, $gap, $tableWidth , $colors) {
  • $a=$values;
  • rsort($a);
  • $maxValue=$a[0]; // gets maximum value of the array which will correspond to $maxLength
  • // Sort the values
  • ($order==0 ? "" : ($order==1 ? asort($values) : arsort($values)));
  • // Separate values and keys
  • $captions=array_keys($values);
  • $values=array_values($values);
  • // Transform values into ratio compared to $maxValue
  • $ratio=$values;
  • for ($i=0;$i<count($ratio);$i++) {
  • $ratio[$i]/=$maxValue;
  • }
  • $numberValues=count($values);
  • // Set colors to default if not set
  • $defaultColors=array('purple','green','gold','gray','blue');
  • if (empty($colors)) {
  • ($colors=$defaultColors);
  • }
  • // If not enough colors, repeat $colors into itself (until enough colors)
  • while (count($colors)<$numberValues) {
  • $colors = array_merge($colors,$colors);
  • }
  • echo "<table width=$tableWidth cellpadding=0 cellspacing=0 border=0 align='left' class='bodystyle'><tbody>";
  • for ($i=0;$i<$numberValues;$i++){
  • echo "<tr height=".($barHeight+$spacing)." valign='middle'>";
  • echo "<td width=".($tableWidth/5-$gap)." align='right'>".$captions[$i]."</td>";
  • echo "<td width=$gap align='right'><IMG src='./Images/bar.png' height=100% width=1px></td>";
  • echo "<td width=".(4*$tableWidth/5).">";
  • for ($j=0; $j<($maxLength*$ratio[$i]); $j++) {
  • echo "<IMG src='./Images/".$colors[$i].".png' height=$barHeight width=1>";
  • }
  • echo ($showValue ? " (<I>".$values[$i]."</I>)" : "");
  • echo "</td></tr>";
  • }
  • echo "</tbody></table>";
  • }
  • ?>
<?php

// Values and corresponding captions (array) => $values  (see example below)
// Order => $order (0 for no order, 1 for ascending, -1 for descending)
// Show the value next to the bars? (boolean) => $showValue
// Length of the longest bar => $maxLength (shouldn't exceed 4/5 of $tableWidth)
// Colors (array) => $colors
// Height of each bar => $barHeight (12px for best rendering)
// Spacing between bars => $spacing
// Gap between caption and bars => $gap
// Captions (array) => $captions
// Width of table => $tableWidth

//This is an example:
$values=array('Vingt' => 20, 'Cinquante' => 50, 'Trente'=>30);
$colors=array('blue','purple');
histogram($values,1,true,200,12,10,6,300,$colors);

//This is the function that echoes (not returns!) the table in HTML
function histogram($values,$order, $showValue, $maxLength, $barHeight, $spacing, $gap, $tableWidth , $colors) {
	
	$a=$values;
	rsort($a);
	$maxValue=$a[0]; // gets maximum value of the array which will correspond to $maxLength
	
	// Sort the values
	
	($order==0 ? "" : ($order==1 ? asort($values) : arsort($values)));
	
	// Separate values and keys
	
	$captions=array_keys($values);
	$values=array_values($values);
	
	// Transform values into ratio compared to $maxValue
	
	$ratio=$values;
	for ($i=0;$i<count($ratio);$i++) {
		$ratio[$i]/=$maxValue;
	}

	$numberValues=count($values);
	
	// Set colors to default if not set
	
	$defaultColors=array('purple','green','gold','gray','blue');
	
	if (empty($colors)) {
		($colors=$defaultColors);
	}
	
	// If not enough colors, repeat $colors into itself (until enough colors)
	
	while (count($colors)<$numberValues) {
		$colors = array_merge($colors,$colors);
	}
	
	
	echo "<table width=$tableWidth cellpadding=0 cellspacing=0 border=0 align='left' class='bodystyle'><tbody>";
	
	for ($i=0;$i<$numberValues;$i++){
		echo "<tr height=".($barHeight+$spacing)." valign='middle'>";
		echo "<td width=".($tableWidth/5-$gap)." align='right'>".$captions[$i]."</td>";
		echo "<td width=$gap align='right'><IMG src='./Images/bar.png' height=100% width=1px></td>";
		echo "<td width=".(4*$tableWidth/5).">";
		
		for ($j=0; $j<($maxLength*$ratio[$i]); $j++) {
			echo "<IMG src='./Images/".$colors[$i].".png' height=$barHeight width=1>";	
		}
		
		echo ($showValue ? "  (<I>".$values[$i]."</I>)" : "");
		
		echo "</td></tr>";
	}
	
	echo "</tbody></table>";
}
?>

 Conclusion

J'attends surtout des avis sur l'utilisation d'un tableau et de la repetition d'un gradient par opposition a la creation d'une image.
Si vous voulez voir un site sur lequel j'utilise l'histogramme essayez http://www.ledirektori.com/fr/statistics.php

 Fichier Zip

Les Membres Club peuvent télécharger directement un fichier contenu dans le zip sans télécharger le zip en entier !

Télécharger le zip


 Sources de la même categorie

Source avec Zip RECHERCHE DE MOTIF DANS UNE IMAGE par ParseError
BARRES PARAMÉTRABLES EN DÉGRADÉ DE COULEUR ET AVEC TEXTE DA... par hornetbzz
Source avec Zip Source avec une capture GALERIE PHOTO SIMPLE À GÉRER par francky6691
Source avec Zip Source avec une capture DIAPORAMA AVEC AJOUT ET REDIMENSIONEMENT DE PHOTOS, CRÉATION... par giloum
Source avec Zip Source avec une capture AFFICHAGE INFOBULLE SUR GRAPHIQUE ARTICHOW par nirronico

Commentaires et avis

Commentaire de cbibi le 30/11/2004 23:47:49

Extra ! 10

Commentaire de ImmortalPC le 08/06/2005 17:17:19

Salut,
Pas super du tout !!!!
5/10 :-{
T'auais pu le mettre au normmes XHTML et CSS
Et c'est super lent et lourd.
Je l'ai totalement remixé et ajouté des fonctions :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr"><head>
<meta http-equiv="Content-LANGUAGE" content="French" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style type="text/css" media="screen">
body {
    background-color: #FFFFFF;
    text-align: left;
    color: #000000;
}
.Italic {
    font-style: italic;
}
</style>
<title>Les types d' OS utilisés</title>

</head><body>
<h1 style="text-align: center">Les types d' OS utilisés</h1>
<?php
// $values : Les valeurs a soumettre
// $order : L'ordre de classement:  0:Pas de classement; 1:Ordre croisant; 2:Ordre décroissant
// $showValue : Montrer la valeur (True/false))
// $maxLength : Longeur Maximale des Bars de statistiques
// $barHeight : Epesseur / Hauteur des bars de statistiques
// $spacing : espace entre les valeurs
// $gap : Espace entre le Nom et les bars de statistiques
// $tableWidth : Taille de la table (<table></table>)
// $colors : Couleurs ( a envoyer dans un array ex: $colors=array('blue','purple','gold');)
// $repeat : Répétition des couleurs si il n'y en a pas assez (true / false) ATTENTION : Si cette fonction est activé (true) vous risquez de perdre de la vitesse
// $Couleur_de_remplissage : L'image qui sera répètée jusqu'a la fin si il n'y a pas assez de couleur. En fonction des images données (ne pas mettre les extentions et les extentions doivent etre png)
$values=array('Windows XP' => 58, 'Windows 98' => 20, 'Windows 3.11' => 30, 'Windows 95' => 18, 'Linux' => 51,'Knoppix' => 12,'Inconnu' => 1);
$colors=array('blue','purple','gold');
histogram($values,2,true,200,12,10,6,'100%',$colors,false,'green');

//This is the function that echoes (not returns!) the table in HTML
function histogram($values,$order, $showValue, $maxLength, $barHeight, $spacing, $gap, $tableWidth, $colors, $repeat, $Couleur_de_remplissage){

    $a=$values;
    rsort($a);
$maxValue=$a[0];// On obtiens la valeur maximum qui correspond à $maxLength

// On classe les résultats
    if($order !== 0){// 0 pas de classement
        if($order === 1){
            asort($values);// 1 Classement par ordre croissant
         }else{
            arsort($values);// 2 ou + Classement par ordre décroissant

         }
    }

// On séparre les Clés et leurs valeurs
$captions=array_keys($values);
    $values=array_values($values);

// Transform values into ratio compared to $maxValue

$ratio=$values;
for($i=0;$i<count($ratio);$i++){
$ratio[$i]/=$maxValue;
}

    // Nombre de Valeur
$nb_value=count($values);

if(empty($colors)){// Couleur par défaut si rien n'est indiqué
$colors=array('gold','gray','purple','blue','green');
}


    $nb_color = count($colors);// Nombre de couleur
    if($repeat){
        // On répète les couleurs
        while(count($colors)<$nb_value) {
            $colors = array_merge($colors,$colors);
        }
    }else{
        // On ne répète pas les couleurs
        if($nb_color<$nb_value){
            // Si il n'y a pas assez de couleur on ajout la couleur $Couleur_de_remplissage jusqu'a ce qu'il y en ai assez
            $colors = array_pad($colors, $nb_value, $Couleur_de_remplissage);
   }
}


echo '<table width="',$tableWidth,'" cellpadding="0" cellspacing="0" border="0">',"\r\n";

for($i=0;$i<$nb_value;$i++){
echo '<tr style="height: ',($barHeight+$spacing),'px;" valign="middle">',"\r\n"
,'<td style="width:',($tableWidth/5-$gap),'px;" align="right">',$captions[$i],'</td>',"\r\n"
,'<td style="width:',$gap,'px; text-align: right;"><img src="./Images/bar.png" style="height:100%; width:1px;" alt="Barre de séparation" /></td>',"\r\n"
,'<td style="width:',(4*$tableWidth/5),'px;">',"\r\n"
,'<img src="./Images/',$colors[$i],'.png" style="height:',$barHeight,'px; width:',($maxLength*$ratio[$i])-1,'px;" alt="',$captions[$i],'" title="Il y a ',$values[$i],' dans ',$captions[$i],'" /> ';
        if($showValue){
            echo '(<span class="Italic">'.$values[$i].'</span>)',"\r\n";
        }
        echo '</td></tr>',"\r\n";
    }

echo '</table>',"\r\n";

    // On supprime toutes les variables utilisées
    unset($a,$values,$maxValue,$order,$captions,$ratio,$i,$nb_value,$colors,$repeat,$Couleur_de_remplissage,$tableWidth,$barHeight,$spacing,$gap,$barHeight,$maxLength,$showValue);
}
?>
</body></html>

Mais l'idée est bien.
@+

Commentaire de Urukai1 le 08/11/2005 18:22:09

salut
super j'aime beaucoup
tu pourrais eviter d'afficher plein de fois les images de taille 1 en affichant une seule image de taille qui convient

voici le bout de code que j'ai modifié

//for ($j=0; $j<($maxLength*$ratio[$i]); $j++) {
    echo "<IMG src='./Images/".$colors[$i].".png' height=$barHeight width=".$maxLength*$ratio[$i].">"; // c'est ici le changement
//}

merci pour le code

Commentaire de gabs77 le 19/05/2006 12:27:50

super ce script, il dechire c'est vrai qu'il marche mieux avec les modifications apportés par ImmortalPC
mais j'aimerai savoir si on peut faire des if dans le values c'est a dire quelque chose de ce style là

$values=array(if (($id1<>0)&&($id2<>0)){'Windows XP' => 58, 'Windows 98' => 20});

Commentaire de ImmortalPC le 19/05/2006 13:09:24

Salut,
Merci ;-)

nan tu peux pas faire
$values=array(if (($id1<>0)&&($id2<>0)){'Windows XP' => 58, 'Windows 98' => 20});
enfin à ma connaissance.
il faut faire
if (($id1<>0)&&($id2<>0)){
  $values=array('Windows XP' => 58, 'Windows 98' => 20);
}else{
  // ton code
}

@+

Commentaire de titoine2000 le 10/08/2006 15:04:03

Exactement ce que je cherchais!
Merci pour le code original et aussi pour les modifications de ImmortalPC
9/10
+++

Commentaire de princesseamiramanai le 21/11/2009 19:26:27

Merci pour le code mais je le cherche en java qui a une idee!

 Ajouter un commentaire




Nos sponsors


Sondage...

Comparez les prix

CalendriCode

Février 2010
LMMJVSD
1234567
891011121314
15161718192021
22232425262728

Consulter la suite du CalendriCode

 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 0,343 sec (4)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales