예제 #1
0
 /**
  * Generates an histogram
  * @param    array    list of exercise names
  * @param    array    my results 0 to 100
  * @param    array    average scores 0-100
  * @return string
  */
 static function generate_session_exercise_graph($names, $my_results, $average)
 {
     /* Create and populate the pData object */
     $myData = new pData();
     $myData->addPoints($names, 'Labels');
     $myData->addPoints($my_results, 'Serie1');
     $myData->addPoints($average, 'Serie2');
     $myData->setSerieWeight('Serie1', 1);
     $myData->setSerieTicks('Serie2', 4);
     $myData->setSerieDescription('Labels', 'Months');
     $myData->setAbscissa('Labels');
     $myData->setSerieDescription('Serie1', get_lang('MyResults'));
     $myData->setSerieDescription('Serie2', get_lang('AverageScore'));
     $myData->setAxisUnit(0, '%');
     $myData->loadPalette(api_get_path(SYS_CODE_PATH) . 'palettes/pchart/default.color', true);
     // Cache definition
     $cachePath = api_get_path(SYS_ARCHIVE_PATH);
     $myCache = new pCache(array('CacheFolder' => substr($cachePath, 0, strlen($cachePath) - 1)));
     $chartHash = $myCache->getHash($myData);
     if ($myCache->isInCache($chartHash)) {
         //if we already created the img
         $imgPath = api_get_path(SYS_ARCHIVE_PATH) . $chartHash;
         $myCache->saveFromCache($chartHash, $imgPath);
         $imgPath = api_get_path(WEB_ARCHIVE_PATH) . $chartHash;
     } else {
         /* Define width, height and angle */
         $mainWidth = 860;
         $mainHeight = 500;
         $angle = 50;
         /* Create the pChart object */
         $myPicture = new pImage($mainWidth, $mainHeight, $myData);
         /* Turn of Antialiasing */
         $myPicture->Antialias = false;
         /* Draw the background */
         $settings = array('R' => 255, 'G' => 255, 'B' => 255);
         $myPicture->drawFilledRectangle(0, 0, $mainWidth, $mainHeight, $settings);
         /* Add a border to the picture */
         $myPicture->drawRectangle(0, 0, $mainWidth - 1, $mainHeight - 1, array('R' => 0, 'G' => 0, 'B' => 0));
         /* Set the default font */
         $myPicture->setFontProperties(array('FontName' => api_get_path(SYS_FONTS_PATH) . 'opensans/OpenSans-Regular.ttf', 'FontSize' => 10));
         /* Write the chart title */
         $myPicture->drawText($mainWidth / 2, 30, get_lang('ExercisesInTimeProgressChart'), array('FontSize' => 12, 'Align' => TEXT_ALIGN_BOTTOMMIDDLE));
         /* Set the default font */
         $myPicture->setFontProperties(array('FontName' => api_get_path(SYS_FONTS_PATH) . 'opensans/OpenSans-Regular.ttf', 'FontSize' => 6));
         /* Define the chart area */
         $myPicture->setGraphArea(60, 60, $mainWidth - 60, $mainHeight - 150);
         /* Draw the scale */
         $scaleSettings = array('XMargin' => 10, 'YMargin' => 10, 'Floating' => true, 'GridR' => 200, 'GridG' => 200, 'GridB' => 200, 'DrawSubTicks' => true, 'CycleBackground' => true, 'LabelRotation' => $angle, 'Mode' => SCALE_MODE_ADDALL_START0);
         $myPicture->drawScale($scaleSettings);
         /* Turn on Antialiasing */
         $myPicture->Antialias = true;
         /* Enable shadow computing */
         $myPicture->setShadow(true, array('X' => 1, 'Y' => 1, 'R' => 0, 'G' => 0, 'B' => 0, 'Alpha' => 10));
         /* Draw the line chart */
         $myPicture->setFontProperties(array('FontName' => api_get_path(SYS_FONTS_PATH) . 'opensans/OpenSans-Regular.ttf', 'FontSize' => 10));
         $myPicture->drawSplineChart();
         $myPicture->drawPlotChart(array('DisplayValues' => true, 'PlotBorder' => true, 'BorderSize' => 1, 'Surrounding' => -60, 'BorderAlpha' => 80));
         /* Write the chart legend */
         $myPicture->drawLegend($mainWidth / 2 + 50, 50, array('Style' => LEGEND_BOX, 'Mode' => LEGEND_HORIZONTAL, 'FontR' => 0, 'FontG' => 0, 'FontB' => 0, 'R' => 220, 'G' => 220, 'B' => 220, 'Alpha' => 100));
         $myCache->writeToCache($chartHash, $myPicture);
         $imgPath = api_get_path(SYS_ARCHIVE_PATH) . $chartHash;
         $myCache->saveFromCache($chartHash, $imgPath);
         $imgPath = api_get_path(WEB_ARCHIVE_PATH) . $chartHash;
     }
     $html = '<img src="' . $imgPath . '">';
     return $html;
 }
예제 #2
0
/**
 * This function draw the graphic to be displayed on the user view as an image
 *
 * @param array $sql_result
 * @param string $start_date
 * @param string $end_date
 * @param string $type
 * @author Jorge Frisancho Jibaja
 * @version OCT-22- 2010
 * @return string
 */
