Skip to content

Commit

Permalink
Fix pChart rotated legend - refs CT#7383
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Barreto committed Nov 17, 2014
1 parent 8eab6cb commit 468ded5
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
9 changes: 6 additions & 3 deletions main/gradebook/lib/fe/flatviewtable.class.php
Expand Up @@ -319,15 +319,18 @@ public function display_graph_by_resource()

$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);
Expand Down
50 changes: 48 additions & 2 deletions main/inc/lib/pchart/pChart.class.php
Expand Up @@ -586,6 +586,7 @@ function drawScale($Data,$DataDescription,$ScaleMode,$R,$G,$B,$DrawTicks=TRUE,$A
/* Horizontal Axis */

This comment has been minimized.

Copy link
@jmontoyaa

jmontoyaa Nov 18, 2014

Member

This is very very bad. We should not update a third party library!
Did you try updating the lib to the latest version?

This comment has been minimized.

Copy link
@danbarretodev

danbarretodev Nov 18, 2014

Contributor

Sorry about this, it's true, should not modify thirdparty libraries, but tried to upgrade to pChart 2.1.4 version but there are a lot of difference from 1.27 version.
I talked to Yannick about this and pChart library should be upgraded for 1.9.12, until them this would be just ok as a temporal fix

This comment has been minimized.

Copy link
@jmontoyaa

jmontoyaa Nov 19, 2014

Member

ok no problem then!

$XPos = $this->GArea_X1 + $this->GAreaXOffset;
$ID = 1; $YMax = NULL;
$maxTextHeight = 0;
foreach ( $Data as $Key => $Values )
{
if ( $ID % $SkipLabels == 0 )
Expand All @@ -607,14 +608,20 @@ function drawScale($Data,$DataDescription,$ScaleMode,$R,$G,$B,$DrawTicks=TRUE,$A
$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;
imagettftext($this->Picture,$this->FontSize,$Angle,floor($XPos)-floor($TextWidth/2),$YPos,$C_TextColor,$this->FontName,$Value);
}
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
Expand All @@ -633,7 +640,7 @@ function drawScale($Data,$DataDescription,$ScaleMode,$R,$G,$B,$DrawTicks=TRUE,$A
$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"]);
}
}

Expand Down Expand Up @@ -3555,6 +3562,45 @@ function isRealInt($Value)
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)
Expand Down

0 comments on commit 468ded5

Please sign in to comment.