From 37f3a54fc476f1c47fd399e4a3ee2ebd7654b7ff Mon Sep 17 00:00:00 2001 From: jakobsandberg Date: Sat, 8 Oct 2016 23:36:13 -0700 Subject: [PATCH 01/59] Added Canvas class --- src/Plots/Canvas.php | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 src/Plots/Canvas.php diff --git a/src/Plots/Canvas.php b/src/Plots/Canvas.php new file mode 100644 index 000000000..ed50dd360 --- /dev/null +++ b/src/Plots/Canvas.php @@ -0,0 +1,8 @@ + Date: Sat, 8 Oct 2016 23:37:14 -0700 Subject: [PATCH 02/59] Added constructor and parameter visibility --- src/Plots/Canvas.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/Plots/Canvas.php b/src/Plots/Canvas.php index ed50dd360..cbc3e63ab 100644 --- a/src/Plots/Canvas.php +++ b/src/Plots/Canvas.php @@ -4,5 +4,14 @@ class Canvas { + protected $width; + protected $height; + protected $canvas; + public function __construct($width = 600, $height = 600) + { + $this->width = $width; + $this->height = $height; + $this->canvas = imagecreate($width, $height); + } } From fdb8063d17af2b10fbd62e3239da593658d6ba59 Mon Sep 17 00:00:00 2001 From: jakobsandberg Date: Sat, 8 Oct 2016 23:37:41 -0700 Subject: [PATCH 03/59] Added addPlot() method --- src/Plots/Canvas.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Plots/Canvas.php b/src/Plots/Canvas.php index cbc3e63ab..9b63941af 100644 --- a/src/Plots/Canvas.php +++ b/src/Plots/Canvas.php @@ -7,6 +7,7 @@ class Canvas protected $width; protected $height; protected $canvas; + protected $plot; public function __construct($width = 600, $height = 600) { @@ -14,4 +15,10 @@ public function __construct($width = 600, $height = 600) $this->height = $height; $this->canvas = imagecreate($width, $height); } + + public function addPlot($padding = 0) + { + $this->plot = new Plot($padding); + return $this->plot; + } } From 356bcd3dc3de0e410f87991e00bdb1810a1f863e Mon Sep 17 00:00:00 2001 From: jakobsandberg Date: Sat, 8 Oct 2016 23:38:23 -0700 Subject: [PATCH 04/59] Added Plot class --- src/Plots/Plot.php | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 src/Plots/Plot.php diff --git a/src/Plots/Plot.php b/src/Plots/Plot.php new file mode 100644 index 000000000..489ef514d --- /dev/null +++ b/src/Plots/Plot.php @@ -0,0 +1,8 @@ + Date: Sat, 8 Oct 2016 23:40:30 -0700 Subject: [PATCH 05/59] Added basic draw() method --- src/Plots/Plot.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Plots/Plot.php b/src/Plots/Plot.php index 489ef514d..5ce552ab6 100644 --- a/src/Plots/Plot.php +++ b/src/Plots/Plot.php @@ -4,5 +4,11 @@ class Plot extends Canvas { - + public function draw($canvas, $width, $height) + { + $black = imagecolorallocate($canvas, 0, 0, 0); + header('Content-type: image/png'); + // Draw something here + return $canvas; + } } From 6208f65f81e6fcb8930b8644f941bd70f9e0169e Mon Sep 17 00:00:00 2001 From: jakobsandberg Date: Sat, 8 Oct 2016 23:40:59 -0700 Subject: [PATCH 06/59] Added constructor to Plot and method visibility --- src/Plots/Plot.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Plots/Plot.php b/src/Plots/Plot.php index 5ce552ab6..8cc6c4bf8 100644 --- a/src/Plots/Plot.php +++ b/src/Plots/Plot.php @@ -4,11 +4,19 @@ class Plot extends Canvas { + private $padding; + + public function __construct($padding) + { + $this->padding = $padding; + } + public function draw($canvas, $width, $height) { + $padding = $this->padding; $black = imagecolorallocate($canvas, 0, 0, 0); header('Content-type: image/png'); - // Draw something here + imagerectangle($canvas, $padding, $padding, $width - $padding, $height - $padding, $black); return $canvas; } } From 63fee35adfd5eaabf5eb2e56e596e70adab0a76a Mon Sep 17 00:00:00 2001 From: jakobsandberg Date: Sat, 8 Oct 2016 23:41:14 -0700 Subject: [PATCH 07/59] Added setPadding() method to Plot --- src/Plots/Plot.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Plots/Plot.php b/src/Plots/Plot.php index 8cc6c4bf8..0aabcc2ae 100644 --- a/src/Plots/Plot.php +++ b/src/Plots/Plot.php @@ -11,6 +11,11 @@ public function __construct($padding) $this->padding = $padding; } + public function setPadding($padding) + { + $this->padding = $padding; + } + public function draw($canvas, $width, $height) { $padding = $this->padding; From 2338c4b3a61635433aa49dd5b70c0c656e17294c Mon Sep 17 00:00:00 2001 From: jakobsandberg Date: Sat, 8 Oct 2016 23:41:32 -0700 Subject: [PATCH 08/59] Added save() method to Canvas --- src/Plots/Canvas.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Plots/Canvas.php b/src/Plots/Canvas.php index 9b63941af..158ee8c3d 100644 --- a/src/Plots/Canvas.php +++ b/src/Plots/Canvas.php @@ -21,4 +21,14 @@ public function addPlot($padding = 0) $this->plot = new Plot($padding); return $this->plot; } + + public function save() + { + $width = $this->width; + $height = $this->height; + $canvas = imagecreate($width, $height); + imagecolorallocate($canvas, 255, 255, 255); + $canvas = $this->plot->draw($canvas, $width, $height); + imagejpeg($canvas, 'image-' . rand() . '.jpg'); + } } From acf8b41a59562c22a01ba6cc2728e454c6be1801 Mon Sep 17 00:00:00 2001 From: jakobsandberg Date: Sat, 8 Oct 2016 23:51:31 -0700 Subject: [PATCH 09/59] Added comments to map out development of Plot class --- src/Plots/Plot.php | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/Plots/Plot.php b/src/Plots/Plot.php index 0aabcc2ae..15405fadf 100644 --- a/src/Plots/Plot.php +++ b/src/Plots/Plot.php @@ -18,10 +18,27 @@ public function setPadding($padding) public function draw($canvas, $width, $height) { + // Build convenience variables for canvas/plot measures $padding = $this->padding; + + // Create axes $black = imagecolorallocate($canvas, 0, 0, 0); - header('Content-type: image/png'); imagerectangle($canvas, $padding, $padding, $width - $padding, $height - $padding, $black); + + // Define input function + + // calculate canvas step size and function step size + + // Calculate function values, min, and max + + // Draw y-axis values, dashes + + // Draw x-axis values, dashes + + // Draw title, x-axis title, y-axis title + + // Draw graph + return $canvas; } } From f8c1253bb007d606f7e6d45bc0bb9efa42001eb4 Mon Sep 17 00:00:00 2001 From: jakobsandberg Date: Sun, 9 Oct 2016 00:15:05 -0700 Subject: [PATCH 10/59] Added convenience variables for canvas/plot measures --- src/Plots/Plot.php | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/src/Plots/Plot.php b/src/Plots/Plot.php index 15405fadf..a7c31e6d7 100644 --- a/src/Plots/Plot.php +++ b/src/Plots/Plot.php @@ -4,22 +4,33 @@ class Plot extends Canvas { - private $padding; - - public function __construct($padding) - { - $this->padding = $padding; - } - - public function setPadding($padding) + public function __construct() { - $this->padding = $padding; + parent::__construct(); + $this->x_label = "x-label"; + $this->y_label = "y-label"; } - public function draw($canvas, $width, $height) + public function draw($canvas) { // Build convenience variables for canvas/plot measures - $padding = $this->padding; + $width = $this->width; + $height = $this->height; + $padding = 50; + list($x_shift, $y_shift) = [ + isset($this->y_label) ? 1 : 0, + isset($this->x_label) ? 1 : 0, + ]; + list($graph_start_x, $graph_start_y, $graph_end_x, $graph_end_y) = [ + (1 + $x_shift)*$padding, + imagesy($canvas) - (1 + $y_shift)*$padding, + imagesx($canvas) - $padding, + $padding + ]; + list($graph_width, $graph_height) = [ + imagesx($canvas) - (2 + $x_shift)*$padding, + imagesy($canvas) - (2 + $y_shift)*$padding + ]; // Create axes $black = imagecolorallocate($canvas, 0, 0, 0); From 4bd2b00b0c26b89f8a83a16d2265c74825b18a67 Mon Sep 17 00:00:00 2001 From: jakobsandberg Date: Sun, 9 Oct 2016 00:17:49 -0700 Subject: [PATCH 11/59] Added rectangle for plot axes --- src/Plots/Plot.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Plots/Plot.php b/src/Plots/Plot.php index a7c31e6d7..16dee9f32 100644 --- a/src/Plots/Plot.php +++ b/src/Plots/Plot.php @@ -34,7 +34,7 @@ public function draw($canvas) // Create axes $black = imagecolorallocate($canvas, 0, 0, 0); - imagerectangle($canvas, $padding, $padding, $width - $padding, $height - $padding, $black); + imagerectangle($canvas, $graph_start_x, $graph_end_y, $graph_end_x, $graph_start_y, $black); // Define input function From 19d52db72cc6efc581fd6360c6f1682decf17ebd Mon Sep 17 00:00:00 2001 From: jakobsandberg Date: Sun, 9 Oct 2016 00:20:29 -0700 Subject: [PATCH 12/59] Defined input function and function domain --- src/Plots/Plot.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Plots/Plot.php b/src/Plots/Plot.php index 16dee9f32..1b5b595c3 100644 --- a/src/Plots/Plot.php +++ b/src/Plots/Plot.php @@ -9,6 +9,9 @@ public function __construct() parent::__construct(); $this->x_label = "x-label"; $this->y_label = "y-label"; + $this->function = function ($x) { return $x; }; + $this->start = 0; + $this->end = 10; } public function draw($canvas) @@ -37,6 +40,9 @@ public function draw($canvas) imagerectangle($canvas, $graph_start_x, $graph_end_y, $graph_end_x, $graph_start_y, $black); // Define input function + $function = $this->function; + $function_start = $this->start; + $function_end = $this->end; // calculate canvas step size and function step size From d3b765c47112b0ae5e708e2f8692ad301a9c6793 Mon Sep 17 00:00:00 2001 From: jakobsandberg Date: Sun, 9 Oct 2016 00:22:11 -0700 Subject: [PATCH 13/59] Calculated graph step size and function step size --- src/Plots/Plot.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Plots/Plot.php b/src/Plots/Plot.php index 1b5b595c3..09f387f90 100644 --- a/src/Plots/Plot.php +++ b/src/Plots/Plot.php @@ -44,7 +44,11 @@ public function draw($canvas) $function_start = $this->start; $function_end = $this->end; - // calculate canvas step size and function step size + // Calculate canvas step size and function step size + $n = 1000; + $graph_step_x = $graph_width/$n; + $graph_step_y = $graph_height/$n; + $function_step = ($b - $a)/$n; // Calculate function values, min, and max From 1bc8e41d46a6d0964bc54a1e9e7bbc051164bee9 Mon Sep 17 00:00:00 2001 From: jakobsandberg Date: Sun, 9 Oct 2016 00:24:55 -0700 Subject: [PATCH 14/59] Calculate function values, min, max, and function scale --- src/Plots/Plot.php | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/Plots/Plot.php b/src/Plots/Plot.php index 09f387f90..200856cd6 100644 --- a/src/Plots/Plot.php +++ b/src/Plots/Plot.php @@ -16,7 +16,7 @@ public function __construct() public function draw($canvas) { - // Build convenience variables for canvas/plot measures + // Build convenience variables for graph measures $width = $this->width; $height = $this->height; $padding = 50; @@ -39,18 +39,25 @@ public function draw($canvas) $black = imagecolorallocate($canvas, 0, 0, 0); imagerectangle($canvas, $graph_start_x, $graph_end_y, $graph_end_x, $graph_start_y, $black); - // Define input function + // Define input function and function domain $function = $this->function; - $function_start = $this->start; - $function_end = $this->end; + $start = $this->start; + $end = $this->end; - // Calculate canvas step size and function step size + // Calculate graph step size and function step size $n = 1000; $graph_step_x = $graph_width/$n; $graph_step_y = $graph_height/$n; - $function_step = ($b - $a)/$n; + $function_step = ($end - $start)/$n; // Calculate function values, min, and max + $image = []; + for ($i = 0; $i <= $n; $i++) { + $image[] = $function($start + $i*$function_step); + } + $min = min($image); + $max = max($image); + $function_scale = $graph_height/($max - $min); // Draw y-axis values, dashes From ff51fd2746f0f022fdb43ac477d955ee06f0373a Mon Sep 17 00:00:00 2001 From: jakobsandberg Date: Sun, 9 Oct 2016 00:28:04 -0700 Subject: [PATCH 15/59] Draw y-axis values and dashes --- src/Plots/Plot.php | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/Plots/Plot.php b/src/Plots/Plot.php index 200856cd6..70708c128 100644 --- a/src/Plots/Plot.php +++ b/src/Plots/Plot.php @@ -50,7 +50,7 @@ public function draw($canvas) $graph_step_y = $graph_height/$n; $function_step = ($end - $start)/$n; - // Calculate function values, min, and max + // Calculate function values, min, max, and function scale $image = []; for ($i = 0; $i <= $n; $i++) { $image[] = $function($start + $i*$function_step); @@ -60,6 +60,23 @@ public function draw($canvas) $function_scale = $graph_height/($max - $min); // Draw y-axis values, dashes + $fontpath = realpath('.'); //replace . with a different directory if needed + putenv('GDFONTPATH='.$fontpath); + $count = 9; + $font = 'arial.ttf'; + $size = 10; + $angle = 0; + $length1 = 1; + $length2 = 5; + $white = imagecolorallocate($canvas, 255, 255, 255); + $style = array_merge(array_fill(0, $length1, $black), array_fill(0, $length2, $white)); + imagesetstyle($canvas, $style); + for ($i = 0; $i <= $count; $i++) { + imagettftext($canvas, $size, $angle, $graph_start_x - $padding*0.75, $size*0.5 + $graph_start_y - $i*($graph_height/$count), $black, $font, round(($min + $i*($max - $min)/$count), 1)); + if ($i !== 0 and $i !== $count) { + imageline($canvas, $graph_start_x, $graph_start_y - $i*($graph_height/$count), $graph_end_x, $graph_start_y - $i*($graph_height/$count), IMG_COLOR_STYLED); + } + } // Draw x-axis values, dashes From f4f8e95a1eca61af5c1a87fb3ff213f0d560a9d7 Mon Sep 17 00:00:00 2001 From: jakobsandberg Date: Sun, 9 Oct 2016 00:33:44 -0700 Subject: [PATCH 16/59] Draw x-axis values and grid --- src/Plots/Plot.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/Plots/Plot.php b/src/Plots/Plot.php index 70708c128..898f0a642 100644 --- a/src/Plots/Plot.php +++ b/src/Plots/Plot.php @@ -59,7 +59,7 @@ public function draw($canvas) $max = max($image); $function_scale = $graph_height/($max - $min); - // Draw y-axis values, dashes + // Draw y-axis values and grid $fontpath = realpath('.'); //replace . with a different directory if needed putenv('GDFONTPATH='.$fontpath); $count = 9; @@ -78,7 +78,14 @@ public function draw($canvas) } } - // Draw x-axis values, dashes + // Draw x-axis values and grid + $newcount = 9; + for ($i = 0; $i <= $newcount; $i++) { + imagettftext($canvas, $size, $angle, $graph_start_x + $i*($graph_width/$newcount), $graph_start_y + $padding/2, $black, $font, round(($start + $i*($end - $start)/$newcount), 1)); + if ($i !== 0 and $i !== $newcount) { + imageline($canvas, $graph_start_x + $i*($graph_width/$newcount), $graph_start_y, $graph_start_x + $i*($graph_width/$newcount), $graph_end_y, IMG_COLOR_STYLED); + } + } // Draw title, x-axis title, y-axis title From c38729572cf375686f30b96d80c421d893da13a7 Mon Sep 17 00:00:00 2001 From: jakobsandberg Date: Sun, 9 Oct 2016 00:35:42 -0700 Subject: [PATCH 17/59] Draw x-axis values and grid --- src/Plots/Plot.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/Plots/Plot.php b/src/Plots/Plot.php index 898f0a642..e3ec99560 100644 --- a/src/Plots/Plot.php +++ b/src/Plots/Plot.php @@ -88,6 +88,23 @@ public function draw($canvas) } // Draw title, x-axis title, y-axis title + $sizeTitle = 20; + $sizeAxis = 16; + if (isset($title)) { + $p = imagettfbbox($sizeTitle, 0, $font, $title); + $title_x = ($width - ($p[2] - $p[0]))/2; + imagettftext($canvas, $sizeTitle, $angle, $title_x, 35, $black, $font, $title); + } + if (isset($x_label)) { + $q = imagettfbbox($sizeAxis, 0, $font, $x_label_text); + $x_label_width = ($width - ($q[2] - $q[0]))/2; + imagettftext($canvas, $sizeAxis, $angle, $x_label_width, $height - 35, $black, $font, $x_label); + } + if (isset($x_label)) { + $r = imagettfbbox($sizeAxis, 90, $font, $y_label_text); + $y_label_height = ($height - ($r[3] - $r[1]))/2; + imagettftext($canvas, $sizeAxis, 90, 40, $y_label_height, $black, $font, $y_label); + } // Draw graph From aab8930aa4c7442bc3bf954317610c1a1569155e Mon Sep 17 00:00:00 2001 From: jakobsandberg Date: Sun, 9 Oct 2016 00:40:05 -0700 Subject: [PATCH 18/59] Added section to fetch object parameters --- src/Plots/Plot.php | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/Plots/Plot.php b/src/Plots/Plot.php index e3ec99560..9eb1fac70 100644 --- a/src/Plots/Plot.php +++ b/src/Plots/Plot.php @@ -9,6 +9,7 @@ public function __construct() parent::__construct(); $this->x_label = "x-label"; $this->y_label = "y-label"; + $this->title = "Working!"; $this->function = function ($x) { return $x; }; $this->start = 0; $this->end = 10; @@ -16,13 +17,18 @@ public function __construct() public function draw($canvas) { - // Build convenience variables for graph measures - $width = $this->width; - $height = $this->height; + // Grab parameters + $width = $this->width; + $height = $this->height; $padding = 50; + $title = $this->title; + $x_label = $this->x_label; + $y_label = $this->y_label; + + // Build convenience variables for graph measures list($x_shift, $y_shift) = [ - isset($this->y_label) ? 1 : 0, - isset($this->x_label) ? 1 : 0, + isset($y_label) ? 1 : 0, + isset($x_label) ? 1 : 0, ]; list($graph_start_x, $graph_start_y, $graph_end_x, $graph_end_y) = [ (1 + $x_shift)*$padding, @@ -96,12 +102,12 @@ public function draw($canvas) imagettftext($canvas, $sizeTitle, $angle, $title_x, 35, $black, $font, $title); } if (isset($x_label)) { - $q = imagettfbbox($sizeAxis, 0, $font, $x_label_text); + $q = imagettfbbox($sizeAxis, 0, $font, $x_label); $x_label_width = ($width - ($q[2] - $q[0]))/2; imagettftext($canvas, $sizeAxis, $angle, $x_label_width, $height - 35, $black, $font, $x_label); } - if (isset($x_label)) { - $r = imagettfbbox($sizeAxis, 90, $font, $y_label_text); + if (isset($y_label)) { + $r = imagettfbbox($sizeAxis, 90, $font, $y_label); $y_label_height = ($height - ($r[3] - $r[1]))/2; imagettftext($canvas, $sizeAxis, 90, 40, $y_label_height, $black, $font, $y_label); } From 3853e6ade3e86bf006e3fb5977917bc44f6b5033 Mon Sep 17 00:00:00 2001 From: jakobsandberg Date: Sun, 9 Oct 2016 00:44:40 -0700 Subject: [PATCH 19/59] Draw graph --- src/Plots/Plot.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Plots/Plot.php b/src/Plots/Plot.php index 9eb1fac70..47aedec0c 100644 --- a/src/Plots/Plot.php +++ b/src/Plots/Plot.php @@ -10,9 +10,10 @@ public function __construct() $this->x_label = "x-label"; $this->y_label = "y-label"; $this->title = "Working!"; - $this->function = function ($x) { return $x; }; + $this->function = function ($x) { return sin($x); }; $this->start = 0; $this->end = 10; + $this->weight = 3; } public function draw($canvas) @@ -24,6 +25,7 @@ public function draw($canvas) $title = $this->title; $x_label = $this->x_label; $y_label = $this->y_label; + $weight = $this->weight; // Build convenience variables for graph measures list($x_shift, $y_shift) = [ @@ -113,6 +115,10 @@ public function draw($canvas) } // Draw graph + imagesetthickness($canvas, $weight); + for ($i = 0; $i < $n; $i++) { + imageline($canvas, $graph_start_x + $i*$graph_step_x, $graph_start_y - ($image[$i]-$min)*$function_scale, $graph_start_x + ($i+1)*$graph_step_x, $graph_start_y - ($image[$i+1]-$min)*$function_scale, $black); + } return $canvas; } From 47f19a22cb148681aa28186e826ddce19f06641f Mon Sep 17 00:00:00 2001 From: jakobsandberg Date: Sun, 9 Oct 2016 00:46:20 -0700 Subject: [PATCH 20/59] Moved padding to Plot class, removed canvas size from draw method --- src/Plots/Canvas.php | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/Plots/Canvas.php b/src/Plots/Canvas.php index 158ee8c3d..b2f7735f8 100644 --- a/src/Plots/Canvas.php +++ b/src/Plots/Canvas.php @@ -16,19 +16,18 @@ public function __construct($width = 600, $height = 600) $this->canvas = imagecreate($width, $height); } - public function addPlot($padding = 0) + public function addPlot() { - $this->plot = new Plot($padding); + $this->plot = new Plot(); return $this->plot; } public function save() { - $width = $this->width; - $height = $this->height; - $canvas = imagecreate($width, $height); + header('Content-type: image/png'); + $canvas = imagecreate($this->width, $this->height); imagecolorallocate($canvas, 255, 255, 255); - $canvas = $this->plot->draw($canvas, $width, $height); + $canvas = $this->plot->draw($canvas); imagejpeg($canvas, 'image-' . rand() . '.jpg'); } } From fd79f1b360fbf39964ee9740226d45ea279bab57 Mon Sep 17 00:00:00 2001 From: jakobsandberg Date: Sun, 9 Oct 2016 17:26:45 -0700 Subject: [PATCH 21/59] Added grid() method --- src/Plots/Plot.php | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/src/Plots/Plot.php b/src/Plots/Plot.php index 47aedec0c..06cae716c 100644 --- a/src/Plots/Plot.php +++ b/src/Plots/Plot.php @@ -4,16 +4,19 @@ class Plot extends Canvas { - public function __construct() + public function __construct(callable $function, $start, $end) { parent::__construct(); $this->x_label = "x-label"; - $this->y_label = "y-label"; - $this->title = "Working!"; - $this->function = function ($x) { return sin($x); }; - $this->start = 0; - $this->end = 10; - $this->weight = 3; + //$this->y_label = "y-label"; + //$this->title = "Working!"; + $this->function = $function; + $this->start = $start; + $this->end = $end; + } + + public function grid(bool $switch) { + $this->grid = $switch; } public function draw($canvas) @@ -22,10 +25,11 @@ public function draw($canvas) $width = $this->width; $height = $this->height; $padding = 50; - $title = $this->title; - $x_label = $this->x_label; - $y_label = $this->y_label; - $weight = $this->weight; + $title = $this->title ?? null; + $x_label = $this->x_label ?? null; + $y_label = $this->y_label ?? null; + $weight = $this->weight ?? 3; + $grid = $this->grid ?? false; // Build convenience variables for graph measures list($x_shift, $y_shift) = [ @@ -81,7 +85,7 @@ public function draw($canvas) imagesetstyle($canvas, $style); for ($i = 0; $i <= $count; $i++) { imagettftext($canvas, $size, $angle, $graph_start_x - $padding*0.75, $size*0.5 + $graph_start_y - $i*($graph_height/$count), $black, $font, round(($min + $i*($max - $min)/$count), 1)); - if ($i !== 0 and $i !== $count) { + if ($i !== 0 and $i !== $count and $grid) { imageline($canvas, $graph_start_x, $graph_start_y - $i*($graph_height/$count), $graph_end_x, $graph_start_y - $i*($graph_height/$count), IMG_COLOR_STYLED); } } @@ -90,7 +94,7 @@ public function draw($canvas) $newcount = 9; for ($i = 0; $i <= $newcount; $i++) { imagettftext($canvas, $size, $angle, $graph_start_x + $i*($graph_width/$newcount), $graph_start_y + $padding/2, $black, $font, round(($start + $i*($end - $start)/$newcount), 1)); - if ($i !== 0 and $i !== $newcount) { + if ($i !== 0 and $i !== $newcount and $grid) { imageline($canvas, $graph_start_x + $i*($graph_width/$newcount), $graph_start_y, $graph_start_x + $i*($graph_width/$newcount), $graph_end_y, IMG_COLOR_STYLED); } } From 16e9194469aae208396246c20a91cf1cb5dd1af3 Mon Sep 17 00:00:00 2001 From: jakobsandberg Date: Sun, 9 Oct 2016 17:28:44 -0700 Subject: [PATCH 22/59] Added yLabel() method --- src/Plots/Plot.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Plots/Plot.php b/src/Plots/Plot.php index 06cae716c..18f11a5c5 100644 --- a/src/Plots/Plot.php +++ b/src/Plots/Plot.php @@ -7,7 +7,7 @@ class Plot extends Canvas public function __construct(callable $function, $start, $end) { parent::__construct(); - $this->x_label = "x-label"; + //$this->x_label = "x-label"; //$this->y_label = "y-label"; //$this->title = "Working!"; $this->function = $function; @@ -19,6 +19,10 @@ public function grid(bool $switch) { $this->grid = $switch; } + public function yLabel(string $label) { + $this->y_label = $label; + } + public function draw($canvas) { // Grab parameters From 48fb58bab4043d3ca9166678b1099ed05cfa03f4 Mon Sep 17 00:00:00 2001 From: jakobsandberg Date: Sun, 9 Oct 2016 17:30:53 -0700 Subject: [PATCH 23/59] Added xLabel() method --- src/Plots/Plot.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Plots/Plot.php b/src/Plots/Plot.php index 18f11a5c5..cb2cc3b0b 100644 --- a/src/Plots/Plot.php +++ b/src/Plots/Plot.php @@ -7,9 +7,6 @@ class Plot extends Canvas public function __construct(callable $function, $start, $end) { parent::__construct(); - //$this->x_label = "x-label"; - //$this->y_label = "y-label"; - //$this->title = "Working!"; $this->function = $function; $this->start = $start; $this->end = $end; @@ -23,6 +20,10 @@ public function yLabel(string $label) { $this->y_label = $label; } + public function xLabel(string $label) { + $this->x_label = $label; + } + public function draw($canvas) { // Grab parameters From 97a5661a638660e662adf0e74577557968e77f52 Mon Sep 17 00:00:00 2001 From: jakobsandberg Date: Sun, 9 Oct 2016 17:33:19 -0700 Subject: [PATCH 24/59] Added title() method --- src/Plots/Plot.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Plots/Plot.php b/src/Plots/Plot.php index cb2cc3b0b..b5b666606 100644 --- a/src/Plots/Plot.php +++ b/src/Plots/Plot.php @@ -16,6 +16,10 @@ public function grid(bool $switch) { $this->grid = $switch; } + public function title(string $title) { + $this->title = $title; + } + public function yLabel(string $label) { $this->y_label = $label; } @@ -105,8 +109,8 @@ public function draw($canvas) } // Draw title, x-axis title, y-axis title - $sizeTitle = 20; - $sizeAxis = 16; + $sizeTitle = 16; + $sizeAxis = 14; if (isset($title)) { $p = imagettfbbox($sizeTitle, 0, $font, $title); $title_x = ($width - ($p[2] - $p[0]))/2; From b92dbcb0f204aa70390fbff679f30fb3c6c15689 Mon Sep 17 00:00:00 2001 From: jakobsandberg Date: Sun, 9 Oct 2016 18:14:31 -0700 Subject: [PATCH 25/59] Added color() method to Plot --- src/Plots/Plot.php | 40 +++++++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/src/Plots/Plot.php b/src/Plots/Plot.php index b5b666606..f25e1a7ec 100644 --- a/src/Plots/Plot.php +++ b/src/Plots/Plot.php @@ -12,33 +12,60 @@ public function __construct(callable $function, $start, $end) $this->end = $end; } - public function grid(bool $switch) { + public function grid(bool $switch) + { $this->grid = $switch; } - public function title(string $title) { + public function title(string $title) + { $this->title = $title; } - public function yLabel(string $label) { + public function yLabel(string $label) + { $this->y_label = $label; } - public function xLabel(string $label) { + public function xLabel(string $label) + { $this->x_label = $label; } + public function color(string $color) + { + //$this->color = $color; + switch($color) { + case 'red': + $color = [255, 0, 0]; + break; + case 'green': + $color = [0, 255, 0]; + break; + case 'blue': + $color = [0, 0, 255]; + break; + default: + $color = [0, 0, 0]; + } + $this->color = $color; + } + public function draw($canvas) { + // Set defaults + $black = imagecolorallocate($canvas, 0, 0, 0); + $padding = 50; + // Grab parameters $width = $this->width; $height = $this->height; - $padding = 50; $title = $this->title ?? null; $x_label = $this->x_label ?? null; $y_label = $this->y_label ?? null; $weight = $this->weight ?? 3; $grid = $this->grid ?? false; + $color = isset($this->color) ? imagecolorallocate($canvas, ... $this->color) : $black; // Build convenience variables for graph measures list($x_shift, $y_shift) = [ @@ -57,7 +84,6 @@ public function draw($canvas) ]; // Create axes - $black = imagecolorallocate($canvas, 0, 0, 0); imagerectangle($canvas, $graph_start_x, $graph_end_y, $graph_end_x, $graph_start_y, $black); // Define input function and function domain @@ -130,7 +156,7 @@ public function draw($canvas) // Draw graph imagesetthickness($canvas, $weight); for ($i = 0; $i < $n; $i++) { - imageline($canvas, $graph_start_x + $i*$graph_step_x, $graph_start_y - ($image[$i]-$min)*$function_scale, $graph_start_x + ($i+1)*$graph_step_x, $graph_start_y - ($image[$i+1]-$min)*$function_scale, $black); + imageline($canvas, $graph_start_x + $i*$graph_step_x, $graph_start_y - ($image[$i]-$min)*$function_scale, $graph_start_x + ($i+1)*$graph_step_x, $graph_start_y - ($image[$i+1]-$min)*$function_scale, $color); } return $canvas; From 2b146bfe72fc22ed51379ceddd1c2ca851664e92 Mon Sep 17 00:00:00 2001 From: jakobsandberg Date: Sun, 9 Oct 2016 22:04:25 -0700 Subject: [PATCH 26/59] Refactored Plot to use imagestring instead of imagefttext --- src/Plots/Plot.php | 116 +++++++++++++++++++-------------------------- 1 file changed, 48 insertions(+), 68 deletions(-) diff --git a/src/Plots/Plot.php b/src/Plots/Plot.php index f25e1a7ec..ef3d4f7a5 100644 --- a/src/Plots/Plot.php +++ b/src/Plots/Plot.php @@ -8,8 +8,8 @@ public function __construct(callable $function, $start, $end) { parent::__construct(); $this->function = $function; - $this->start = $start; - $this->end = $end; + $this->start = $start; + $this->end = $end; } public function grid(bool $switch) @@ -24,17 +24,16 @@ public function title(string $title) public function yLabel(string $label) { - $this->y_label = $label; + $this->yLabel = $label; } public function xLabel(string $label) { - $this->x_label = $label; + $this->xLabel = $label; } public function color(string $color) { - //$this->color = $color; switch($color) { case 'red': $color = [255, 0, 0]; @@ -55,42 +54,39 @@ public function draw($canvas) { // Set defaults $black = imagecolorallocate($canvas, 0, 0, 0); + $white = imagecolorallocate($canvas, 255, 255, 255); $padding = 50; // Grab parameters - $width = $this->width; - $height = $this->height; - $title = $this->title ?? null; - $x_label = $this->x_label ?? null; - $y_label = $this->y_label ?? null; - $weight = $this->weight ?? 3; - $grid = $this->grid ?? false; - $color = isset($this->color) ? imagecolorallocate($canvas, ... $this->color) : $black; - - // Build convenience variables for graph measures - list($x_shift, $y_shift) = [ - isset($y_label) ? 1 : 0, - isset($x_label) ? 1 : 0, - ]; - list($graph_start_x, $graph_start_y, $graph_end_x, $graph_end_y) = [ - (1 + $x_shift)*$padding, - imagesy($canvas) - (1 + $y_shift)*$padding, - imagesx($canvas) - $padding, - $padding - ]; - list($graph_width, $graph_height) = [ - imagesx($canvas) - (2 + $x_shift)*$padding, - imagesy($canvas) - (2 + $y_shift)*$padding - ]; - - // Create axes - imagerectangle($canvas, $graph_start_x, $graph_end_y, $graph_end_x, $graph_start_y, $black); - - // Define input function and function domain + $width = $this->width; + $height = $this->height; + $title = $this->title ?? null; + $xLabel = $this->xLabel ?? null; + $yLabel = $this->yLabel ?? null; + $weight = $this->weight ?? 3; + $grid = $this->grid ?? false; + $color = isset($this->color) ? imagecolorallocate($canvas, ... $this->color) : $black; $function = $this->function; $start = $this->start; $end = $this->end; + // Determine if we need to add padding to make room for axis labels + $x_shift = isset($yLabel) ? 40 : 0; + $y_shift = isset($xLabel) ? 10 : 0; + + // Measure start and end points of plot on canvas + $graph_start_x = $padding + $x_shift; + $graph_start_y = imagesy($canvas) - ($padding + $y_shift); + $graph_end_x = imagesx($canvas) - $padding; + $graph_end_y = $padding; + + // Measure height and width of plot on canvas + $graph_width = $graph_end_x - $graph_start_x; + $graph_height = $graph_start_y - $graph_end_y; + + // Create axes + imagerectangle($canvas, $graph_start_x, $graph_end_y, $graph_end_x, $graph_start_y, $black); + // Calculate graph step size and function step size $n = 1000; $graph_step_x = $graph_width/$n; @@ -102,55 +98,39 @@ public function draw($canvas) for ($i = 0; $i <= $n; $i++) { $image[] = $function($start + $i*$function_step); } - $min = min($image); - $max = max($image); + $min = min($image); + $max = max($image); $function_scale = $graph_height/($max - $min); // Draw y-axis values and grid - $fontpath = realpath('.'); //replace . with a different directory if needed - putenv('GDFONTPATH='.$fontpath); - $count = 9; - $font = 'arial.ttf'; - $size = 10; - $angle = 0; - $length1 = 1; - $length2 = 5; - $white = imagecolorallocate($canvas, 255, 255, 255); - $style = array_merge(array_fill(0, $length1, $black), array_fill(0, $length2, $white)); + $gridCountY = 10; + $style = array_merge(array_fill(0, 1, $black), array_fill(0, 5, $white)); imagesetstyle($canvas, $style); - for ($i = 0; $i <= $count; $i++) { - imagettftext($canvas, $size, $angle, $graph_start_x - $padding*0.75, $size*0.5 + $graph_start_y - $i*($graph_height/$count), $black, $font, round(($min + $i*($max - $min)/$count), 1)); - if ($i !== 0 and $i !== $count and $grid) { - imageline($canvas, $graph_start_x, $graph_start_y - $i*($graph_height/$count), $graph_end_x, $graph_start_y - $i*($graph_height/$count), IMG_COLOR_STYLED); + for ($i = 0; $i <= $gridCountY; $i++) { + imagestring($canvas, 2, $graph_start_x - 50, $graph_start_y - 8 - $i*($graph_height/$gridCountY), round(($min + $i*($max - $min)/$gridCountY), 1), $black); + if ($i !== 0 and $i !== $gridCountY and $grid) { + imageline($canvas, $graph_start_x, $graph_start_y - $i*($graph_height/$gridCountY), $graph_end_x, $graph_start_y - $i*($graph_height/$gridCountY), IMG_COLOR_STYLED); } } // Draw x-axis values and grid - $newcount = 9; - for ($i = 0; $i <= $newcount; $i++) { - imagettftext($canvas, $size, $angle, $graph_start_x + $i*($graph_width/$newcount), $graph_start_y + $padding/2, $black, $font, round(($start + $i*($end - $start)/$newcount), 1)); - if ($i !== 0 and $i !== $newcount and $grid) { - imageline($canvas, $graph_start_x + $i*($graph_width/$newcount), $graph_start_y, $graph_start_x + $i*($graph_width/$newcount), $graph_end_y, IMG_COLOR_STYLED); + $gridCountX = 10; + for ($i = 0; $i <= $gridCountX; $i++) { + imagestring($canvas, 2, $graph_start_x + $i*($graph_width/$gridCountX), $graph_start_y + 8, round(($start + $i*($end - $start)/$gridCountX), 1), $black); + if ($i !== 0 and $i !== $gridCountX and $grid) { + imageline($canvas, $graph_start_x + $i*($graph_width/$gridCountX), $graph_start_y, $graph_start_x + $i*($graph_width/$gridCountX), $graph_end_y, IMG_COLOR_STYLED); } } // Draw title, x-axis title, y-axis title - $sizeTitle = 16; - $sizeAxis = 14; if (isset($title)) { - $p = imagettfbbox($sizeTitle, 0, $font, $title); - $title_x = ($width - ($p[2] - $p[0]))/2; - imagettftext($canvas, $sizeTitle, $angle, $title_x, 35, $black, $font, $title); + imagestring($canvas, 5, ($width - strlen($title)*9)/2, 18, $title, $black); } - if (isset($x_label)) { - $q = imagettfbbox($sizeAxis, 0, $font, $x_label); - $x_label_width = ($width - ($q[2] - $q[0]))/2; - imagettftext($canvas, $sizeAxis, $angle, $x_label_width, $height - 35, $black, $font, $x_label); + if (isset($xLabel)) { + imagestring($canvas, 4, ($width - strlen($xLabel)*8)/2, $height - 30, $xLabel, $black); } - if (isset($y_label)) { - $r = imagettfbbox($sizeAxis, 90, $font, $y_label); - $y_label_height = ($height - ($r[3] - $r[1]))/2; - imagettftext($canvas, $sizeAxis, 90, 40, $y_label_height, $black, $font, $y_label); + if (isset($yLabel)) { + imagestringup($canvas, 4, 10, ($height + strlen($yLabel)*8)/2, $yLabel, $black); } // Draw graph From 462ad3af2ae65d563be109e4a7c4c92d5e06352e Mon Sep 17 00:00:00 2001 From: jakobsandberg Date: Sun, 9 Oct 2016 22:55:48 -0700 Subject: [PATCH 27/59] Centered axis points --- src/Plots/Plot.php | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/src/Plots/Plot.php b/src/Plots/Plot.php index ef3d4f7a5..334735ff3 100644 --- a/src/Plots/Plot.php +++ b/src/Plots/Plot.php @@ -87,16 +87,16 @@ public function draw($canvas) // Create axes imagerectangle($canvas, $graph_start_x, $graph_end_y, $graph_end_x, $graph_start_y, $black); - // Calculate graph step size and function step size + // Calculate function step size (h) and graph step size $n = 1000; + $h = ($end - $start)/$n; $graph_step_x = $graph_width/$n; $graph_step_y = $graph_height/$n; - $function_step = ($end - $start)/$n; // Calculate function values, min, max, and function scale $image = []; for ($i = 0; $i <= $n; $i++) { - $image[] = $function($start + $i*$function_step); + $image[] = $function($start + $i*$h); } $min = min($image); $max = max($image); @@ -107,18 +107,26 @@ public function draw($canvas) $style = array_merge(array_fill(0, 1, $black), array_fill(0, 5, $white)); imagesetstyle($canvas, $style); for ($i = 0; $i <= $gridCountY; $i++) { - imagestring($canvas, 2, $graph_start_x - 50, $graph_start_y - 8 - $i*($graph_height/$gridCountY), round(($min + $i*($max - $min)/$gridCountY), 1), $black); - if ($i !== 0 and $i !== $gridCountY and $grid) { - imageline($canvas, $graph_start_x, $graph_start_y - $i*($graph_height/$gridCountY), $graph_end_x, $graph_start_y - $i*($graph_height/$gridCountY), IMG_COLOR_STYLED); + $value = round(($min + $i*($max - $min)/$gridCountY), 1); + $X₀ = $graph_start_x; + $Xₙ = $graph_end_x; + $Y₀ = $graph_start_y - $i*($graph_height/$gridCountY); + imagestring($canvas, 2, $X₀ - 10 - 6*strlen($value), $Y₀ - 8, $value, $black); + if ($i !== 0 && $i !== $gridCountY && $grid) { + imageline($canvas, $X₀, $Y₀, $Xₙ, $Y₀, IMG_COLOR_STYLED); } } // Draw x-axis values and grid $gridCountX = 10; for ($i = 0; $i <= $gridCountX; $i++) { - imagestring($canvas, 2, $graph_start_x + $i*($graph_width/$gridCountX), $graph_start_y + 8, round(($start + $i*($end - $start)/$gridCountX), 1), $black); - if ($i !== 0 and $i !== $gridCountX and $grid) { - imageline($canvas, $graph_start_x + $i*($graph_width/$gridCountX), $graph_start_y, $graph_start_x + $i*($graph_width/$gridCountX), $graph_end_y, IMG_COLOR_STYLED); + $value = round(($start + $i*($end - $start)/$gridCountX), 1); + $X₀ = $graph_start_x + $i*($graph_width/$gridCountX); + $Y₀ = $graph_start_y; + $Yₙ = $graph_end_y; + imagestring($canvas, 2, $X₀ + 2 - strlen($value)*3, $Y₀ + 8, $value, $black); + if ($i !== 0 && $i !== $gridCountX && $grid) { + imageline($canvas, $X₀, $Y₀, $X₀, $Yₙ, IMG_COLOR_STYLED); } } @@ -136,7 +144,11 @@ public function draw($canvas) // Draw graph imagesetthickness($canvas, $weight); for ($i = 0; $i < $n; $i++) { - imageline($canvas, $graph_start_x + $i*$graph_step_x, $graph_start_y - ($image[$i]-$min)*$function_scale, $graph_start_x + ($i+1)*$graph_step_x, $graph_start_y - ($image[$i+1]-$min)*$function_scale, $color); + $xᵢ = $graph_start_x + $i*$graph_step_x; + $xᵢ₊₁ = $graph_start_x + ($i+1)*$graph_step_x; + $f⟮xᵢ⟯ = $graph_start_y - ($image[$i]-$min)*$function_scale; + $f⟮xᵢ₊₁⟯ = $graph_start_y - ($image[$i+1]-$min)*$function_scale; + imageline($canvas, $xᵢ, $f⟮xᵢ⟯, $xᵢ₊₁, $f⟮xᵢ₊₁⟯, $color); } return $canvas; From b2b95e1dc8faeb52f68565ac2ca326a2fd2c333f Mon Sep 17 00:00:00 2001 From: jakobsandberg Date: Sun, 9 Oct 2016 22:56:01 -0700 Subject: [PATCH 28/59] Centered title and axis labels --- src/Plots/Plot.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Plots/Plot.php b/src/Plots/Plot.php index 334735ff3..f921e8506 100644 --- a/src/Plots/Plot.php +++ b/src/Plots/Plot.php @@ -132,13 +132,13 @@ public function draw($canvas) // Draw title, x-axis title, y-axis title if (isset($title)) { - imagestring($canvas, 5, ($width - strlen($title)*9)/2, 18, $title, $black); + imagestring($canvas, 5, ($width + $x_shift - strlen($title)*9)/2, 18, $title, $black); } if (isset($xLabel)) { - imagestring($canvas, 4, ($width - strlen($xLabel)*8)/2, $height - 30, $xLabel, $black); + imagestring($canvas, 4, ($width + $x_shift - strlen($xLabel)*8)/2, $height - 30, $xLabel, $black); } if (isset($yLabel)) { - imagestringup($canvas, 4, 10, ($height + strlen($yLabel)*8)/2, $yLabel, $black); + imagestringup($canvas, 4, 10, ($height - $y_shift + strlen($yLabel)*8)/2, $yLabel, $black); } // Draw graph From 5d68ad4b97698313034dd7cf023b9f77590d8d8c Mon Sep 17 00:00:00 2001 From: jakobsandberg Date: Sun, 9 Oct 2016 22:58:09 -0700 Subject: [PATCH 29/59] Centered title and axis labels --- src/Plots/Plot.php | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/src/Plots/Plot.php b/src/Plots/Plot.php index f921e8506..73d4a6f9c 100644 --- a/src/Plots/Plot.php +++ b/src/Plots/Plot.php @@ -50,25 +50,29 @@ public function color(string $color) $this->color = $color; } + public function thickness(int $thickness) + { + $this->thickness = $thickness; + } + public function draw($canvas) { - // Set defaults $black = imagecolorallocate($canvas, 0, 0, 0); $white = imagecolorallocate($canvas, 255, 255, 255); $padding = 50; // Grab parameters - $width = $this->width; - $height = $this->height; - $title = $this->title ?? null; - $xLabel = $this->xLabel ?? null; - $yLabel = $this->yLabel ?? null; - $weight = $this->weight ?? 3; - $grid = $this->grid ?? false; - $color = isset($this->color) ? imagecolorallocate($canvas, ... $this->color) : $black; - $function = $this->function; - $start = $this->start; - $end = $this->end; + $width = $this->width; + $height = $this->height; + $title = $this->title ?? null; + $xLabel = $this->xLabel ?? null; + $yLabel = $this->yLabel ?? null; + $thickness = $this->thickness ?? 3; + $grid = $this->grid ?? false; + $color = isset($this->color) ? imagecolorallocate($canvas, ... $this->color) : $black; + $function = $this->function; + $start = $this->start; + $end = $this->end; // Determine if we need to add padding to make room for axis labels $x_shift = isset($yLabel) ? 40 : 0; @@ -142,7 +146,7 @@ public function draw($canvas) } // Draw graph - imagesetthickness($canvas, $weight); + imagesetthickness($canvas, $thickness); for ($i = 0; $i < $n; $i++) { $xᵢ = $graph_start_x + $i*$graph_step_x; $xᵢ₊₁ = $graph_start_x + ($i+1)*$graph_step_x; From ac00cd5b9cf803b81fb1c30e2b9afaf504fb9ba1 Mon Sep 17 00:00:00 2001 From: jakobsandberg Date: Sun, 9 Oct 2016 23:07:27 -0700 Subject: [PATCH 30/59] Added class description for Plots --- src/Plots/Canvas.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Plots/Canvas.php b/src/Plots/Canvas.php index b2f7735f8..2b706224d 100644 --- a/src/Plots/Canvas.php +++ b/src/Plots/Canvas.php @@ -2,6 +2,11 @@ namespace MathPHP\Plots; +/** + * Plotting Canvas + * + * The base class for the plotting utility. + */ class Canvas { protected $width; @@ -16,9 +21,9 @@ public function __construct($width = 600, $height = 600) $this->canvas = imagecreate($width, $height); } - public function addPlot() + public function addPlot(callable $function, $start = 0, $end = 10) { - $this->plot = new Plot(); + $this->plot = new Plot($function, $start, $end); return $this->plot; } From 72888599f14c7413d52e5f45930a635058a1508a Mon Sep 17 00:00:00 2001 From: jakobsandberg Date: Sun, 9 Oct 2016 23:08:27 -0700 Subject: [PATCH 31/59] Added class description for Plots --- src/Plots/Canvas.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Plots/Canvas.php b/src/Plots/Canvas.php index 2b706224d..05cce0986 100644 --- a/src/Plots/Canvas.php +++ b/src/Plots/Canvas.php @@ -27,6 +27,12 @@ public function addPlot(callable $function, $start = 0, $end = 10) return $this->plot; } + public function size($width = 600, $height = 600) + { + $this->width = $width; + $this->height = $height; + } + public function save() { header('Content-type: image/png'); From 17a7f66018ca89d56d31ea8b3d5d20a5eb93f298 Mon Sep 17 00:00:00 2001 From: jakobsandberg Date: Sun, 9 Oct 2016 23:29:29 -0700 Subject: [PATCH 32/59] Added canvas width and height into new Plot() class within addPlot --- src/Plots/Canvas.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/Plots/Canvas.php b/src/Plots/Canvas.php index 05cce0986..b0b9f8841 100644 --- a/src/Plots/Canvas.php +++ b/src/Plots/Canvas.php @@ -11,19 +11,19 @@ class Canvas { protected $width; protected $height; - protected $canvas; - protected $plot; + private $plot; public function __construct($width = 600, $height = 600) { $this->width = $width; $this->height = $height; - $this->canvas = imagecreate($width, $height); } public function addPlot(callable $function, $start = 0, $end = 10) { - $this->plot = new Plot($function, $start, $end); + $width = $this->width; + $height = $this->height; + $this->plot = new Plot($function, $start, $end, $width, $height); return $this->plot; } @@ -31,6 +31,9 @@ public function size($width = 600, $height = 600) { $this->width = $width; $this->height = $height; + if(isset($this->plot)) { + $this->plot->size($width, $height); + } } public function save() From 0803a194741b844b1c800e49f37696c396b70b90 Mon Sep 17 00:00:00 2001 From: jakobsandberg Date: Sun, 9 Oct 2016 23:29:55 -0700 Subject: [PATCH 33/59] Added parent classes width and height into constructor --- src/Plots/Plot.php | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/Plots/Plot.php b/src/Plots/Plot.php index 73d4a6f9c..a4de9cf8d 100644 --- a/src/Plots/Plot.php +++ b/src/Plots/Plot.php @@ -4,14 +4,24 @@ class Plot extends Canvas { - public function __construct(callable $function, $start, $end) + private $function; + private $start; + private $end; + + public function __construct(callable $function, $start, $end, $width, $height) { - parent::__construct(); + parent::__construct($width, $height); $this->function = $function; $this->start = $start; $this->end = $end; } + public function size($width = 600, $height = 600) + { + $this->width = $width; + $this->height = $height; + } + public function grid(bool $switch) { $this->grid = $switch; From e6fdca9ca0ed29abe1661a68ab950eb2ced3e656 Mon Sep 17 00:00:00 2001 From: jakobsandberg Date: Sun, 9 Oct 2016 23:30:51 -0700 Subject: [PATCH 34/59] Added parent classes width and height into constructor --- src/Plots/Plot.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Plots/Plot.php b/src/Plots/Plot.php index a4de9cf8d..d3e2989c3 100644 --- a/src/Plots/Plot.php +++ b/src/Plots/Plot.php @@ -16,7 +16,7 @@ public function __construct(callable $function, $start, $end, $width, $height) $this->end = $end; } - public function size($width = 600, $height = 600) + public function size($width, $height) { $this->width = $width; $this->height = $height; From 9c2b0630b9d9c52a5acfb50765e476fd4b5d080c Mon Sep 17 00:00:00 2001 From: jakobsandberg Date: Sun, 9 Oct 2016 23:32:47 -0700 Subject: [PATCH 35/59] Updated default canvas size to 700 x 500 --- src/Plots/Canvas.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Plots/Canvas.php b/src/Plots/Canvas.php index b0b9f8841..69c79389f 100644 --- a/src/Plots/Canvas.php +++ b/src/Plots/Canvas.php @@ -13,7 +13,7 @@ class Canvas protected $height; private $plot; - public function __construct($width = 600, $height = 600) + public function __construct($width = 700, $height = 500) { $this->width = $width; $this->height = $height; @@ -27,7 +27,7 @@ public function addPlot(callable $function, $start = 0, $end = 10) return $this->plot; } - public function size($width = 600, $height = 600) + public function size($width, $height) { $this->width = $width; $this->height = $height; From 9e1b298217ef675c32158d9dfcf79cdf37b0d2d8 Mon Sep 17 00:00:00 2001 From: jakobsandberg Date: Wed, 12 Oct 2016 22:39:41 -0700 Subject: [PATCH 36/59] Add method descriptions to Canvas class --- src/Plots/Canvas.php | 50 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/src/Plots/Canvas.php b/src/Plots/Canvas.php index 69c79389f..6fa040c6a 100644 --- a/src/Plots/Canvas.php +++ b/src/Plots/Canvas.php @@ -13,21 +13,57 @@ class Canvas protected $height; private $plot; - public function __construct($width = 700, $height = 500) + /** + * Construct a new plotting canvas. + * + * The input arguments refer to the size of the canvas in pixels. Thus, + * when you run the save() method, the size of the resulting image file + * will be determined by these parameters. For example, running + * (new Canvas())->save() will produce an image with a default size of + * 700px by 500px. + * + * @param int $width The width of our canvas, in pixels + * @param int $height The height of our canvas, in pixels + */ + public function __construct(int $width = 700, int $height = 500) { $this->width = $width; $this->height = $height; } + /** + * Add a single plot to our canvas object. + * + * This method is used when we are including a single plot in a canvas + * object. To add multiple plots to a single canvas, use the addSubplot() + * method (to be added in a future release). + * + * @param callable $function The callback function we are plotting + * @param number $start The start of our plotting interval + * @param number $end The end of the plotting interval + * + * @return object The resulting Plot object constructed from our inputs + * and the parameters of the parent canvas object + */ public function addPlot(callable $function, $start = 0, $end = 10) { $width = $this->width; $height = $this->height; $this->plot = new Plot($function, $start, $end, $width, $height); + return $this->plot; } - public function size($width, $height) + /** + * Modify the size of our canvas. + * + * Refer to the __construct() method for for further understanding of + * canvas sizes. + * + * @param int $width The width of our canvas, in pixels + * @param int $height The height of our canvas, in pixels + */ + public function size(int $width, int $height) { $this->width = $width; $this->height = $height; @@ -36,6 +72,16 @@ public function size($width, $height) } } + /** + * Draw plot(s) and output resulting canvas. + * + * Draw the plot object(s) stored within our canvas' plot parameter. Then, + * output the resulting canvas in a certain format. Currently, only + * JPG outputs are supported. More support should be added soon, such as + * outputting directly to a webpage, different file formats, etc. + * + * By default, this gives our canvas a white background. + */ public function save() { header('Content-type: image/png'); From ea6639e809e3886db956785dc3f1d5da69b6db72 Mon Sep 17 00:00:00 2001 From: jakobsandberg Date: Wed, 12 Oct 2016 22:48:14 -0700 Subject: [PATCH 37/59] Add description to Canvas Class --- src/Plots/Canvas.php | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/Plots/Canvas.php b/src/Plots/Canvas.php index 6fa040c6a..7dc75d135 100644 --- a/src/Plots/Canvas.php +++ b/src/Plots/Canvas.php @@ -6,6 +6,22 @@ * Plotting Canvas * * The base class for the plotting utility. + * + * This is the parent class for our plotting utility. To create a plot image, + * you first start with this class to build the foundation for our plot. This + * will create an environment for which you can add a single plot or, in the + * future, multiple plots. + * + * This class currently supports the following methods: + * - addPlot(): add a single plot objects (graphs) to our canvas + * - size(): adjust the size of our canvas + * - save(): draw the current + * + * Example: Plot the graph f(x) = x over [0, 100] + * $canvas = new Canvas(1000, 500); + * $canvas->addPlot(function ($x) { return $x; }, 0, 100); + * $canvas->save(); + * */ class Canvas { @@ -38,6 +54,8 @@ public function __construct(int $width = 700, int $height = 500) * object. To add multiple plots to a single canvas, use the addSubplot() * method (to be added in a future release). * + * The default interval of our plot is [0, 10]. + * * @param callable $function The callback function we are plotting * @param number $start The start of our plotting interval * @param number $end The end of the plotting interval @@ -85,9 +103,14 @@ public function size(int $width, int $height) public function save() { header('Content-type: image/png'); + $canvas = imagecreate($this->width, $this->height); imagecolorallocate($canvas, 255, 255, 255); - $canvas = $this->plot->draw($canvas); + + if (isset($this->plot)) { + $canvas = $this->plot->draw($canvas); + } + imagejpeg($canvas, 'image-' . rand() . '.jpg'); } } From 2a5aabfde89f951e6cee9ac224c1c94f3663e4a4 Mon Sep 17 00:00:00 2001 From: jakobsandberg Date: Wed, 12 Oct 2016 23:09:31 -0700 Subject: [PATCH 38/59] Added description to Plot class --- src/Plots/Plot.php | 80 +++++++++++++++++++++++++++++++++------------- 1 file changed, 58 insertions(+), 22 deletions(-) diff --git a/src/Plots/Plot.php b/src/Plots/Plot.php index d3e2989c3..c7cc93ef5 100644 --- a/src/Plots/Plot.php +++ b/src/Plots/Plot.php @@ -2,6 +2,46 @@ namespace MathPHP\Plots; +/* +* Single Plot +* +* Contains paramters for a single plot to draw on a parent Canvas object. +* +* To create a plot, first start with a canvas object. Then, construct a child +* Plot object by using the addPlot() method in the Canvas class. This will +* ensure that our resulting Plot object is tied to the canvas object to which +* it corresponds. Then, we explicitly use Plot methods() for the resulting +* plot object. +* +* This class currently supports the following methods: +* - grid(): Turn on or off the grid lines for our plot, and specify the +* number of grid lines for each axis +* - title(): Assign a title to our plot +* - yLabel(): Assign a label to our y-axis +* - xLabel(): Assign a label to our x-axis +* - color(): Set the color of our plot line/curve +* - thickness(): Set the thickness of our plot line/curve +* +* Example: Graph the function f(x) = x*sin(x) on [0, 20] with a grid, title, +* y-axis label, x-axis label, a color of red, and thickness of 5 +* $canvas = new Canvas(); +* $plot = $canvas->addPlot(function ($x) { return $x*sin($x); }, 0, 20); +* $plot->grid(true); +* $plot->yLabel("This is a working y-label"); +* $plot->xLabel("Time (seconds)"); +* $plot->title("Sample Title"); +* $plot->color("red"); +* $plot->thickness(5); +* $canvas->save(); +* +* There are plans to add support for the following: +* - A method to change the font for all text +* - A method to change the font size for all text +* - A method to change the font color for all text +* - A method to add more graphs (functions) to the same plot +* - A child class for each text field (title and axis labels) which contains +* methods to adjust the color, size, and font of that specific text field +*/ class Plot extends Canvas { private $function; @@ -16,15 +56,11 @@ public function __construct(callable $function, $start, $end, $width, $height) $this->end = $end; } - public function size($width, $height) + public function grid(bool $switch = true, int $gridCountX = 10, int $gridCountY = 10) { - $this->width = $width; - $this->height = $height; - } - - public function grid(bool $switch) - { - $this->grid = $switch; + $this->grid = $switch; + $this->gridCountX = 10; + $this->gridCountY = 10; } public function title(string $title) @@ -71,18 +107,20 @@ public function draw($canvas) $white = imagecolorallocate($canvas, 255, 255, 255); $padding = 50; - // Grab parameters - $width = $this->width; - $height = $this->height; - $title = $this->title ?? null; - $xLabel = $this->xLabel ?? null; - $yLabel = $this->yLabel ?? null; - $thickness = $this->thickness ?? 3; - $grid = $this->grid ?? false; - $color = isset($this->color) ? imagecolorallocate($canvas, ... $this->color) : $black; - $function = $this->function; - $start = $this->start; - $end = $this->end; + // Grab parameters or assign defaults + $width = $this->width; + $height = $this->height; + $title = $this->title ?? null; + $xLabel = $this->xLabel ?? null; + $yLabel = $this->yLabel ?? null; + $color = isset($this->color) ? imagecolorallocate($canvas, ... $this->color) : $black; + $thickness = $this->thickness ?? 3; + $grid = $this->grid ?? false; + $gridCountY = $this->gridCountY ?? 10; + $gridCountX = $this->gridCountX ?? 10; + $function = $this->function; + $start = $this->start; + $end = $this->end; // Determine if we need to add padding to make room for axis labels $x_shift = isset($yLabel) ? 40 : 0; @@ -117,7 +155,6 @@ public function draw($canvas) $function_scale = $graph_height/($max - $min); // Draw y-axis values and grid - $gridCountY = 10; $style = array_merge(array_fill(0, 1, $black), array_fill(0, 5, $white)); imagesetstyle($canvas, $style); for ($i = 0; $i <= $gridCountY; $i++) { @@ -132,7 +169,6 @@ public function draw($canvas) } // Draw x-axis values and grid - $gridCountX = 10; for ($i = 0; $i <= $gridCountX; $i++) { $value = round(($start + $i*($end - $start)/$gridCountX), 1); $X₀ = $graph_start_x + $i*($graph_width/$gridCountX); From 13ae056cea9d961aba9a90e71c6cf7b5823cd164 Mon Sep 17 00:00:00 2001 From: jakobsandberg Date: Wed, 12 Oct 2016 23:12:19 -0700 Subject: [PATCH 39/59] Added type checking for method outputs in Canvas class --- src/Plots/Canvas.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Plots/Canvas.php b/src/Plots/Canvas.php index 7dc75d135..afbff2c13 100644 --- a/src/Plots/Canvas.php +++ b/src/Plots/Canvas.php @@ -63,7 +63,7 @@ public function __construct(int $width = 700, int $height = 500) * @return object The resulting Plot object constructed from our inputs * and the parameters of the parent canvas object */ - public function addPlot(callable $function, $start = 0, $end = 10) + public function addPlot(callable $function, $start = 0, $end = 10): Plot { $width = $this->width; $height = $this->height; From 4a3d3fa098c10b1dd853363f55f6f0823430d356 Mon Sep 17 00:00:00 2001 From: jakobsandberg Date: Wed, 12 Oct 2016 23:16:07 -0700 Subject: [PATCH 40/59] Added exception if input in draw() is not a GD resource --- src/Plots/Plot.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Plots/Plot.php b/src/Plots/Plot.php index c7cc93ef5..cb348952d 100644 --- a/src/Plots/Plot.php +++ b/src/Plots/Plot.php @@ -48,7 +48,8 @@ class Plot extends Canvas private $start; private $end; - public function __construct(callable $function, $start, $end, $width, $height) + + public function __construct(callable $function, float $start, float $end, int $width, int $height) { parent::__construct($width, $height); $this->function = $function; @@ -103,6 +104,10 @@ public function thickness(int $thickness) public function draw($canvas) { + if (get_resource_type($canvas) !== "gd") { + throw new \Exception("The was an error constructing the canvas") + } + $black = imagecolorallocate($canvas, 0, 0, 0); $white = imagecolorallocate($canvas, 255, 255, 255); $padding = 50; From d8d9a0be09061960c2c9a43650ffa4cbf9552fa4 Mon Sep 17 00:00:00 2001 From: jakobsandberg Date: Thu, 13 Oct 2016 01:23:32 -0700 Subject: [PATCH 41/59] Added method descriptions for Plot class --- src/Plots/Plot.php | 132 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 102 insertions(+), 30 deletions(-) diff --git a/src/Plots/Plot.php b/src/Plots/Plot.php index cb348952d..10960b799 100644 --- a/src/Plots/Plot.php +++ b/src/Plots/Plot.php @@ -22,7 +22,7 @@ * - color(): Set the color of our plot line/curve * - thickness(): Set the thickness of our plot line/curve * -* Example: Graph the function f(x) = x*sin(x) on [0, 20] with a grid, title, +* Example: Plot the function f(x) = x*sin(x) on [0, 20] with a grid, title, * y-axis label, x-axis label, a color of red, and thickness of 5 * $canvas = new Canvas(); * $plot = $canvas->addPlot(function ($x) { return $x*sin($x); }, 0, 20); @@ -38,9 +38,10 @@ * - A method to change the font for all text * - A method to change the font size for all text * - A method to change the font color for all text -* - A method to add more graphs (functions) to the same plot +* - A method to add more plots (functions) to the same plot * - A child class for each text field (title and axis labels) which contains * methods to adjust the color, size, and font of that specific text field +* - A method to add a yRange to our final plot */ class Plot extends Canvas { @@ -48,15 +49,35 @@ class Plot extends Canvas private $start; private $end; - - public function __construct(callable $function, float $start, float $end, int $width, int $height) + /** + * Construct a Plot object + * + * The constructed Plot object should be a child of an existing Canvas object. + * In the construction, we assign a callback function, and the start and end + * points of the interval to which we will graph the function. + * + * We should not construct a Plot object explicity (e.g. using new Plot). + * Rather, this constructor is accessed implicitly in the parent Canvas + * class, as it needs to correspond to an instance of Canvas. This is + * because the save() method draws our plot onto a specific instance of + * a GD image, and this image is initially built in the parent Canvas class. + */ + public function __construct(callable $function, float $start, float $end) { - parent::__construct($width, $height); + parent::__construct(); $this->function = $function; $this->start = $start; $this->end = $end; } + /** + * Turn the plot grid lines on or of, and specify the number of grids in + * each direction. + * + * @param bool $switch A boolean for if the grid is shown or not + * @param int $gridCountX The number of grid lines on the x-axis + * @param int $gridCountY The number of grid lines on the y-axis + */ public function grid(bool $switch = true, int $gridCountX = 10, int $gridCountY = 10) { $this->grid = $switch; @@ -64,21 +85,48 @@ public function grid(bool $switch = true, int $gridCountX = 10, int $gridCountY $this->gridCountY = 10; } + /** + * Add a title (or modify the existing one) to our plot. + * + * @param string $title The title of our plot + */ public function title(string $title) { $this->title = $title; } + /** + * Add a y-axis label (or modify the existing one) to our plot. + * + * @param string $label The y-axis of our plot + */ public function yLabel(string $label) { $this->yLabel = $label; } + /** + * Add a x-axis label (or modify the existing one) to our plot. + * + * @param string $label The x-axis of our plot + */ public function xLabel(string $label) { $this->xLabel = $label; } + /** + * Modify the color of our plot line/curve. + * + * Input is a string which can correspond to a number of preset colors. + * Currently, only red, green, and blue are supported, although more colors + * can easily be extended. + * + * If an input string does not match a supported color, the color will + * default to black. + * + * @param string $color The color of our plot line/curve + */ public function color(string $color) { switch($color) { @@ -97,17 +145,41 @@ public function color(string $color) $this->color = $color; } + /** + * Modify the thickness of our plot line/curve. + * + * @param int $thickness The thickness of our plot line/curve + */ public function thickness(int $thickness) { $this->thickness = $thickness; } + /** + * Draw the plot to our input canvas. + * + * Draw aspects of a single plot: x- and y-axis, (optional) x- and y-labels, + * x- and y-axis reference numbers, (optional) title, (optional) grid lines, + * and the actual plot itself. + * + * This method should not be called explicitly. Rather, it is accessed + * implicitly when you run the save() method on the parent canvas of + * a plot object. This ensures that a property $canvas property is + * created before it is passed to this method, which draws a plot onto + * a GD canvas. + * + * @param resource $canvas A GD resource passed in via a Canvas parent object + * + * @throws Exception if $canvas is not a GD resource + */ public function draw($canvas) { + // Verify the input is a GD resource if (get_resource_type($canvas) !== "gd") { - throw new \Exception("The was an error constructing the canvas") + throw new \Exception("The was an error constructing the canvas"); } + // Set convenience variables $black = imagecolorallocate($canvas, 0, 0, 0); $white = imagecolorallocate($canvas, 255, 255, 255); $padding = 50; @@ -121,8 +193,8 @@ public function draw($canvas) $color = isset($this->color) ? imagecolorallocate($canvas, ... $this->color) : $black; $thickness = $this->thickness ?? 3; $grid = $this->grid ?? false; - $gridCountY = $this->gridCountY ?? 10; - $gridCountX = $this->gridCountX ?? 10; + $gridCountY = $this->gridCountY ?? null; + $gridCountX = $this->gridCountX ?? null; $function = $this->function; $start = $this->start; $end = $this->end; @@ -132,23 +204,23 @@ public function draw($canvas) $y_shift = isset($xLabel) ? 10 : 0; // Measure start and end points of plot on canvas - $graph_start_x = $padding + $x_shift; - $graph_start_y = imagesy($canvas) - ($padding + $y_shift); - $graph_end_x = imagesx($canvas) - $padding; - $graph_end_y = $padding; + $plot_start_x = $padding + $x_shift; + $plot_start_y = imagesy($canvas) - ($padding + $y_shift); + $plot_end_x = imagesx($canvas) - $padding; + $plot_end_y = $padding; // Measure height and width of plot on canvas - $graph_width = $graph_end_x - $graph_start_x; - $graph_height = $graph_start_y - $graph_end_y; + $plot_width = $plot_end_x - $plot_start_x; + $plot_height = $plot_start_y - $plot_end_y; // Create axes - imagerectangle($canvas, $graph_start_x, $graph_end_y, $graph_end_x, $graph_start_y, $black); + imagerectangle($canvas, $plot_start_x, $plot_end_y, $plot_end_x, $plot_start_y, $black); - // Calculate function step size (h) and graph step size + // Calculate function step size (h) and plot step size $n = 1000; $h = ($end - $start)/$n; - $graph_step_x = $graph_width/$n; - $graph_step_y = $graph_height/$n; + $plot_step_x = $plot_width/$n; + $plot_step_y = $plot_height/$n; // Calculate function values, min, max, and function scale $image = []; @@ -157,16 +229,16 @@ public function draw($canvas) } $min = min($image); $max = max($image); - $function_scale = $graph_height/($max - $min); + $function_scale = $plot_height/($max - $min); // Draw y-axis values and grid $style = array_merge(array_fill(0, 1, $black), array_fill(0, 5, $white)); imagesetstyle($canvas, $style); for ($i = 0; $i <= $gridCountY; $i++) { $value = round(($min + $i*($max - $min)/$gridCountY), 1); - $X₀ = $graph_start_x; - $Xₙ = $graph_end_x; - $Y₀ = $graph_start_y - $i*($graph_height/$gridCountY); + $X₀ = $plot_start_x; + $Xₙ = $plot_end_x; + $Y₀ = $plot_start_y - $i*($plot_height/$gridCountY); imagestring($canvas, 2, $X₀ - 10 - 6*strlen($value), $Y₀ - 8, $value, $black); if ($i !== 0 && $i !== $gridCountY && $grid) { imageline($canvas, $X₀, $Y₀, $Xₙ, $Y₀, IMG_COLOR_STYLED); @@ -176,9 +248,9 @@ public function draw($canvas) // Draw x-axis values and grid for ($i = 0; $i <= $gridCountX; $i++) { $value = round(($start + $i*($end - $start)/$gridCountX), 1); - $X₀ = $graph_start_x + $i*($graph_width/$gridCountX); - $Y₀ = $graph_start_y; - $Yₙ = $graph_end_y; + $X₀ = $plot_start_x + $i*($plot_width/$gridCountX); + $Y₀ = $plot_start_y; + $Yₙ = $plot_end_y; imagestring($canvas, 2, $X₀ + 2 - strlen($value)*3, $Y₀ + 8, $value, $black); if ($i !== 0 && $i !== $gridCountX && $grid) { imageline($canvas, $X₀, $Y₀, $X₀, $Yₙ, IMG_COLOR_STYLED); @@ -196,13 +268,13 @@ public function draw($canvas) imagestringup($canvas, 4, 10, ($height - $y_shift + strlen($yLabel)*8)/2, $yLabel, $black); } - // Draw graph + // Draw plot imagesetthickness($canvas, $thickness); for ($i = 0; $i < $n; $i++) { - $xᵢ = $graph_start_x + $i*$graph_step_x; - $xᵢ₊₁ = $graph_start_x + ($i+1)*$graph_step_x; - $f⟮xᵢ⟯ = $graph_start_y - ($image[$i]-$min)*$function_scale; - $f⟮xᵢ₊₁⟯ = $graph_start_y - ($image[$i+1]-$min)*$function_scale; + $xᵢ = $plot_start_x + $i*$plot_step_x; + $xᵢ₊₁ = $plot_start_x + ($i+1)*$plot_step_x; + $f⟮xᵢ⟯ = $plot_start_y - ($image[$i]-$min)*$function_scale; + $f⟮xᵢ₊₁⟯ = $plot_start_y - ($image[$i+1]-$min)*$function_scale; imageline($canvas, $xᵢ, $f⟮xᵢ⟯, $xᵢ₊₁, $f⟮xᵢ₊₁⟯, $color); } From c28cde54feaaed0eb8d819abb3b855c994d8acdc Mon Sep 17 00:00:00 2001 From: jakobsandberg Date: Thu, 13 Oct 2016 01:25:59 -0700 Subject: [PATCH 42/59] Added xRange() method to Plot class --- src/Plots/Plot.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/Plots/Plot.php b/src/Plots/Plot.php index 10960b799..acef35a76 100644 --- a/src/Plots/Plot.php +++ b/src/Plots/Plot.php @@ -61,6 +61,10 @@ class Plot extends Canvas * class, as it needs to correspond to an instance of Canvas. This is * because the save() method draws our plot onto a specific instance of * a GD image, and this image is initially built in the parent Canvas class. + * + * @param callable $function The callback function we are plotting + * @param number $start The start of our plotting interval + * @param number $end The end of the plotting interval */ public function __construct(callable $function, float $start, float $end) { @@ -70,6 +74,19 @@ public function __construct(callable $function, float $start, float $end) $this->end = $end; } + /** + * Ajust the start and endpoint of the interval to which we are plotting + * a function. + * + * @param number $start The start of our plotting interval + * @param number $end The end of the plotting interval + */ + public function xRange(float $start, float $end) + { + $this->start = $start; + $this->end = $end; + } + /** * Turn the plot grid lines on or of, and specify the number of grids in * each direction. From c56feb748dde847c24b3a69f3f9a90ee4d05b1f5 Mon Sep 17 00:00:00 2001 From: jakobsandberg Date: Thu, 13 Oct 2016 01:32:08 -0700 Subject: [PATCH 43/59] Added conditional statements and exception to validate start and end inputs --- src/Plots/Plot.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/Plots/Plot.php b/src/Plots/Plot.php index acef35a76..5b2005345 100644 --- a/src/Plots/Plot.php +++ b/src/Plots/Plot.php @@ -65,11 +65,28 @@ class Plot extends Canvas * @param callable $function The callback function we are plotting * @param number $start The start of our plotting interval * @param number $end The end of the plotting interval + * + * @throws Exception if $start = $end (not an interval, just a point) */ public function __construct(callable $function, float $start, float $end) { parent::__construct(); + $this->function = $function; + + if ($start === $end) { + throw new \Exception("Start and end points the interval of our + graph cannot be the same. Your current input + would produce a graph over the interval + [{$start}, {$end}], which is just a single + point"); + } + + // Swap variables if start point is greater than end point + if ($start > $end) { + list($start, $end) = [$end, $start]; + } + $this->start = $start; $this->end = $end; } From e6683f38ee6a7512f5c1fcce05733cafcf9df0fe Mon Sep 17 00:00:00 2001 From: jakobsandberg Date: Thu, 13 Oct 2016 01:37:44 -0700 Subject: [PATCH 44/59] Moved the interval validation as a sharable component in the Canvas class --- src/Plots/Canvas.php | 25 +++++++++++++++++++++++++ src/Plots/Plot.php | 20 +++----------------- 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/src/Plots/Canvas.php b/src/Plots/Canvas.php index afbff2c13..cd0419697 100644 --- a/src/Plots/Canvas.php +++ b/src/Plots/Canvas.php @@ -113,4 +113,29 @@ public function save() imagejpeg($canvas, 'image-' . rand() . '.jpg'); } + + /** + * Valide that our input is a proper interval (not just a point). + * + * If the start point is greater than the input, swap the variables. + * + * @throws Exception if $start = $end (not an interval, just a point) + */ + public function validateInterval(int $start, int $end) + { + if ($start === $end) { + throw new \Exception("Start and end points the interval of our + graph cannot be the same. Your current input + would produce a graph over the interval + [{$start}, {$end}], which is just a single + point"); + } + + // Swap variables if start point is greater than end point + if ($start > $end) { + list($start, $end) = [$end, $start]; + } + + return [$start, $end]; + } } diff --git a/src/Plots/Plot.php b/src/Plots/Plot.php index 5b2005345..684dc0943 100644 --- a/src/Plots/Plot.php +++ b/src/Plots/Plot.php @@ -65,8 +65,6 @@ class Plot extends Canvas * @param callable $function The callback function we are plotting * @param number $start The start of our plotting interval * @param number $end The end of the plotting interval - * - * @throws Exception if $start = $end (not an interval, just a point) */ public function __construct(callable $function, float $start, float $end) { @@ -74,21 +72,9 @@ public function __construct(callable $function, float $start, float $end) $this->function = $function; - if ($start === $end) { - throw new \Exception("Start and end points the interval of our - graph cannot be the same. Your current input - would produce a graph over the interval - [{$start}, {$end}], which is just a single - point"); - } - - // Swap variables if start point is greater than end point - if ($start > $end) { - list($start, $end) = [$end, $start]; - } - - $this->start = $start; - $this->end = $end; + list($start, $end) = $this->validateInterval($start, $end); + $this->start = $start; + $this->end = $end; } /** From 6ee3bff539585472e080e72cbd11d62f32e300b5 Mon Sep 17 00:00:00 2001 From: jakobsandberg Date: Thu, 13 Oct 2016 01:46:07 -0700 Subject: [PATCH 45/59] Added Exception to grid() method if either grid line count is negative --- src/Plots/Plot.php | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/Plots/Plot.php b/src/Plots/Plot.php index 684dc0943..8b2cb7187 100644 --- a/src/Plots/Plot.php +++ b/src/Plots/Plot.php @@ -68,11 +68,11 @@ class Plot extends Canvas */ public function __construct(callable $function, float $start, float $end) { - parent::__construct(); + list($start, $end) = $this->validateInterval($start, $end); - $this->function = $function; + parent::__construct(); - list($start, $end) = $this->validateInterval($start, $end); + $this->function = $function; $this->start = $start; $this->end = $end; } @@ -86,6 +86,8 @@ public function __construct(callable $function, float $start, float $end) */ public function xRange(float $start, float $end) { + list($start, $end) = $this->validateInterval($start, $end); + $this->start = $start; $this->end = $end; } @@ -97,9 +99,15 @@ public function xRange(float $start, float $end) * @param bool $switch A boolean for if the grid is shown or not * @param int $gridCountX The number of grid lines on the x-axis * @param int $gridCountY The number of grid lines on the y-axis + * + * @throws Exception if $gridCountX or $gridCountY is negative */ public function grid(bool $switch = true, int $gridCountX = 10, int $gridCountY = 10) { + if ($gridCountX < 0 || $gridCountY < 0) { + throw new \Exception("Number of grid lines cannot be negative"); + } + $this->grid = $switch; $this->gridCountX = 10; $this->gridCountY = 10; @@ -162,6 +170,7 @@ public function color(string $color) default: $color = [0, 0, 0]; } + $this->color = $color; } From 77a5d37a07b4a29673e9c9e8f70a178f4d14c8d3 Mon Sep 17 00:00:00 2001 From: jakobsandberg Date: Thu, 13 Oct 2016 02:16:40 -0700 Subject: [PATCH 46/59] Added validateSize() method to Canvas --- src/Plots/Canvas.php | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/Plots/Canvas.php b/src/Plots/Canvas.php index cd0419697..ca01e8937 100644 --- a/src/Plots/Canvas.php +++ b/src/Plots/Canvas.php @@ -43,6 +43,8 @@ class Canvas */ public function __construct(int $width = 700, int $height = 500) { + $this->validateSize($width, $height); + $this->width = $width; $this->height = $height; } @@ -67,7 +69,9 @@ public function addPlot(callable $function, $start = 0, $end = 10): Plot { $width = $this->width; $height = $this->height; - $this->plot = new Plot($function, $start, $end, $width, $height); + + list($start, $end) = $this->validateInterval($start, $end); + $this->plot = new Plot($function, $start, $end, $width, $height); return $this->plot; } @@ -83,8 +87,12 @@ public function addPlot(callable $function, $start = 0, $end = 10): Plot */ public function size(int $width, int $height) { + $this->validateSize($width, $height); + $this->width = $width; $this->height = $height; + + // If we've already added a plot to the canvas, adjust it's size as well if(isset($this->plot)) { $this->plot->size($width, $height); } @@ -114,6 +122,18 @@ public function save() imagejpeg($canvas, 'image-' . rand() . '.jpg'); } + /** + * Validate the input size of our canvus + * + * @throws Exception if $width or $height is negative + */ + public function validateSize(int $width, int $height) + { + if ($width < 0 || $height < 0) { + throw new \Exception("Canvas dimensions cannot be negative"); + } + } + /** * Valide that our input is a proper interval (not just a point). * From 81e9fef1498fff731312a56ed8bf541b92a8b247 Mon Sep 17 00:00:00 2001 From: jakobsandberg Date: Thu, 13 Oct 2016 02:17:32 -0700 Subject: [PATCH 47/59] Added Exception if input is negative --- src/Plots/Plot.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Plots/Plot.php b/src/Plots/Plot.php index 8b2cb7187..db43efe94 100644 --- a/src/Plots/Plot.php +++ b/src/Plots/Plot.php @@ -178,9 +178,15 @@ public function color(string $color) * Modify the thickness of our plot line/curve. * * @param int $thickness The thickness of our plot line/curve + * + * @throws Exception if $thickness is negative */ public function thickness(int $thickness) { + if ($thickness < 0) { + throw new \Exception("Thickness cannot be negative"); + } + $this->thickness = $thickness; } From f41612a7e1cc3727f4ac545a6959494e30866c23 Mon Sep 17 00:00:00 2001 From: jakobsandberg Date: Thu, 13 Oct 2016 02:24:06 -0700 Subject: [PATCH 48/59] Added test for Plots class --- tests/Plots/CanvasTest.php | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 tests/Plots/CanvasTest.php diff --git a/tests/Plots/CanvasTest.php b/tests/Plots/CanvasTest.php new file mode 100644 index 000000000..1ddea3dc6 --- /dev/null +++ b/tests/Plots/CanvasTest.php @@ -0,0 +1,8 @@ + Date: Thu, 13 Oct 2016 02:24:25 -0700 Subject: [PATCH 49/59] Added test to Plots for validateSize() exceptions --- tests/Plots/CanvasTest.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/Plots/CanvasTest.php b/tests/Plots/CanvasTest.php index 1ddea3dc6..5ae0a6b2d 100644 --- a/tests/Plots/CanvasTest.php +++ b/tests/Plots/CanvasTest.php @@ -4,5 +4,15 @@ class CanvasTest extends \PHPUnit_Framework_TestCase { + public function testValidateSizeException() + { + // Giving a negative size for canvas dimensions + $this->setExpectedException('\Exception'); + new Canvas(-100, 500); + // Adjust to a negative size for canvas dimensions + $this->setExpectedException('\Exception'); + $canvas = new Canvas(); + $canvas->size(-100, 500); + } } From f5143e20e9fcba85d6233507d0e4595f851b9c5c Mon Sep 17 00:00:00 2001 From: jakobsandberg Date: Thu, 13 Oct 2016 02:26:09 -0700 Subject: [PATCH 50/59] Added test to Canvas for validateInterval() Exceptions --- tests/Plots/CanvasTest.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/Plots/CanvasTest.php b/tests/Plots/CanvasTest.php index 5ae0a6b2d..be45fb6b6 100644 --- a/tests/Plots/CanvasTest.php +++ b/tests/Plots/CanvasTest.php @@ -15,4 +15,18 @@ public function testValidateSizeException() $canvas = new Canvas(); $canvas->size(-100, 500); } + + public function testValidateInterval() + { + // The input interval is set to single point (start = end) + $this->setExpectedException('\Exception'); + $canvas = new Canvas(); + $canvas->addPlot(function ($x) { return 1; }, 0, 0); + + // The plot interval is adjusted to single point (start = end) + $this->setExpectedException('\Exception'); + $canvas = new Canvas(); + $plot = $canvas->addPlot(function ($x) { return 1; }, 0, 10); + $plot->xRange(10, 10); + } } From 8635ec5b03ebcfae1f407c422b88de85a94983d3 Mon Sep 17 00:00:00 2001 From: jakobsandberg Date: Thu, 13 Oct 2016 02:27:35 -0700 Subject: [PATCH 51/59] Added additional tests for validateSize() exceptions --- tests/Plots/CanvasTest.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/Plots/CanvasTest.php b/tests/Plots/CanvasTest.php index be45fb6b6..7a3a18aec 100644 --- a/tests/Plots/CanvasTest.php +++ b/tests/Plots/CanvasTest.php @@ -10,6 +10,12 @@ public function testValidateSizeException() $this->setExpectedException('\Exception'); new Canvas(-100, 500); + $this->setExpectedException('\Exception'); + new Canvas(100, -500); + + $this->setExpectedException('\Exception'); + new Canvas(-100, -500); + // Adjust to a negative size for canvas dimensions $this->setExpectedException('\Exception'); $canvas = new Canvas(); From c120d76d6839ac65336e7067cb243ad5a2b5bab5 Mon Sep 17 00:00:00 2001 From: jakobsandberg Date: Thu, 13 Oct 2016 02:30:09 -0700 Subject: [PATCH 52/59] Split exception tests so there is only one exception in each --- tests/Plots/CanvasTest.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/Plots/CanvasTest.php b/tests/Plots/CanvasTest.php index 7a3a18aec..67e1e47cc 100644 --- a/tests/Plots/CanvasTest.php +++ b/tests/Plots/CanvasTest.php @@ -4,31 +4,31 @@ class CanvasTest extends \PHPUnit_Framework_TestCase { - public function testValidateSizeException() + public function testValidateSizeExceptionSet() { // Giving a negative size for canvas dimensions $this->setExpectedException('\Exception'); new Canvas(-100, 500); + } - $this->setExpectedException('\Exception'); - new Canvas(100, -500); - - $this->setExpectedException('\Exception'); - new Canvas(-100, -500); - + public function testValidateSizeExceptionUpdate() + { // Adjust to a negative size for canvas dimensions $this->setExpectedException('\Exception'); $canvas = new Canvas(); $canvas->size(-100, 500); } - public function testValidateInterval() + public function testValidateIntervalSet() { // The input interval is set to single point (start = end) $this->setExpectedException('\Exception'); $canvas = new Canvas(); $canvas->addPlot(function ($x) { return 1; }, 0, 0); + } + public function testValidateIntervalUpdate() + { // The plot interval is adjusted to single point (start = end) $this->setExpectedException('\Exception'); $canvas = new Canvas(); From 2942c9791246beb0b2f63cc575f43783c78875d0 Mon Sep 17 00:00:00 2001 From: jakobsandberg Date: Thu, 13 Oct 2016 02:31:45 -0700 Subject: [PATCH 53/59] Added test for Plot Class --- tests/Plots/PlotTest.php | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 tests/Plots/PlotTest.php diff --git a/tests/Plots/PlotTest.php b/tests/Plots/PlotTest.php new file mode 100644 index 000000000..523f47b94 --- /dev/null +++ b/tests/Plots/PlotTest.php @@ -0,0 +1,8 @@ + Date: Thu, 13 Oct 2016 02:31:57 -0700 Subject: [PATCH 54/59] Added test for grid exception --- tests/Plots/PlotTest.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tests/Plots/PlotTest.php b/tests/Plots/PlotTest.php index 523f47b94..8bb016b7e 100644 --- a/tests/Plots/PlotTest.php +++ b/tests/Plots/PlotTest.php @@ -4,5 +4,12 @@ class PlotTest extends \PHPUnit_Framework_TestCase { - + public function testGridException() + { + // Giving a negative number + $this->setExpectedException('\Exception'); + $canvas = new Canvas(); + $plot = $canvas->addPlot(function ($x) { return 1; }, 0, 10); + $plot->grid(true, -10, 5); + } } From b7acb7d5e2c3f1ad8792185dbac16d153382b2a3 Mon Sep 17 00:00:00 2001 From: jakobsandberg Date: Thu, 13 Oct 2016 02:32:08 -0700 Subject: [PATCH 55/59] Added test for thickness exception --- tests/Plots/PlotTest.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/Plots/PlotTest.php b/tests/Plots/PlotTest.php index 8bb016b7e..c2b357ba0 100644 --- a/tests/Plots/PlotTest.php +++ b/tests/Plots/PlotTest.php @@ -12,4 +12,13 @@ public function testGridException() $plot = $canvas->addPlot(function ($x) { return 1; }, 0, 10); $plot->grid(true, -10, 5); } + + public function testThicknessException() + { + // Giving a negative number + $this->setExpectedException('\Exception'); + $canvas = new Canvas(); + $plot = $canvas->addPlot(function ($x) { return 1; }, 0, 10); + $plot->thickness(-10); + } } From 60795b25325eaca688c4e6158153e9b8243f4e31 Mon Sep 17 00:00:00 2001 From: jakobsandberg Date: Thu, 13 Oct 2016 02:36:45 -0700 Subject: [PATCH 56/59] Added test for Plot() Exception --- tests/Plots/PlotTest.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/Plots/PlotTest.php b/tests/Plots/PlotTest.php index c2b357ba0..9fc4d88be 100644 --- a/tests/Plots/PlotTest.php +++ b/tests/Plots/PlotTest.php @@ -21,4 +21,14 @@ public function testThicknessException() $plot = $canvas->addPlot(function ($x) { return 1; }, 0, 10); $plot->thickness(-10); } + + public function testPlotException() + { + // Giving a $canvas input which is not a GD resource + $not_a_gd_resource = "this is a string, not a GD resource"; + $this->setExpectedException('\Exception'); + $canvas = new Canvas(); + $plot = $canvas->addPlot(function ($x) { return 1; }, 0, 10); + $plot->draw($not_a_gd_resource); + } } From 96e3a0e72dd9db9eb9a1dbfc5f81f614fffbb661 Mon Sep 17 00:00:00 2001 From: jakobsandberg Date: Thu, 13 Oct 2016 02:37:09 -0700 Subject: [PATCH 57/59] Added catch to ensure is a resource before checking its type --- src/Plots/Plot.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Plots/Plot.php b/src/Plots/Plot.php index db43efe94..1c79527c9 100644 --- a/src/Plots/Plot.php +++ b/src/Plots/Plot.php @@ -210,7 +210,7 @@ public function thickness(int $thickness) public function draw($canvas) { // Verify the input is a GD resource - if (get_resource_type($canvas) !== "gd") { + if (!(is_resource($canvas) && get_resource_type($canvas) === "gd")) { throw new \Exception("The was an error constructing the canvas"); } From 22061668409bde749f3d33566069bca2efe63a46 Mon Sep 17 00:00:00 2001 From: jakobsandberg Date: Thu, 13 Oct 2016 02:43:54 -0700 Subject: [PATCH 58/59] PSR --- src/Plots/Canvas.php | 4 ++-- src/Plots/Plot.php | 2 +- tests/Plots/CanvasTest.php | 10 ++++++++-- tests/Plots/PlotTest.php | 16 +++++++++++++--- 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/Plots/Canvas.php b/src/Plots/Canvas.php index ca01e8937..df3fd2cd6 100644 --- a/src/Plots/Canvas.php +++ b/src/Plots/Canvas.php @@ -27,7 +27,7 @@ class Canvas { protected $width; protected $height; - private $plot; + private $plot; /** * Construct a new plotting canvas. @@ -93,7 +93,7 @@ public function size(int $width, int $height) $this->height = $height; // If we've already added a plot to the canvas, adjust it's size as well - if(isset($this->plot)) { + if (isset($this->plot)) { $this->plot->size($width, $height); } } diff --git a/src/Plots/Plot.php b/src/Plots/Plot.php index 1c79527c9..4a1eecdb8 100644 --- a/src/Plots/Plot.php +++ b/src/Plots/Plot.php @@ -157,7 +157,7 @@ public function xLabel(string $label) */ public function color(string $color) { - switch($color) { + switch ($color) { case 'red': $color = [255, 0, 0]; break; diff --git a/tests/Plots/CanvasTest.php b/tests/Plots/CanvasTest.php index 67e1e47cc..dd9a15dfe 100644 --- a/tests/Plots/CanvasTest.php +++ b/tests/Plots/CanvasTest.php @@ -24,7 +24,10 @@ public function testValidateIntervalSet() // The input interval is set to single point (start = end) $this->setExpectedException('\Exception'); $canvas = new Canvas(); - $canvas->addPlot(function ($x) { return 1; }, 0, 0); + $function = function ($x) { + return 1; + }; + $canvas->addPlot($function, 0, 0); } public function testValidateIntervalUpdate() @@ -32,7 +35,10 @@ public function testValidateIntervalUpdate() // The plot interval is adjusted to single point (start = end) $this->setExpectedException('\Exception'); $canvas = new Canvas(); - $plot = $canvas->addPlot(function ($x) { return 1; }, 0, 10); + $function = function ($x) { + return 1; + }; + $canvas->addPlot($function, 0, 10); $plot->xRange(10, 10); } } diff --git a/tests/Plots/PlotTest.php b/tests/Plots/PlotTest.php index 9fc4d88be..3cbe04e03 100644 --- a/tests/Plots/PlotTest.php +++ b/tests/Plots/PlotTest.php @@ -9,7 +9,11 @@ public function testGridException() // Giving a negative number $this->setExpectedException('\Exception'); $canvas = new Canvas(); - $plot = $canvas->addPlot(function ($x) { return 1; }, 0, 10); + $function = function ($x) { + return 1; + }; + $canvas->addPlot($function, 0, 0); + $plot = $canvas->addPlot($function, 0, 10); $plot->grid(true, -10, 5); } @@ -18,7 +22,10 @@ public function testThicknessException() // Giving a negative number $this->setExpectedException('\Exception'); $canvas = new Canvas(); - $plot = $canvas->addPlot(function ($x) { return 1; }, 0, 10); + $function = function ($x) { + return 1; + }; + $plot = $canvas->addPlot($function, 0, 10); $plot->thickness(-10); } @@ -28,7 +35,10 @@ public function testPlotException() $not_a_gd_resource = "this is a string, not a GD resource"; $this->setExpectedException('\Exception'); $canvas = new Canvas(); - $plot = $canvas->addPlot(function ($x) { return 1; }, 0, 10); + $function = function ($x) { + return 1; + }; + $plot = $canvas->addPlot($function, 0, 10); $plot->draw($not_a_gd_resource); } } From 3e1bba472f6e18920901cc592bf98e2519f9b638 Mon Sep 17 00:00:00 2001 From: jakobsandberg Date: Thu, 13 Oct 2016 03:20:25 -0700 Subject: [PATCH 59/59] Added catch if GD extension is not loaded --- src/Plots/Canvas.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/Plots/Canvas.php b/src/Plots/Canvas.php index df3fd2cd6..0d1208bb0 100644 --- a/src/Plots/Canvas.php +++ b/src/Plots/Canvas.php @@ -43,6 +43,14 @@ class Canvas */ public function __construct(int $width = 700, int $height = 500) { + if (!extension_loaded('gd')) { + if (!dl('gd.so')) { + echo "GD extension is not installed/loaded. Ensure it is setup + property and then try again"; + exit; + } + } + $this->validateSize($width, $height); $this->width = $width;