<?php
/**
 * lwtCSSMiniChars.
 * Version 0.1
 * Copyright (C) 2008 Vassilis Dourdounis.
 * http://projects.littlewebthings.com/cssminicharts/
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */


/**
 * Renders a Horizontal Bar Chart
 * Returns html string.
 *
 * @param (array) data
 * @param (array) options
 */
function lwtMiniHorizontalBarChart($data$options=null) {

    
$width        $options['width']        ? $options['width']            : 100;
    
$height        $options['height']    ? $options['height']        : 20;
    
    
$show_grid    $options['show_grid']    ? $options['show_grid']        : true;
    
    
$scale        $options['scale']        ? $options['scale']            : 100;
    
    
$grid_width    $options['grid_width']? $options['grid_width']    : 10;

    
$default_colors = array('#dec''#e88');

    
$ret .= '<div class="lwtMiniBarChart" style="position: relative; width: '.$width.'px; height: '.$height.'px;">';
    
    
$h $height;

    
// Render grid
    
$grid_w $width $grid_width/$scale;
    if (
$show_grid) {
        for (
$i 0$i*$grid_w+$i $width$i++) {
            
$ret .= '<div style="height: '.$height.'px; border-left: 1px solid #eee; width: 1px; position: absolute; left: '.($i*$grid_w).'px;"></div>';
        }

        for (
$i 0$i*$grid_w+$i $width$i++) {
            
$ret .= '<div style="height: 2px; border-left: 1px solid #333; width: 1px; position: absolute; left: '.($i*$grid_w).'px; bottom: 0px;"></div>';
        }

    }

    
// Render Bars
    
$h $height 0.6;
    
$color_pivot 0;
    foreach (
$data as $color => $value) {
        if (
is_numeric($color)) {
            
$color $default_colors[$color_pivot++ % count($default_colors)];
        }

        
$ret .= '<div style="background-color: '.$color.'; position: absolute;  width: '.($value/$scale*100).'%; height: '.$h.'px; top: '.(($height-$h)/2).'px; padding: 0; margin: 0;  left: 0;"></div>';
        
$h *= 0.5;
    }

    
$ret .= '</div>';
    
    return 
$ret;
}

/**
 * Renders a Vertical Bar Chart
 * Returns html string.
 *
 * @param (array) data
 * @param (array) options
 */
function lwtMiniVerticalBarChart($data$options=null) {

    
$width        $options['width']        ? $options['width']            : 100;
    
$height        $options['height']    ? $options['height']        : 20;
    
    
$show_grid    $options['show_grid']    ? $options['show_grid']        : true;
    
    
$colors        $options['colors']    ? $options['colors']        : array('#e88''#b55');

    
$ret .= '<div class="lwtMiniBarChart" style="position: relative; width: '.$width.'px; height: '.$height.'px;">';
    
    
$w max(1floor(($width-count($data))/count($data)));
    
$i 0;
    foreach (
$data as $value) {
        
$ret .= '<div style="background-color: '.$colors[$i%count($colors)].'; position: absolute; width: '.($w).'px; height: '.($value/max($data)*$height).'px; left: '.($i*$w $i).'px; bottom: 0px;"></div>';
        
$i++;
    }
    
    
$ret .= '</div>';
    
    return 
$ret;
}



?>

1