Improvements to SSL classes
This commit is contained in:
@@ -18,8 +18,11 @@ abstract class GoogleChart implements Iterator,Countable {
|
||||
protected $_max = array();
|
||||
// Chart title
|
||||
protected $_title = '';
|
||||
protected $_dataurl = '';
|
||||
protected $_divname = '';
|
||||
// Default chart size.
|
||||
protected $_size = '700x200';
|
||||
protected $_height = '200';
|
||||
protected $_width = '700';
|
||||
|
||||
// Colors to use for series
|
||||
private $series_colors = array('AAACCC','E0E0E0','CCC888','EEEBBB','666CCC','888888');
|
||||
@@ -46,8 +49,16 @@ abstract class GoogleChart implements Iterator,Countable {
|
||||
|
||||
public function __call($name,$args) {
|
||||
switch ($name) {
|
||||
case 'dataurl': $this->_dataurl = array_shift($args);
|
||||
break;
|
||||
case 'div': $this->_divname = array_shift($args);
|
||||
break;
|
||||
case 'height': $this->_height = array_shift($args);
|
||||
break;
|
||||
case 'title': $this->_title = array_shift($args);
|
||||
break;
|
||||
case 'width': $this->_width = array_shift($args);
|
||||
break;
|
||||
default:
|
||||
throw new Kohana_Exception('Unknown method :name',array(':name'=>$name));
|
||||
}
|
||||
@@ -79,6 +90,9 @@ abstract class GoogleChart implements Iterator,Countable {
|
||||
return new $c();
|
||||
}
|
||||
|
||||
// Render the chart data in a json format
|
||||
abstract public function json();
|
||||
|
||||
// Our child class should define how to render as a string
|
||||
abstract public function render();
|
||||
|
||||
@@ -90,7 +104,7 @@ abstract class GoogleChart implements Iterator,Countable {
|
||||
* Example:
|
||||
* $this->data('yl'=>'Base Down Peak',array('11-12'=>1,'11-11'=>2));
|
||||
*/
|
||||
public function data(array $axis,array $data) {
|
||||
public function sdata(array $axis,array $data) {
|
||||
// Some sanity checking
|
||||
if (count($axis) != 1)
|
||||
throw new Kohana_Exception('We can only take 1 series at time.');
|
||||
@@ -115,6 +129,34 @@ abstract class GoogleChart implements Iterator,Countable {
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Record on plot event
|
||||
* @param $data Should contain an "X" with a "YL" and/or "YR"
|
||||
*/
|
||||
public function pdata($x,array $data) {
|
||||
if (! is_string($x))
|
||||
throw new Kohana_Exception('X should be a string');
|
||||
|
||||
foreach ($data as $key => $values) {
|
||||
switch ($key) {
|
||||
case 'yr':
|
||||
case 'yl':
|
||||
foreach ($values as $k=>$v) {
|
||||
if (! in_array($k,$this->_axis))
|
||||
$this->_axis[$k] = $key;
|
||||
|
||||
$this->_data[$k][$x] = $v;
|
||||
$this->_plotdata[$x][$k] = $v;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new Kohana_Exception('Unknown key :key',array(':key'=>$key));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the colors that will be used for this series
|
||||
*/
|
||||
|
156
modules/gchart/classes/GoogleChart/ComboChart.php
Normal file
156
modules/gchart/classes/GoogleChart/ComboChart.php
Normal file
@@ -0,0 +1,156 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
?>
|
@@ -140,6 +140,8 @@ class GoogleChart_Legacy extends GoogleChart {
|
||||
return implode('|',$return);
|
||||
}
|
||||
|
||||
public function json() {}
|
||||
|
||||
/**
|
||||
* Return URL that renders the chart
|
||||
*/
|
||||
@@ -155,7 +157,7 @@ class GoogleChart_Legacy extends GoogleChart {
|
||||
return array(
|
||||
'chf'=>'bg,s,FFFFFF00',
|
||||
'cht'=>$this->_type,
|
||||
'chs'=>$this->_size,
|
||||
'chs'=>sprintf('%sx%s',$this->_width,$this->_height),
|
||||
'chtt'=>$this->_title,
|
||||
'chbh'=>'a', // @todo This might need to be calculated, valid options (a,r);
|
||||
'chg'=>'7.7,12.5,1,5', // @todo This should be calculated
|
||||
|
Reference in New Issue
Block a user