Spaces:
No application file
No application file
namespace Mautic\CoreBundle\Helper\Chart; | |
class PieChart extends AbstractChart implements ChartInterface | |
{ | |
/** | |
* Holds the suma of the all dataset values. | |
* | |
* @var float | |
*/ | |
protected $totalCount = 0; | |
/** | |
* @return array{labels: mixed[], datasets: mixed[]} | |
*/ | |
public function render($withCounts = true): array | |
{ | |
$data = ['data' => [], 'backgroundColor' => [], 'hoverBackgroundColor' => []]; | |
foreach ($this->datasets as $datasetId => $value) { | |
$color = $this->configureColorHelper($datasetId); | |
$data['data'][] = $value; | |
$data['backgroundColor'][] = $color->toRgba(0.8); | |
$data['hoverBackgroundColor'][] = $color->toRgba(0.9); | |
if ($withCounts) { | |
$this->labels[$datasetId] = $this->buildFullLabel($this->labels[$datasetId], $value); | |
} | |
} | |
return [ | |
'labels' => $this->labels, | |
'datasets' => [$data], | |
]; | |
} | |
/** | |
* Define a dataset by name and count number. Method will add the rest. | |
* | |
* @param string $label | |
* @param int $value | |
* | |
* @return $this | |
*/ | |
public function setDataset($label, $value) | |
{ | |
$this->totalCount += $value; | |
$this->datasets[] = $value; | |
$this->labels[] = $label; | |
return $this; | |
} | |
/** | |
* Adds to the label also the value and the percentage. | |
* | |
* @param string $label | |
* @param int $value | |
* | |
* @return string | |
*/ | |
public function buildFullLabel($label, $value) | |
{ | |
if (!$this->totalCount) { | |
return $label; | |
} | |
$percentage = round($value / $this->totalCount * 100, 2); | |
return $label.'; '.$value.'x, '.$percentage.'%'; | |
} | |
} | |