| 1 |
<?php |
| 2 |
/** |
| 3 |
* Copyright (C) 2019-2023 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 |
class CrossHairs { |
| 25 |
|
| 26 |
private $graph; |
| 27 |
private $show_h = false; |
| 28 |
private $show_v = false; |
| 29 |
|
| 30 |
private $left, $top, $width, $height; |
| 31 |
private $x_axis, $y_axis; |
| 32 |
private $assoc; |
| 33 |
private $flip_axes; |
| 34 |
private $encoding; |
| 35 |
|
| 36 |
public function __construct(&$graph, $left, $top, $w, $h, $x_axis, $y_axis, |
| 37 |
$assoc, $flip_axes, $encoding) |
| 38 |
{ |
| 39 |
if($graph->getOption('crosshairs')) { |
| 40 |
$this->show_h = $graph->getOption('crosshairs_show_h'); |
| 41 |
$this->show_v = $graph->getOption('crosshairs_show_v'); |
| 42 |
} |
| 43 |
|
| 44 |
if(!$this->show_h && !$this->show_v) |
| 45 |
return; |
| 46 |
|
| 47 |
$this->left = $left; |
| 48 |
$this->top = $top; |
| 49 |
$this->width = $w; |
| 50 |
$this->height = $h; |
| 51 |
$this->x_axis = $x_axis; |
| 52 |
$this->y_axis = $y_axis; |
| 53 |
$this->assoc = $assoc; |
| 54 |
$this->flip_axes = $flip_axes; |
| 55 |
$this->encoding = $encoding; |
| 56 |
$this->graph =& $graph; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Returns TRUE if the crosshairs are enabled |
| 61 |
*/ |
| 62 |
public function enabled() |
| 63 |
{ |
| 64 |
return $this->show_h || $this->show_v; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Returns a horizontal or vertical hair |
| 69 |
*/ |
| 70 |
private function getHair($orientation, $ch) |
| 71 |
{ |
| 72 |
// line is always added, stays hidden if not enabled |
| 73 |
if($orientation == 'h') |
| 74 |
$hch = ['class' => 'chX', 'x2' => $ch['x1'] + $this->width]; |
| 75 |
else |
| 76 |
$hch = ['class' => 'chY', 'y2' => $ch['y1'] + $this->height]; |
| 77 |
|
| 78 |
$show = 'show_' . $orientation; |
| 79 |
if($this->$show) { |
| 80 |
$stroke = $this->graph->getOption('crosshairs_colour_' . $orientation, |
| 81 |
'crosshairs_colour'); |
| 82 |
$hch['stroke'] = new Colour($this->graph, $stroke, false, false); |
| 83 |
$hch['stroke-width'] = $this->graph->getOption( |
| 84 |
'crosshairs_stroke_width_' . $orientation, 'crosshairs_stroke_width'); |
| 85 |
$opacity = $this->graph->getOption( |
| 86 |
'crosshairs_opacity_' . $orientation, 'crosshairs_opacity'); |
| 87 |
if($opacity > 0 && $opacity < 1) |
| 88 |
$hch['opacity'] = $opacity; |
| 89 |
$dash = $this->graph->getOption('crosshairs_dash_' . $orientation, |
| 90 |
'crosshairs_dash'); |
| 91 |
if(!empty($dash)) |
| 92 |
$hch['stroke-dasharray'] = $dash; |
| 93 |
} |
| 94 |
return $this->graph->element('line', array_merge($ch, $hch)); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Returns the crosshair code and also adds the JS and defs |
| 99 |
*/ |
| 100 |
public function getCrossHairs() |
| 101 |
{ |
| 102 |
if(!($this->show_v || $this->show_h)) |
| 103 |
return ''; |
| 104 |
|
| 105 |
// make the crosshair lines |
| 106 |
$crosshairs = ''; |
| 107 |
$ch = [ |
| 108 |
'x1' => $this->left, 'y1' => $this->top, |
| 109 |
'x2' => $this->left, 'y2' => $this->top, |
| 110 |
'visibility' => 'hidden', // don't show them to start with! |
| 111 |
]; |
| 112 |
|
| 113 |
$crosshairs .= $this->getHair('h', $ch); |
| 114 |
$crosshairs .= $this->getHair('v', $ch); |
| 115 |
|
| 116 |
$text_options = [ |
| 117 |
'back_colour', 'round', 'stroke_width', 'colour', 'font_size', 'font', |
| 118 |
'font_weight', 'padding', 'space', |
| 119 |
]; |
| 120 |
$t_opt = []; |
| 121 |
foreach($text_options as $opt) |
| 122 |
$t_opt[$opt] = $this->graph->getOption('crosshairs_text_' . $opt); |
| 123 |
|
| 124 |
|
| 125 |
// text group for grid details |
| 126 |
$text_group = ['id' => $this->graph->newId(), 'visibility' => 'hidden']; |
| 127 |
$text_rect = [ |
| 128 |
'x' => '0', 'y' => '0', 'width' => '10', 'height' => '10', |
| 129 |
'fill' => new Colour($this->graph, $t_opt['back_colour']), |
| 130 |
]; |
| 131 |
if($t_opt['round']) |
| 132 |
$text_rect['rx'] = $text_rect['ry'] = $t_opt['round']; |
| 133 |
if($t_opt['stroke_width']) { |
| 134 |
$text_rect['stroke-width'] = $t_opt['stroke_width']; |
| 135 |
$text_rect['stroke'] = $t_opt['colour']; |
| 136 |
} |
| 137 |
$font_size = max(3, Number::units($t_opt['font_size'])); |
| 138 |
$text_element = [ |
| 139 |
'x' => 0, 'y' => $font_size, |
| 140 |
'font-family' => $t_opt['font'], |
| 141 |
'font-size' => $font_size, |
| 142 |
'fill' => new Colour($this->graph, $t_opt['colour']), |
| 143 |
]; |
| 144 |
$weight = $t_opt['font_weight']; |
| 145 |
if($weight && $weight != 'normal') |
| 146 |
$text_element['font-weight'] = $weight; |
| 147 |
|
| 148 |
$svg_text = new Text($this->graph); |
| 149 |
$text = $this->graph->element('g', $text_group, null, |
| 150 |
$this->graph->element('rect', $text_rect) . |
| 151 |
$svg_text->text('', $font_size, $text_element)); |
| 152 |
$this->graph->addBackMatter($text); |
| 153 |
|
| 154 |
// add in the details of the grid scales |
| 155 |
$zero_x = $this->x_axis->zero(); |
| 156 |
$scale_x = $this->x_axis->unit(); |
| 157 |
$zero_y = $this->y_axis->zero(); |
| 158 |
$scale_y = $this->y_axis->unit(); |
| 159 |
$prec_x = $this->graph->getOption('crosshairs_text_precision_h', |
| 160 |
max(0, ceil(log10($scale_x)))); |
| 161 |
$prec_y = $this->graph->getOption('crosshairs_text_precision_v', |
| 162 |
max(0, ceil(log10($scale_y)))); |
| 163 |
|
| 164 |
$scale_x = new Number($scale_x); |
| 165 |
$scale_x->precision = 7; |
| 166 |
$scale_y = new Number($scale_y); |
| 167 |
$scale_y->precision = 7; |
| 168 |
|
| 169 |
$gridx_attrs = [ |
| 170 |
'function' => 'strValueX', |
| 171 |
'zero' => $zero_x, |
| 172 |
'scale' => $scale_x, |
| 173 |
'precision' => $prec_x, |
| 174 |
]; |
| 175 |
$gridy_attrs = [ |
| 176 |
'function' => 'strValueY', |
| 177 |
'zero' => $zero_y, |
| 178 |
'scale' => $scale_y, |
| 179 |
'precision' => $prec_y, |
| 180 |
]; |
| 181 |
$chtextitem_attrs = [ |
| 182 |
'type' => 'xy', |
| 183 |
'groupid' => $text_group['id'], |
| 184 |
]; |
| 185 |
$u = $this->x_axis->afterUnits(); |
| 186 |
if(!empty($u)) |
| 187 |
$chtextitem_attrs['unitsx'] = $u; |
| 188 |
$u = $this->y_axis->afterUnits(); |
| 189 |
if(!empty($u)) |
| 190 |
$chtextitem_attrs['unitsy'] = $u; |
| 191 |
$u = $this->x_axis->beforeUnits(); |
| 192 |
if(!empty($u)) |
| 193 |
$chtextitem_attrs['unitsbx'] = $u; |
| 194 |
$u = $this->y_axis->beforeUnits(); |
| 195 |
if(!empty($u)) |
| 196 |
$chtextitem_attrs['unitsby'] = $u; |
| 197 |
|
| 198 |
$log_x = $this->graph->getOption(['log_axis_x', 0]); |
| 199 |
$log_y = $this->graph->getOption(['log_axis_y', 0]); |
| 200 |
if($log_x || $log_y) { |
| 201 |
$base_y = $this->graph->getOption('log_axis_y_base'); |
| 202 |
$base_x = $this->graph->getOption('log_axis_x_base'); |
| 203 |
$log_h = $this->flip_axes ? $log_y : $log_x; |
| 204 |
$log_v = $this->flip_axes ? $log_x : $log_y; |
| 205 |
|
| 206 |
if($log_h) { |
| 207 |
$gridx_attrs['base'] = $this->flip_axes ? $base_y : $base_x; |
| 208 |
$gridx_attrs['zero'] = $this->x_axis->value(0); |
| 209 |
$gridx_attrs['scale'] = $this->x_axis->value($this->width); |
| 210 |
$this->graph->getJavascript()->addFunction('logStrValueX'); |
| 211 |
$gridx_attrs['function'] = 'logStrValueX'; |
| 212 |
} |
| 213 |
if($log_v) { |
| 214 |
$gridy_attrs['base'] = $this->flip_axes ? $base_x : $base_y; |
| 215 |
$gridy_attrs['zero'] = $this->y_axis->value(0); |
| 216 |
$gridy_attrs['scale'] = $this->y_axis->value($this->height); |
| 217 |
$this->graph->getJavascript()->addFunction('logStrValueY'); |
| 218 |
$gridy_attrs['function'] = 'logStrValueY'; |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
if($this->graph->getOption('datetime_keys') && |
| 223 |
(method_exists($this->x_axis, 'GetFormat') || |
| 224 |
method_exists($this->y_axis, 'GetFormat'))) { |
| 225 |
$dtf = new DateTimeFormatter; |
| 226 |
if($this->flip_axes) { |
| 227 |
$this->graph->getJavascript()->addFunction('dateStrValueY'); |
| 228 |
$zy = (int)$this->y_axis->value(0); |
| 229 |
$ey = (int)$this->y_axis->value($this->width); |
| 230 |
$dt = new \DateTime('@' . $zy); |
| 231 |
$gridy_attrs['scale'] = ($ey - $zy) / $this->height; |
| 232 |
$gridy_attrs['zero'] = $dtf->format($dt, 'c', true); |
| 233 |
$gridy_attrs['function'] = 'dateStrValueY'; |
| 234 |
$gridy_attrs['format'] = $this->y_axis->getFormat(); |
| 235 |
} else { |
| 236 |
$this->graph->getJavascript()->addFunction('dateStrValueX'); |
| 237 |
$zx = (int)$this->x_axis->value(0); |
| 238 |
$ex = (int)$this->x_axis->value($this->width); |
| 239 |
$dt = new \DateTime('@' . $zx); |
| 240 |
$gridx_attrs['scale'] = ($ex - $zx) / $this->width; |
| 241 |
$gridx_attrs['zero'] = $dtf->format($dt, 'c', true); |
| 242 |
$gridx_attrs['function'] = 'dateStrValueX'; |
| 243 |
$gridx_attrs['format'] = $this->x_axis->getFormat(); |
| 244 |
} |
| 245 |
$long_days = $dtf->getLongDays(); |
| 246 |
$short_days = $dtf->getShortDays(); |
| 247 |
$long_months = $dtf->getLongMonths(); |
| 248 |
$short_months = $dtf->getShortMonths(); |
| 249 |
foreach($long_days as $day) |
| 250 |
$this->graph->getJavascript()->insertVariable('daysLong', null, $day); |
| 251 |
foreach($short_days as $day) |
| 252 |
$this->graph->getJavascript()->insertVariable('daysShort', null, $day); |
| 253 |
foreach($long_months as $month) |
| 254 |
$this->graph->getJavascript()->insertVariable('monthsLong', null, $month); |
| 255 |
foreach($short_months as $month) |
| 256 |
$this->graph->getJavascript()->insertVariable('monthsShort', null, $month); |
| 257 |
} |
| 258 |
|
| 259 |
// build associative data keys XML |
| 260 |
$keys_xml = ''; |
| 261 |
if($this->assoc) { |
| 262 |
|
| 263 |
$k_max = $this->graph->getMaxKey(); |
| 264 |
for($i = 0; $i <= $k_max; ++$i) { |
| 265 |
$k = $this->graph->getKey($i); |
| 266 |
$keys_xml .= $this->graph->element('svggraph:key', ['value' => $k]); |
| 267 |
} |
| 268 |
$keys_xml = $this->graph->element('svggraph:keys', null, null, $keys_xml); |
| 269 |
|
| 270 |
// choose a rounding function |
| 271 |
$round_function = 'kround'; |
| 272 |
if($this->graph->getOption('label_centre')) |
| 273 |
$round_function = 'kroundDown'; |
| 274 |
$this->graph->getJavascript()->addFunction($round_function); |
| 275 |
|
| 276 |
// set the string function |
| 277 |
if($this->flip_axes) { |
| 278 |
$this->graph->getJavascript()->addFunction('keyStrValueY'); |
| 279 |
$gridy_attrs['function'] = 'keyStrValueY'; |
| 280 |
$gridy_attrs['round'] = $round_function; |
| 281 |
} else { |
| 282 |
$this->graph->getJavascript()->addFunction('keyStrValueX'); |
| 283 |
$gridx_attrs['function'] = 'keyStrValueX'; |
| 284 |
$gridx_attrs['round'] = $round_function; |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
$gridx = $this->graph->element('svggraph:gridx', $gridx_attrs); |
| 289 |
$gridy = $this->graph->element('svggraph:gridy', $gridy_attrs); |
| 290 |
$chtext = $this->graph->element('svggraph:chtext', null, null, |
| 291 |
$this->graph->element('svggraph:chtextitem', $chtextitem_attrs)); |
| 292 |
|
| 293 |
$xml = $gridx . $gridy . $chtext . $keys_xml; |
| 294 |
$defs = $this->graph->element('svggraph:data', |
| 295 |
['xmlns:svggraph' => 'http://www.goat1000.com/svggraph'], null, $xml); |
| 296 |
$this->graph->defs->add($defs); |
| 297 |
|
| 298 |
// add the main function at the end - it can fill in any defaults |
| 299 |
$this->graph->getJavascript()->addFunction('crosshairs'); |
| 300 |
return $crosshairs; |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
|