Source for file Axis.class.php
Documentation is available at Axis.class.php
* This work is hereby released into the Public Domain.
* To view a copy of the public domain dedication,
* visit http://creativecommons.org/licenses/publicdomain/ or send a letter to
* Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
require_once dirname(__FILE__ ). "/../Graph.class.php";
* @package linea21.externals
* Axis left and right padding
* Axis range callback function
'toValue' => 'toProportionalValue',
'toPosition' => 'toProportionalPosition'
* @param float $min Begin of the range of the axis
* @param float $max End of the range of the axis
public function __construct($min = NULL, $max = NULL) {
if($min !== NULL and $max !== NULL) {
* Enable/disable auto-scaling mode
public function auto($auto) {
$this->auto = (bool) $auto;
* Get auto-scaling mode status
public function hide($hide = TRUE) {
$this->hide = (bool) $hide;
public function show($show = TRUE) {
$this->hide = !(bool) $show;
* Return a tick object from its name
* @param string $name Tick object name
public function tick($name) {
if(array_key_exists($name, $this->ticks)) {
return $tick = &$this->ticks[$name];
* @param string $name Tick object name
* @param awTick $tick Tick object
public function addTick($name, awTick $tick) {
$this->ticks[$name] = $tick;
$this->ticks[$name] = &$tick;
* @param string $name Tick object name
unset ($this->ticks[$name]);
* @param bool $hide Hide or not ?
foreach($this->ticks as $tick) {
foreach($this->ticks as $key => $tick) {
$this->ticks[$key]->hide($hide);
* @param int $style Ticks style
foreach($this->ticks as $tick) {
foreach($this->ticks as $key => $tick) {
$this->ticks[$key]->setStyle($style);
* @param int $interval Ticks interval
foreach($this->ticks as $tick) {
$tick->setInterval($interval);
foreach($this->ticks as $key => $tick) {
$this->ticks[$key]->setInterval($interval);
* Change number of ticks relative to others ticks
* @param awTick $to Change number of theses ticks
* @param awTick $from Ticks reference
* @param float $number Number of ticks by the reference
$this->ticks[$to]->setNumberByTick($this->ticks[$from], $number);
foreach($this->ticks as $tick) {
if($tick->getStyle() === awTick::IN) {
} else if($tick->getStyle() === awTick::OUT) {
foreach($this->ticks as $key => $tick) {
if($this->ticks[$key]->getStyle() === awTick::IN) {
$this->ticks[$key]->setStyle(awTick::OUT);
} else if($this->ticks[$key]->getStyle() === awTick::OUT) {
$this->ticks[$key]->setStyle(awTick::IN);
* Change interval of labels
* @param int $interval Interval
$this->label->setInterval($interval);
* Change number of labels
* @param int $number Number of labels to display (can be NULL)
* Change precision of labels
* @param int $precision Precision
eval ('function '. $function. '($value) {
return sprintf("%.'.(int) $precision. 'f", $value);
$this->label->setCallbackFunction($function);
* @param array $texts Some texts
eval ('function '. $function. '($value) {
return isset($texts[$value]) ? $texts[$value] : \'?\';
$this->label->setCallbackFunction($function);
* Get the position of a point
* @param awAxis $xAxis X axis
* @param awAxis $yAxis Y axis
* @param awPoint $p Position of the point
* @return Point Position on the axis
public static function toPosition(awAxis $xAxis, awAxis $yAxis, awPoint $p) {
$p1 = $xAxis->getPointFromValue($p->x);
$p2 = $yAxis->getPointFromValue($p->y);
* @param int $alignment New Alignment
* Change title position on the axis
* @param float $position A new awposition between 0 and 1
* Change axis and axis title color
public function setColor(awColor $color) {
$this->title->setColor($color);
* @param int $left Left padding in pixels
* @param int $right Right padding in pixels
$this->padding->set($left, $right);
$this->range[0] = (float) $min;
$this->range[1] = (float) $max;
* Change axis range callback function
* @param string $toValue Transform a position between 0 and 1 to a value
* @param string $toPosition Transform a value to a position between 0 and 1 on the axis
'toValue' => (string) $toValue,
'toPosition' => (string) $toPosition
* Center X values of the axis
* @param awAxis $axis An axis
* @param float $value The reference value on the axis
public function setXCenter(awAxis $axis, $value) {
if($this->line->isVertical() === FALSE) {
awImage::drawError("Class Axis: setXCenter() can only be used on vertical axes.");
$p = $axis->getPointFromValue($value);
* Center Y values of the axis
* @param awAxis $axis An axis
* @param float $value The reference value on the axis
public function setYCenter(awAxis $axis, $value) {
if($this->line->isHorizontal() === FALSE) {
awImage::drawError("Class Axis: setYCenter() can only be used on horizontal axes.");
$p = $axis->getPointFromValue($value);
* Get the distance between to values on the axis
* @param float $from The first value
* @param float $to The last value
return $p1->getDistance($p2);
* Get a point on the axis from a value
list ($min, $max) = $this->range;
$position = $callback($value, $min, $max);
* Get a point on the axis from a position
* @param float $position A position between 0 and 1
$angle = $vector->getAngle();
$size = $vector->getSize();
return $vector->p1->move(
cos($angle) * $size * $position,
- 1 * sin($angle) * $size * $position
* @param awDriver $driver A driver
public function draw(awDriver $driver) {
$this->title->draw($driver, $p);
if($this->isAuto() === FALSE) {
$partMax = $max / $interval;
$partMin = $min / $interval;
$difference = log($interval) / log(10);
$difference = floor($difference);
$pow = pow(10, $difference);
$intervalNormalize = $interval / $pow;
$precision = $difference * - 1 + 1;
if($intervalNormalize > 2) {
if($min != 0 and $max != 0) {
if($this->label->getCallbackFunction() === NULL) {
if($intervalNormalize <= 1.5) {
} else if($intervalNormalize <= 2) {
} else if($intervalNormalize <= 3) {
} else if($intervalNormalize <= 4) {
} else if($intervalNormalize <= 5) {
} else if($intervalNormalize <= 8) {
} else if($intervalNormalize <= 10) {
$intervalReal * $pow * - 1,
protected function line(awDriver $driver) {
protected function drawTicks(awDriver $driver, awVector $vector) {
foreach($this->ticks as $tick) {
$tick->setColor($this->color);
$tick->draw($driver, $vector);
list ($min, $max) = $this->range;
for($i = 0; $i <= $number; $i++ ) {
$labels[] = $function($i / $number, $min, $max);
$this->label->set($labels);
$labels = $this->label->count();
for($i = 0; $i < $labels; $i++ ) {
$this->label->draw($driver, $p, $i);
$angle = $this->line->getAngle();
foreach($this->ticks as $name => $tick) {
$this->ticks[$name] = clone $tick;
return $min + ($max - $min) * $position;
return ($value - $min) / ($max - $min);
|