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.
lnhighchart/classes/HighChart.php

156 lines
3.8 KiB
PHP
Raw Permalink Normal View History

2013-11-14 12:17:30 +00:00
<?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 = '';
2013-11-14 12:17:30 +00:00
// Default chart size.
protected $_height = '300';
protected $_width = '575';
protected $_xmetric = '';
2013-11-22 04:19:01 +00:00
protected $_ymetric = '';
protected $_tooltip = '';
2015-09-30 11:33:00 +00:00
protected $_pointurl = ''; // URL when clicking on a plot point.
2013-11-14 12:17:30 +00:00
// 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();
2013-11-14 12:17:30 +00:00
// 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;
2015-09-30 11:33:00 +00:00
case 'pointurl': $this->_pointurl = array_shift($args);
break;
case 'subtitle': $this->_subtitle = array_shift($args);
break;
2013-11-14 12:17:30 +00:00
case 'title': $this->_title = array_shift($args);
break;
case 'tooltip': $this->_tooltip = array_shift($args);
break;
2013-11-14 12:17:30 +00:00
case 'width': $this->_width = array_shift($args);
break;
case 'xmetric': $this->_xmetric = array_shift($args);
break;
2013-11-22 04:19:01 +00:00
case 'ymetric': $this->_ymetric = array_shift($args);
break;
2013-11-14 12:17:30 +00:00
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));
2013-11-14 12:17:30 +00:00
$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();
2013-11-14 12:17:30 +00:00
$c = count($this->_series[$name]);
2013-11-14 12:17:30 +00:00
$this->_series[$name][$c] = new $x;
2013-11-14 12:17:30 +00:00
return $this->_series[$name][$c];
2013-11-14 12:17:30 +00:00
}
/**
* Return the colors that will be used for this series
*/
protected function series_colour($c) {
if (is_null($c))
return NULL;
static $colours = NULL;
2013-11-14 12:17:30 +00:00
if (is_null($colours))
$colors = count($this->_series_colours);
return '#'.$this->_series_colours[$c%$colors];
}
}
?>