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
GESTION D'EXCEPTION AVEC LES TASKSGESTION D'EXCEPTION AVEC LES TASKS par richardc
Nous avons vu dans un précédent article comment utiliser Task pour effectuer des opérations dans un autre thread.
Malheureusement, comme tout le monde n'est pas parfait, il se peut que cette exécution se passe mal et qu'une exception se produise.
La...
Cliquez pour lire la suite de l'article par richardc DéMARRONS AVEC LES TASKSDéMARRONS AVEC LES TASKS par richardc
Que vous le vouliez ou non, le développement multi-tâche est maintenant une obligation pour toute nouvelle application. Il est donc vital d'en comprendre les mécanismes et de s'y mettre le plus tôt possible.
En attendant le .NET Framework 4.5 avec le...
Cliquez pour lire la suite de l'article par richardc SLIDE & DéMO TECHDAYS 2012 - FAST & FURIOUS XAML APPSSLIDE & DéMO TECHDAYS 2012 - FAST & FURIOUS XAML APPS par Vko
Retrouvez les slides et les démo de ma session Fast & Furious XAML Apps. A ceux qui se posent la question : "est-ce que le code de la DataGrid est disponible?", je vous répondrais "pas encore". Je vais mettre en place un projet codeplex pour part...
Cliquez pour lire la suite de l'article par Vko XNA IS DEAD!XNA IS DEAD! par richardc
Depuis la semaine dernière (et grâce aux TechDays 2012), je me penche activement sur la nouvelle version de Windows, aka Windows 8. Vous me direz, il était temps puisque la première preview date de Septembre dernier.
OK. Remarquez, on n'en est qu'aux...
Cliquez pour lire la suite de l'article par richardc TECHDAYS PARIS 2012 : WINDOWS SERVER "8" QUOI DE 9 !TECHDAYS PARIS 2012 : WINDOWS SERVER "8" QUOI DE 9 ! par ROMELARD Fabrice
Speakers: Fabrice Meillon et Stanislas Quastana Cette session est basée entièrement sur celle donnée lors de la BUILD cet hiver. Il n'y a pas d'ajout d'information en rapport avec cet évènement passé. Windows 8 Server sera intégralem...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice
Forum
CHAMPS TIMECHAMPS TIME par vargas
Cliquez pour lire la suite par vargas
Logiciels
DocTranslate (V3.1.0.0)DOCTRANSLATE (V3.1.0.0)DocTranslate est un traducteur de document Microsoft Word, PowerPoint et Excel. Il permet d'autom... Cliquez pour télécharger DocTranslate Tribler (2012)TRIBLER (2012)Tribler est un client pair à pair (P2P/Peer-to-Peer) open source avec la capacité de regarder des... Cliquez pour télécharger Tribler OneSwarm (2012)ONESWARM (2012)Le peer-to-peer qui protège votre vie privée, c'est OneSwarm.
Ce logiciel de peer-to-peer crypté... Cliquez pour télécharger OneSwarm PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V8.4)PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V8.4)PONAMEDIA TV DEVIENS HELLLOOO FLASH
LA TV SUR VOTRE ORDINATEUR.
Toute une plateforme Multi... Cliquez pour télécharger PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO Academy System (17.2.1.0)ACADEMY SYSTEM (17.2.1.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System
|