├── composer.json ├── LICENSE.md ├── README.md └── sparkline.php /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jamiebicknell/Sparkline", 3 | "description": "PHP script to generate sparklines", 4 | "keywords": [ 5 | "sparkline", 6 | "sparklines", 7 | "php", 8 | "gd" 9 | ], 10 | "homepage": "http://github.com/jamiebicknell/Sparkline", 11 | "license": "MIT", 12 | "authors": [ 13 | { 14 | "name": "Jamie Bicknell", 15 | "homepage": "http://www.jamiebicknell.com" 16 | } 17 | ], 18 | "require": { 19 | "php": ">=5.2.0" 20 | } 21 | } -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Jamie Bicknell - @jamiebicknell 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Sparkline 2 | 3 | PHP script to generate sparklines, with browser cachine with ETag. 4 | 5 | ## Usage 6 | 7 | ```html 8 | 9 | ``` 10 | 11 | ## Examples 12 | 13 | EG1
14 | `sparkline.php` 15 | 16 | EG2
17 | `sparkline.php?data=5` 18 | 19 | EG3
20 | `sparkline.php?data=2,4,5,6,10,7,8,5,7,7,11,8,6,9,11,9,13,14,12,16` 21 | 22 | EG4
23 | `sparkline.php?data=2,4,5,6,10,7,8,5,7,7,11,8,6,9,11,9,13,14,12,16&line=5bb763&fill=d5f7d8` 24 | 25 | EG5
26 | `sparkline.php?data=2,4,5,6,10,7,8,5,7,7,11,8,6,9,11,9,13,14,12,16&line=fd8626&fill=ffedde` 27 | 28 | EG6
29 | `sparkline.php?data=2,4,5,6,10,7,8,5,7,7,11,8,6,9,11,9,13,14,12,16&line=ed5565&fill=ffe2e2` 30 | 31 | EG7
32 | `sparkline.php?data=2,4,5,6,10,7,8,5,7,7,11,8,6,9,11,9,13,14,12,16&line=444&fill=eee` 33 | 34 | EG8
35 | `sparkline.php?data=2,4,5,6,10,7,8,5,7,7,11,8,6,9,11,9,13,14,12,16&line=31475c&fill=fff` 36 | 37 | EG9
38 | `sparkline.php?size=185x40&data=2,4,5,6,10,7,8,5,7,7,11,8,6,9,11,9,13,14,12,16` 39 | 40 | 41 | ## Query Parameters 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 |
KeyExample ValueDefaultDescription
size100x25, 10080x20Width must be between 50 and 80
Height must be between 20 and 800
data10,20,50,20,30,40,50,120,90Comma separated list of values to plot
backeeeeee, dddffffffHexadecimal code for background colour
line555555, 2221388dbHexadecimal code for line colour
fillcccccc, bbbe6f2faHexadecimal code for fill colour
81 | 82 | ## Size Parameter 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 |
ValueDescription
100Creates a square image 100px in width and 100px in height
80x20Creates an image 80px in width and 20px in height
98 | 99 | ## License 100 | 101 | Sparkline is licensed under the [MIT license](http://opensource.org/licenses/MIT), see [LICENSE.md](https://github.com/jamiebicknell/Sparkline/blob/master/LICENSE.md) for details. -------------------------------------------------------------------------------- /sparkline.php: -------------------------------------------------------------------------------- 1 | > 0x10), 0xFF & ($dec >> 0x8), 0xFF & $dec); 21 | } 22 | 23 | $size = isset($_GET['size']) ? str_replace('x', '', $_GET['size']) != '' ? $_GET['size'] : '80x20' : '80x20'; 24 | $back = isset($_GET['back']) ? isHex($_GET['back']) ? $_GET['back'] : 'ffffff' : 'ffffff'; 25 | $line = isset($_GET['line']) ? isHex($_GET['line']) ? $_GET['line'] : '1388db' : '1388db'; 26 | $fill = isset($_GET['fill']) ? isHex($_GET['fill']) ? $_GET['fill'] : 'e6f2fa' : 'e6f2fa'; 27 | $data = isset($_GET['data']) ? explode(',', $_GET['data']) : array(); 28 | 29 | list($w, $h) = explode('x', $size); 30 | $w = floor(max(50, min(800, $w))); 31 | $h = !strstr($size, 'x') ? $w : floor(max(20, min(800, $h))); 32 | $t = 1.75; 33 | $s = 4; 34 | 35 | $w *= $s; 36 | $h *= $s; 37 | $t *= $s; 38 | 39 | $salt = 'v1.0.1'; 40 | $hash = md5($salt . $_SERVER['QUERY_STRING']); 41 | 42 | $data = (count($data) < 2) ? array_fill(0, 2, $data[0]) : $data; 43 | $count = count($data); 44 | $step = $w / ($count - 1); 45 | 46 | $min = min($data); 47 | $max = max($data); 48 | if ($max != $min) { 49 | foreach ($data as $k => $v) { 50 | $data[$k] -= $min; 51 | } 52 | $max = max($data); 53 | } 54 | 55 | if (!extension_loaded('gd')) { 56 | die('GD extension is not installed'); 57 | } 58 | 59 | if (isset($_SERVER['HTTP_IF_NONE_MATCH'])) { 60 | if ($_SERVER['HTTP_IF_NONE_MATCH'] == $hash) { 61 | header($_SERVER['SERVER_PROTOCOL'] . ' 304 Not Modified'); 62 | die(); 63 | } 64 | } 65 | 66 | $im = imagecreatetruecolor($w, $h); 67 | list($r, $g, $b) = hexToRgb($back); 68 | $bg = imagecolorallocate($im, $r, $g, $b); 69 | list($r, $g, $b) = hexToRgb($line); 70 | $fg = imagecolorallocate($im, $r, $g, $b); 71 | list($r, $g, $b) = hexToRgb($fill); 72 | $lg = imagecolorallocate($im, $r, $g, $b); 73 | imagefill($im, 0, 0, $bg); 74 | 75 | imagesetthickness($im, $t); 76 | 77 | foreach ($data as $k => $v) { 78 | $v = $v > 0 ? round($v / $max * $h) : 0; 79 | $data[$k] = max($s, min($v, $h - $s)); 80 | } 81 | 82 | $x1 = 0; 83 | $y1 = $h - $data[0]; 84 | $line = array(); 85 | $poly = array(0, $h + 50, $x1, $y1); 86 | for ($i = 1; $i < $count; $i++) { 87 | $x2 = $x1 + $step; 88 | $y2 = $h - $data[$i]; 89 | array_push($line, array($x1, $y1, $x2, $y2)); 90 | array_push($poly, $x2, $y2); 91 | $x1 = $x2; 92 | $y1 = $y2; 93 | } 94 | array_push($poly, $x2, $h + 50); 95 | 96 | imagefilledpolygon($im, $poly, $count + 2, $lg); 97 | 98 | foreach ($line as $k => $v) { 99 | list($x1, $y1, $x2, $y2) = $v; 100 | imageline($im, $x1, $y1, $x2, $y2, $fg); 101 | } 102 | 103 | $om = imagecreatetruecolor($w / $s, $h / $s); 104 | imagecopyresampled($om, $im, 0, 0, 0, 0, $w / $s, $h / $s, $w, $h); 105 | imagedestroy($im); 106 | 107 | header('Content-Type: image/png'); 108 | header('Content-Disposition: inline; filename="sparkline_' . time() . substr(microtime(), 2, 3) . '.png"'); 109 | header('ETag: ' . $hash); 110 | header('Accept-Ranges: none'); 111 | header('Cache-Control: max-age=604800, must-revalidate'); 112 | header('Expires: ' . gmdate('D, d M Y H:i:s T', strtotime('+7 days'))); 113 | imagepng($om); 114 | imagedestroy($om); 115 | --------------------------------------------------------------------------------