Jump to content

Turning an array into a bar chart?


extrovertive

Recommended Posts

http://hccu.blogspot.com/2008/07/hccu-prepares-to-launch-its-first-debit.html

 

How would I create a simple horizontal bar chart like the one above?

 

Assuming I have an array like

 

$results = array("Q01"=>12, "Q02"=>23....)

 

 

What's the best way to create something like that? Any external tutorial?

Link to comment
https://forums.phpfreaks.com/topic/137616-turning-an-array-into-a-bar-chart/
Share on other sites

Google Charts!

 

The chart gets it data from the the variables you send in the url...

 

Example chart url:

http://chart.apis.google.com/chart?cht=bhs&chs=200x125&chd=s:ello&chco=4d89f9

 

Google Charts:

http://code.google.com/apis/chart/

 

 

lets say

 

the numbers are all

 

<?

$array = array("giants" => 25,"packers" => 32,"cowboys" => 77,"vikings" => 82,"jets" => 92);

$sum = array_sum($array);

$i = 0;

foreach ($array as $k => $v) {

  $i++;

  $a[$i][] = (($v / $sum) * 100);

  $a[$i][] = $k;

}

?>

 

biw after the for each loop, your $a array should hold

[ 0 ] => percent

[ 1 ] => name

 

percent would be between 0 and 100 and then..

 

you just make 1 bar per $a index and give it the width of $a[index][0]px

 

viola

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.