Skip to content

Commit

Permalink
added static method to generate HTML for a funnel chart in the Geckob…
Browse files Browse the repository at this point in the history
…oard Text Widget
  • Loading branch information
franklam committed Jun 25, 2012
1 parent 3c59504 commit 0b30ec7
Showing 1 changed file with 94 additions and 5 deletions.
99 changes: 94 additions & 5 deletions lib/Geckoboard/Widget/TextWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ class TextWidget extends \sfAltumoPlugin\Geckoboard\Widget\AbstractWidget
const TYPE_INFO = 2;
const TYPE_ALERT = 1;

// enumerations for the different widget heights available on Geckoboard
const WIDGET_TYPE_SINGLE = 1;
const WIDGET_TYPE_DOUBLE = 2;

// the default color of the bars displayed in the funnel chart
const DEFAULT_CHART_COLOR = '#77ab13';

/**
* Maximum number of items that can be sent to Geckoboard in a Text widget.
Expand All @@ -20,10 +26,10 @@ class TextWidget extends \sfAltumoPlugin\Geckoboard\Widget\AbstractWidget
const MAXIMUM_ITEM_COUNT = 10;


/**
* @var \stdClass[]
*/
protected $items = array();
/**
* @var \stdClass[]
*/
protected $items = array();


/**
Expand Down Expand Up @@ -81,5 +87,88 @@ public function getContent()

return $wrapper;
}




/**
* create HTML to display a funnel chart
*
* @static
* @param $chart_title
* @param $chart_data array containing attributes for each funnel chart item (label, value, color)
* @param int $widget_type
* @return string
*
* e.g.
* $funnel_data[] = array(
* 'label' => 'Item A',
* 'value' => 3,
* 'color' => '#c00'
* );
*
* $widget->addItem( \sfAltumoPlugin\Geckoboard\Widget\TextWidget::GenerateFunnelChartHtml( "pane title", $funnel_data, \sfAltumoPlugin\Geckoboard\Widget\TextWidget::WIDGET_TYPE_SINGLE ) );
*
*/
public static function GenerateFunnelChartHtml( $chart_title, $chart_data, $widget_type = self::WIDGET_TYPE_DOUBLE )
{
$chart_data_count = sizeof( $chart_data );
$row_height = ( $chart_data_count !== 0 ) ? floor( 100 / $chart_data_count ) : 0;

// set the widget attributes based on the widget type
switch( $widget_type )
{
case self::WIDGET_TYPE_SINGLE:
$container_height = 92;
$font_size = 11;
$line_height = 12;
break;
case self::WIDGET_TYPE_DOUBLE:
default:
$container_height = 343;
$font_size = 14;
$line_height = 48;
break;
}


// find the largest value in the data set (this will be the largest bar)
$max_value = 0;
foreach( $chart_data as $data ) {
$value = intval( $data['value'] );
if ( $value > $max_value )
$max_value = $value;
}

// build the HTML
$html = array();
$html[] = '<section class="b-widget-body">';
$html[] = '<h2 style="font-size:14px; color:#D3D4D4;">' . $chart_title . '</h2>';
$html[] = '<div class="b-widget-inner">';
$html[] = '<ul style="list-style-type:none; height:' . $container_height . 'px;">';

foreach( $chart_data as $data ) {
$label = $data['label'];
$value = $data['value'];
$color = ( !empty( $data['color'] ) )? $data['color'] : self::DEFAULT_CHART_COLOR;
$bar_size = ( $max_value !== 0 ) ? $value / $max_value * 100 : 0;

$html[] = '<li style="margin-bottom:1px; height:' . $row_height . '%; line-height:' . $line_height . 'px;">';

$html[] = '<div style="width:50%; clear:both; float:right; margin:0; padding:0; font-size:' . $font_size . 'px; line-height:' . $line_height . 'px;">';
$html[] = '<span style="color:#D3D4D4; font-size:1.1em; font:bold; margin:0;">&nbsp;' . $value . '</span>&nbsp;' . $label;
$html[] = '</div>';

$html[] = '<div style="width:50%; float:right; margin:0; padding:0; line-height:' . $line_height . 'px;">';
$html[] = '<div style="width:' . $bar_size . '%; height:100%; float:right; margin:0 0 1px 0; background-color:' . $color . '; border-right:1px solid ' . $color . ';">&nbsp;</div>';
$html[] = '</div>';

$html[] = '</li>';
}

$html[] = '</ul>';
$html[] = '</div>';
$html[] = '</section>';

return implode('', $html);
}
}

0 comments on commit 0b30ec7

Please sign in to comment.