1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
<?php
include ("php/jpgraph.php");
include ("php/jpgraph_bar.php");
define('MYSQL_HOST', 'localhost');
define('MYSQL_USER', 'root');
define('MYSQL_PASS', '');
define('MYSQL_DATABASE', 'mydb');
// Tableau de données (secteurs et nombre de projets)
$tableauSecteurs = array();
$tableauNombreProjets = array();
// **********************
// Extraction des données
// **********************
$sql_secteur= <<<EOF
SELECT `secteur` AS SECTEUR
FROM projet
GROUP BY SECTEUR
EOF;
//------------------------------
// Les pays
$sql_pays = <<<EOF
SELECT `PaysOrigine` AS PAYS
FROM projet
GROUP BY PAYS
EOF;
//------------------------------
// Pour chaque pays, récupérer le nombre d'investissements
$sql_invests_par_secteur = <<<EOF
SELECT
`secteur` AS SECTEUR,
COUNT( `id_projet` ) AS NOMBRE_PROJETS,
SUM( `nbr_emplois` ) AS NOMBRE_EMPLOIS,
SUM( `MI` ) AS MONTANT_INVEST
FROM projet
WHERE `PaysOrigine` = '%s'
GROUP BY SECTEUR
EOF;
$mysqlCnx = @mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS) or die('Pb de connxion mysql');
@mysql_select_db(MYSQL_DATABASE) or die('Pb de sélection de la base');
$mysqlQuery = @mysql_query($sql_pays, $mysqlCnx) or die(mysql_error().$mysqlQuery);
$myQuery = @mysql_query($sql_secteur, $mysqlCnx) or die(mysql_error().$mysqlQuery);
while($row = mysql_fetch_array($myQuery, MYSQL_ASSOC)){
$tableauSecteurs[]=$row['SECTEUR'];
}
while ($row_pays = mysql_fetch_array($mysqlQuery, MYSQL_ASSOC))
{
$tableauInvestsParPays[$row_pays['PAYS']] = array("","","","","");
$mysqlQuery2 = @mysql_query(sprintf($sql_invests_par_secteur, $row_pays['PAYS']), $mysqlCnx) or die('second query'.mysql_error().$mysqlQuery2);
while ($row_secteur = mysql_fetch_array($mysqlQuery2, MYSQL_ASSOC))
{
$tableauInvestsParSecteur[$row_pays['PAYS']][$row_secteur['SECTEUR']-1] = $row_secteur['NOMBRE_PROJETS'];
}
}
// **********************
// Création du graphique
// **********************
// Création du graphique conteneur
$graph = new Graph(1000,500,'auto');
// Type d'échelle
$graph->SetScale("textlin",0,8);
// Fixer les marges
$graph->img->SetMargin(60,80,30,40);
// Positionner la légende
$graph->legend->Pos(0.02,0.05);
// Couleur de l'ombre et du fond de la légende
$graph->legend->SetShadow('darkgray@0.5');
$graph->legend->SetFillColor('lightblue@0.3');
// Obtenir le mois (localisation fr possible ?)
$graph->xaxis->SetTickLabels($tableauSecteurs);
// Afficher une image de fond
$graph->SetBackgroundImage('images/R0011940.jpg',BGIMG_COPY);
// AXE X
$graph->xaxis->title->Set('Secteurs');
$graph->xaxis->title->SetFont(FF_FONT1,FS_BOLD);
$graph->xaxis->title->SetColor('black');
$graph->xaxis->SetFont(FF_FONT1,FS_BOLD);
$graph->xaxis->SetColor('black');
// AXE Y
$graph->yaxis->SetFont(FF_FONT1,FS_BOLD);
$graph->yaxis->SetColor('black');
$graph->ygrid->SetColor('black@0.5');
// TITRE: texte
$graph->title->Set("Nombre d'investissements par pays");
// TITRE: marge et apparence
$graph->title->SetMargin(6);
$graph->title->SetFont(FF_ARIAL,FS_NORMAL,12);
// Couleurs et transparence par histogramme
$aColors=array('red@0.4', 'blue@0.4', 'green@0.4', 'pink@0.4', 'teal@0.4', 'navy@0.4');
$i=0;
// Chaque histogramme est un élément du tableau:
$aGroupBarPlot = array();
foreach ($tableauInvestsParSecteur as $key => $value) {
$bplot = new BarPlot($tableauInvestsParSecteur[$key]);
$bplot->SetFillColor($aColors[$i++]);
$bplot->SetLegend($key);
$bplot->SetShadow('black@0.4');
$aGroupBarPlot[] = $bplot;
}
// Création de l'objet qui regroupe nos histogrammes
$gbarplot = new GroupBarPlot($aGroupBarPlot);
$gbarplot->SetWidth(0.8);
// Ajouter au graphique
$graph->Add($gbarplot);
// Afficher
$graph->Stroke();
?> |
Partager