Histogramme horizontal

Description

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 / Exemple :


<?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

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.