_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(); $result['cols'][] = array( 'id'=>'date', 'label'=>'date', 'type'=>'string', ); // Columns foreach (array_keys($this->_axis) as $l) { $result['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)); $result['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'=>$result,'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('
',$this->_divname,$this->_width,$this->_height); } private function series() { $result = 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($result,array('type'=>'bar','color'=>$c[$i%$j],'targetAxisIndex'=>0)); else array_push($result,array('type'=>'line','color'=>$c[$i%$j],'targetAxisIndex'=>1)); $i++; } return $result; } } ?>