PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 1.3.1
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v1.3.1
5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / app / core / Visualization / Sparkline.php
matomo / app / core / Visualization Last commit date
Sparkline.php 6 years ago
Sparkline.php
212 lines
1 <?php
2 /**
3 * Piwik - free/libre analytics platform
4 *
5 * @link https://matomo.org
6 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
7 *
8 */
9
10 namespace Piwik\Visualization;
11
12 use Piwik\Common;
13 use Piwik\Piwik;
14 use Piwik\View\ViewInterface;
15
16 /**
17 * Renders a sparkline image given a PHP data array.
18 * Using the Sparkline PHP Graphing Library sparkline.org
19 */
20 class Sparkline implements ViewInterface
21 {
22 const DEFAULT_WIDTH = 200;
23 const DEFAULT_HEIGHT = 50;
24 const MAX_WIDTH = 1000;
25 const MAX_HEIGHT = 1000;
26
27
28 /**
29 * Width of the sparkline
30 * @var int
31 */
32 protected $_width = self::DEFAULT_WIDTH;
33 /**
34 * Height of sparkline
35 * @var int
36 */
37 protected $_height = self::DEFAULT_HEIGHT;
38 private $serieses = array();
39 /**
40 * @var \Davaxi\Sparkline
41 */
42 private $sparkline;
43
44 /**
45 * Array with format: array( x, y, z, ... )
46 * @param array $data,...
47 */
48 public function setValues()
49 {
50 $this->serieses = func_get_args();
51 }
52
53 public function addSeries(array $values)
54 {
55 $this->serieses[] = $values;
56 }
57
58 public function main()
59 {
60 $sparkline = new \Davaxi\Sparkline();
61
62 $thousandSeparator = Piwik::translate('Intl_NumberSymbolGroup');
63 $decimalSeparator = Piwik::translate('Intl_NumberSymbolDecimal');
64
65 $sparkline->setData(); // remove default series
66 foreach ($this->serieses as $seriesIndex => $series) {
67 $values = [];
68 $hasFloat = false;
69
70 foreach ($series as $value) {
71 // replace localized decimal separator
72 $value = str_replace($thousandSeparator, '', $value);
73 $value = str_replace($decimalSeparator, '.', $value);
74
75 // sanitize value
76 $value = filter_var($value, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION | FILTER_FLAG_ALLOW_SCIENTIFIC);
77
78 if (empty($value) || !is_numeric($value)) {
79 $value = 0;
80 }
81
82 $values[] = $value;
83
84 if (is_float($value + 0)) { // coerce to int/float type before checking
85 $hasFloat = true;
86 }
87 }
88
89 // the sparkline lib used converts everything to integers (see the FormatTrait.php file) which means float
90 // numbers that are close to 1.0 or 0.0 will get floored. this can happen in the average page generation time
91 // report, and cause some values which are, eg, around ~.9 to appear as 0 in the sparkline. to workaround this, we
92 // scale the values.
93 if ($hasFloat) {
94 $values = array_map(function ($x) {
95 return $x * 1000.0;
96 }, $values);
97 }
98
99 $sparkline->addSeries($values);
100 $this->setSparklineColors($sparkline, $seriesIndex);
101 }
102
103 $sparkline->setWidth($this->getWidth());
104 $sparkline->setHeight($this->getHeight());
105 $sparkline->setLineThickness(1);
106 $sparkline->setPadding('5');
107
108 $this->sparkline = $sparkline;
109 }
110
111 /**
112 * Returns the width of the sparkline
113 * @return int
114 */
115 public function getWidth() {
116 return $this->_width;
117 }
118
119 /**
120 * Sets the width of the sparkline
121 * @param int $width
122 */
123 public function setWidth($width) {
124 if (!is_numeric($width) || $width <= 0) {
125 return;
126 }
127 if ($width > self::MAX_WIDTH) {
128 $this->_width = self::MAX_WIDTH;
129 } else {
130 $this->_width = (int)$width;
131 }
132 }
133
134 /**
135 * Returns the height of the sparkline
136 * @return int
137 */
138 public function getHeight() {
139 return $this->_height;
140 }
141
142 /**
143 * Sets the height of the sparkline
144 * @param int $height
145 */
146 public function setHeight($height) {
147 if (!is_numeric($height) || $height <= 0) {
148 return;
149 }
150 if ($height > self::MAX_HEIGHT) {
151 $this->_height = self::MAX_HEIGHT;
152 } else {
153 $this->_height = (int)$height;
154 }
155 }
156
157 /**
158 * Sets the sparkline colors
159 *
160 * @param \Davaxi\Sparkline $sparkline
161 */
162 private function setSparklineColors($sparkline, $seriesIndex) {
163 $colors = Common::getRequestVar('colors', false, 'json');
164
165 if (empty($colors)) { // quick fix so row evolution sparklines will have color in widgetize's iframes
166 $colors = array(
167 'backgroundColor' => '#ffffff',
168 'lineColor' => '#162C4A',
169 'minPointColor' => '#ff7f7f',
170 'maxPointColor' => '#75BF7C',
171 'lastPointColor' => '#55AAFF',
172 'fillColor' => '#ffffff'
173 );
174 }
175
176 if (strtolower($colors['backgroundColor']) !== '#ffffff') {
177 $sparkline->setBackgroundColorHex($colors['backgroundColor']);
178 } else {
179 $sparkline->deactivateBackgroundColor();
180 }
181
182 if (is_array($colors['lineColor'])) {
183 $sparkline->setLineColorHex($colors['lineColor'][$seriesIndex], $seriesIndex);
184
185 // set point colors to same as line colors so they can be better differentiated
186 $colors['minPointColor'] = $colors['maxPointColor'] = $colors['lastPointColor'] = $colors['lineColor'][$seriesIndex];
187 } else {
188 $sparkline->setLineColorHex($colors['lineColor']);
189 }
190
191 if (strtolower($colors['fillColor'] !== "#ffffff")) {
192 $sparkline->setFillColorHex($colors['fillColor']);
193 } else {
194 $sparkline->deactivateFillColor();
195 }
196 if (strtolower($colors['minPointColor'] !== "#ffffff")) {
197 $sparkline->addPoint("minimum", 5, $colors['minPointColor'], $seriesIndex);
198 }
199 if (strtolower($colors['maxPointColor'] !== "#ffffff")) {
200 $sparkline->addPoint("maximum", 5, $colors['maxPointColor'], $seriesIndex);
201 }
202 if (strtolower($colors['lastPointColor'] !== "#ffffff")) {
203 $sparkline->addPoint("last", 5, $colors['lastPointColor'], $seriesIndex);
204 }
205 }
206
207 public function render() {
208 $this->sparkline->display();
209 $this->sparkline->destroy();
210 }
211 }
212