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 |