I am using the below php script (from Simple Horizontal Bar Graph using php) to create a horizontal bar graph from and I'm dynamically replacing the static array values with database values which is working great except when there are duplicate values. I need to allow duplicates numbers for the bar chart to be accurate - however due to the array, when there is a duplicate one of the "bars" goes missing.
Could I re-write this long hand without using an array and/or the foreach loop ?
$values = array(
$values['one'] => 'apples',
$values['two'] => 'oranges',
$values['three'] => 'pears',
$values['four'] => 'bananas'
);
// Find the maximum percentage
$max = max(array_keys($values));
foreach($values as $percentage => $label) {
// Calculate the position, maximum value gets position 0, smaller values approach 200
$pos = 200 - ($percentage / $max) * 200;
// Output the label that shows the percentage
echo '<span><label>'.$percentage.'%</label></span>';
// Output the span, apply style rule for the background position
echo '<span class="bar" style="background-position: -'.$pos.'px 0;">'.$label.'</span>';
}
?>