File size: 1,724 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
<?php

namespace Mautic\CoreBundle\Helper\Chart;

class BarChart extends AbstractChart implements ChartInterface
{
    /**
     * Defines the basic chart values, generates the time axe labels from it.
     */
    public function __construct(array $labels)
    {
        $this->labels = $labels;
    }

    /**
     * @return array{labels: mixed[], datasets: mixed[]}
     */
    public function render(): array
    {
        ksort($this->datasets);

        return [
            'labels'   => $this->labels,
            'datasets' => $this->datasets,
        ];
    }

    /**
     * Define a dataset by name and data. Method will add the rest.
     *
     * @param string $label
     * @param int    $order
     *
     * @return $this
     */
    public function setDataset($label, array $data, $order = null)
    {
        $datasetId = count($this->datasets);

        $baseData = [
            'label' => $label,
            'data'  => $data,
        ];

        if (null === $order) {
            $order = count($this->datasets);
        }

        $this->datasets[$order] = array_merge($baseData, $this->generateColors($datasetId));

        return $this;
    }

    /**
     * Generate unique color for the dataset.
     *
     * @param int $datasetId
     */
    public function generateColors($datasetId): array
    {
        $color = $this->configureColorHelper($datasetId);

        return [
            'fill'                      => true,
            'backgroundColor'           => $color->toRgba(0.7),
            'borderColor'               => $color->toRgba(0.8),
            'pointHoverBackgroundColor' => $color->toRgba(0.9),
            'pointHoverBorderColor'     => $color->toRgba(1),
        ];
    }
}