From 468ded54a5a8b8456fe4c28c6dc8fb7011d55b0b Mon Sep 17 00:00:00 2001 From: Daniel Barreto Date: Mon, 17 Nov 2014 14:21:13 -0500 Subject: [PATCH 1/4] Fix pChart rotated legend - refs CT#7383 --- main/gradebook/lib/fe/flatviewtable.class.php | 9 ++-- main/inc/lib/pchart/pChart.class.php | 50 ++++++++++++++++++- 2 files changed, 54 insertions(+), 5 deletions(-) diff --git a/main/gradebook/lib/fe/flatviewtable.class.php b/main/gradebook/lib/fe/flatviewtable.class.php index 24b8442872..0b0f1abeaf 100755 --- a/main/gradebook/lib/fe/flatviewtable.class.php +++ b/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); diff --git a/main/inc/lib/pchart/pChart.class.php b/main/inc/lib/pchart/pChart.class.php index b8132ab32b..92b75ddd11 100755 --- a/main/inc/lib/pchart/pChart.class.php +++ b/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) From 0a5e7b6cf950b825e8501b790ff9daca4bb0f681 Mon Sep 17 00:00:00 2001 From: Daniel Barreto Date: Mon, 17 Nov 2014 14:41:34 -0500 Subject: [PATCH 2/4] Apply fix to dashboard block graphs - refs CT#7383 --- .../block_evaluation_graph.class.php | 20 +++++++++++-------- .../block_teacher_graph.class.php | 4 +++- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/plugin/dashboard/block_evaluation_graph/block_evaluation_graph.class.php b/plugin/dashboard/block_evaluation_graph/block_evaluation_graph.class.php index b9898cb797..bcd398388c 100755 --- a/plugin/dashboard/block_evaluation_graph/block_evaluation_graph.class.php +++ b/plugin/dashboard/block_evaluation_graph/block_evaluation_graph.class.php @@ -166,14 +166,16 @@ class BlockEvaluationGraph extends Block $img_file = $cache->GetHash($graph_id, $data); } else { // Initialise the graph + $angle = -30; $test = new pChart($this->bg_width,$this->bg_height); $test->setFontProperties(api_get_path(LIBRARY_PATH).'pchart/fonts/tahoma.ttf',8); - $test->setGraphArea(50,30,$this->bg_width-75,$this->bg_height-75); - $test->drawFilledRoundedRectangle(7,7,$this->bg_width-20,$this->bg_height-20,5,240,240,240); - $test->drawRoundedRectangle(5,5,$this->bg_width-18,$this->bg_height-18,5,230,230,230); + $test = $test->fixHeightByRotation($data_set->GetData(),$data_set->GetDataDescription(), $angle); + $test->setGraphArea(50,30,$this->bg_width-75,$test->YSize - 75); + $test->drawFilledRoundedRectangle(7,7,$this->bg_width-20,$test->YSize - 20,5,240,240,240); + $test->drawRoundedRectangle(5,5,$this->bg_width-18,$test->YSize - 18,5,230,230,230); $test->drawGraphArea(255,255,255,TRUE); $test->setFixedScale(0,100,5); - $test->drawScale($data_set->GetData(),$data_set->GetDataDescription(),SCALE_ADDALL,150,150,150,TRUE,0,2,TRUE); + $test->drawScale($data_set->GetData(),$data_set->GetDataDescription(),SCALE_ADDALL,150,150,150,TRUE,$angle,2,TRUE); $test->setColorPalette(0,105,221,34); $test->setColorPalette(1,255,135,30); $test->setColorPalette(2,255,0,0); @@ -258,14 +260,16 @@ class BlockEvaluationGraph extends Block $img_file = $cache->GetHash($graph_id, $data); } else { // Initialise the graph + $angle = -30; $test = new pChart($this->bg_width,$this->bg_height); $test->setFontProperties(api_get_path(LIBRARY_PATH).'pchart/fonts/tahoma.ttf',8); - $test->setGraphArea(50,30,$this->bg_width-75,$this->bg_height-75); - $test->drawFilledRoundedRectangle(7,7,$this->bg_width-20,$this->bg_height-20,5,240,240,240); - $test->drawRoundedRectangle(5,5,$this->bg_width-18,$this->bg_height-18,5,230,230,230); + $test->fixHeightByRotation($data_set->GetData(),$data_set->GetDataDescription(), $angle); + $test->setGraphArea(50,30,$this->bg_width-75,$test->YSize-75); + $test->drawFilledRoundedRectangle(7,7,$this->bg_width-20,$test->YSize-20,5,240,240,240); + $test->drawRoundedRectangle(5,5,$this->bg_width-18,$test->YSize-18,5,230,230,230); $test->drawGraphArea(255,255,255,TRUE); $test->setFixedScale(0,100,5); - $test->drawScale($data_set->GetData(),$data_set->GetDataDescription(),SCALE_ADDALL,150,150,150,TRUE,0,2,TRUE); + $test->drawScale($data_set->GetData(),$data_set->GetDataDescription(),SCALE_ADDALL,150,150,150,TRUE,$angle,2,TRUE); $test->setColorPalette(0,105,221,34); $test->setColorPalette(1,255,135,30); $test->setColorPalette(2,255,0,0); diff --git a/plugin/dashboard/block_teacher_graph/block_teacher_graph.class.php b/plugin/dashboard/block_teacher_graph/block_teacher_graph.class.php index bd81c97a7d..1218cc5786 100755 --- a/plugin/dashboard/block_teacher_graph/block_teacher_graph.class.php +++ b/plugin/dashboard/block_teacher_graph/block_teacher_graph.class.php @@ -144,13 +144,15 @@ class BlockTeacherGraph extends Block // Initializing the graph $bg_width = 440; $bg_height = 350; + $angle = -30; $test = new pChart($bg_width+10,$bg_height+20); $test->setFontProperties(api_get_path(LIBRARY_PATH).'pchart/fonts/tahoma.ttf',8); + $test = $test->fixHeightByRotation($data_set->GetData(),$data_set->GetDataDescription(), $angle); $test->setGraphArea(65,30,$bg_width-70,$bg_height-50); $test->drawFilledRoundedRectangle(7,7,$bg_width,$bg_height,5,240,240,240); $test->drawRoundedRectangle(5,5,$bg_width+2,$bg_height+2,5,230,230,230); $test->drawGraphArea(255,255,255,TRUE); - $test->drawScale($data_set->GetData(),$data_set->GetDataDescription(),SCALE_NORMAL,150,150,150,TRUE,0,2,TRUE); + $test->drawScale($data_set->GetData(),$data_set->GetDataDescription(),SCALE_NORMAL,150,150,150,TRUE,$angle,2,TRUE); $test->drawGrid(4,TRUE,230,230,230,50); // Drawing lines From 6c3ab74967feb12a0204e5f3e79f50619a36b606 Mon Sep 17 00:00:00 2001 From: Daniel Barreto Date: Mon, 17 Nov 2014 14:57:35 -0500 Subject: [PATCH 3/4] Apply fix to gradebook graphs - refs CT#7383 --- main/gradebook/lib/fe/flatviewtable.class.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/main/gradebook/lib/fe/flatviewtable.class.php b/main/gradebook/lib/fe/flatviewtable.class.php index 0b0f1abeaf..ac08eea365 100755 --- a/main/gradebook/lib/fe/flatviewtable.class.php +++ b/main/gradebook/lib/fe/flatviewtable.class.php @@ -147,8 +147,14 @@ class FlatViewTable extends SortableTable } else { // if the image does not exist in the archive/ folder // Initialise the graph + $angle = -30; $Test = new pChart(760, 360); + // 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); + //which schema of color will be used $quant_resources = count($data[0]) - 1; // Adding the color schemma @@ -158,11 +164,9 @@ class FlatViewTable extends SortableTable $Test->loadColorPalette(api_get_path(LIBRARY_PATH) . "pchart/palette/default.txt"); } - // set font of the axes - $Test->setFontProperties(api_get_path(LIBRARY_PATH) . "pchart/fonts/tahoma.ttf", 8); $Test->setGraphArea(50, 30, 610, 300); - $Test->drawFilledRoundedRectangle(7, 7, 780, 330, 5, 240, 240, 240); + $Test->drawFilledRoundedRectangle(7, 7, $Test->XSize + 20, $Test->YSize - 30, 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); @@ -316,6 +320,7 @@ class FlatViewTable extends SortableTable // Initialise the graph $chart_size_w = 480; $chart_size_h = 250; + $angle = -30; $Test = new pChart($chart_size_w, $chart_size_h); From df256d657859b672571b2d3269d95cbd2b42ec93 Mon Sep 17 00:00:00 2001 From: Daniel Barreto Date: Mon, 17 Nov 2014 16:46:45 -0500 Subject: [PATCH 4/4] Minor - Improve code style - refs CT#7383 --- main/gradebook/lib/fe/flatviewtable.class.php | 30 ++++++- main/inc/lib/pchart/pChart.class.php | 20 +++-- .../block_evaluation_graph.class.php | 86 ++++++++++++++++--- .../block_teacher_graph.class.php | 15 +++- 4 files changed, 125 insertions(+), 26 deletions(-) diff --git a/main/gradebook/lib/fe/flatviewtable.class.php b/main/gradebook/lib/fe/flatviewtable.class.php index ac08eea365..290e86111d 100755 --- a/main/gradebook/lib/fe/flatviewtable.class.php +++ b/main/gradebook/lib/fe/flatviewtable.class.php @@ -327,7 +327,11 @@ class FlatViewTable extends SortableTable // 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); + $Test = $Test->fixHeightByRotation( + $DataSet->GetData(), + $DataSet->GetDataDescription(), + $angle + ); // Adding the color schemma $Test->loadColorPalette(api_get_path(LIBRARY_PATH) . "pchart/palette/pastel.txt"); @@ -335,7 +339,16 @@ class FlatViewTable extends SortableTable $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, $Test->YSize - 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); @@ -349,7 +362,18 @@ class FlatViewTable extends SortableTable } } - $Test->drawScale($DataSet->GetData(), $DataSet->GetDataDescription(), SCALE_ADDALLSTART0, 150, 150, 150, TRUE, 0, 1, FALSE); + $Test->drawScale( + $DataSet->GetData(), + $DataSet->GetDataDescription(), + SCALE_ADDALLSTART0, + 150, + 150, + 150, + TRUE, + 0, + 1, + FALSE + ); //background grid $Test->drawGrid(4, TRUE, 230, 230, 230, 50); diff --git a/main/inc/lib/pchart/pChart.class.php b/main/inc/lib/pchart/pChart.class.php index 92b75ddd11..45b56cdc04 100755 --- a/main/inc/lib/pchart/pChart.class.php +++ b/main/inc/lib/pchart/pChart.class.php @@ -3574,22 +3574,24 @@ function fixHeightByRotation ($data, $dataDescription, $angle) { $maxTextHeight = 0; - foreach ( $data as $key => $values ) - { + foreach ($data as $key => $values) { // Get legend value $value = $data[$key][$dataDescription["Position"]]; - if ( $dataDescription["Format"]["X"] == "number" ) + if ($dataDescription["Format"]["X"] == "number") { $value = $value.$dataDescription["Unit"]["X"]; - if ( $dataDescription["Format"]["X"] == "time" ) + } elseif ($dataDescription["Format"]["X"] == "time") { $value = $this->ToTime($value); - if ( $dataDescription["Format"]["X"] == "date" ) + } elseif ($dataDescription["Format"]["X"] == "date") { $value = $this->ToDate($value); - if ( $dataDescription["Format"]["X"] == "metric" ) + } elseif ($dataDescription["Format"]["X"] == "metric") { $value = $this->ToMetric($value); - if ( $dataDescription["Format"]["X"] == "currency" ) + } elseif ($dataDescription["Format"]["X"] == "currency") { $value = $this->ToCurrency($value); - $Position = imageftbbox($this->FontSize,$angle,$this->FontName,$value); - $TextHeight = abs($Position[1])+abs($Position[3]); + } + // Get positions array from label generated + $Position = imageftbbox($this->FontSize, $angle, $this->FontName, $value); + $TextHeight = abs($Position[1]) + abs($Position[3]); + // Save max height if ($maxTextHeight < $TextHeight) { $maxTextHeight = $TextHeight; } diff --git a/plugin/dashboard/block_evaluation_graph/block_evaluation_graph.class.php b/plugin/dashboard/block_evaluation_graph/block_evaluation_graph.class.php index bcd398388c..6e93224ef8 100755 --- a/plugin/dashboard/block_evaluation_graph/block_evaluation_graph.class.php +++ b/plugin/dashboard/block_evaluation_graph/block_evaluation_graph.class.php @@ -169,13 +169,46 @@ class BlockEvaluationGraph extends Block $angle = -30; $test = new pChart($this->bg_width,$this->bg_height); $test->setFontProperties(api_get_path(LIBRARY_PATH).'pchart/fonts/tahoma.ttf',8); - $test = $test->fixHeightByRotation($data_set->GetData(),$data_set->GetDataDescription(), $angle); - $test->setGraphArea(50,30,$this->bg_width-75,$test->YSize - 75); - $test->drawFilledRoundedRectangle(7,7,$this->bg_width-20,$test->YSize - 20,5,240,240,240); - $test->drawRoundedRectangle(5,5,$this->bg_width-18,$test->YSize - 18,5,230,230,230); - $test->drawGraphArea(255,255,255,TRUE); - $test->setFixedScale(0,100,5); - $test->drawScale($data_set->GetData(),$data_set->GetDataDescription(),SCALE_ADDALL,150,150,150,TRUE,$angle,2,TRUE); + $test = $test->fixHeightByRotation( + $data_set->GetData(), + $data_set->GetDataDescription(), + $angle + ); + $test->setGraphArea(50, 30, $this->bg_width-75, $test->YSize - 75); + $test->drawFilledRoundedRectangle( + 7, + 7, + $this->bg_width-20, + $test->YSize - 20, + 5, + 240, + 240, + 240 + ); + $test->drawRoundedRectangle( + 5, + 5, + $this->bg_width-18, + $test->YSize - 18, + 5, + 230, + 230, + 230 + ); + $test->drawGraphArea(255,255,255,TRUE); + $test->setFixedScale(0,100,5); + $test->drawScale( + $data_set->GetData(), + $data_set->GetDataDescription(), + SCALE_ADDALL, + 150, + 150, + 150, + TRUE, + $angle, + 2, + TRUE + ); $test->setColorPalette(0,105,221,34); $test->setColorPalette(1,255,135,30); $test->setColorPalette(2,255,0,0); @@ -263,13 +296,42 @@ class BlockEvaluationGraph extends Block $angle = -30; $test = new pChart($this->bg_width,$this->bg_height); $test->setFontProperties(api_get_path(LIBRARY_PATH).'pchart/fonts/tahoma.ttf',8); - $test->fixHeightByRotation($data_set->GetData(),$data_set->GetDataDescription(), $angle); - $test->setGraphArea(50,30,$this->bg_width-75,$test->YSize-75); - $test->drawFilledRoundedRectangle(7,7,$this->bg_width-20,$test->YSize-20,5,240,240,240); - $test->drawRoundedRectangle(5,5,$this->bg_width-18,$test->YSize-18,5,230,230,230); + $test->fixHeightByRotation($data_set->GetData(), $data_set->GetDataDescription(), $angle); + $test->setGraphArea(50, 30, $this->bg_width-75, $test->YSize-75); + $test->drawFilledRoundedRectangle( + 7, + 7, + $this->bg_width-20, + $test->YSize-20, + 5, + 240, + 240, + 240 + ); + $test->drawRoundedRectangle( + 5, + 5, + $this->bg_width-18, + $test->YSize-18, + 5, + 230, + 230, + 230 + ); $test->drawGraphArea(255,255,255,TRUE); $test->setFixedScale(0,100,5); - $test->drawScale($data_set->GetData(),$data_set->GetDataDescription(),SCALE_ADDALL,150,150,150,TRUE,$angle,2,TRUE); + $test->drawScale( + $data_set->GetData(), + $data_set->GetDataDescription(), + SCALE_ADDALL, + 150, + 150, + 150, + TRUE, + $angle, + 2, + TRUE + ); $test->setColorPalette(0,105,221,34); $test->setColorPalette(1,255,135,30); $test->setColorPalette(2,255,0,0); diff --git a/plugin/dashboard/block_teacher_graph/block_teacher_graph.class.php b/plugin/dashboard/block_teacher_graph/block_teacher_graph.class.php index 1218cc5786..7fd2ae7adb 100755 --- a/plugin/dashboard/block_teacher_graph/block_teacher_graph.class.php +++ b/plugin/dashboard/block_teacher_graph/block_teacher_graph.class.php @@ -147,12 +147,23 @@ class BlockTeacherGraph extends Block $angle = -30; $test = new pChart($bg_width+10,$bg_height+20); $test->setFontProperties(api_get_path(LIBRARY_PATH).'pchart/fonts/tahoma.ttf',8); - $test = $test->fixHeightByRotation($data_set->GetData(),$data_set->GetDataDescription(), $angle); + $test = $test->fixHeightByRotation($data_set->GetData(), $data_set->GetDataDescription(), $angle); $test->setGraphArea(65,30,$bg_width-70,$bg_height-50); $test->drawFilledRoundedRectangle(7,7,$bg_width,$bg_height,5,240,240,240); $test->drawRoundedRectangle(5,5,$bg_width+2,$bg_height+2,5,230,230,230); $test->drawGraphArea(255,255,255,TRUE); - $test->drawScale($data_set->GetData(),$data_set->GetDataDescription(),SCALE_NORMAL,150,150,150,TRUE,$angle,2,TRUE); + $test->drawScale( + $data_set->GetData(), + $data_set->GetDataDescription(), + SCALE_NORMAL, + 150, + 150, + 150, + TRUE, + $angle, + 2, + TRUE + ); $test->drawGrid(4,TRUE,230,230,230,50); // Drawing lines