| 1 |
<?php |
| 2 |
/** |
| 3 |
* Copyright (C) 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 |
class ParetoChart extends BarAndLineGraph { |
| 25 |
|
| 26 |
public function __construct($w, $h, array $settings, array $fixed_settings = []) |
| 27 |
{ |
| 28 |
$fs = [ |
| 29 |
'dataset_axis' => [0,1], |
| 30 |
'line_dataset' => [1], |
| 31 |
'datetime_keys' => false, |
| 32 |
]; |
| 33 |
$s = [ |
| 34 |
'tooltip_callback' => function($d, $k, $v) { |
| 35 |
if($k === null || $v === null) |
| 36 |
return null; |
| 37 |
if($d == 0) |
| 38 |
return $k . ": " . new Number($v); |
| 39 |
return new Number($v) . '%'; |
| 40 |
}, |
| 41 |
]; |
| 42 |
$fs = array_merge($fs, $fixed_settings); |
| 43 |
|
| 44 |
// to pass settings into line graph |
| 45 |
$settings = array_merge($s, $settings); |
| 46 |
|
| 47 |
parent::__construct($w, $h, $settings, $fs); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Override to process values into order and add line graph |
| 52 |
*/ |
| 53 |
function values($values) |
| 54 |
{ |
| 55 |
$res = parent::values($values); |
| 56 |
if(empty($values) || $this->values->error) |
| 57 |
return $res; |
| 58 |
|
| 59 |
if($this->values instanceof Data) |
| 60 |
$this->values = StructuredData::convertFrom($this->values, true, false, false); |
| 61 |
|
| 62 |
$dataset = $this->getOption(['dataset', 0], 0); |
| 63 |
$this->values->sort($dataset, true); |
| 64 |
$sum = 0; |
| 65 |
foreach($this->values[$dataset] as $item) { |
| 66 |
if($item->value < 0) |
| 67 |
throw new \Exception('Negative values not supported'); |
| 68 |
$sum += $item->value; |
| 69 |
} |
| 70 |
|
| 71 |
$running = 0; |
| 72 |
$this->values->revalue(2, function($key, $row) use(&$running, $sum, $dataset) { |
| 73 |
$value = $row[$dataset]; |
| 74 |
$running += $value; |
| 75 |
$new_row = [$value, $sum > 0 ? 100 * $running / $sum : 100]; |
| 76 |
return $new_row; |
| 77 |
}); |
| 78 |
|
| 79 |
$this->setOption('line_bar', 1); |
| 80 |
$this->setOption('units_y', [null, '%']); |
| 81 |
$this->setOption('minimum_units_y', 1); |
| 82 |
$this->setOption('dataset', null); |
| 83 |
|
| 84 |
// update MultiGraph with new data |
| 85 |
$this->multi_graph = new MultiGraph($this->values, false, false, false); |
| 86 |
$this->multi_graph->setEnabledDatasets([0,1]); |
| 87 |
return $res; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Override to prevent offset |
| 92 |
*/ |
| 93 |
public function getLineOffset($dataset) |
| 94 |
{ |
| 95 |
$g_width = $this->x_axes[$this->main_x_axis]->unit(); |
| 96 |
return $g_width; |
| 97 |
return 0; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Adds starting point to line |
| 102 |
*/ |
| 103 |
public function drawLine($dataset, $points, $y_bottom) |
| 104 |
{ |
| 105 |
$x = $this->gridX(0); |
| 106 |
$y = $this->gridY(0, 1); |
| 107 |
$p = [$x, $y, null, $dataset, 0]; |
| 108 |
$points = array_merge([$p], $points); |
| 109 |
|
| 110 |
// add a marker at start of line |
| 111 |
$item = new DataItem(0, 0); |
| 112 |
$this->linegraph->addMarker($x, $y, $item, null, $dataset); |
| 113 |
return $this->linegraph->drawLine($dataset, $points, $y_bottom); |
| 114 |
} |
| 115 |
} |
| 116 |
|