Add vim mode lines
[phpmyadmin.git] / libraries / chart / pma_pchart_chart.php
blobb8e5375e9ca72e658ec562261a57e582fc59f80f
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 * @author Martynas Mickevicius <mmartynas@gmail.com>
7 * @package phpMyAdmin
8 */
10 /**
13 define('TOP', 0);
14 define('RIGHT', 1);
15 define('BOTTOM', 2);
16 define('LEFT', 3);
18 require_once 'pma_chart.php';
20 require_once 'pChart/pData.class';
21 require_once 'pChart/pChart.class';
23 /**
24 * Base class for every chart implemented using pChart.
25 * @abstract
26 * @package phpMyAdmin
28 abstract class PMA_pChart_chart extends PMA_chart
30 /**
31 * @var String title text
33 protected $titleText;
35 /**
36 * @var array data for the chart
38 protected $data;
40 /**
41 * @var object pData object that holds the description of the data
43 protected $dataSet;
45 /**
46 * @var object pChart object that holds the chart
48 protected $chart;
50 /**
51 * @var array holds base64 encoded chart image parts
53 protected $partsEncoded = array();
55 public function __construct($data, $options = null)
57 parent::__construct($options);
59 $this->data = $data;
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
75 // area border
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()
92 parent::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';
103 // initialize colors
104 foreach ($this->getColors() as $key => $color) {
105 $this->chart->setColorPalette(
106 $key,
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
120 * @abstract
122 abstract protected function prepareDataSet();
125 * all components of the chart are drawn
127 protected function prepareChart()
129 $this->drawBackground();
130 $this->drawChart();
134 * draws the background
136 protected function drawBackground()
138 $this->drawCommon();
139 $this->drawTitle();
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()
162 // Draw the title
163 $this->chart->drawTextBox(
166 $this->getWidth(),
167 $this->getLabelHeight(),
168 $this->getTitleText(),
170 $this->getTitleColor(RED),
171 $this->getTitleColor(GREEN),
172 $this->getTitleColor(BLUE),
173 ALIGN_CENTER,
174 True,
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),
204 FALSE
206 $this->chart->drawScale(
207 $this->dataSet->GetData(),
208 $this->dataSet->GetDataDescription(),
209 $this->getScale(),
210 $this->getScaleColor(RED),
211 $this->getScaleColor(GREEN),
212 $this->getScaleColor(BLUE),
213 TRUE,0,2,TRUE
215 $this->chart->drawGraphAreaGradient(
216 $this->getGraphAreaGradientColor(RED),
217 $this->getGraphAreaGradientColor(GREEN),
218 $this->getGraphAreaGradientColor(BLUE),
221 $this->chart->drawGrid(
223 TRUE,
224 $this->getGridColor(RED),
225 $this->getGridColor(GREEN),
226 $this->getGridColor(BLUE),
232 * draws the chart
233 * @abstract
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
243 * to some length.
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)
251 $fullWidth = 0;
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
273 ob_start();
274 imagepng($part, NULL, 9, PNG_ALL_FILTERS);
275 $output = ob_get_contents();
276 ob_end_clean();
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);
292 return '';
295 $this->init();
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()) {
304 $this->render(1);
306 else {
307 $this->render(20);
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')) {
318 $returnData .= '
319 <script type="text/javascript">
320 //<![CDATA[
321 imageMap.loadImageMap(\''.json_encode($this->getImageMap()).'\');
322 //]]>
323 </script>
326 else {
327 array_push($this->errors, ERR_NO_JSON);
330 return $returnData;
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);