function grapher($sql_result, $start_date, $end_date, $type = "")
{
    if (empty($start_date)) {
        $start_date = "";
    }
    if (empty($end_date)) {
        $end_date = "";
    }
    if ($type == "") {
        $type = 'day';
    }
    $main_year = $main_month_year = $main_day = array();
    // get last 8 days/months
    $last_days = 5;
    $last_months = 3;
    for ($i = $last_days; $i >= 0; $i--) {
        $main_day[date('d-m-Y', mktime() - $i * 3600 * 24)] = 0;
    }
    for ($i = $last_months; $i >= 0; $i--) {
        $main_month_year[date('m-Y', mktime() - $i * 30 * 3600 * 24)] = 0;
    }
    $i = 0;
    if (is_array($sql_result) && count($sql_result) > 0) {
        foreach ($sql_result as $key => $data) {
            //creating the main array
            $main_month_year[date('m-Y', $data['login'])] += float_format(($data['logout'] - $data['login']) / 60, 0);
            $main_day[date('d-m-Y', $data['login'])] += float_format(($data['logout'] - $data['login']) / 60, 0);
            if ($i > 500) {
                break;
            }
            $i++;
        }
        switch ($type) {
            case 'day':
                $main_date = $main_day;
                break;
            case 'month':
                $main_date = $main_month_year;
                break;
            case 'year':
                $main_date = $main_year;
                break;
        }
        // the nice graphics :D
        $labels = array_keys($main_date);
        if (count($main_date) == 1) {
            $labels = $labels[0];
            $main_date = $main_date[$labels];
        }
        /* Create and populate the pData object */
        $myData = new pData();
        $myData->addPoints($main_date, 'Serie1');
        if (count($main_date) != 1) {
            $myData->addPoints($labels, 'Labels');
            $myData->setSerieDescription('Labels', 'Months');
            $myData->setAbscissa('Labels');
        }
        $myData->setSerieWeight('Serie1', 1);
        $myData->setSerieDescription('Serie1', get_lang('MyResults'));
        $myData->setAxisName(0, get_lang('Minutes'));
        $myData->loadPalette(api_get_path(SYS_CODE_PATH) . 'palettes/pchart/default.color', true);
        // Cache definition
        $cachePath = api_get_path(SYS_ARCHIVE_PATH);
        $myCache = new pCache(array('CacheFolder' => substr($cachePath, 0, strlen($cachePath) - 1)));
        $chartHash = $myCache->getHash($myData);
        if ($myCache->isInCache($chartHash)) {
            //if we already created the img
            $imgPath = api_get_path(SYS_ARCHIVE_PATH) . $chartHash;
            $myCache->saveFromCache($chartHash, $imgPath);
            $imgPath = api_get_path(WEB_ARCHIVE_PATH) . $chartHash;
        } else {
            /* Define width, height and angle */
            $mainWidth = 760;
            $mainHeight = 230;
            $angle = 50;
            /* Create the pChart object */
            $myPicture = new pImage($mainWidth, $mainHeight, $myData);
            /* Turn of Antialiasing */
            $myPicture->Antialias = false;
            /* Draw the background */
            $settings = array("R" => 255, "G" => 255, "B" => 255);
            $myPicture->drawFilledRectangle(0, 0, $mainWidth, $mainHeight, $settings);
            /* Add a border to the picture */
            $myPicture->drawRectangle(0, 0, $mainWidth - 1, $mainHeight - 1, array("R" => 0, "G" => 0, "B" => 0));
            /* Set the default font */
            $myPicture->setFontProperties(array("FontName" => api_get_path(SYS_FONTS_PATH) . 'opensans/OpenSans-Regular.ttf', "FontSize" => 10));
            /* Write the chart title */
            $myPicture->drawText($mainWidth / 2, 30, get_lang('ExercisesInTimeProgressChart'), array("FontSize" => 12, "Align" => TEXT_ALIGN_BOTTOMMIDDLE));
            /* Set the default font */
            $myPicture->setFontProperties(array("FontName" => api_get_path(SYS_FONTS_PATH) . 'opensans/OpenSans-Regular.ttf', "FontSize" => 8));
            /* Define the chart area */
            $myPicture->setGraphArea(50, 40, $mainWidth - 40, $mainHeight - 80);
            /* Draw the scale */
            $scaleSettings = array('XMargin' => 10, 'YMargin' => 10, 'Floating' => true, 'GridR' => 200, 'GridG' => 200, 'GridB' => 200, 'DrawSubTicks' => true, 'CycleBackground' => true, 'LabelRotation' => $angle, 'Mode' => SCALE_MODE_ADDALL_START0);
            $myPicture->drawScale($scaleSettings);
            /* Turn on Antialiasing */
            $myPicture->Antialias = true;
            /* Enable shadow computing */
            $myPicture->setShadow(true, array("X" => 1, "Y" => 1, "R" => 0, "G" => 0, "B" => 0, "Alpha" => 10));
            /* Draw the line chart */
            $myPicture->setFontProperties(array("FontName" => api_get_path(SYS_FONTS_PATH) . 'opensans/OpenSans-Regular.ttf', "FontSize" => 10));
            $myPicture->drawSplineChart();
            $myPicture->drawPlotChart(array("DisplayValues" => true, "PlotBorder" => true, "BorderSize" => 1, "Surrounding" => -60, "BorderAlpha" => 80));
            /* Do NOT Write the chart legend */
            /* Write and save into cache */
            $myCache->writeToCache($chartHash, $myPicture);
            $imgPath = api_get_path(SYS_ARCHIVE_PATH) . $chartHash;
            $myCache->saveFromCache($chartHash, $imgPath);
            $imgPath = api_get_path(WEB_ARCHIVE_PATH) . $chartHash;
        }
        $html = '<img src="' . $imgPath . '">';
        return $html;
    } else {
        $foo_img = api_convert_encoding('<div id="messages" class="warning-message">' . get_lang('GraphicNotAvailable') . '</div>', 'UTF-8');
        return $foo_img;
    }
}