chart.class.php
<?php
class Chart {
private static $_first = true;
private static $_count = 0;
private $_chartType;
private $_data;
private $_dataType;
private $_skipFirstRow;
/**
* sets the chart type and updates the chart counter
*/
public function __construct($chartType, $skipFirstRow = false){
$this->_chartType = $chartType;
$this->_skipFirstRow = $skipFirstRow;
self::$_count++;
}
/**
* loads the dataset and converts it to the correct format
*/
public function load($data, $dataType = 'json'){
$this->_data = ($dataType != 'json') ? $this->dataToJson($data) : $data;
}
/**
* load jsapi
*/
private function initChart(){
self::$_first = false;
$output = '';
// start a code block
$output .= '<script type="text/javascript" src="https://www.google.com/jsapi"></script>'."\n";
$output .= '<script type="text/javascript">google.load(\'visualization\', \'1.0\', {\'packages\':[\'corechart\']});</script>'."\n";
return $output;
}
/**
* draws the chart
*/
public function draw($div, Array $options = array()){
$output = '';
if(self::$_first)$output .= $this->initChart();
// start a code block
$output .= '<script type="text/javascript">';
// set callback function
$output .= 'google.setOnLoadCallback(drawChart' . self::$_count . ');';
// create callback function
$output .= 'function drawChart' . self::$_count . '(){';
$output .= 'var data = new google.visualization.DataTable(' . $this->_data . ');';
// set the options
$output .= 'var options = ' . json_encode($options) . ';';
// create and draw the chart
$output .= 'var chart = new google.visualization.' . $this->_chartType . '(document.getElementById(\'' . $div . '\'));';
$output .= 'chart.draw(data, options);';
$output .= '} </script>' . "\n";
return $output;
}
/**
* substracts the column names from the first and second row in the dataset
*/
private function getColumns($data){
$cols = array();
foreach($data[0] as $key => $value){
if(is_numeric($key)){
if(is_string($data[1][$key])){
$cols[] = array('id' => '', 'label' => $value, 'type' => 'string');
} else {
$cols[] = array('id' => '', 'label' => $value, 'type' => 'number');
}
$this->_skipFirstRow = true;
} else {
if(is_string($value)){
$cols[] = array('id' => '', 'label' => $key, 'type' => 'string');
} else {
$cols[] = array('id' => '', 'label' => $key, 'type' => 'number');
}
}
}
return $cols;
}
/**
* convert array data to json
* info: http://code.google.com/intl/nl-NL/apis/chart/interactive/docs/datatables_dataviews.html#javascriptliteral
*/
private function dataToJson($data){
$cols = $this->getColumns($data);
$rows = array();
foreach($data as $key => $row){
if($key != 0 || !$this->_skipFirstRow){
$c = array();
foreach($row as $v){
$c[] = array('v' => $v);
}
$rows[] = array('c' => $c);
}
}
return json_encode(array('cols' => $cols, 'rows' => $rows));
}
}
?>
index.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHP class for Google Chart Tools</title>
<?php
include('chart.class.php');
// demonstration of a line chart and formatted array
$chart = new Chart('LineChart');
$data = array(
'cols' => array(
array('id' => '', 'label' => 'Year', 'type' => 'string'),
array('id' => '', 'label' => 'Net income', 'type' => 'number'),
array('id' => '', 'label' => 'Revenue', 'type' => 'number')
),
'rows' => array(
array('c' => array(array('v' => '1990'), array('v' => 150), array('v' => 100))),
array('c' => array(array('v' => '1995'), array('v' => 300), array('v' => 50))),
array('c' => array(array('v' => '2000'), array('v' => 180), array('v' => 200))),
array('c' => array(array('v' => '2005'), array('v' => 400), array('v' => 100))),
array('c' => array(array('v' => '2010'), array('v' => 300), array('v' => 600))),
array('c' => array(array('v' => '2015'), array('v' => 350), array('v' => 400)))
)
);
$chart->load(json_encode($data));
$options = array('title' => 'revenue', 'theme' => 'maximized', 'width' => 500, 'height' => 200);
echo $chart->draw('revenue', $options);
// demonstration of pie chart and simple array
$chart = new Chart('PieChart');
$data = array(
array('mushrooms', 'slices'),
array('onions', 2),
array('olives', 1),
array('cheese', 4)
);
$chart->load($data, 'array');
$options = array('title' => 'pizza', 'is3D' => true, 'width' => 500, 'height' => 400);
echo $chart->draw('pizza', $options);
// demonstration of a line chart and formatted array
$chart1 = new Chart('ColumnChart');
$data = array(
'cols' => array(
array('id' => '', 'label' => '日付', 'type' => 'string'),
array('id' => '', 'label' => '前人数', 'type' => 'number'),
array('id' => '', 'label' => '後人数', 'type' => 'number')
),
'rows' => array(
array('c' => array(array('v' => '2013年8月25日'), array('v' => 2), array('v' => 1))),
array('c' => array(array('v' => '2013年8月26日'), array('v' => 3), array('v' => 5))),
array('c' => array(array('v' => '2013年8月27日'), array('v' => 5), array('v' => 3))),
array('c' => array(array('v' => '2013年8月28日'), array('v' => 8), array('v' => 7))),
array('c' => array(array('v' => '2013年8月29日'), array('v' => 7), array('v' => 4)))
)
);
$chart1->load(json_encode($data));
$options = array('title' => 'This is my column test');
echo $chart1->draw('column', $options);
?>
</head>
<body>
<div id="revenue"></div>
<div id="pizza"></div>
<div id="column"></div>
</body>
</html>