Browse Source

Fix pChart rotated legend - refs CT#7383

Daniel Barreto 10 years ago
parent
commit
468ded54a5

+ 6 - 3
main/gradebook/lib/fe/flatviewtable.class.php

@@ -319,15 +319,18 @@ class FlatViewTable extends SortableTable
 
                             $Test = new pChart($chart_size_w, $chart_size_h);
 
+                            // set font of the axes
+                            $Test->setFontProperties(api_get_path(LIBRARY_PATH) . "pchart/fonts/tahoma.ttf", 8);
+
+                            $Test = $Test->fixHeightByRotation($DataSet->GetData(), $DataSet->GetDataDescription(), $angle);
+
                             // Adding the color schemma
                             $Test->loadColorPalette(api_get_path(LIBRARY_PATH) . "pchart/palette/pastel.txt");
 
-                            // set font of the axes
-                            $Test->setFontProperties(api_get_path(LIBRARY_PATH) . "pchart/fonts/tahoma.ttf", 8);
                             $area_graph_w = $chart_size_w - 130;
                             $Test->setGraphArea(50, 30, $area_graph_w, $chart_size_h - 50);
 
-                            $Test->drawFilledRoundedRectangle(5, 5, $chart_size_w - 1, $chart_size_h - 20, 5, 240, 240, 240);
+                            $Test->drawFilledRoundedRectangle(5, 5, $chart_size_w - 1, $Test->YSize - 20, 5, 240, 240, 240);
                             //$Test->drawRoundedRectangle(5,5,790,330,5,230,230,230);
                             //background color area & stripe or not
                             $Test->drawGraphArea(255, 255, 255, TRUE);

+ 48 - 2
main/inc/lib/pchart/pChart.class.php

@@ -586,6 +586,7 @@
      /* Horizontal Axis */
      $XPos = $this->GArea_X1 + $this->GAreaXOffset;
      $ID = 1; $YMax = NULL;
+     $maxTextHeight = 0;
      foreach ( $Data as $Key => $Values )
       {
        if ( $ID % $SkipLabels == 0 )
@@ -607,6 +608,10 @@
          $TextWidth  = abs($Position[2])+abs($Position[0]);
          $TextHeight = abs($Position[1])+abs($Position[3]);
 
+            // Save max text height
+            if ($maxTextHeight < $TextHeight) {
+                $maxTextHeight = $TextHeight;
+            }
          if ( $Angle == 0 )
           {
            $YPos = $this->GArea_Y2+18;
@@ -614,7 +619,9 @@
           }
          else
           {
-           $YPos = $this->GArea_Y2+10+$TextHeight;
+           $YPos = $this->GArea_Y2+10;
+              // If rotation is top down, then add TextHeight;
+              $YPos = (sin($Angle) < 0) ? $YPos + $TextHeight : $YPos;
            if ( $Angle <= 90 )
             imagettftext($this->Picture,$this->FontSize,$Angle,floor($XPos)-$TextWidth+5,$YPos,$C_TextColor,$this->FontName,$Value);
            else
@@ -633,7 +640,7 @@
        $Position   = imageftbbox($this->FontSize,90,$this->FontName,$DataDescription["Axis"]["X"]);
        $TextWidth  = abs($Position[2])+abs($Position[0]);
        $TextLeft   = (($this->GArea_X2 - $this->GArea_X1) / 2) + $this->GArea_X1 + ($TextWidth/2);
-       imagettftext($this->Picture,$this->FontSize,0,$TextLeft,$YMax+$this->FontSize+5,$C_TextColor,$this->FontName,$DataDescription["Axis"]["X"]);
+       imagettftext($this->Picture,$this->FontSize,0,$TextLeft,$YMax+$this->FontSize+5+$maxTextHeight,$C_TextColor,$this->FontName,$DataDescription["Axis"]["X"]);
       }
     }
 
@@ -3555,6 +3562,45 @@
       return(TRUE);
      return(FALSE);
     }
+
+     /**
+      * Return a new pChart object fixing its height by legend rotations
+      * Must be set fonts before call this method
+      * @param array $data
+      * @param array $dataDescription
+      * @param float $angle
+      * @return pChart
+      */
+     function fixHeightByRotation ($data, $dataDescription, $angle)
+     {
+         $maxTextHeight = 0;
+         foreach ( $data as $key => $values )
+         {
+             // Get legend value
+             $value = $data[$key][$dataDescription["Position"]];
+             if ( $dataDescription["Format"]["X"] == "number" )
+                 $value = $value.$dataDescription["Unit"]["X"];
+             if ( $dataDescription["Format"]["X"] == "time" )
+                 $value = $this->ToTime($value);
+             if ( $dataDescription["Format"]["X"] == "date" )
+                 $value = $this->ToDate($value);
+             if ( $dataDescription["Format"]["X"] == "metric" )
+                 $value = $this->ToMetric($value);
+             if ( $dataDescription["Format"]["X"] == "currency" )
+                 $value = $this->ToCurrency($value);
+             $Position   = imageftbbox($this->FontSize,$angle,$this->FontName,$value);
+             $TextHeight = abs($Position[1])+abs($Position[3]);
+             if ($maxTextHeight < $TextHeight) {
+                 $maxTextHeight = $TextHeight;
+             }
+         }
+         $this->YSize += $maxTextHeight;
+         $pChart = new pChart($this->XSize, $this->YSize);
+         // set font of the axes
+         $pChart->setFontProperties($this->FontName, $this->FontSize);
+
+         return $pChart;
+     }
   }
 
  function RaiseFatal($Message)