This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
khosb/modules/gchart/classes/GoogleChart/ComboChart.php
2013-03-20 23:04:51 +11:00

171 lines
3.8 KiB
PHP

<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class provides access to Google's Chart API
*
* @package GoogleChart
* @category Helper
* @author Deon George
* @copyright (c) 2009-2013 Deon George
* @license http://dev.leenooks.net/license.html
*/
class GoogleChart_ComboChart extends GoogleChart {
// Should the Y column range be a log() function
protected $_logy = FALSE;
// Should the bar values be stacked
protected $_stacked = FALSE;
// Default line type to use
protected $_type = 'bars';
public function logy($value) {
$this->_logy = $value;
return $this;
}
public function stacked($value) {
$this->_stacked = $value;
return $this;
}
/**
* Set the type of the chart
* @param $type Chart type as per $this->cht
*/
public function ltitle($side,$title) {
if (! in_array($side,array('yl','yr','x')))
throw new Kohana_Exception('Unknown side :side',array(':side'=>$side));
$this->_ltitle[$side] = $title;
return $this;
}
public function json() {
$return = array();
$return['cols'][] = array(
'id'=>'date',
'label'=>'date',
'type'=>'string',
);
// Columns
foreach (array_keys($this->_axis) as $l) {
$return['cols'][] = array(
'id'=>$l,
'label'=>$l,
'type'=>'number',
);
}
// Values
foreach ($this as $k => $v) {
$data = array();
array_push($data,array('v'=>$k));
foreach ($this->_axis as $l => $axis)
array_push($data,array('v'=>isset($v[$l]) ? $v[$l] : 0));
$return['rows'][] = array('c'=>$data);
}
$options = array(
'bar' => array('groupWidth'=>'75%'),
'vAxis' => array('logScale'=>$this->_logy ? 1:0),
'title' => $this->_title,
'isStacked' => $this->_stacked ? 1:0,
'seriesType' => $this->_type,
'series' => $this->series(),
);
return json_encode(array('data'=>$return,'options'=>$options));
}
public function render() {
Script::add(array(
'type'=>'src',
'data'=>'https://www.google.com/jsapi',
));
Script::add(array(
'type'=>'stdin',
'data'=>'google.load("visualization", "1", {packages: ["corechart"]});',
));
Script::add(array(
'type'=>'stdin',
'data'=>"
function drawChart_".$this->_divname."() {
div = document.getElementById('".$this->_divname."');
if (! div) {
alert(\"Unable to render chart, DIV ['".$this->_divname."'] doesnt exist.\");
return;
}
$.ajax({
url: '".$this->_dataurl."',
dataType: 'json',
async: true,
timeout: 10000,
success: function(jsonData) {
for(var key in jsonData) {
if (key == 'data')
data = jsonData[key];
else if (key == 'options')
options = jsonData[key];
else
alert('UNKNOWN Key: '+key);
}
// Create our data table out of JSON data loaded from server.
var x = new google.visualization.DataTable(data);
// Instantiate and draw our chart, passing in some options.
var chart = new google.visualization.ComboChart(document.getElementById('".$this->_divname."'));
chart.draw(x, options);
},
error: function(x, t, m) {
if (t==='timeout') {
alert('got timeout');
} else {
alert('t is: '+t+', X: '+x+', M: '+m);
}
}
});
}
",
));
Script::add(array(
'type'=>'stdin',
'data'=>'$(document).ready(function() {google.setOnLoadCallback(drawChart_'.$this->_divname.'); });',
));
return sprintf('<div id="%s" style="width: %spx; height: %spx;"></div>',$this->_divname,$this->_width,$this->_height);
}
private function series() {
$return = array();
$c = $this->seriescolors();
$j = count($c);
$i = 0;
foreach ($this->_axis as $l => $axis) {
// @todo This shouldnt be hard coded
if ($axis == 'yl')
array_push($return,array('type'=>'bar','color'=>$c[$i%$j],'targetAxisIndex'=>0));
else
array_push($return,array('type'=>'line','color'=>$c[$i%$j],'targetAxisIndex'=>1));
$i++;
}
return $return;
}
}
?>