Sparkline graphs have had a bit of press lately, so I’ve been playing about with generating them on my commutes to and from work.
If you’ve not heard of them before, a Sparkline is a small graph that is often used inline with text.
I’ve been using PHP, so I’ve created a simple class called Spark
, with a single static method called line
. This returns a simple string as an inline graph from an array of values that have been passed in.
Sparkline Examples
Here’s an example plotting a simple staircase type graph.
require_once('lib/Spark.php'); echo Spark::line(array(1, 2, 3, 4, 5, 6, 7, 8)) . "n"; > ▁▂▃▄▅▆▇█
Here’s an example plotting a cosine wave.
$cos = array(); for($i=0; $i <= 360; $i=$i+20) { $cos[] = cos($i * M_PI / 180); } echo Spark::line($cos) . "n"; > ██▇▆▅▄▃▂▁▁▁▂▃▄▅▆▇██
Summary
The code is available on GitHub as php-spark, and is free to use. It was inspired by a simple project for Go called Spark, which was inspired from elsewhere.