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
2012-12-21 10:34:09 +11:00

157 lines
3.4 KiB
PHP

<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class provides access to Google's Chart API
*
* @package lnApp
* @subpackage GoogleChart
* @category Helper
* @author Deon George
* @copyright (c) 2010 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."() {
var jsonData = $.ajax({
url: '".$this->_dataurl."',
dataType:'json',
async: false,
}).responseText;
var x = JSON.parse(jsonData);
for(var key in x) {
if (key == 'data')
data = x[key];
else if (key == 'options')
options = x[key];
else
alert('UNKNOWN Key: '+key);
}
// Create our data table out of JSON data loaded from server.
var data = 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(data, options);
}
",
));
Script::add(array(
'type'=>'stdin',
'data'=>'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;
}
}
?>