156 lines
3.8 KiB
PHP
156 lines
3.8 KiB
PHP
<?php defined('SYSPATH') or die('No direct access allowed.');
|
|
|
|
/**
|
|
* This class provides access to HighCharts Chart Drawing
|
|
*
|
|
* @package HighChart
|
|
* @category Helper
|
|
* @author Deon George
|
|
* @copyright (c) 2012-2013 Deon George
|
|
* @license http://dev.leenooks.net/license.html
|
|
*/
|
|
abstract class HighChart {
|
|
protected $_series = array();
|
|
|
|
protected $_divname = 'hchart';
|
|
|
|
// Chart title
|
|
protected $_title = '';
|
|
protected $_subtitle = '';
|
|
|
|
// Default chart size.
|
|
protected $_height = '300';
|
|
protected $_width = '575';
|
|
protected $_xmetric = '';
|
|
protected $_ymetric = '';
|
|
protected $_tooltip = '';
|
|
protected $_pointurl = ''; // URL when clicking on a plot point.
|
|
|
|
// Colors to use for series
|
|
private $_series_colours = array('AAACCC','E0E0E0','CCC888','EEEBBB','666CCC','888888');
|
|
|
|
// Specific data types valid for chart (eg: stacked, bar, pie)
|
|
protected $_data_type = array();
|
|
|
|
// 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();
|
|
|
|
public function __call($name,$args) {
|
|
switch ($name) {
|
|
case 'div': $this->_divname = array_shift($args);
|
|
break;
|
|
case 'height': $this->_height = array_shift($args);
|
|
break;
|
|
case 'pointurl': $this->_pointurl = array_shift($args);
|
|
break;
|
|
case 'subtitle': $this->_subtitle = array_shift($args);
|
|
break;
|
|
case 'title': $this->_title = array_shift($args);
|
|
break;
|
|
case 'tooltip': $this->_tooltip = array_shift($args);
|
|
break;
|
|
case 'width': $this->_width = array_shift($args);
|
|
break;
|
|
case 'xmetric': $this->_xmetric = array_shift($args);
|
|
break;
|
|
case 'ymetric': $this->_ymetric = array_shift($args);
|
|
break;
|
|
default:
|
|
throw new Kohana_Exception('Unknown method :name',array(':name'=>$name));
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Render the URL that generates the graph
|
|
*/
|
|
final public function __toString() {
|
|
try {
|
|
return (string)$this->render();
|
|
} catch (Exception $e) {
|
|
echo Debug::vars(array('m'=>__METHOD__,'e'=>$e));die();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Pick the driver that will render the graph
|
|
* @param $class The child class to invoke
|
|
*/
|
|
final public static function factory($class) {
|
|
$c = sprintf('%s_%s',get_called_class(),$class);
|
|
|
|
if (! class_exists($c))
|
|
throw new Kohana_Exception('Unknown HighChart Type :class',array(':class'=>$class));
|
|
else
|
|
return new $c();
|
|
}
|
|
|
|
/**
|
|
* Common required items for all graphs
|
|
* @todo: Call this in all sub-classes for consitency
|
|
*/
|
|
final protected function render_init() {
|
|
Script::add(array(
|
|
'type'=>'file',
|
|
'data'=>'media/js/highcharts.js',
|
|
));
|
|
|
|
Script::add(array(
|
|
'type'=>'file',
|
|
'data'=>'media/js/modules/exporting.js',
|
|
));
|
|
}
|
|
|
|
/**
|
|
* Common required items for all graphs
|
|
* @todo: Call this in all sub-classes for consitency
|
|
*/
|
|
final protected function render_post() {
|
|
return sprintf('<div id="%s"></div>',$this->_divname);
|
|
}
|
|
|
|
/**
|
|
* Add values to a series
|
|
*
|
|
* @param: string Series Type
|
|
* @param: string Series Name (used for the legend)
|
|
*/
|
|
public function series($type,$name) {
|
|
if ($this->_data_type AND ! in_array($type,$this->_data_type))
|
|
throw new Kohana_Exception('Type not allowed for Chart (:type)',array(':type'=>$type));
|
|
|
|
$x = 'HighChart_Type_'.ucfirst($type);
|
|
if (! class_exists($x))
|
|
throw new Kohana_Exception('Class does not exist :class',array(':class'=>$x));
|
|
|
|
if (! isset($this->_series[$name]))
|
|
$this->_series[$name] = array();
|
|
|
|
$c = count($this->_series[$name]);
|
|
|
|
$this->_series[$name][$c] = new $x;
|
|
|
|
return $this->_series[$name][$c];
|
|
}
|
|
|
|
/**
|
|
* Return the colors that will be used for this series
|
|
*/
|
|
protected function series_colour($c) {
|
|
if (is_null($c))
|
|
return NULL;
|
|
|
|
static $colours = NULL;
|
|
|
|
if (is_null($colours))
|
|
$colors = count($this->_series_colours);
|
|
|
|
return '#'.$this->_series_colours[$c%$colors];
|
|
}
|
|
}
|
|
?>
|