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

171 lines
3.8 KiB
PHP
Raw Normal View History

2012-12-19 06:28:39 +00:00
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class provides access to Google's Chart API
*
2013-03-19 22:35:19 +00:00
* @package GoogleChart
2012-12-19 06:28:39 +00:00
* @category Helper
* @author Deon George
2013-03-19 22:35:19 +00:00
* @copyright (c) 2009-2013 Deon George
2012-12-19 06:28:39 +00:00
* @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() {
$result = array();
2012-12-19 06:28:39 +00:00
$result['cols'][] = array(
2012-12-19 06:28:39 +00:00
'id'=>'date',
'label'=>'date',
'type'=>'string',
);
// Columns
foreach (array_keys($this->_axis) as $l) {
$result['cols'][] = array(
2012-12-19 06:28:39 +00:00
'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));
$result['rows'][] = array('c'=>$data);
2012-12-19 06:28:39 +00:00
}
$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'=>$result,'options'=>$options));
2012-12-19 06:28:39 +00:00
}
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'=>"
2013-01-04 04:13:48 +00:00
function drawChart_".$this->_divname."() {
div = document.getElementById('".$this->_divname."');
2012-12-19 06:28:39 +00:00
2013-01-04 04:13:48 +00:00
if (! div) {
alert(\"Unable to render chart, DIV ['".$this->_divname."'] doesnt exist.\");
return;
}
2012-12-19 06:28:39 +00:00
2013-01-04 04:13:48 +00:00
$.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);
}
}
});
2012-12-19 06:28:39 +00:00
}
",
));
Script::add(array(
'type'=>'stdin',
2013-01-04 04:13:48 +00:00
'data'=>'$(document).ready(function() {google.setOnLoadCallback(drawChart_'.$this->_divname.'); });',
2012-12-19 06:28:39 +00:00
));
return sprintf('<div id="%s" style="width: %spx; height: %spx;"></div>',$this->_divname,$this->_width,$this->_height);
}
private function series() {
$result = array();
2012-12-19 06:28:39 +00:00
$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($result,array('type'=>'bar','color'=>$c[$i%$j],'targetAxisIndex'=>0));
2012-12-19 06:28:39 +00:00
else
array_push($result,array('type'=>'line','color'=>$c[$i%$j],'targetAxisIndex'=>1));
2012-12-19 06:28:39 +00:00
$i++;
}
return $result;
2012-12-19 06:28:39 +00:00
}
}
?>