Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Sparkline.php « Visualization « core - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e3cff4fb11416c3fdc912d4928875597485c0858 (plain)
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
/**
 * Matomo - free/libre analytics platform
 *
 * @link https://matomo.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 */

namespace Piwik\Visualization;

use Piwik\Common;
use Piwik\Piwik;
use Piwik\View\ViewInterface;

/**
 * Renders a sparkline image given a PHP data array.
 * Using the Sparkline PHP Graphing Library sparkline.org
 */
class Sparkline implements ViewInterface
{
    const DEFAULT_WIDTH = 200;
    const DEFAULT_HEIGHT = 50;
    const MAX_WIDTH = 1000;
    const MAX_HEIGHT = 1000;


    /**
     * Width of the sparkline
     * @var int
     */
    protected $_width = self::DEFAULT_WIDTH;
    /**
     * Height of sparkline
     * @var int
     */
    protected $_height = self::DEFAULT_HEIGHT;
    private $serieses = array();
    /**
     * @var \Davaxi\Sparkline
     */
    private $sparkline;

    /**
     * Array with format: array( x, y, z, ... )
     * @param array $data,...
     */
    public function setValues()
    {
        $this->serieses = func_get_args();
    }

    public function addSeries(array $values)
    {
        $this->serieses[] = $values;
    }

    public function main()
    {
        try {
            $sparkline = new \Davaxi\Sparkline();
        } catch (\Exception $exception) {
            // Ignore GD not installed exception
            return;
        }


        $thousandSeparator = Piwik::translate('Intl_NumberSymbolGroup');
        $decimalSeparator = Piwik::translate('Intl_NumberSymbolDecimal');

        $sparkline->setData(); // remove default series
        foreach ($this->serieses as $seriesIndex => $series) {
            $values = [];
            $hasFloat = false;

            foreach ($series as $value) {
                // replace localized decimal separator
                $value = str_replace($thousandSeparator, '', $value);
                $value = str_replace($decimalSeparator, '.', $value);

                // sanitize value
                $value = filter_var($value, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION | FILTER_FLAG_ALLOW_SCIENTIFIC);

                if (empty($value) || !is_numeric($value)) {
                    $value = 0;
                }

                $values[] = $value;

                if (is_float($value + 0)) { // coerce to int/float type before checking
                    $hasFloat = true;
                }
            }

            // the sparkline lib used converts everything to integers (see the FormatTrait.php file) which means float
            // numbers that are close to 1.0 or 0.0 will get floored. this can happen in the average page generation time
            // report, and cause some values which are, eg, around ~.9 to appear as 0 in the sparkline. to workaround this, we
            // scale the values.
            if ($hasFloat) {
                $values = array_map(function ($x) {
                    return $x * 1000.0;
                }, $values);
            }

            $sparkline->addSeries($values);
            $this->setSparklineColors($sparkline, $seriesIndex);
        }

        $sparkline->setWidth($this->getWidth());
        $sparkline->setHeight($this->getHeight());
        $sparkline->setLineThickness(1);
        $sparkline->setPadding('5');

        $this->sparkline = $sparkline;
    }

    /**
     * Returns the width of the sparkline
     * @return int
     */
    public function getWidth() {
        return $this->_width;
    }

    /**
     * Sets the width of the sparkline
     * @param int $width
     */
    public function setWidth($width) {
        if (!is_numeric($width) || $width <= 0) {
            return;
        }
        if ($width > self::MAX_WIDTH) {
            $this->_width = self::MAX_WIDTH;
        } else {
            $this->_width = (int)$width;
        }
    }

    /**
     * Returns the height of the sparkline
     * @return int
     */
    public function getHeight() {
        return $this->_height;
    }

    /**
     * Sets the height of the sparkline
     * @param int $height
     */
    public function setHeight($height) {
        if (!is_numeric($height) || $height <= 0) {
            return;
        }
        if ($height > self::MAX_HEIGHT) {
            $this->_height = self::MAX_HEIGHT;
        } else {
            $this->_height = (int)$height;
        }
    }

    /**
     * Sets the sparkline colors
     *
     * @param \Davaxi\Sparkline $sparkline
     */
    private function setSparklineColors($sparkline, $seriesIndex) {
        $colors = Common::getRequestVar('colors', false, 'json');

        $defaultColors = array(
            'backgroundColor' => '#ffffff',
            'lineColor' => '#162C4A',
            'minPointColor' => '#ff7f7f',
            'maxPointColor' => '#75BF7C',
            'lastPointColor' => '#55AAFF',
            'fillColor' => '#ffffff'
        );

        if (empty($colors)) { // quick fix so row evolution sparklines will have color in widgetize's iframes
            $colors = $defaultColors;
        }

        if (strtolower($colors['backgroundColor']) !== '#ffffff') {
            $sparkline->setBackgroundColorHex($colors['backgroundColor']);
        } else {
            $sparkline->deactivateBackgroundColor();
        }

        if (is_array($colors['lineColor'])) {
            $sparkline->setLineColorHex($colors['lineColor'][$seriesIndex] ?? $defaultColors['lineColor'], $seriesIndex);

            // set point colors to same as line colors so they can be better differentiated
            $colors['minPointColor'] = $colors['maxPointColor'] = $colors['lastPointColor'] = $colors['lineColor'][$seriesIndex] ?? $defaultColors['lineColor'];
        } else {
            $sparkline->setLineColorHex($colors['lineColor']);
        }

        if (strtolower($colors['fillColor'] !== "#ffffff")) {
            $sparkline->setFillColorHex($colors['fillColor']);
        } else {
            $sparkline->deactivateFillColor();
        }
        if (strtolower($colors['minPointColor'] !== "#ffffff")) {
            $sparkline->addPoint("minimum", 5, $colors['minPointColor'], $seriesIndex);
        }
        if (strtolower($colors['maxPointColor'] !== "#ffffff")) {
            $sparkline->addPoint("maximum", 5, $colors['maxPointColor'], $seriesIndex);
        }
        if (strtolower($colors['lastPointColor'] !== "#ffffff")) {
            $sparkline->addPoint("last", 5, $colors['lastPointColor'], $seriesIndex);
        }
    }

    public function render() {
        if ($this->sparkline instanceof \Davaxi\Sparkline) {
            $this->sparkline->display();
            $this->sparkline->destroy();
        }
    }
}