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
|
<?php
/**
* Piwik - 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 $values = array();
/**
* @var \Davaxi\Sparkline
*/
private $sparkline;
/**
* Array with format: array( x, y, z, ... )
* @param array $data
*/
public function setValues($data) {
$this->values = $data;
}
public function main() {
$sparkline = new \Davaxi\Sparkline();
$thousandSeparator = Piwik::translate('Intl_NumberSymbolGroup');
$decimalSeparator = Piwik::translate('Intl_NumberSymbolDecimal');
$values = [];
$hasFloat = false;
foreach ($this->values 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->setData($values);
$sparkline->setWidth($this->getWidth());
$sparkline->setHeight($this->getHeight());
$this->setSparklineColors($sparkline);
$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) {
$colors = Common::getRequestVar('colors', false, 'json');
if (empty($colors)) { // quick fix so row evolution sparklines will have color in widgetize's iframes
$colors = array(
'backgroundColor' => '#ffffff',
'lineColor' => '#162C4A',
'minPointColor' => '#ff7f7f',
'maxPointColor' => '#75BF7C',
'lastPointColor' => '#55AAFF',
'fillColor' => '#ffffff'
);
}
if (strtolower($colors['backgroundColor']) !== '#ffffff') {
$sparkline->setBackgroundColorHex($colors['backgroundColor']);
} else {
$sparkline->deactivateBackgroundColor();
}
$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']);
}
if (strtolower($colors['maxPointColor'] !== "#ffffff")) {
$sparkline->addPoint("maximum", 5, $colors['maxPointColor']);
}
if (strtolower($colors['lastPointColor'] !== "#ffffff")) {
$sparkline->addPoint("last", 5, $colors['lastPointColor']);
}
}
public function render() {
$this->sparkline->display();
$this->sparkline->destroy();
}
}
|