Jump to content

Horizontol Bar Graph


MxGucci

Recommended Posts

Hi,

 

The script below is suppose to create a horizontal bar graph, however the owner of the script didn't give any documentation on how to proceed with using this script to create graphs. The example he gave on his site doesnt make much sense. I will paste it after the script. Can someone please help me create a graph.php file where it'll graph the example he put on his site.

 

<?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;
}

 

 

Example:

 


lwtMiniHorizontalBarChart(
  array(
   'blue' => 90, 
   'white' => 40, 
   'red' => 20
  ), 
  array(
   'width' => 300, 
   'height' => 40
  )
);

 

PLEASE HELP!!

 

Link to comment
https://forums.phpfreaks.com/topic/132244-horizontol-bar-graph/
Share on other sites

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.