Viewing file: graphs.php (6.49 KB) -rw-r--r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<? /* +-------------------------------------------------------------------+ | H T M L - G R A P H S (v1.2) | | | | Copyright Gerd Tentler info@gerd-tentler.de | | Created 17.09.2002 Last Modified 01.12.2003 | +-------------------------------------------------------------------+ | This program may be used and hosted free of charge by anyone for | | personal purpose as long as this copyright notice remains intact. | | | | Obtain permission before selling the code for this program or | | hosting this software on a commercial website or redistributing | | this software over the Internet or in any other medium. In all | | cases copyright must remain intact. | +-------------------------------------------------------------------+ */ //====================================================================================================== // Parameters: // // - graph type ("hBar", "vBar", "pBar") // - values (string with comma-separated values or array) // - labels (string with comma-separated values or array) // - bar color (string with comma-separated values or array) // - hBar/vBar: label color; pBar: background color // - show values (1 = yes, 0 = no) // - hBar/vBar: legend items (string with comma-separated values or array) // // Returns HTML code //======================================================================================================
function bar_graph($type, $values, $labels = '', $bColor = '', $lColor = '', $showVal = 0, $legend = '') { error_reporting(E_WARNING);
$colors = array('#0000FF', '#FF0000', '#00E000', '#A0A0FF', '#FFA0A0', '#00A000'); $graph = ''; $d = (is_array($values)) ? $values : explode(',', $values); if(is_array($labels)) $r = $labels; else $r = (strlen($labels) > 1) ? explode(',', $labels) : array(); $lColor = (strlen($lColor) < 3) ? '#C0E0FF' : trim($lColor); $drf = (is_array($bColor)) ? $bColor : explode(',', $bColor);
for($i = $sum = $max = $cnt = 0; $i < count($d); $i++) { $drw = explode(';', $d[$i]); for($j = 0; $j < count($drw); $j++) { $val[$i][$j] = trim($drw[$j]); $sum += $val[$i][$j]; if($val[$i][$j] > $max) $max = $val[$i][$j]; if(!$bf[$j]) { if($cnt >= count($colors)) $cnt = 0; $bf[$j] = (strlen($drf[$j]) < 3) ? $colors[$cnt++] : trim($drf[$j]); } } } $mPercent = $sum ? round($max * 100 / $sum) : 0; $mul = $mPercent ? 100 / $mPercent : 1; $type = strtolower($type);
if($legend && $type != 'pbar') $graph .= '<table border=0 cellspacing=0 cellpadding=0><tr valign=top><td>'; $graph .= '<table border=0 cellspacing=2 cellpadding=0>';
if($type == 'hbar') { for($i = 0; $i < count($d); $i++) { $label = ($i < count($r)) ? trim($r[$i]) : $i+1; $graph .= '<tr><td rowspan=' . count($val[$i]) . " bgcolor=$lColor align=center>$label</td>";
for($j = 0; $j < count($val[$i]); $j++) { $percent = $sum ? round($val[$i][$j] * 100 / $sum) : 0; if($j) $graph .= '<tr>'; if($showVal) $graph .= '<td bgcolor=' . $lColor . ' align=right>' . $val[$i][$j] . '</td>';
$graph .= '<td><table border=0 cellspacing=0 cellpadding=0><tr>';
if($percent) { $graph .= '<td bgcolor=' . $bf[$j] . ' width=' . round($percent * $mul) . '> </td>'; } $graph .= "<td> $percent%</td>"; $graph .= '</tr></table></td>'; $graph .= '</tr>'; } } } else if($type == 'vbar') { $graph .= '<tr align=center valign=bottom>'; for($i = 0; $i < count($d); $i++) {
for($j = 0; $j < count($val[$i]); $j++) { $percent = $sum ? round($val[$i][$j] * 100 / $sum) : 0; $graph .= '<td><table border=0 cellspacing=0 cellpadding=0 width=25 align=center>'; $graph .= '<tr align=center valign=bottom>'; $graph .= "<td>$percent%</td>"; if($percent) { $graph .= '</tr><tr align=center valign=bottom>'; $graph .= '<td style="font-size:1px" bgcolor=' . $bf[$j] . ' height=' . round($percent * $mul) . '> </td>'; } $graph .= '</tr></table></td>'; } } if($showVal) { $graph .= '</tr><tr align=center>'; for($i = 0; $i < count($d); $i++) { for($j = 0; $j < count($val[$i]); $j++) { $graph .= "<td bgcolor=$lColor>" . $val[$i][$j] . '</td>'; } } } $graph .= '</tr><tr align=center>'; for($i = 0; $i < count($d); $i++) { $label = ($i < count($r)) ? trim($r[$i]) : $i+1; $graph .= '<td colspan=' . count($val[$i]) . " bgcolor=$lColor>$label</td>"; } $graph .= '</tr>'; } else if($type == 'pbar') { for($i = 0; $i < count($d); $i++) { $label = ($i < count($r)) ? trim($r[$i]) : ''; $graph .= '<tr><td align=right>' . $label . '</td>';
$sum = $val[$i][1]; $percent = $sum ? round($val[$i][0] * 100 / $sum) : 0; if($showVal) $graph .= '<td bgcolor=' . $lColor . ' align=right>' . $val[$i][0] . ' / ' . $sum . '</td>';
$graph .= "<td width=200 bgcolor=$lColor>"; $graph .= '<table border=0 cellspacing=0 cellpadding=0><tr>';
if($percent) { $bColor = $drf[$i] ? trim($drf[$i]) : $colors[0]; $graph .= '<td bgcolor=' . $bColor . ' width=' . round($percent * 2) . '> </td>'; } $graph .= '</tr></table></td>'; $graph .= "<td> $percent%</td>"; $graph .= '</tr>'; } } $graph .= '</table>';
if($legend && $type != 'pbar') { $graph .= '</td><td width=10> </td><td>'; $graph .= '<table border=0 cellspacing=0 cellpadding=1><tr><td bgcolor=#808080>'; $graph .= '<table border=0 cellspacing=0 cellpadding=0><tr><td bgcolor=#F8F0F8>'; $graph .= '<table border=0 cellspacing=4 cellpadding=0>'; $l = (is_array($legend)) ? $legend : explode(',', $legend); for($i = 0; $i < count($bf); $i++) { $graph .= '<tr>'; $graph .= '<td bgcolor=' . $bf[$i] . ' nowrap> </td>'; $graph .= '<td nowrap>' . trim($l[$i]) . '</td>'; $graph .= '</tr>'; } $graph .= '</table></td></tr></table></td></tr></table>'; $graph .= '</td></tr></table>'; } return $graph; } ?>
|