-
Notifications
You must be signed in to change notification settings - Fork 31
/
BestFit.php
207 lines (182 loc) · 6.37 KB
/
BestFit.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php
/**
* Copyright (C) 2020-2023 Graham Breach
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* For more information, please contact <[email protected]>
*/
namespace Goat1000\SVGGraph;
/**
* Class for drawing best-fit lines
*/
class BestFit {
protected $graph;
protected $bbox;
protected $lines_below = [];
protected $lines_above = [];
public function __construct(Graph &$graph, BoundingBox $bbox)
{
$this->graph =& $graph;
$this->bbox = $bbox;
}
/**
* Adds a line
*/
public function add($dataset, $points)
{
$type = $this->graph->getOption(['best_fit', $dataset]);
if($type !== 'straight' && $type !== 'curve')
return;
// range and projection
$r_start = $r_end = $p_start = $p_end = null;
list($start, $end) = $this->getRange($dataset);
if($start !== null || $end !== null) {
if($start !== null)
$r_start = $this->graph->unitsX($start);
if($end !== null)
$r_end = $this->graph->unitsX($end);
$project = $this->graph->getOption(['best_fit_project', $dataset]);
$p_start = $project == 'start' || $project == 'both';
$p_end = $project == 'end' || $project == 'both';
$project = $p_start || $p_end;
$points = $this->filterPoints($points, $r_start, $r_end);
}
$class = '\\Goat1000\\SVGGraph\\' . ($type === 'straight' ? 'BestFitLine' : 'BestFitCurve');
$subtypes = $this->graph->getOption(['best_fit_type', $dataset]);
$best_fit = new $class($this->graph, $points, $subtypes);
$best_fit->calculate($this->bbox, $r_start, $r_end, $p_start, $p_end);
if($this->graph->getOption(['best_fit_above', $dataset]))
$this->lines_above[$dataset] = $best_fit;
else
$this->lines_below[$dataset] = $best_fit;
}
/**
* Returns the start and end of selection range
*/
protected function getRange($dataset)
{
$range = $this->graph->getOption(['best_fit_range', $dataset]);
if(!is_array($range))
$range = $this->graph->getOption('best_fit_range');
if(!is_array($range))
return [null, null];
if(count($range) !== 2)
throw new \Exception('Best fit range must contain start and end values');
if($range[0] !== null && !is_numeric($range[0]))
throw new \Exception('Best fit range start not numeric or NULL');
if($range[1] !== null && !is_numeric($range[1]))
throw new \Exception('Best fit range end not numeric or NULL');
if($range[0] !== null && $range[1] !== null && $range[1] <= $range[0])
throw new \Exception('Best fit range start >= end');
return $range;
}
/**
* Filters out points outside the range
*/
protected function filterPoints($points, $start, $end)
{
if($start === null && $end === null)
return $points;
if($start === null)
$callback = function($p) use ($end) { return $p->x <= $end; };
elseif($end === null)
$callback = function($p) use ($start) { return $p->x >= $start; };
else
$callback = function($p) use ($start, $end) { return $p->x <= $end && $p->x >= $start; };
return array_filter($points, $callback);
}
/**
* Returns the lines that go above the graph
*/
public function getAbove()
{
return $this->getLines('lines_above');
}
/**
* Returns the lines below the graph
*/
public function getBelow()
{
return $this->getLines('lines_below');
}
/**
* Creates the markup for a group of lines
*/
private function getLines($which_lines)
{
$lines = '';
foreach($this->{$which_lines} as $dataset => $best_fit) {
$line_path = $best_fit->getLine();
if($line_path->isEmpty())
continue;
$proj_path = $best_fit->getProjection();
$lines .= $this->getLinePath($dataset, $line_path, $proj_path);
}
if($lines == '')
return $lines;
if($this->graph->getOption('semantic_classes')) {
$cls = ['class' => 'bestfit'];
$lines = $this->graph->element('g', $cls, null, $lines);
}
return $lines;
}
/**
* Wraps up the PathData with SVG
*/
protected function getLinePath($dataset, $line_path, $proj_path)
{
// use ColourGroup to support fill and fillColour
$cg = new ColourGroup($this->graph, null, 0, $dataset, 'best_fit_colour');
$colour = $cg->stroke();
$stroke_width = $this->graph->getOption(['best_fit_width', $dataset]);
$dash = $this->graph->getOption(['best_fit_dash', $dataset]);
$opacity = $this->graph->getOption(['best_fit_opacity', $dataset]);
$above = $this->graph->getOption(['best_fit_above', $dataset]);
$type = $this->graph->getOption(['best_fit', $dataset]);
$path = [
'd' => $line_path,
'stroke' => $colour->isNone() ? '#000' : $colour,
];
if($stroke_width != 1 && $stroke_width > 0)
$path['stroke-width'] = $stroke_width;
if(!empty($dash))
$path['stroke-dasharray'] = $dash;
if($opacity != 1)
$path['opacity'] = $opacity;
if($type != 'straight')
$path['fill'] = 'none'; // don't fill curves
$line = $this->graph->element('path', $path);
if($proj_path->isEmpty())
return $line;
// append the projection path
$path['d'] = $proj_path;
$cg = new ColourGroup($this->graph, null, 0, $dataset, 'best_fit_project_colour');
$colour = $cg->stroke();
$stroke_width = $this->graph->getOption(['best_fit_project_width', $dataset]);
$dash = $this->graph->getOption(['best_fit_project_dash', $dataset]);
$opacity = $this->graph->getOption(['best_fit_project_opacity', $dataset]);
if(!$colour->isNone())
$path['stroke'] = $colour;
if($stroke_width > 0)
$path['stroke-width'] = $stroke_width;
if(!empty($dash))
$path['stroke-dasharray'] = $dash;
if($opacity > 0)
$path['opacity'] = $opacity;
$line .= $this->graph->element('path', $path);
return $line;
}
}