Spaces:
No application file
No application file
File size: 3,804 Bytes
d2897cd |
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 |
<?php
namespace Mautic\CoreBundle\Helper\Chart;
/**
* Line chart requires the same data as Bar chart.
*/
class LineChart extends AbstractChart implements ChartInterface
{
/**
* Match date/time unit to a humanly readable label
* {@link php.net/manual/en/function.date.php#refsect1-function.date-parameters}.
*
* @var array
*/
protected $labelFormats = [
's' => 'H:i:s',
'i' => 'H:i',
'H' => 'M j ga',
'd' => 'M j, y',
'D' => 'M j, y', // ('D' is BC. Can be removed when all charts use this class)
'W' => '\W\e\e\k W', // (Week is escaped here so it's not interpreted when creating labels)
'm' => 'M Y',
'M' => 'M Y', // ('M' is BC. Can be removed when all charts use this class)
'Y' => 'Y',
];
/**
* Defines the basic chart values, generates the time axe labels from it.
*
* @param string|null $unit {@link php.net/manual/en/function.date.php#refsect1-function.date-parameters}
* @param \DateTime $dateFrom
* @param \DateTime $dateTo
* @param string $dateFormat
*/
public function __construct(
?string $unit = null,
$dateFrom = null,
$dateTo = null,
protected $dateFormat = null
) {
$this->unit = $unit ?? $this->getTimeUnitFromDateRange($dateFrom, $dateTo);
$this->isTimeUnit = in_array($this->unit, ['H', 'i', 's']);
$this->setDateRange($dateFrom, $dateTo);
$this->amount = $this->countAmountFromDateRange();
$this->generateTimeLabels($this->amount);
$this->addOneUnitMinusOneSec($this->dateTo);
}
/**
* @return array{labels: mixed[], datasets: mixed[]}
*/
public function render(): array
{
return [
'labels' => $this->labels,
'datasets' => $this->datasets,
];
}
/**
* Define a dataset by name and data. Method will add the rest.
*
* @param string $label
*
* @return $this
*/
public function setDataset($label, array $data)
{
$datasetId = count($this->datasets);
$baseData = [
'label' => $label,
'data' => $data,
];
$this->datasets[] = array_merge($baseData, $this->generateColors($datasetId));
return $this;
}
/**
* Generate array of labels from the form data.
*
* @param int $amount
*/
public function generateTimeLabels($amount): void
{
if (!isset($this->labelFormats[$this->unit])) {
throw new \UnexpectedValueException('Date/Time unit "'.$this->unit.'" is not available for a label.');
}
/** @var \DateTime $date */
$date = clone $this->dateFrom;
$oneUnit = $this->getUnitInterval();
$format = !empty($this->dateFormat) ? $this->dateFormat : $this->labelFormats[$this->unit];
for ($i = 0; $i < $amount; ++$i) {
$this->labels[] = $date->format($format);
// Special case for months because PHP behaves weird with February
if ('m' === $this->unit) {
$date->modify('first day of next month');
} else {
$date->add($oneUnit);
}
}
}
/**
* Generate unique color for the dataset.
*
* @param int $datasetId
*/
public function generateColors($datasetId): array
{
$color = $this->configureColorHelper($datasetId);
return [
'backgroundColor' => $color->toRgba(0.1),
'borderColor' => $color->toRgba(0.8),
'pointHoverBackgroundColor' => $color->toRgba(1),
'pointHoverBorderColor' => $color->toRgba(1),
];
}
}
|