Skip to content

Commit

Permalink
New class for controlling Axis Ticks
Browse files Browse the repository at this point in the history
  • Loading branch information
tatai committed Jun 8, 2010
1 parent 13bcfbe commit e602c1e
Show file tree
Hide file tree
Showing 6 changed files with 394 additions and 14 deletions.
26 changes: 12 additions & 14 deletions includes/classes/Burndown/Burndown.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,12 @@ private function _calculateXAxisSplit() {

private function _drawXAxisTicks($split) {
$this->_styleChanger->change($this->_pdf, LineStyleFactory::thinContinuous());
for($i = 0; $i < $this->_days; $i++) {
$from = new Point($this->_margins->left() + $split * $i, $this->_margins->bottom() - ($this->_tick_size / 2));
$to = new Point($this->_margins->left() + $split * $i, $this->_margins->bottom() + ($this->_tick_size / 2));

$line = new Line($from, $to);
$this->_drawLine->draw($line);
}

$axisTicks = new DrawAxisTicks($this->_drawLine);

$start = new Point($this->_margins->left(), $this->_margins->bottom());
$end = new Point($this->_pdf->getPageWidth() - $this->_margins->right(), $this->_margins->bottom());
$axisTicks->draw($split, $this->_tick_size, new Line($start, $end));
}

private function _drawXAxisGrid($split) {
Expand Down Expand Up @@ -215,13 +214,12 @@ private function _calculateYAxisProperties() {

private function _drawYAxisTicks($split, $points) {
$this->_styleChanger->change($this->_pdf, LineStyleFactory::thinContinuous());
for($i = 0; $i < $points; $i++) {
$from = new Point($this->_margins->left() - ($this->_tick_size / 2), $this->_margins->bottom() + $split * $i);
$to = new Point($this->_margins->left() + ($this->_tick_size / 2), $this->_margins->bottom() + $split * $i);

$line = new Line($from, $to);
$this->_drawLine->draw($line);
}

$axisTicks = new DrawAxisTicks($this->_drawLine);

$start = new Point($this->_margins->left(), $this->_margins->bottom());
$end = new Point($this->_margins->left(), $this->_pdf->getPageHeight() - $this->_margins->top());
$axisTicks->draw($split, $this->_tick_size, new Line($start, $end));
}

private function _drawYAxisGrid($split, $points) {
Expand Down
50 changes: 50 additions & 0 deletions includes/classes/Chart/DrawAxisTicks.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/*
* Yet another burndown online generator, http://www.burndowngenerator.com
* Copyright (C) 2010 Francisco José Naranjo <fran.naranjo@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
class DrawAxisTicks {
/**
*
* @var DrawLine
*/
private $_draw_line = null;

/**
*
* @param DrawLine $drawLine
*/
public function __construct(DrawLine $drawLine) {
$this->_draw_line = $drawLine;
}

public function draw($distanceBetweenTicks, $tickSize, Line $line) {
$operations = new LineOperations();

$distance = $line->size() / $distanceBetweenTicks;
$nTicks = (int)floor($distance);

$xSplit = ($line->to()->x() - $line->from()->x()) / $distance;
$ySplit = ($line->to()->y() - $line->from()->y()) / $distance;

for($i = 0; $i < $nTicks + 1; $i++) {
$at = new Point($line->from()->x() + $xSplit * $i, $line->from()->y() + $ySplit * $i);
$perpendicular = $operations->perpendicular($line, $at, $tickSize);

$this->_draw_line->draw($perpendicular);
}
}
}
80 changes: 80 additions & 0 deletions includes/classes/Chart/LineOperations.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php
/*
* Yet another burndown online generator, http://www.burndowngenerator.com
* Copyright (C) 2010 Francisco José Naranjo <fran.naranjo@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
class LineOperations {

/**
*
* @param Line $line
* @param Point $at
* @param float $length
* @return Line
*/
public function perpendicular(Line $line, Point $at, $length) {
if($line->from()->x() == $line->to()->x()) {
return $this->_perpendicularFromVerticalLine($line, $at, $length);
}
else if($line->from()->y() == $line->to()->y()) {
return $this->_perpendicularFromHorizontalLine($line, $at, $length);
}
}

/**
*
* @param Line $line
* @param Point $at
* @param float $length
* @return Line
*/
private function _perpendicularFromHorizontalLine(Line $line, Point $at, $length) {
$medTickSize = $length / 2;

$medTickSize *= $this->_chooseSign($line, 'x');

$from = new Point($at->x(), $at->y() - $medTickSize);
$to = new Point($at->x(), $at->y() + $medTickSize);

return new Line($from, $to);
}

/**
*
* @param Line $line
* @param Point $at
* @param float $length
* @return Line
*/
private function _perpendicularFromVerticalLine(Line $line, Point $at, $length) {
$medTickSize = $length / 2;

$medTickSize *= $this->_chooseSign($line, 'y');

$from = new Point($at->x() - $medTickSize, $at->y());
$to = new Point($at->x() + $medTickSize, $at->y());

return new Line($from, $to);
}

private function _chooseSign(Line $line, $dimension) {
if($line->to()->$dimension() - $line->from()->$dimension() < 0) {
return -1;
}

return 1;
}
}
106 changes: 106 additions & 0 deletions tests/Chart/DrawAxisTicksTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php
/*
* Yet another burndown online generator, http://www.burndowngenerator.com
* Copyright (C) 2010 Francisco José Naranjo <fran.naranjo@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
require_once (dirname(__FILE__) . '/../test_startup.php');

class DrawAxisTicksTest extends PHPUnit_Framework_TestCase {
/**
*
* @var DrawLine
*/
private $_draw_line = null;

/**
*
* @var int
*/
private $_tick_size = null;

/**
*
* @var DrawAxisTicks
*/
private $_draw_axis_ticks = null;

/**
*
* @var Line
*/
private $_line = null;

/**
*
* @var float
*/
private $_step = null;

public function setUp() {
$pdf = $this->getMock('MetricsPdf', array(), array(
'a4',
'landscape'));
$styleChanger = new LineStyleChanger();

$this->_draw_line = $this->getMock('DrawLine', array(), array(
$pdf,
$styleChanger));

$this->_tick_size = 3;
$this->_step = 2;

$this->_line = new Line(new Point(2, 4), new Point(8.5, 4));

$this->_draw_axis_ticks = new DrawAxisTicks($this->_draw_line);
}

/**
* @test
*/
public function createsTicksWithinLine() {
$distanceBetweenTicks = 3;
$lineSize = $this->_line->to()->x() - $this->_line->from()->x();

$ticks = (int)floor($lineSize / $distanceBetweenTicks);

$this->_draw_line->expects($this->exactly($ticks))->method('draw');

$this->_draw_axis_ticks->draw($distanceBetweenTicks, $this->_tick_size, $this->_line);
}

/**
* @TODO: impossible to make this work with PHPUnit 3.4.1
*/
public function twoTicksMeansOneTickAtStartPointAndOneAtTheEnd() {
$ticks = 2;
$startTick = new Line(
new Point($this->_start_point->x(), $this->_start_point->y() - $this->_tick_size / 2),
new Point($this->_start_point->x(), $this->_start_point->y() + $this->_tick_size / 2)
);

$endTick = new Line(
new Point($this->_end_point->x(), $this->_end_point->y() - $this->_tick_size / 2),
new Point($this->_end_point->x(), $this->_end_point->y() + $this->_tick_size / 2)
);

$this->_draw_line->expects($this->once())->method('draw')->with($startTick);
$this->_draw_line->expects($this->once())->method('draw')->with($endTick);

$this->_draw_axis_ticks->draw($ticks, $this->_tick_size, $this->_line);
}
}

require_once (dirname(__FILE__) . '/../test_shutdown.php');
73 changes: 73 additions & 0 deletions tests/Chart/LinePerpendicularFromHorizontalTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
/*
* Yet another burndown online generator, http://www.burndowngenerator.com
* Copyright (C) 2010 Francisco José Naranjo <fran.naranjo@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
require_once (dirname(__FILE__) . '/../test_startup.php');

class LinePerpendicularFromHorizontalTest extends PHPUnit_Framework_TestCase {
private $_from = null;
private $_to = null;
private $_at = null;

/**
*
* @var LineOperations
*/
private $_operations = null;

public function setUp() {
$this->_from = new Point(1, 2);
$this->_to = new Point(4, 2);
$this->_at = new Point(3, 2);

$this->_operations = new LineOperations();
}

/**
* @test
*/
public function createPerpendicularLineInPointFromHorizontal0Degrees() {
$length = 2;

$perpendicularFrom = new Point(3, 1);
$perpendicularTo = new Point(3, 3);

$line = new Line($this->_from, $this->_to);

$perpendicular = $this->_operations->perpendicular($line, $this->_at, $length);
$this->assertEquals($perpendicularFrom, $perpendicular->from());
$this->assertEquals($perpendicularTo, $perpendicular->to());
}

/**
* @test
*/
public function createPerpendicularLineInPointFromHorizontal180Degrees() {
$length = 2;

$perpendicularFrom = new Point(3, 3);
$perpendicularTo = new Point(3, 1);

$line = new Line($this->_to, $this->_from);

$perpendicular = $this->_operations->perpendicular($line, $this->_at, $length);
$this->assertEquals($perpendicularFrom, $perpendicular->from());
$this->assertEquals($perpendicularTo, $perpendicular->to());
}
}

require_once (dirname(__FILE__) . '/../test_shutdown.php');
Loading

0 comments on commit e602c1e

Please sign in to comment.