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
[DESIGN PATTERNS] PARTIE 2: DIP: DEPENDENCY INVERSION PRINCIPLE[DESIGN PATTERNS] PARTIE 2: DIP: DEPENDENCY INVERSION PRINCIPLE par tja
C'est le dernier principe des principes du Design Orienté Objet (The Principles of Object Oriented Design) fondés par Robert C. Martin plus connu sous le pseudonyme d'Uncle Bob.
l'image empruntée de LosTechies.
Je ne traite pas les principes dans...
Cliquez pour lire la suite de l'article par tja TECHDAYS PARIS 2010 : SHAREPOINT 2010 POUR LES DéVELOPPEURSTECHDAYS PARIS 2010 : SHAREPOINT 2010 POUR LES DéVELOPPEURS par ROMELARD Fabrice
Animé par: Laurent Cotton Le développement dans SharePoint 2010 passe par plusieurs axes qui seront évoqués dans cette session, mais plus particulièrement les développements simples lié au besoin Business Business Connectivity Services Ce BCS es...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice TECHDAYS PARIS 2010 : PLEINIèRE DERNIER JOURTECHDAYS PARIS 2010 : PLEINIèRE DERNIER JOUR par ROMELARD Fabrice
Cette session est la dernière pleinière de ces 3 jours de TechDays Paris 2010. Généralement, cette troisième journée est plus axée sur l'avenir vu par Microsoft. Après un retour sur l'avenir vu par la Science Fiction ou par ...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice UNE JOLIE-HORLOGE ET PAS QU'UN PEU !UNE JOLIE-HORLOGE ET PAS QU'UN PEU ! par neodante
Pour les possesseurs d'iPhone, ça y est Bijin Tokei - qui se traduit littéralement en Français par " Jolie Horloge " - est arrivé et GRATUITEMENT s'il vous plaît ! Après la version Tokyo, Hokkaido, night club, racing, Gal, "pour les mademoiselles'", . voi...
Cliquez pour lire la suite de l'article par neodante TECHDAYS PARIS 2010 : CONNECTEZ VOS DONNéES à SHAREPOINT 2010 AVEC LES BUSINESS CONNECTIVITY SERVICESTECHDAYS PARIS 2010 : CONNECTEZ VOS DONNéES à SHAREPOINT 2010 AVEC LES BUSINESS CONNECTIVITY SERVICES par ROMELARD Fabrice
Animé par: Gaetan Bouveret et Julien Chomarat Business Connectivity Services (BCS) est dans SharePoint 2010 la version 2 de Business Data Catalog (BDC dans SharePoint 2007). Il s'agit de la solution permettant de visualiser des données provenan...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice
Logiciels
DB-MAIN (9.1.0)DB-MAIN (9.1.0)DB-MAIN is a data-modeling and data-architecture tool. It is designed to help developers and anal... Cliquez pour télécharger DB-MAIN Xilisoft DPG Convertisseur (5.1.37.0120)XILISOFT DPG CONVERTISSEUR (5.1.37.0120)Xilisoft DPG Convertisseur offre aux fans de Nintendo DS une bonne solution leur permettant de dé... Cliquez pour télécharger Xilisoft DPG Convertisseur GraphicsGale (2.01.01)GRAPHICSGALE (2.01.01)GraphicsGale est un logiciel de PixelArt avec de nombreuse fonctionnalités permettant de réalisé ... Cliquez pour télécharger GraphicsGale Architecte 3D (Platinum 2010)ARCHITECTE 3D (PLATINUM 2010)Architecte 3D Platinium vous permet de concevoir facilement les plans votre future maison, de l'é... Cliquez pour télécharger Architecte 3D TeamViewer 5 (TeamViewer 5)TEAMVIEWER 5 (TEAMVIEWER 5)Dépanner un ami,expliquer une manipulation devient un jeu d'enfant.
Prise en main d'un autre ord... Cliquez pour télécharger TeamViewer 5
|