| 1 |
<?php |
| 2 |
/** |
| 3 |
* Copyright (C) 2010-2022 Graham Breach |
| 4 |
* |
| 5 |
* This program is free software: you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU Lesser General Public License as published by |
| 7 |
* the Free Software Foundation, either version 3 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU Lesser General Public License |
| 16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
*/ |
| 18 |
/** |
| 19 |
* For more information, please contact <graham@goat1000.com> |
| 20 |
*/ |
| 21 |
|
| 22 |
namespace Goat1000\SVGGraph; |
| 23 |
|
| 24 |
/** |
| 25 |
* ScatterGraph - points with axes and grid |
| 26 |
*/ |
| 27 |
class ScatterGraph extends PointGraph { |
| 28 |
|
| 29 |
public function __construct($w, $h, array $settings, array $fixed_settings = []) |
| 30 |
{ |
| 31 |
$fs = [ |
| 32 |
'repeated_keys' => 'accept', |
| 33 |
'require_integer_keys' => false, |
| 34 |
]; |
| 35 |
$fs = array_merge($fs, $fixed_settings); |
| 36 |
parent::__construct($w, $h, $settings, $fs); |
| 37 |
} |
| 38 |
|
| 39 |
protected function draw() |
| 40 |
{ |
| 41 |
$body = $this->grid() . $this->underShapes(); |
| 42 |
|
| 43 |
// a scatter graph without markers is empty! |
| 44 |
if($this->getOption('marker_size') == 0) |
| 45 |
$this->setOption('marker_size', 1); |
| 46 |
$dataset = $this->getOption(['dataset', 0], 0); |
| 47 |
|
| 48 |
$bnum = 0; |
| 49 |
foreach($this->values[$dataset] as $item) { |
| 50 |
$x = $this->gridPosition($item, $bnum); |
| 51 |
if($item->value !== null && $x !== null) { |
| 52 |
$y = $this->gridY($item->value); |
| 53 |
if($y !== null) { |
| 54 |
$marker_id = $this->markerLabel($dataset, $bnum, $item, $x, $y); |
| 55 |
$extra = empty($marker_id) ? null : ['id' => $marker_id]; |
| 56 |
$this->addMarker($x, $y, $item, $extra, $dataset); |
| 57 |
} |
| 58 |
} |
| 59 |
++$bnum; |
| 60 |
} |
| 61 |
|
| 62 |
list($best_fit_above, $best_fit_below) = $this->bestFitLines(); |
| 63 |
$body .= $best_fit_below; |
| 64 |
$body .= $this->overShapes(); |
| 65 |
$body .= $this->axes(); |
| 66 |
$body .= $this->drawMarkers(); |
| 67 |
$body .= $best_fit_above; |
| 68 |
return $body; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Checks that the data produces a 2-D plot |
| 73 |
*/ |
| 74 |
protected function checkValues() |
| 75 |
{ |
| 76 |
parent::checkValues(); |
| 77 |
|
| 78 |
// using force_assoc makes things work properly |
| 79 |
if($this->values->associativeKeys()) |
| 80 |
$this->setOption('force_assoc', true); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
|