From 9970ce91da7655537ff442e98e987612f5bd39d0 Mon Sep 17 00:00:00 2001 From: Martynas Mickevicius Date: Mon, 21 Jun 2010 17:34:50 +0300 Subject: [PATCH] restructured the code to be more OOP friendly --- libraries/chart/pma_pchart_chart.php | 37 ++++++++++++++++++--- libraries/chart/pma_pchart_pie.php | 49 ++++++++++++---------------- 2 files changed, 53 insertions(+), 33 deletions(-) diff --git a/libraries/chart/pma_pchart_chart.php b/libraries/chart/pma_pchart_chart.php index fbf1a1c69..356f435a4 100644 --- a/libraries/chart/pma_pchart_chart.php +++ b/libraries/chart/pma_pchart_chart.php @@ -2,22 +2,51 @@ require_once 'pma_chart.php'; +include "pChart/pData.class"; +include "pChart/pChart.class"; + /* * Base class for every chart implemented using pChart. */ -class PMA_pChart_Chart extends PMA_Chart +abstract class PMA_pChart_Chart extends PMA_Chart { + protected $titleText; + protected $data; + + protected $dataSet; + protected $chart; + protected $imageEncoded; protected $fontPath = './libraries/chart/pChart/fonts/'; - function __construct($options = null) + public function __construct($titleText, $data, $options = null) { parent::__construct($options); - } - function toString() + $this->titleText = $titleText; + $this->data = $data; + } + + abstract protected function prepareDataSet(); + abstract protected function prepareChart(); + + protected function render() { + ob_start(); + imagepng($this->chart->Picture); + $output = ob_get_contents(); + ob_end_clean(); + + $this->imageEncoded = base64_encode($output); + } + + public function toString() + { + $this->prepareDataSet(); + $this->prepareChart(); + $this->render(); + return ''; } } diff --git a/libraries/chart/pma_pchart_pie.php b/libraries/chart/pma_pchart_pie.php index 4bf886fa2..9b9f0d08c 100644 --- a/libraries/chart/pma_pchart_pie.php +++ b/libraries/chart/pma_pchart_pie.php @@ -7,32 +7,30 @@ class PMA_pChart_Pie extends PMA_pChart_Chart private $border1Width = 7; private $border2Width = 5; - function __construct($titleText, $data, $options = null) + protected function prepareDataSet() { - parent::__construct($options); - - require_once './libraries/chart/pChart/pData.class'; - require_once './libraries/chart/pChart/pChart.class'; - // Dataset definition - $dataSet = new pData; - $dataSet->AddPoint(array_values($data),"Values"); - $dataSet->AddPoint(array_keys($data),"Keys"); - $dataSet->AddAllSeries(); - $dataSet->SetAbsciseLabelSerie("Keys"); + $this->dataSet = new pData; + $this->dataSet->AddPoint(array_values($this->data),"Values"); + $this->dataSet->AddPoint(array_keys($this->data),"Keys"); + $this->dataSet->AddAllSeries(); + $this->dataSet->SetAbsciseLabelSerie("Keys"); + } + protected function prepareChart() + { // Initialise the graph - $chart = new pChart($this->width, $this->height); + $this->chart = new pChart($this->width, $this->height); foreach ($this->colors as $key => $color) { - $chart->setColorPalette( + $this->chart->setColorPalette( $key, hexdec(substr($color, 1, 2)), hexdec(substr($color, 3, 2)), hexdec(substr($color, 5, 2)) ); } - $chart->setFontProperties($this->fontPath.'tahoma.ttf', 8); - $chart->drawFilledRoundedRectangle( + $this->chart->setFontProperties($this->fontPath.'tahoma.ttf', 8); + $this->chart->drawFilledRoundedRectangle( $this->border1Width, $this->border1Width, $this->width - $this->border1Width, @@ -42,7 +40,7 @@ class PMA_pChart_Pie extends PMA_pChart_Chart $this->getBgColorComp(1), $this->getBgColorComp(2) ); - $chart->drawRoundedRectangle( + $this->chart->drawRoundedRectangle( $this->border1Width, $this->border1Width, $this->width - $this->border1Width, @@ -50,22 +48,15 @@ class PMA_pChart_Pie extends PMA_pChart_Chart 5,0,0,0); // Draw the pie chart - $chart->AntialiasQuality = 0; - $chart->setShadowProperties(2,2,200,200,200); + $this->chart->AntialiasQuality = 0; + $this->chart->setShadowProperties(2,2,200,200,200); //$Test->drawFlatPieGraphWithShadow($DataSet->GetData(),$DataSet->GetDataDescription(),180,160,120,PIE_PERCENTAGE,8); //$Test->drawBasicPieGraph($DataSet->GetData(),$DataSet->GetDataDescription(),180,160,120,PIE_PERCENTAGE,255,255,218,2); - $chart->drawPieGraph($dataSet->GetData(),$dataSet->GetDataDescription(),180,160,120,PIE_PERCENTAGE,FALSE,60,30,10,1); - $chart->clearShadow(); + $this->chart->drawPieGraph($this->dataSet->GetData(),$this->dataSet->GetDataDescription(),180,160,120,PIE_PERCENTAGE,FALSE,60,30,10,1); + $this->chart->clearShadow(); - $chart->drawTitle(20,20,$titleText,0,0,0); - $chart->drawPieLegend(350,15,$dataSet->GetData(),$dataSet->GetDataDescription(),250,250,250); - - ob_start(); - imagepng($chart->Picture); - $output = ob_get_contents(); - ob_end_clean(); - - $this->imageEncoded = base64_encode($output); + $this->chart->drawTitle(20,20,$this->titleText,0,0,0); + $this->chart->drawPieLegend(350,15,$this->dataSet->GetData(),$this->dataSet->GetDataDescription(),250,250,250); } }