diff options
| author | David Drury <david@drury.me.uk> | 2015-02-22 14:26:53 +0000 |
|---|---|---|
| committer | David Drury <david@drury.me.uk> | 2015-02-22 14:26:53 +0000 |
| commit | c72236f2977039fd57d063518c52cea9b0e7701a (patch) | |
| tree | b8d7da78266ced4602e1e6c6edb5c28c20773439 /app/Controller/PedigreeController.php | |
| parent | f306643cddf12841dd71da34cd18f91bd91b60e6 (diff) | |
| download | webtrees-c72236f2977039fd57d063518c52cea9b0e7701a.tar.gz webtrees-c72236f2977039fd57d063518c52cea9b0e7701a.tar.bz2 webtrees-c72236f2977039fd57d063518c52cea9b0e7701a.zip | |
Pedigree chart tidying
Remove more "magic numbers"
Accurately setting the #pedigree_chart width & height allows the chart
to be positioned by css thus allowing a few chart variables to be
removed
Diffstat (limited to 'app/Controller/PedigreeController.php')
| -rw-r--r-- | app/Controller/PedigreeController.php | 95 |
1 files changed, 30 insertions, 65 deletions
diff --git a/app/Controller/PedigreeController.php b/app/Controller/PedigreeController.php index 2bd3915958..9cd33c1554 100644 --- a/app/Controller/PedigreeController.php +++ b/app/Controller/PedigreeController.php @@ -23,6 +23,7 @@ class PedigreeController extends ChartController { /** * Chart orientation codes + * Dont change them! the offset calculations rely on this order */ const PORTRAIT = 0; const LANDSCAPE = 1; @@ -49,9 +50,6 @@ class PedigreeController extends ChartController { /** @var array Used to calculate personbox positions */ public $offsetarray = array(); - /** @var integer Vertical start position of the chart */ - public $basexoffset; - /** * Create a pedigree controller */ @@ -61,8 +59,6 @@ class PedigreeController extends ChartController { parent::__construct(); $this->orientation = Filter::getInteger('orientation', 0, 3, $WT_TREE->getPreference('PEDIGREE_LAYOUT')); $this->generations = Filter::getInteger('PEDIGREE_GENERATIONS', 2, $WT_TREE->getPreference('MAX_PEDIGREE_GENERATIONS'), $WT_TREE->getPreference('DEFAULT_PEDIGREE_GENERATIONS')); - - $this->basexoffset = Theme::theme()->parameter('chart-offset-x'); $bxspacing = Theme::theme()->parameter('chart-spacing-x'); $byspacing = Theme::theme()->parameter('chart-spacing-y'); @@ -80,33 +76,28 @@ class PedigreeController extends ChartController { $this->setPageTitle(I18N::translate('Pedigree')); } - //-- adjustments for portrait mode - if ($this->orientation == self::PORTRAIT) { - $bxspacing += 12; - } - - $this->treesize = pow(2, (int) ($this->generations)) - 1; + $this->treesize = pow(2, $this->generations) - 1; // sosaAncestors() starts array at index 1 we need it to start at 0 $this->ancestors = array_values($this->sosaAncestors($this->generations)); // -- this next section will create and position the DIV layers for the pedigree tree - $curgen = 1; // -- publiciable to track which generation the algorithm is currently working on + $curgen = 1; // -- track which generation the algorithm is currently working on $xoffset = 0; if ($this->treesize < 3) { $this->treesize = 3; } - // -- loop through all of IDs in the array starting at the last and working to the first - //-- calculation the box positions + // -- loop through all of IDs in the array from last to first + // -- calculating the box positions for ($i = ($this->treesize - 1); $i >= 0; $i--) { // -- check to see if we have moved to the next generation - if ($i < (int) ($this->treesize / (pow(2, $curgen)))) { + if ($i < intval($this->treesize / pow(2, $curgen))) { $curgen++; } - //-- box position in current generation + // -- box position in current generation $boxpos = $i - pow(2, $this->generations - $curgen); - //-- offset multiple for current generation + // -- offset multiple for current generation if ($this->orientation < self::OLDEST_AT_TOP) { $genoffset = pow(2, $curgen - $this->orientation); $boxspacing = $this->getBoxDimensions()->height + $byspacing; @@ -114,28 +105,21 @@ class PedigreeController extends ChartController { $genoffset = pow(2, $curgen - 1); $boxspacing = $this->getBoxDimensions()->width + $byspacing; } - // -- calculate the yoffset Position in the generation Spacing between boxes put child between parents + // -- calculate the yoffset position in the generation put child between parents $yoffset = ($boxpos * ($boxspacing * $genoffset)) + (($boxspacing / 2) * $genoffset) + ($boxspacing * $genoffset); // -- calculate the xoffset switch ($this->orientation) { case self::PORTRAIT: - $yoffset -= $this->getBoxDimensions()->height; - if ($this->generations < 6) { - $addxoffset = $this->basexoffset + (10 + 60 * (5 - $this->generations)); - $xoffset = ($this->generations - $curgen) * (($this->getBoxDimensions()->width + $bxspacing) / 2) + $addxoffset; - } else { - $addxoffset = $this->basexoffset + 10; - $xoffset = ($this->generations - $curgen) * (($this->getBoxDimensions()->width + $bxspacing) / 2) + $addxoffset; - } - //-- compact the tree + $xoffset = ($this->generations - $curgen) * (($this->getBoxDimensions()->width + ($bxspacing * 3)) / 2); + // -- compact the tree if ($curgen < $this->generations) { - $parent = (int)(($i - 1) / 2); if ($i % 2 == 0) { $yoffset = $yoffset - (($boxspacing / 2) * ($curgen - 1)); } else { $yoffset = $yoffset + (($boxspacing / 2) * ($curgen - 1)); } + $parent = (int)(($i - 1) / 2); $pgen = $curgen; while ($parent > 0) { if ($parent % 2 == 0) { @@ -168,55 +152,36 @@ class PedigreeController extends ChartController { $yoffset = $yoffset + (($boxspacing / 2) * $temp); } } - } $yoffset -= (($boxspacing / 2) * pow(2, ($this->generations - 2)) - ($boxspacing / 2)); break; case self::LANDSCAPE: - $xoffset = self::ARROW_SIZE + $this->basexoffset + (($this->generations - $curgen) * ($this->getBoxDimensions()->width + $bxspacing)); - if ($this->generations < 4) { - $xoffset += 60; + $xoffset = ($this->generations - $curgen) * ($this->getBoxDimensions()->width + $bxspacing); + if ($curgen == 1) { + $xoffset += 10; } break; case self::OLDEST_AT_TOP: // x & y values are reversed in pedigree.php - if ($this->generations > 3) { - $yoffset -= $this->getBoxDimensions()->height; - } - if ($this->showFull()) { - $xoffset = (int) ($curgen-1) * (($this->getBoxDimensions()->width + $bxspacing) / 2) + $curgen + self::ARROW_SIZE; - } else { - $xoffset = (int) ($curgen-1) * (($this->getBoxDimensions()->width + $bxspacing) / 3) + $curgen + self::ARROW_SIZE; - } + $divisor = $this->showFull() ? 2 : 3; + $xoffset = ($curgen-1) * (($this->getBoxDimensions()->width + $bxspacing) / $divisor) + $curgen; break; case self::OLDEST_AT_BOTTOM: // x & y values are reversed in pedigree.php - if ($this->generations > 3) { - $yoffset -= $this->getBoxDimensions()->height; - } - if ($this->showFull()) { - $xoffset = (int) ($this->generations - $curgen) * (($this->getBoxDimensions()->width + $bxspacing) / 2) + $this::ARROW_SIZE; - } else { - $xoffset = (int) ($this->generations - $curgen) * (($this->getBoxDimensions()->width + $bxspacing) / 3) + $this::ARROW_SIZE; - } + $divisor = $this->showFull() ? 2 : 3; + $xoffset = ($this->generations - $curgen) * (($this->getBoxDimensions()->width + $bxspacing) / $divisor); break; } - if ($curgen == 1 && $this->orientation == self::LANDSCAPE) { - $xoffset += 10; - } - $this->offsetarray[$i]["x"] = (int) $xoffset; - $this->offsetarray[$i]["y"] = (int) $yoffset; + $this->offsetarray[$i]["x"] = intval($xoffset); + $this->offsetarray[$i]["y"] = intval($yoffset); } - } - /** - * Get the name of the person at the root of the tree. - * - * @return string - */ - function getPersonName() { - if (is_null($this->root)) { - return I18N::translate('unknown'); - } else { - return $this->root->getFullName(); - } + // find the minimum x & y offsets and deduct that number from + // each value in the array so that offsets start from zero + // could use array_column but that only works with php >= 5.5 + $min_xoffset = min(array_map(function($item) {return $item['x'];}, $this->offsetarray)) - self::ARROW_SIZE; + $min_yoffset = min(array_map(function($item) {return $item['y'];}, $this->offsetarray)); + array_walk($this->offsetarray, function(&$item) use ($min_xoffset, $min_yoffset) { + $item['y'] -= $min_yoffset; + $item['x'] -= $min_xoffset; + }); } } |
