3.4.0-rc1
[phpmyadmin/thilanka.git] / libraries / chart / pma_pchart_chart.php
blob1dac4cfb2eb4166661959fd85082a225f4fae85a
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Holds the base class that all charts using pChart inherit from and some
5 * widely used constants
6 * @package phpMyAdmin
7 */
9 /**
12 define('TOP', 0);
13 define('RIGHT', 1);
14 define('BOTTOM', 2);
15 define('LEFT', 3);
17 require_once 'pma_chart.php';
19 require_once 'pChart/pData.class';
20 require_once 'pChart/pChart.class';
22 /**
23 * Base class for every chart implemented using pChart.
24 * @abstract
25 * @package phpMyAdmin
27 abstract class PMA_pChart_chart extends PMA_chart
29 /**
30 * @var String title text
32 protected $titleText;
34 /**
35 * @var array data for the chart
37 protected $data;
39 /**
40 * @var object pData object that holds the description of the data
42 protected $dataSet;
44 /**
45 * @var object pChart object that holds the chart
47 protected $chart;
49 /**
50 * @var array holds base64 encoded chart image parts
52 protected $partsEncoded = array();
54 public function __construct($data, $options = null)
56 parent::__construct($options);
58 $this->data = $data;
60 $this->settings['fontPath'] = './libraries/chart/pChart/fonts/';
62 $this->settings['scale'] = SCALE_ADDALLSTART0;
64 $this->settings['labelHeight'] = 20;
66 $this->settings['fontSize'] = 8;
68 $this->settings['continuous'] = 'off';
70 // as in CSS (top, right, bottom, left)
71 $this->setAreaMargins(array(20, 20, 40, 60));
73 // when graph area gradient is used, this is the color of the graph
74 // area border
75 $this->settings['graphAreaColor'] = '#D5D9DD';
77 // the background color of the graph area
78 $this->settings['graphAreaGradientColor'] = '#A3CBA7';
80 // the color of the grid lines in the graph area
81 $this->settings['gridColor'] = '#E6E6E6';
83 // the color of the scale and the labels
84 $this->settings['scaleColor'] = '#D5D9DD';
86 $this->settings['titleBgColor'] = '#000000';
89 protected function init()
91 parent::init();
93 // create pChart object
94 $this->chart = new pChart($this->getWidth(), $this->getHeight());
96 // create pData object
97 $this->dataSet = new pData;
99 $this->chart->reportWarnings('GD');
100 $this->chart->ErrorFontName = $this->getFontPath().'DejaVuSans.ttf';
102 // initialize colors
103 foreach ($this->getColors() as $key => $color) {
104 $this->chart->setColorPalette(
105 $key,
106 hexdec(substr($color, 1, 2)),
107 hexdec(substr($color, 3, 2)),
108 hexdec(substr($color, 5, 2))
112 $this->chart->setFontProperties($this->getFontPath().'DejaVuSans.ttf', $this->getFontSize());
114 $this->chart->setImageMap(true, 'mapid');
118 * data is put to the $dataSet object according to what type chart is
119 * @abstract
121 abstract protected function prepareDataSet();
124 * all components of the chart are drawn
126 protected function prepareChart()
128 $this->drawBackground();
129 $this->drawChart();
133 * draws the background
135 protected function drawBackground()
137 $this->drawCommon();
138 $this->drawTitle();
139 $this->setGraphAreaDimensions();
140 $this->drawGraphArea();
144 * draws the part of the background which is common to most of the charts
146 protected function drawCommon()
148 $this->chart->drawGraphAreaGradient(
149 $this->getBgColor(RED),
150 $this->getBgColor(GREEN),
151 $this->getBgColor(BLUE),
152 50,TARGET_BACKGROUND);
153 $this->chart->addBorder(2);
157 * draws the chart title
159 protected function drawTitle()
161 // Draw the title
162 $this->chart->drawTextBox(
165 $this->getWidth(),
166 $this->getLabelHeight(),
167 $this->getTitleText(),
169 $this->getTitleColor(RED),
170 $this->getTitleColor(GREEN),
171 $this->getTitleColor(BLUE),
172 ALIGN_CENTER,
173 True,
174 $this->getTitleBgColor(RED),
175 $this->getTitleBgColor(GREEN),
176 $this->getTitleBgColor(BLUE),
182 * calculates and sets the dimensions that will be used for the actual graph
184 protected function setGraphAreaDimensions()
186 $this->chart->setGraphArea(
187 $this->getAreaMargin(LEFT),
188 $this->getLabelHeight() + $this->getAreaMargin(TOP),
189 $this->getWidth() - $this->getAreaMargin(RIGHT),
190 $this->getHeight() - $this->getAreaMargin(BOTTOM)
195 * draws graph area (the area where all bars, lines, points will be seen)
197 protected function drawGraphArea()
199 $this->chart->drawGraphArea(
200 $this->getGraphAreaColor(RED),
201 $this->getGraphAreaColor(GREEN),
202 $this->getGraphAreaColor(BLUE),
203 FALSE
205 $this->chart->drawScale(
206 $this->dataSet->GetData(),
207 $this->dataSet->GetDataDescription(),
208 $this->getScale(),
209 $this->getScaleColor(RED),
210 $this->getScaleColor(GREEN),
211 $this->getScaleColor(BLUE),
212 TRUE,0,2,TRUE
214 $this->chart->drawGraphAreaGradient(
215 $this->getGraphAreaGradientColor(RED),
216 $this->getGraphAreaGradientColor(GREEN),
217 $this->getGraphAreaGradientColor(BLUE),
220 $this->chart->drawGrid(
222 TRUE,
223 $this->getGridColor(RED),
224 $this->getGridColor(GREEN),
225 $this->getGridColor(BLUE),
231 * draws the chart
232 * @abstract
234 protected abstract function drawChart();
237 * Renders the chart, base 64 encodes the output and puts it into
238 * array partsEncoded.
240 * Parameter can be used to slice the chart vertically into parts. This
241 * solves an issue where some browsers (IE8) accept base64 images only up
242 * to some length.
244 * @param integer $parts number of parts to render.
245 * Default value 1 means that all the
246 * chart will be in one piece.
248 protected function render($parts = 1)
250 $fullWidth = 0;
252 for ($i = 0; $i < $parts; $i++) {
254 // slicing is vertical so part height is the full height
255 $partHeight = $this->chart->YSize;
257 // there will be some rounding erros, will compensate later
258 $partWidth = round($this->chart->XSize / $parts);
259 $fullWidth += $partWidth;
260 $partX = $partWidth * $i;
262 if ($i == $parts - 1) {
263 // if this is the last part, compensate for the rounding errors
264 $partWidth += $this->chart->XSize - $fullWidth;
267 // get a part from the full chart image
268 $part = imagecreatetruecolor($partWidth, $partHeight);
269 imagecopy($part, $this->chart->Picture, 0, 0, $partX, 0, $partWidth, $partHeight);
271 // render part and save it to variable
272 ob_start();
273 imagepng($part, NULL, 9, PNG_ALL_FILTERS);
274 $output = ob_get_contents();
275 ob_end_clean();
277 // base64 encode the current part
278 $partEncoded = base64_encode($output);
279 $this->partsEncoded[$i] = $partEncoded;
284 * get the HTML and JS code for the configured chart
285 * @return string HTML and JS code for the chart
287 public function toString()
289 if (!function_exists('gd_info')) {
290 array_push($this->errors, ERR_NO_GD);
291 return '';
294 $this->init();
295 $this->prepareDataSet();
296 $this->prepareChart();
298 //$this->chart->debugImageMap();
299 //$this->chart->printErrors('GD');
301 // check if a user wanted a chart in one part
302 if ($this->isContinuous()) {
303 $this->render(1);
305 else {
306 $this->render(20);
309 $returnData = '<div id="chart">';
310 foreach ($this->partsEncoded as $part) {
311 $returnData .= '<img src="data:image/png;base64,'.$part.'" />';
313 $returnData .= '</div>';
315 // add tooltips only if json is available
316 if (function_exists('json_encode')) {
317 $returnData .= '
318 <script type="text/javascript">
319 //<![CDATA[
320 imageMap.loadImageMap(\''.json_encode($this->getImageMap()).'\');
321 //]]>
322 </script>
325 else {
326 array_push($this->errors, ERR_NO_JSON);
329 return $returnData;
332 protected function getLabelHeight()
334 return $this->settings['labelHeight'];
337 protected function setAreaMargins($areaMargins)
339 $this->settings['areaMargins'] = $areaMargins;
342 protected function getAreaMargin($side)
344 return $this->settings['areaMargins'][$side];
347 protected function getFontPath()
349 return $this->settings['fontPath'];
352 protected function getScale()
354 return $this->settings['scale'];
357 protected function getFontSize()
359 return $this->settings['fontSize'];
362 protected function isContinuous()
364 return $this->settings['continuous'] == 'on';
367 protected function getImageMap()
369 return $this->chart->getImageMap();
372 protected function getGraphAreaColor($component)
374 return $this->hexStrToDecComp($this->settings['graphAreaColor'], $component);
377 protected function getGraphAreaGradientColor($component)
379 return $this->hexStrToDecComp($this->settings['graphAreaGradientColor'], $component);
382 protected function getGridColor($component)
384 return $this->hexStrToDecComp($this->settings['gridColor'], $component);
387 protected function getScaleColor($component)
389 return $this->hexStrToDecComp($this->settings['scaleColor'], $component);
392 protected function getTitleBgColor($component)
394 return $this->hexStrToDecComp($this->settings['titleBgColor'], $component);