| 1 |
<?php |
| 2 |
/** |
| 3 |
* Copyright (C) 2013-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 |
* BubbleGraph - scatter graph with bubbles instead of markers |
| 26 |
*/ |
| 27 |
class BubbleGraph 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 |
'require_structured' => ['area'], |
| 35 |
]; |
| 36 |
$fs = array_merge($fs, $fixed_settings); |
| 37 |
parent::__construct($w, $h, $settings, $fs); |
| 38 |
} |
| 39 |
|
| 40 |
protected function draw() |
| 41 |
{ |
| 42 |
$body = $this->grid() . $this->underShapes(); |
| 43 |
$dataset = $this->getOption(['dataset', 0], 0); |
| 44 |
|
| 45 |
$bnum = 0; |
| 46 |
$y_axis = $this->y_axes[$this->main_y_axis]; |
| 47 |
$series = ''; |
| 48 |
|
| 49 |
foreach($this->values[$dataset] as $item) { |
| 50 |
$area = $item->area; |
| 51 |
$x = $this->gridPosition($item, $bnum); |
| 52 |
$y = null; |
| 53 |
if($item->value !== null && $x !== null) |
| 54 |
$y = $this->gridY($item->value); |
| 55 |
|
| 56 |
if($y !== null) { |
| 57 |
$r = $this->getOption('bubble_scale') * $y_axis->unit() * sqrt(abs($area) / M_PI); |
| 58 |
$circle = ['cx' => $x, 'cy' => $y, 'r' => $r]; |
| 59 |
$colour = $this->getColour($item, $bnum, $dataset); |
| 60 |
$circle_style = ['fill' => $colour]; |
| 61 |
if($area < 0) { |
| 62 |
// draw negative bubbles with a checked pattern |
| 63 |
$pattern = [$colour, 'pattern' => 'check', 'size' => 8]; |
| 64 |
$pid = $this->defs->addPattern($pattern); |
| 65 |
$circle_style['fill'] = 'url(#' . $pid . ')'; |
| 66 |
} |
| 67 |
$this->setStroke($circle_style, $item, $bnum, $dataset); |
| 68 |
$this->addDataLabel($dataset, $bnum, $circle, $item, |
| 69 |
$x - $r, $y - $r, $r * 2, $r * 2); |
| 70 |
|
| 71 |
if($this->getOption('show_tooltips')) |
| 72 |
$this->setTooltip($circle, $item, $dataset, $item->key, $area, true); |
| 73 |
if($this->getOption('show_context_menu')) |
| 74 |
$this->setContextMenu($circle, $dataset, $item, true); |
| 75 |
if($this->getOption('semantic_classes')) |
| 76 |
$circle['class'] = 'series0'; |
| 77 |
$bubble = $this->element('circle', array_merge($circle, $circle_style)); |
| 78 |
$series .= $this->getLink($item, $item->key, $bubble); |
| 79 |
|
| 80 |
$this->addMarker($x, $y, $item, null, $dataset, false); |
| 81 |
$this->setLegendEntry($dataset, $bnum, $item, $circle_style); |
| 82 |
} |
| 83 |
|
| 84 |
++$bnum; |
| 85 |
} |
| 86 |
|
| 87 |
$group = []; |
| 88 |
if($this->getOption('semantic_classes')) |
| 89 |
$group['class'] = 'series'; |
| 90 |
$shadow_id = $this->defs->getShadow(); |
| 91 |
if($shadow_id !== null) |
| 92 |
$group['filter'] = 'url(#' . $shadow_id . ')'; |
| 93 |
if(!empty($group)) |
| 94 |
$series = $this->element('g', $group, null, $series); |
| 95 |
|
| 96 |
list($best_fit_above, $best_fit_below) = $this->bestFitLines(); |
| 97 |
$body .= $best_fit_below; |
| 98 |
$body .= $series; |
| 99 |
$body .= $this->overShapes(); |
| 100 |
$body .= $this->axes(); |
| 101 |
$body .= $this->drawMarkers(); |
| 102 |
$body .= $best_fit_above; |
| 103 |
return $body; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Checks that the data produces a 2-D plot |
| 108 |
*/ |
| 109 |
protected function checkValues() |
| 110 |
{ |
| 111 |
parent::checkValues(); |
| 112 |
|
| 113 |
// using force_assoc makes things work properly |
| 114 |
if($this->values->associativeKeys()) |
| 115 |
$this->setOption('force_assoc', true); |
| 116 |
|
| 117 |
// prevent drawing actual markers |
| 118 |
$this->setOption('marker_size', 0); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Return bubble for legend |
| 123 |
*/ |
| 124 |
public function drawLegendEntry($x, $y, $w, $h, $entry) |
| 125 |
{ |
| 126 |
$bubble = [ |
| 127 |
'cx' => $x + $w / 2, |
| 128 |
'cy' => $y + $h / 2, |
| 129 |
'r' => min($w, $h) / 2 |
| 130 |
]; |
| 131 |
return $this->element('circle', array_merge($bubble, $entry->style)); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
|