Files
DomFramework/src/GraphAxisHorizontal.php
2023-04-14 20:52:27 +02:00

119 lines
3.7 KiB
PHP

<?php
/**
* DomFramework
* @package domframework
* @author Dominique Fournier <dominique@fournier38.fr>
* @license BSD
*/
namespace Domframework;
/**
* The graph Axis Horizontal class
*/
class GraphAxisHorizontal extends GraphAxisGeneral
{
/**
* Calculate the position in pixels for a value
* If the value is out of range, return null to not draw the point
* @param string|float|integer $value The value to position
*/
public function position($value)
{
if ($value === null) {
return null;
}
if (! is_numeric($value) && ! is_string($value)) {
throw new \Exception(dgettext(
"domframework",
"Invalid value provided to graph Axis for position"
) . " " .
get_class($this), 406);
}
if ($this->numerical === null) {
throw new \Exception(dgettext(
"domframework",
"No numerical type defined for Axis"
) . " " . get_class($this), 406);
}
if ($this->numerical) {
// Numerical axis, use a standard scale
if ($value < $this->min || $value > $this->max) {
return null;
}
if (($this->max - $this->min) == 0) {
$dividor = 1;
} else {
$dividor = ($this->max - $this->min);
}
$scale = ($value - $this->min) / $dividor;
return intval($this->left + $scale * ($this->right - $this->left));
} else {
// Label axis, count them
if (! is_array($this->data)) {
throw new \Exception(dgettext(
"domframework",
"No data defined for Axis"
) . " " . get_class($this), 406);
}
$pos = array_search($value, $this->data, true);
if ($pos === false) {
return null;
}
$width = ($this->right - $this->left) / count($this->data);
return intval($this->left + $width * $pos + $width / 2);
}
}
/**
* Calculate the positionMin, used for labeled axies
* If the value is out of range, return null to not draw the point
* @param string|float|integer $value The value to position
*/
public function positionMin($value)
{
$posCenter = $this->position($value);
if ($this->numerical) {
return $posCenter;
}
if (! is_array($this->data)) {
throw new \Exception(dgettext(
"domframework",
"No data defined for Axis"
) . " " . get_class($this), 406);
}
$pos = array_search($value, $this->data, true);
if ($pos === false) {
return null;
}
$width = ($this->right - $this->left) / count($this->data);
return intval($this->left + $width * $pos);
}
/**
* Calculate the positionMax, used for labeled axies
* If the value is out of range, return null to not draw the point
* @param string|float|integer $value The value to position
*/
public function positionMax($value)
{
$posCenter = $this->position($value);
if ($this->numerical) {
return $posCenter;
}
if (! is_array($this->data)) {
throw new \Exception(dgettext(
"domframework",
"No data defined for Axis"
) . " " . get_class($this), 406);
}
$pos = array_search($value, $this->data, true);
if ($pos === false) {
return null;
}
$width = ($this->right - $this->left) / count($this->data);
return intval($this->left + $width * $pos + $width);
}
}