Example #1
0
/**
 * eval_ccpc_genGraphPie - Génère un graphique de type camenbert sous forme d'image au format PNG
 *
 * @category : eval_ccpc_functions
 * @param array $data Données à partir desquelles le graphique est généré
 * @return string URL de l'image générée
 *
 * @Author Ali Bellamine
 *
 * Structure de $data :<br>
 * 	['data'][nom du label] => (int) Valeur liée au label<br>
 * 	['settings']['height'] => (int) Hauteur du graphique (en px)<br>
 * 	['settings']['width'] => (int) Largeur du graphique (en px)
 *
 */
function eval_ccpc_genGraphPie($data)
{
    // On vérifie les données fournit
    if (isset($data) && isset($data['data']) && count($data['data']) > 0) {
        // On récupère le hash de $data
        $hash = md5(json_encode($data));
        // Chemin du fichier
        $filePath = PLUGIN_PATH . 'cache/' . $hash . '.png';
        $filePathURI = ROOT . 'evaluations/ccpc/cache/' . $hash . '.png';
        // Si le hash existe déjà : on renvoie le lien de l'image // sinon en crée le graphique
        if (is_file($filePath)) {
            return $filePathURI;
        } else {
            // On crée l'image
            /* On inclut la librairie */
            require_once PLUGIN_PATH . 'core/pChart2.1.4/class/pData.class.php';
            require_once PLUGIN_PATH . 'core/pChart2.1.4/class/pDraw.class.php';
            require_once PLUGIN_PATH . 'core/pChart2.1.4/class/pImage.class.php';
            /* On crée l'objet pData */
            // Préparation des données
            $tempDataArray = array();
            // Contient les données chiffrés de chaque part du camenbert
            $tempLegendArray = array();
            // Contient la légend chaque part de camenbert
            foreach ($data['data'] as $tempDataLegend => $tempDataValue) {
                $tempLegendArray[] = $tempDataLegend;
                if (isset($tempDataValue) && is_numeric($tempDataValue)) {
                    $tempDataArray[] = $tempDataValue;
                } else {
                    return FALSE;
                }
            }
            $MyData = new pData();
            $MyData->addPoints($tempDataArray, 'Values');
            $MyData->addPoints($tempLegendArray, 'Label');
            $MyData->setAbscissa("Label");
            /* On crée l'objet pChart */
            if (isset($data['settings']['width'])) {
                $width = $data['settings']['width'];
            } else {
                $width = 600;
            }
            if (isset($data['settings']['height'])) {
                $height = $data['settings']['height'];
            } else {
                $height = 300;
            }
            $myPicture = new pImage($width, $height, $MyData);
            $myPicture->setFontProperties(array("FontName" => PLUGIN_PATH . 'core/pChart2.1.4/fonts/verdana.ttf', "FontSize" => 8, "R" => 223, "G" => 223, "B" => 223));
            /* Set the graph area */
            $myPicture->setGraphArea(10, 20, $width - 20, $height - 20);
            $myPicture->drawGradientArea(10, 20, $width - 20, $height - 20, DIRECTION_HORIZONTAL, array("StartR" => 200, "StartG" => 200, "StartB" => 200, "EndR" => 255, "EndG" => 255, "EndB" => 255, "Alpha" => 30));
            /* Draw the chart scale */
            $scaleSettings = array("RemoveXAxis" => TRUE, "AxisAlpha" => 10, "TickAlpha" => 10, "DrawXLines" => FALSE, "Mode" => SCALE_MODE_START0, "GridR" => 0, "GridG" => 0, "GridB" => 0, "GridAlpha" => 10, "Pos" => SCALE_POS_TOPBOTTOM);
            $myPicture->drawScale($scaleSettings);
            /* Turn on shadow computing */
            $myPicture->setShadow(TRUE, array("X" => 1, "Y" => 1, "R" => 0, "G" => 0, "B" => 0, "Alpha" => 10));
            /* Draw the chart */
            $myPicture->drawBarChartAli(array("DisplayValues" => TRUE, "DisplayShadow" => TRUE, "DisplayPos" => LABEL_POS_INSIDE, "Rounded" => TRUE, "Surrounding" => 30));
            $myPicture->render($filePath);
            return $filePathURI;
        }
    } else {
        return FALSE;
    }
}