I need to calculate the Ticklabels and the Tickrange for charts manually.
I know the \"standard\" algorithm for nice ticks (see http://books.google.de/books?id=fvA7
I found this thread while writing some php, so now the same code is available in php too!
class CNiceScale {
private $minPoint;
private $maxPoint;
private $maxTicks = 10;
private $tickSpacing;
private $range;
private $niceMin;
private $niceMax;
public function setScale($min, $max) {
$this->minPoint = $min;
$this->maxPoint = $max;
$this->calculate();
}
private function calculate() {
$this->range = $this->niceNum($this->maxPoint - $this->minPoint, false);
$this->tickSpacing = $this->niceNum($this->range / ($this->maxTicks - 1), true);
$this->niceMin = floor($this->minPoint / $this->tickSpacing) * $this->tickSpacing;
$this->niceMax = ceil($this->maxPoint / $this->tickSpacing) * $this->tickSpacing;
}
private function niceNum($range, $round) {
$exponent; /** exponent of range */
$fraction; /** fractional part of range */
$niceFraction; /** nice, rounded fraction */
$exponent = floor(log10($range));
$fraction = $range / pow(10, $exponent);
if ($round) {
if ($fraction < 1.5)
$niceFraction = 1;
else if ($fraction < 3)
$niceFraction = 2;
else if ($fraction < 7)
$niceFraction = 5;
else
$niceFraction = 10;
} else {
if ($fraction <= 1)
$niceFraction = 1;
else if ($fraction <= 2)
$niceFraction = 2;
else if ($fraction <= 5)
$niceFraction = 5;
else
$niceFraction = 10;
}
return $niceFraction * pow(10, $exponent);
}
public function setMinMaxPoints($minPoint, $maxPoint) {
$this->minPoint = $minPoint;
$this->maxPoint = $maxPoint;
$this->calculate();
}
public function setMaxTicks($maxTicks) {
$this->maxTicks = $maxTicks;
$this->calculate();
}
public function getTickSpacing() {
return $this->tickSpacing;
}
public function getNiceMin() {
return $this->niceMin;
}
public function getNiceMax() {
return $this->niceMax;
}
}