Accueil > > > HISTOGRAMME HORIZONTAL
HISTOGRAMME HORIZONTAL
Information sur la source
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
- <?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
Sources de la même categorie
Commentaires et avis
|
Derniers Blogs
[MSTD10] SHAREPOINT 2010 ET TEAM FOUNDATION SERVER[MSTD10] SHAREPOINT 2010 ET TEAM FOUNDATION SERVER par phil
Un post rapide pour vous informer de la disponibilité de la vidéo de ma présentation sur SharePoint 2010 & Team Foundation Server. http://www.microsoft.com/france/vision/mstechdays10/Webcast.aspx?EID=20215d48-02e3-4d43-8c36-e53505c3b316 Dans la ...
Cliquez pour lire la suite de l'article par phil [MSTD10] SQL SERVER 2008 POUR LES DéVELOPPEURS ET VISUAL STUDIO TEAM SYSTEM DATABASE EDITION[MSTD10] SQL SERVER 2008 POUR LES DéVELOPPEURS ET VISUAL STUDIO TEAM SYSTEM DATABASE EDITION par Miiitch
Lors de cette session que j'ai animé avec Rui , j'ai eu l'occasion de présenter, en plus des nouveautés de SQL Server 2008, comment utiliser l'édition "database" de Visual Studio 2008. Le principe? Avoir les mêmes outils lors que l'on travaille avec une b...
Cliquez pour lire la suite de l'article par Miiitch [WF4] LOCALISER SIMPLEMENT UNE ACTIVITé ET SON DESIGNER WPF[WF4] LOCALISER SIMPLEMENT UNE ACTIVITé ET SON DESIGNER WPF par JeremyJeanson
La localisation . vous connaissez ce system miraculeux qui permet à votre application de s'afficher dans la langue de l'utilisateur (à condition de l'avoir intégrée dans votre programme). Comment mettre en place la localisation dans WF4? Cette question m'...
Cliquez pour lire la suite de l'article par JeremyJeanson [TECHDAYS 2010] WEBCASTS ET SLIDES[TECHDAYS 2010] WEBCASTS ET SLIDES par Audrey
Les webcasts des sessions des Techdays 2010 sont maintenant disponibles en ligne ! Me concernant, voici les slides et le webcast de la session Blend 3 / Skechtflow : Dessinez c'est gagné ! (animée avec Michel Perfetti ) Les slides et le webcast : Blend 3/...
Cliquez pour lire la suite de l'article par Audrey NDEPEND V3. J'ADORE SON INTéGRATION AVEC VS2010NDEPEND V3. J'ADORE SON INTéGRATION AVEC VS2010 par tja
Après une petite introduction à son utilisation que vous pouvez lire ici j'ai également eu envie d'installer ma copie et de jouer un peu avec. Après les premières minutes de son utilisation. J'ai tout de suite ADORE. L'utilisation intuitive sans besoi...
Cliquez pour lire la suite de l'article par tja
Forum
PAS DE LIEN?PAS DE LIEN? par nabilwael
Cliquez pour lire la suite par nabilwael
Logiciels
Rigs of Rods (63.3)RIGS OF RODS (63.3)c'est un jeu de multi-simulation camions,autobus voitures, avions, bateaux, hélicoptère avec défo... Cliquez pour télécharger Rigs of Rods Konvertor (4.00)KONVERTOR (4.00)Le logiciel est un gestionnaire multimedia affichant, jouant et convertissant plus de 2000 format... Cliquez pour télécharger Konvertor Xlite (v 3.0 build 41150)XLITE (V 3.0 BUILD 41150)c'est un logiciel qui permet de téléphoner par Internet à l'aide d'un compte SIP pratique pour le... Cliquez pour télécharger Xlite Academy System (10.9.3.0)ACADEMY SYSTEM (10.9.3.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System
|