2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Holds the base class that all charts using pChart inherit from and some
5 * widely used constants
6 * @author Martynas Mickevicius <mmartynas@gmail.com>
18 require_once 'pma_chart.php';
20 require_once 'pChart/pData.class';
21 require_once 'pChart/pChart.class';
24 * Base class for every chart implemented using pChart.
28 abstract class PMA_pChart_chart
extends PMA_chart
31 * @var String title text
36 * @var array data for the chart
41 * @var object pData object that holds the description of the data
46 * @var object pChart object that holds the chart
51 * @var array holds base64 encoded chart image parts
53 protected $partsEncoded = array();
55 public function __construct($data, $options = null)
57 parent
::__construct($options);
61 $this->settings
['fontPath'] = './libraries/chart/pChart/fonts/';
63 $this->settings
['scale'] = SCALE_ADDALLSTART0
;
65 $this->settings
['labelHeight'] = 20;
67 $this->settings
['fontSize'] = 8;
69 $this->settings
['continuous'] = 'off';
71 // as in CSS (top, right, bottom, left)
72 $this->setAreaMargins(array(20, 20, 40, 60));
74 // when graph area gradient is used, this is the color of the graph
76 $this->settings
['graphAreaColor'] = '#D5D9DD';
78 // the background color of the graph area
79 $this->settings
['graphAreaGradientColor'] = '#A3CBA7';
81 // the color of the grid lines in the graph area
82 $this->settings
['gridColor'] = '#E6E6E6';
84 // the color of the scale and the labels
85 $this->settings
['scaleColor'] = '#D5D9DD';
87 $this->settings
['titleBgColor'] = '#000000';
90 protected function init()
94 // create pChart object
95 $this->chart
= new pChart($this->getWidth(), $this->getHeight());
97 // create pData object
98 $this->dataSet
= new pData
;
100 $this->chart
->reportWarnings('GD');
101 $this->chart
->ErrorFontName
= $this->getFontPath().'tahoma.ttf';
104 foreach ($this->getColors() as $key => $color) {
105 $this->chart
->setColorPalette(
107 hexdec(substr($color, 1, 2)),
108 hexdec(substr($color, 3, 2)),
109 hexdec(substr($color, 5, 2))
113 $this->chart
->setFontProperties($this->getFontPath().'tahoma.ttf', $this->getFontSize());
115 $this->chart
->setImageMap(true, 'mapid');
119 * data is put to the $dataSet object according to what type chart is
122 abstract protected function prepareDataSet();
125 * all components of the chart are drawn
127 protected function prepareChart()
129 $this->drawBackground();
134 * draws the background
136 protected function drawBackground()
140 $this->setGraphAreaDimensions();
141 $this->drawGraphArea();
145 * draws the part of the background which is common to most of the charts
147 protected function drawCommon()
149 $this->chart
->drawGraphAreaGradient(
150 $this->getBgColor(RED
),
151 $this->getBgColor(GREEN
),
152 $this->getBgColor(BLUE
),
153 50,TARGET_BACKGROUND
);
154 $this->chart
->addBorder(2);
158 * draws the chart title
160 protected function drawTitle()
163 $this->chart
->drawTextBox(
167 $this->getLabelHeight(),
168 $this->getTitleText(),
170 $this->getTitleColor(RED
),
171 $this->getTitleColor(GREEN
),
172 $this->getTitleColor(BLUE
),
175 $this->getTitleBgColor(RED
),
176 $this->getTitleBgColor(GREEN
),
177 $this->getTitleBgColor(BLUE
),
183 * calculates and sets the dimensions that will be used for the actual graph
185 protected function setGraphAreaDimensions()
187 $this->chart
->setGraphArea(
188 $this->getAreaMargin(LEFT
),
189 $this->getLabelHeight() +
$this->getAreaMargin(TOP
),
190 $this->getWidth() - $this->getAreaMargin(RIGHT
),
191 $this->getHeight() - $this->getAreaMargin(BOTTOM
)
196 * draws graph area (the area where all bars, lines, points will be seen)
198 protected function drawGraphArea()
200 $this->chart
->drawGraphArea(
201 $this->getGraphAreaColor(RED
),
202 $this->getGraphAreaColor(GREEN
),
203 $this->getGraphAreaColor(BLUE
),
206 $this->chart
->drawScale(
207 $this->dataSet
->GetData(),
208 $this->dataSet
->GetDataDescription(),
210 $this->getScaleColor(RED
),
211 $this->getScaleColor(GREEN
),
212 $this->getScaleColor(BLUE
),
215 $this->chart
->drawGraphAreaGradient(
216 $this->getGraphAreaGradientColor(RED
),
217 $this->getGraphAreaGradientColor(GREEN
),
218 $this->getGraphAreaGradientColor(BLUE
),
221 $this->chart
->drawGrid(
224 $this->getGridColor(RED
),
225 $this->getGridColor(GREEN
),
226 $this->getGridColor(BLUE
),
235 protected abstract function drawChart();
238 * Renders the chart, base 64 encodes the output and puts it into
239 * array partsEncoded.
241 * Parameter can be used to slice the chart vertically into parts. This
242 * solves an issue where some browsers (IE8) accept base64 images only up
245 * @param integer $parts number of parts to render.
246 * Default value 1 means that all the
247 * chart will be in one piece.
249 protected function render($parts = 1)
253 for ($i = 0; $i < $parts; $i++
) {
255 // slicing is vertical so part height is the full height
256 $partHeight = $this->chart
->YSize
;
258 // there will be some rounding erros, will compensate later
259 $partWidth = round($this->chart
->XSize
/ $parts);
260 $fullWidth +
= $partWidth;
261 $partX = $partWidth * $i;
263 if ($i == $parts - 1) {
264 // if this is the last part, compensate for the rounding errors
265 $partWidth +
= $this->chart
->XSize
- $fullWidth;
268 // get a part from the full chart image
269 $part = imagecreatetruecolor($partWidth, $partHeight);
270 imagecopy($part, $this->chart
->Picture
, 0, 0, $partX, 0, $partWidth, $partHeight);
272 // render part and save it to variable
274 imagepng($part, NULL, 9, PNG_ALL_FILTERS
);
275 $output = ob_get_contents();
278 // base64 encode the current part
279 $partEncoded = base64_encode($output);
280 $this->partsEncoded
[$i] = $partEncoded;
285 * get the HTML and JS code for the configured chart
286 * @return string HTML and JS code for the chart
288 public function toString()
290 if (!function_exists('gd_info')) {
291 array_push($this->errors
, ERR_NO_GD
);
296 $this->prepareDataSet();
297 $this->prepareChart();
299 //$this->chart->debugImageMap();
300 //$this->chart->printErrors('GD');
302 // check if a user wanted a chart in one part
303 if ($this->isContinuous()) {
310 $returnData = '<div id="chart">';
311 foreach ($this->partsEncoded
as $part) {
312 $returnData .= '<img src="data:image/png;base64,'.$part.'" />';
314 $returnData .= '</div>';
316 // add tooltips only if json is available
317 if (function_exists('json_encode')) {
319 <script type="text/javascript">
321 imageMap.loadImageMap(\''.json_encode($this->getImageMap()).'\');
327 array_push($this->errors
, ERR_NO_JSON
);
333 protected function getLabelHeight()
335 return $this->settings
['labelHeight'];
338 protected function setAreaMargins($areaMargins)
340 $this->settings
['areaMargins'] = $areaMargins;
343 protected function getAreaMargin($side)
345 return $this->settings
['areaMargins'][$side];
348 protected function getFontPath()
350 return $this->settings
['fontPath'];
353 protected function getScale()
355 return $this->settings
['scale'];
358 protected function getFontSize()
360 return $this->settings
['fontSize'];
363 protected function isContinuous()
365 return $this->settings
['continuous'] == 'on';
368 protected function getImageMap()
370 return $this->chart
->getImageMap();
373 protected function getGraphAreaColor($component)
375 return $this->hexStrToDecComp($this->settings
['graphAreaColor'], $component);
378 protected function getGraphAreaGradientColor($component)
380 return $this->hexStrToDecComp($this->settings
['graphAreaGradientColor'], $component);
383 protected function getGridColor($component)
385 return $this->hexStrToDecComp($this->settings
['gridColor'], $component);
388 protected function getScaleColor($component)
390 return $this->hexStrToDecComp($this->settings
['scaleColor'], $component);
393 protected function getTitleBgColor($component)
395 return $this->hexStrToDecComp($this->settings
['titleBgColor'], $component);