Simple chart from <div>s
Sunday, March 21st, 2010 | Author:

Here is a simple PHP function for generating simple charts from div elements.

function chart($array, $size){
	$max = max($array);
	$ratio = $size / $max;
	$out = '';

	foreach($array as $el){
		$width = ceil($el * $ratio);
		$out .= "<div class=\"chart\" style=\"width: ".$width."px;\">$el</div>\n";
	}

	return $out;
}

There is a lot of room for improvements but I kept it as simple as possible deliberately. Many times you can find useful PHP scripts on the internet which are so complex that they aren’t useful any more.

Usage:

$test = array (35070, 24440, 4730, 35700, 29380, 22860, 28870, 22730, 26270);

echo chart($test, 400);

You can style your chart elements as you like, of course!

That’s it, you can check demo here!

Category: Web development  | Tags: ,