コード例 #1
0
ファイル: PChart.php プロジェクト: ksecor/civicrm
 /**
  * Build The Bar Gharph image with given params 
  * and store in upload/pChart directory.
  *
  * @param  array  $params    an assoc array of name/value pairs          
  * @return array  $filesPath created image files Path.
  *
  * @static
  */
 static function barGraph($params, $divisionWidth = 44)
 {
     if (empty($params)) {
         return;
     }
     //get the required directory path.
     $config =& CRM_Core_Config::singleton();
     //get the default currency.
     $currency = $config->defaultCurrency;
     $pChartPath = str_replace('templates', 'packages', $config->templateDir);
     $pChartPath .= 'pChart/Fonts/';
     $uploadDirURL = str_replace('persist/contribute/', 'upload/pChart/', $config->imageUploadURL);
     $uploadDirPath = $config->uploadDir . 'pChart/';
     //create pchart directory, if exist clean and then create again.
     if (is_dir($uploadDirPath)) {
         CRM_Utils_File::cleanDir($uploadDirPath);
         CRM_Utils_File::createDir($uploadDirPath);
     } else {
         CRM_Utils_File::createDir($uploadDirPath);
     }
     require_once 'packages/pChart/pData.class.php';
     require_once 'packages/pChart/pChart.class.php';
     $chartCount = 0;
     $filesValues = array();
     foreach ($params as $chartIndex => $chartValues) {
         $chartCount++;
         $shades = 0;
         $names = $values = array();
         foreach ($chartValues['values'] as $indexName => $indexValue) {
             $names[] = $indexName;
             $values[] = $indexValue;
             $shades++;
         }
         $legend = CRM_Utils_Array::value('legend', $chartValues);
         $xname = CRM_Utils_Array::value('xname', $chartValues);
         $yname = CRM_Utils_Array::value('yname', $chartValues);
         //calculate max scale for graph.
         $maxScale = ceil(max($values) * 1.1);
         $fontSize = 8;
         $angleOfIncline = 45;
         $monetaryformatting = true;
         require_once 'CRM/Utils/Money.php';
         $formatedMoney = CRM_Utils_Money::format(max($values));
         $positions = imageftbbox($fontSize, 0, $pChartPath . "tahoma.ttf", $formatedMoney);
         $scaleTextWidth = $positions[2] - $positions[0];
         //need to increase Ysize if we incline money value.
         $increaseYBy = 0;
         $inclinePositions = imageftbbox($fontSize, $angleOfIncline, $pChartPath . "tahoma.ttf", $formatedMoney);
         $inclineTextWidth = $inclinePositions[2] - $inclinePositions[0];
         if ($inclineTextWidth > $divisionWidth) {
             $increaseYBy = $inclineTextWidth / 2;
         }
         //Initialise the co-ordinates.
         $xComponent = 20;
         $yComponent = 35;
         $ySize = 300;
         //calculate coords.
         $x1 = $xComponent + $scaleTextWidth;
         $y1 = $yComponent + $increaseYBy;
         $ySize += $increaseYBy;
         $y2 = $ySize - $yComponent;
         //calculate x axis size as per number of months.
         $x2 = $xComponent + $divisionWidth + $scaleTextWidth + (count($chartValues['values']) - 1) * $divisionWidth;
         $xSize = $x2 + $xComponent;
         $dataSet = new pData();
         $dataSet->AddPoint($values, "Serie1");
         $dataSet->AddPoint($names, "Serie2");
         $dataSet->AddSerie("Serie1");
         $dataSet->SetAbsciseLabelSerie("Serie2");
         //Initialise the graph
         $chart = new pChart($xSize, $ySize);
         $chart->setFontProperties($pChartPath . "tahoma.ttf", $fontSize);
         $chart->setGraphArea($x1, $y1, $x2, $y2);
         //set the y axis scale.
         $chart->setFixedScale(0, $maxScale, 1);
         $chart->drawFilledRoundedRectangle(0, 0, $xSize, $ySize, 5, 240, 240, 240);
         $chart->drawRoundedRectangle(0, 0, $xSize, $ySize, 5, 230, 230, 230);
         $chart->drawGraphArea(255, 255, 255, TRUE);
         $chart->drawScale($dataSet->GetData(), $dataSet->GetDataDescription(), SCALE_NORMAL, 150, 150, 150, TRUE, 0, 2, TRUE, 1, FALSE, $divisionWidth, $monetaryformatting);
         $chart->drawGrid(4, TRUE, 230, 230, 230, 50);
         //set colors.
         $chart->setColorShades($shades, self::$_colors);
         //Draw the bar chart
         $chart->drawBarGraph($dataSet->GetData(), $dataSet->GetDataDescription(), TRUE, 80, true);
         //get the series values and write at top.
         $chart->setColorPalette(0, 0, 0, 255);
         $dataDesc = $dataSet->GetDataDescription();
         $chart->writeValues($dataSet->GetData(), $dataSet->GetDataDescription(), $dataDesc['Values'], $monetaryformatting, $angleOfIncline);
         //Write the title
         if ($legend) {
             $chart->setFontProperties($pChartPath . "tahoma.ttf", 10);
             $chart->drawTitle(10, 20, $legend, 50, 50, 50);
         }
         if ($xname) {
             $chart->setFontProperties($pChartPath . "tahoma.ttf", 8);
             $chart->drawTitle(0, 90, $xname, 2, 0, 2);
         }
         if ($yname) {
             $chart->setFontProperties($pChartPath . "tahoma.ttf", 8);
             $chart->drawTitle(40, 290, $yname, 2, 0, 20);
         }
         $fileName = "pChartByMonth{$chartCount}" . time() . '.png';
         $chart->Render($uploadDirPath . $fileName);
         //get the file path.
         $filesValues[$chartIndex]['file_name'] = $uploadDirURL . $fileName;
         //get the co-ordinates
         $coords = $chart->coordinates();
         //format the coordinates to make graph clickable.
         $position = 0;
         $chartCoords = array();
         foreach ($chartValues['values'] as $name => $value) {
             $chartCoords[$name] = implode(',', array($coords['xCoords'][$position], $coords['yCoords'][$position], $coords['xCoords'][$position] + $divisionWidth / 2, $y2));
             $position++;
         }
         $filesValues[$chartIndex]['coords'] = $chartCoords;
         //free the chart and data objects.
         unset($chart);
         unset($dataSet);
     }
     return $filesValues;
 }