Files
DomFramework/src/GraphAxisX.php

215 lines
7.2 KiB
PHP

<?php
/** DomFramework
* @package domframework
* @author Dominique Fournier <dominique@fournier38.fr>
* @license BSD
*/
namespace Domframework;
/** The X axis management */
class GraphAxisX extends GraphAxisHorizontal
{
/** The angle choosed to draw the graph
*/
private $angle;
/** The padding between the label and the axis
*/
private $padding = 7;
/** The heigth of the labels + padding (it is the base of the graph) in pixels
*/
private $height;
/** The number of chars to be displayed in one label
*/
protected $nbcharsLabel = 0;
/** Look for the height of the X axis based on the angle of the text when it
* will be drawn
* @param resource $gd The resource to modify
*/
public function getHeight($gd)
{
// Look for the angle of the value. Start Horizontally (angle=0), then
// try 45°, then finish at 90°. As all the values must in the same angle,
// test all the values. If one is bad, change the angle and retry all the
// values
$width = null;
if ($this->numerical) {
if ($this->max === null) {
throw new \Exception(dgettext(
"domframework",
"The max is not defined for graphAxisHorizontal"
), 406);
}
$powMax = intval(log10($this->max));
$tenPercent = round($this->max / pow(10, $powMax), 1) + 0.1;
$labelMax = $tenPercent * pow(10, $powMax);
$this->nbcharsLabel = strlen($labelMax);
$bbox = imagettfbbox(
$this->fontsize,
$this->angle,
$this->fontfile,
$labelMax
);
$height = abs($bbox[4] - $bbox[0]);
$this->height = $height + 2 * $this->padding;
return $this->height;
} else {
if ($this->data === null) {
return 0;
}
foreach (array(0, 45, 90) as $this->angle) {
$bboxMaxHeight = 0;
foreach ($this->data as $key => $value) {
if ($width === null) {
$width = $this->positionMax($value) - $this->positionMin($value);
}
// Look for the bounding box around the text. The bounding is not
// write on the image and return the coordinates for the text box
$bbox = imagettfbbox(
$this->fontsize,
$this->angle,
$this->fontfile,
$value
);
if (abs($bbox[4] - $bbox[0]) > $width) {
continue 2;
}
$bboxMaxHeight = max($bboxMaxHeight, abs($bbox[5] - $bbox[1]));
}
// All the values are OK : we have found the angle : break the angle
// loop
break;
}
}
$this->height = $bboxMaxHeight + 2 * $this->padding;
return $bboxMaxHeight + 2 * $this->padding;
}
/** Draw the axis
* @param resource $gd The resource to modify
*/
public function draw($gd)
{
$axisColor = Color::allocateFromText($gd, $this->axisColor);
if ($this->numerical) {
if ($this->angle === null) {
$this->getHeight($gd);
}
if ($this->data === null) {
return;
}
foreach ($this->data as $key => $value) {
$position = $this->position($value);
// Draw the labels
$bbox = imagettfbbox(
$this->fontsize,
$this->angle,
$this->fontfile,
$value
);
$width = abs($bbox[4] - $bbox[0]);
$x = $position - $width / 2;
$y = $this->bottom - $this->padding;
// The font color is forced to black
imagettftext(
$gd,
$this->fontsize,
$this->angle,
$x,
$y,
Color::allocateFromText($gd, "black"),
$this->fontfile,
$value
);
// Draw the scale
imageline(
$gd,
$position,
$this->bottom - $this->height + 1,
$position,
$this->bottom - $this->height + 1 + $this->padding,
$axisColor
);
// Draw the grid
$this->drawGrid($gd, $this->bottom - $this->height, $position);
}
// Draw the axis
$y = $this->bottom - $this->height + 1;
imageline($gd, $this->left, $y, $this->right, $y, $axisColor);
} else {
if ($this->angle === null) {
$this->getHeight($gd);
}
if ($this->data === null) {
return;
}
foreach ($this->data as $key => $value) {
$position = $this->position($value);
// Draw the labels
$bbox = imagettfbbox(
$this->fontsize,
$this->angle,
$this->fontfile,
$value
);
$width = abs($bbox[4] - $bbox[0]);
$x = $position - $width / 2;
$y = $this->bottom - $this->padding;
// The font color is forced to black
imagettftext(
$gd,
$this->fontsize,
$this->angle,
$x,
$y,
Color::allocateFromText($gd, "black"),
$this->fontfile,
$value
);
// Draw the separators
$y = $this->bottom - $this->height + 1;
$xmin = $this->positionMin($value);
$xmax = $this->positionMax($value);
imageline($gd, $xmin, $y, $xmin, $y + $this->padding, $axisColor);
imageline($gd, $xmax, $y, $xmax, $y + $this->padding, $axisColor);
// Draw the grid
$this->drawGrid($gd, $this->bottom - $this->height, $position);
}
// Draw the axis
$y = $this->bottom - $this->height + 1;
imageline($gd, $this->left, $y, $this->right, $y, $axisColor);
}
}
/** Draw the grid
* @param resource $gd The resource to modify
* @param integer $width The width of the labels on the axis
* @param integer|float $position The position to draw
*/
protected function drawGrid($gd, $width, $position)
{
if ($this->gridColor === null || $this->gridColor === "transparent") {
return;
}
$gridColor = Color::allocateFromText($gd, $this->gridColor);
if ($position === null) {
return;
}
$y = $this->bottom - $this->height;
imageline($gd, $position, $y, $position, $this->top, $gridColor);
}
}