Ok.... Full code are as below.
This is to generate the graph on the site.
goofunctions.php (The function is all correct with NO error...) So its return all value when I put dummy result at the display page.
PHP Code:
class MyGoogleChart
{
const URL = 'http://chart.apis.google.com/chart?';
protected $ChartTypes = array(
'pie' => 'p',
'pie3' => 'p3',
'line' => 'lc',
'sparkline' => 'ls',
'bar-horizontal' => 'bhg',
'bar-vertical' => 'bvg',
);
protected $get_mychart;
protected $title;
protected $data = array();
protected $size = array();
protected $color = array();
protected $fill = array();
protected $labelsXY = true;
protected $legend;
protected $useLegend = true;
protected $background;
protected $max;
protected $min;
protected $query = array();
public function __toString()
{
return $this->display();
}
protected function display()
{
$this->query = array(
'cht' => $this->ChartTypes[strtolower($this->get_mychart)], // get_mychart
'chtt' => $this->title, // Title
'chd' => 't:'.$this->data['values'], // Data
'chl' => $this->data['names'], // Data labels
'chdl' => ( ($this->useLegend) && (is_array($this->legend)) ) ? implode('|',$this->legend) : null, // Data legend
'chds' => $this->min.','.$this->max, // Data scale
'chs' => $this->size[0].'x'.$this->size[1], // Size
'chco' => preg_replace( '/[#]+/', '', implode(',',$this->color)), // Color ( Remove # from string )
'chm' => preg_replace( '/[#]+/', '', implode('|',$this->fill)), // Fill ( Remove # from string )
'chxt' => ( $this->labelsXY == true) ? 'y' : null, // X & Y axis labels
'chxr' => '0,1,'.$this->max, // Data range
'chf' => preg_replace( '/[#]+/', '', $this->background), // Background color ( Remove # from string )
);
// Return chart
return $this->img(
MyGoogleChart::URL.http_build_query($this->query),
$this->title
);
}
/* Set attributes */
public function setChartAttrs( $attrs )
{
$this->debug[] = $attrs;
foreach( $attrs as $key => $value )
{
$this->{"set$key"}($value);
}
}
protected function setGet_MyChart( $get_mychart )
{
$this->get_mychart = $get_mychart;
}
protected function setTitle( $title )
{
$this->title = $title;
}
protected function setData( $data )
{
// Clear any previous data
unset( $this->data );
// Check if multiple data
if( is_array(reset($data)) )
{
/* Multiple sets of data */
foreach( $data as $key => $value )
{
// Set highest & lowest
foreach( $value as $valueS ) {
if( empty($this->min) || $valueS < $this->min )
$this->min = $valueS;
if( empty($this->max) || $valueS > $this->max )
$this->max = $valueS;
}
// Add data values
$this->data['values'][] = implode( ',', $value );
// Add data names
$this->data['names'] = implode( '|', array_keys( $value ) );
}
/* Implode data correctly */
$this->data['values'] = implode('|', $this->data['values']);
/* Create legend */
$this->legend = array_keys( $data );
}
else
{
// Set highest & lowest value for X & Y-axis
foreach( $data as $value ) {
if( empty($this->min) || $value < $this->min )
$this->min = $value;
if( empty($this->max) || $value > $this->max )
$this->max = $value;
}
// Add data values
$this->data['values'] = implode( ',', $data );
// Add data names
$this->data['names'] = implode( '|', array_keys( $data ) );
}
}
protected function setLegend( $legend )
{
$this->useLegend = $legend;
}
protected function setSize( $width, $height = null )
{
if(is_array( $width ) )
{
$this->size = $width;
}
else
{
$this->size[] = $width;
$this->size[] = $height;
}
}
protected function setColor( $color )
{
$this->color = $color;
}
protected function setLabelsXY( $labels )
{
$this->labelsXY = $labels;
}
protected function setFill( $fill )
{
if( count( $fill ) < 4 )
{
// Add remaining params
$count = count( $fill );
for( $i = 0; $i < $count; ++$i )
$fill[$i] = 'b,'.$fill[$i].','.$i.','.($i+1).',0';
}
$this->fill = $fill;
}
protected function setBackground( $background )
{
$this->background = 'bg,s,000000';
}
protected function img( $url, $alt = null )
{
return sprintf('<img src="%s" alt="%s" style="width:%spx;height:%spx;" />', $url, $alt, $this->size[0], $this->size[1]);
}
}
display.php (This page is to display the result of Google Graph I generate thru OO PHP... )
PHP Code:
$getchart = new MyGoogleChart();
$color = array('#FF6600');
$gettitle = 'Total Time';
//I want to get the dynamic value from database for the result and attempt value. I just stucked here because the value not return to my graph.
/* foreach ($measurement_data as $count => $totalTime)
{
$count = $count+1;
if ($count < 20 )
{
echo "$count => $totalTime <br>";
}
} */
// This is dummy value... You can see the value.
$dataTimeline = array(
$gettitle => array(
'1' => 9,
'2' => 25,
'3' => 2,
'4' => 22,
'5' => 16,
'6' => 18,
'7' => 13,
'Aug' => 10,
'Sep' => 18,
'Okt' => 5,
'Nov' => 1,
'Dec' => 3,
),
);
$getchart->setChartAttrs( array(
'get_mychart' => 'line',
'title' => My testing result,
'data' => $dataTimeline,
'size' => array( 640, 320 ),
'color' => $color,
'labelsXY' => true,
'background' => $background,
'fill' => array( '#222222', '#000000' ),
));
echo $getchart;
Bookmarks