PDA

View Full Version : A PHP Google Chart class


Merlin
06-07-2009, 12:21 AM
I incorporated some charts on the site using Google charts, and was pleased with the result. What sets my class apart from the others out there is it will automatically scale and label the y-axis for bar and line charts. I didn't try to do everything the chart widget does, incorporating just the features that I was going to use. It should be easy to extend for your own use, if you wish.

Sample calling method:

$graph->set_title('Stores Total');
$graph->set_chart_type('p3');
$graph->set_chart_size('450x160');
$graph->set_colors($city_data_colors); // Array of colors
$graph->set_x_labels($city_data_label); // Array of labels
$graph->set_data($data); // Array of data

echo $graph->render_img();
The class:
<?
/* Aragon Online - Copyright 2003-2009 by Russell Shilling */
/* Free to use for personal or commercial applications. All */
/* I ask is a link to this code: */
/* http://aragon-online.net/forums/showthread.php?t=558 */

/* See http://code.google.com/apis/chart/ for documentation */
/* of the chart api. */

/* cls_graph.php */

class Graph {

// Chart Type, Chart Size, Bar Size, Image style
var $chart_type = "bvs";
var $chart_size = "300x150";
var $bar_size;
var $img_style;
var $colors = array('555555');
var $line_style;

// Title, Labels
var $title = "A Chart";
var $legend;
var $x_labels;
var $y_labels;

// Data, Minimum and Maximum data values, Scaling options
var $data;
var $min;
var $max;

var $allow_negative = 1;
var $force_zero = 0;

// Axis scaling (Bar and Line)
var $y_steps = 5;
var $step;
var $zero;

/**
* Simple data encoding
*/
function simple_encode($value) {

$simple_encode = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwx yz0123456789";

return substr($simple_encode,$value,1);
}

/**
* Scale data and encode
*/
function scale_data() {

$scaled = array();
foreach ($this->data as $key => $dataset) {
foreach ($dataset as $value) {
if ($value < $this->min)
$value = $this->min;
$index = floor(($value - $this->min) / ($this->max - $this->min) * 61.999);
$scaled[$key] .= $this->simple_encode($index);
}
}
return implode(",", $scaled);
}

/**
* Create scale parameters for pie chart
*/
function pie_scale() {

// Set min and max
$this->min = 0;
$this->max = max(1, array_sum(array_map('array_sum',$this->data)));
}

/**
* Create scale parameters for y-axis
*/
function axis_scale() {

// Set min and max
$this->min = min(array_map('min', $this->data));
$this->max = max(1, max(array_map('max', $this->data)));

if ($this->force_zero) $this->min = 0;
if (! $this->allow_negative) $this->min = max(0, $this->min);

// Make a round number
$range = max(1, $this->max - $this->min);
$tmpstep = $range / $this->y_steps;

$mag = floor(log10($tmpstep));
$pow = pow(10, $mag);

$msd = ceil($tmpstep / $pow);

if ($msd > 5)
$msd = 10;
elseif ($msd > 2)
$msd = 5;
elseif ($msd > 1)
$msd = 2;

$this->step = max(1, $msd * $pow);

$this->min = floor($this->min / $this->step) * $this->step;
$this->max = max(1, ceil($this->max / $this->step) * $this->step);

// Set zero point
if ($this->min < 0) {
$this->zero = round(- $this->min / ($this->max - $this->min), 2);
} else {
$this->zero = 0;
}

// Create labels
$this->y_labels = array();
if ($this->max > $this->min && $this->step > 0)
for ($x = $this->min; $x <= $this->max; $x += $this->step) {
$this->y_labels[] = number_format($x);
}
}

/**
* Set various parameters
*/

/**
* Chart title
*/
function set_title($t) {
$this->title = $t;
}

/**
* Chart type: bvs, lc, p3
*/
function set_chart_type($t) {
$this->chart_type = $t;
}

/**
* Chart size in pixels, '500x200'
*/
function set_chart_size($v) {
$this->chart_size = $v;
}

/**
* A single color, or an array of colors
*/
function set_colors($v) {
$this->colors = $v;
}

/**
* Bar size argument, as "width,space"
*/
function set_bar_size($v) {
$this->bar_size = $v;
}

/**
* Array of legend text
*/
function set_legend($t) {
$this->legend = $t;
}

/**
* The line style argument for line chart
*/
function set_line_style($v) {
$this->line_style = $v;
}

/**
* The style attribute for the image, such as 'float:right;'
*/
function set_img_style($v) {
$this->img_style = $v;
}

/**
* Set to 1 to force the minimum value for y-axis to zero
*/
function set_force_zero($v) {
$this->force_zero = $v;
}

/**
* Set to 1 to allow the graph to have a negative area
*/
function set_allow_negative($v) {
$this->allow_negative = $v;
}

/**
* Number of intervals suggested for the y-axis labels
*/
function set_y_steps($v) {
$this->y_steps = $v;
}

/**
* Array of labels for the x-axis
*/
function set_x_labels($v) {
$this->x_labels = $v;
}

/**
* Data is a two-dimensioanl array: $data[Data set][Data points]
*/
function set_data($v) {
$this->data = $v;
}

/**
* Render the graph as an image
*/
function render_img() {

$chart = "";

if ($this->chart_type == "bvs")
$chart = $this->bar_params();
elseif ($this->chart_type == "lc")
$chart = $this->lc_params();
elseif ($this->chart_type == "p3")
$chart = $this->p3_params();


// Build the image tag
$img = "<img src='http://chart.apis.google.com/chart?" . $chart;

// Add image style, if any
if ($this->img_style) {
$img .= "' style='{$this->img_style}'";
}

// Alternate text
$img .= "' title='{$this->title}'>";

return $img;
}

/**
* Render the params for a Bar Chart - Vertical Stacked
*/
function bar_params() {

$chart = "";

// Chart type, Title, Bar size and color
$x_title = urlencode($this->title);
$chart .= "cht={$this->chart_type}" .
"&chs={$this->chart_size}" .
"&chtt={$x_title}" .
"&chbh={$this->bar_size}" .
"&chts=000000";
if (is_array($this->colors))
$chart .= "&chco=" . implode("|", $this->colors);
elseif ($this->colors)
$chart .= "&chco=" . $this->colors;

// Transparent background, light gray chart
$chart .= "&chf=bg,s,FFFFFF00|c,s,EEEEEE";

$this->axis_scale($this->data);

// Shade negative region
if ($this->zero) {
$chart .= "&chp={$this->zero}&chm=r,FFAAAA,0,0,{$this->zero}";
}

// Axis labels, Black
$x_labels = str_replace(" ","+","|" . implode("|", $this->x_labels) . "|");
$y_labels = str_replace(" ","+","|" . implode("|", $this->y_labels));

$chart .= "&chxt=x,y&chxl=0:" . $x_labels . "1:" . $y_labels;
$chart .= "&chxs=0,000000|1,000000";

// Get the data points
$chart .= "&chd=s:".$this->scale_data();

return $chart;
}

/**
* Render the params for a Pie Chart - 3D
*/
function p3_params() {

$chart = "";

// Chart type, Title, Bar size and color
$x_title = urlencode($this->title);
$chart .= "cht={$this->chart_type}" .
"&chs={$this->chart_size}" .
"&chtt={$x_title}" .
"&chts=000000";

if (is_array($this->colors))
$chart .= "&chco=" . implode(",", $this->colors);
elseif ($this->colors)
$chart .= "&chco=" . $this->colors;

$this->pie_scale($this->data);

// Transparent background
$chart .= "&chf=bg,s,FFFFFF00";

// Axis labels, Black
$x_labels = str_replace(" ","+",implode("|", $this->x_labels));

$chart .= "&chl=" . $x_labels;

// Get the data points
$chart .= "&chd=s:".$this->scale_data();

return $chart;
}

/**
* Render the params for a Line Chart
*/
function lc_params() {

$chart = "";

// Chart type, Title, Bar size and color
$x_title = urlencode($this->title);
$chart .= "cht={$this->chart_type}" .
"&chs={$this->chart_size}" .
"&chtt={$x_title}" .
"&chts=000000";

if (is_array($this->colors))
$chart .= "&chco=" . implode(",", $this->colors);
elseif ($this->colors)
$chart .= "&chco=" . $this->colors;

// Transparent background, light gray chart
$chart .= "&chf=bg,s,FFFFFF00|c,s,EEEEEE";

if ($this->legend)
$chart .= "&chdl=" . implode("|", $this->legend);

if ($this->line_style)
$chart .= "&chls=" . $this->line_style;

$this->axis_scale($this->data);

// Shade negative region
if ($this->zero) {
$chart .= "&chp={$this->zero}&chm=r,FFAAAA,0,0,{$this->zero}";
}

// Axis labels, Black
$x_labels = str_replace(" ","+","|" . implode("|", $this->x_labels) . "|");
$y_labels = str_replace(" ","+","|" . implode("|", $this->y_labels));

$chart .= "&chxt=x,y&chxl=0:" . $x_labels . "1:" . $y_labels;
$chart .= "&chxs=0,000000|1,000000";

// Get the data points
$chart .= "&chd=s:".$this->scale_data();

return $chart;
}
}

?>

MariaDMarin
10-25-2014, 12:47 AM
I incorporated some charts on the site using Google charts, and was pleased with the result. What sets my class apart from the others out there is it will automatically scale and label the y-axis for bar and line charts. I didn't try to do everything the chart widget does, incorporating just the features that I was going to use. It should be easy to extend for your own use, if you wish.

Sample calling method:

$graph->set_title('Stores Total');
$graph->set_chart_type('p3');
$graph->set_chart_size('450x160');
$graph->set_colors($city_data_colors); // Array of colors
$graph->set_x_labels($city_data_label); // Array of labels
$graph->set_data($data); // Array of data

echo $graph->render_img();
The class:
<?
/* Aragon Online - Copyright 2003-2009 by Russell Shilling */
/* Free to use for personal or commercial applications. All */
/* I ask is a link to this code: */
/* http://aragon-online.net/forums/showthread.php?t=558 */

/* See http://code.google.com/apis/chart/ for documentation */
/* of the chart api. */

/* cls_graph.php */

class Graph {

// Chart Type, Chart Size, Bar Size, Image style
var $chart_type = "bvs";
var $chart_size = "300x150";
var $bar_size;
var $img_style;
var $colors = array('555555');
var $line_style;

// Title, Labels
var $title = "A Chart";
var $legend;
var $x_labels;
var $y_labels;

// Data, Minimum and Maximum data values, Scaling options
var $data;
var $min;
var $max;

var $allow_negative = 1;
var $force_zero = 0;

// Axis scaling (Bar and Line)
var $y_steps = 5;
var $step;
var $zero;

/**
* Simple data encoding
*/
function simple_encode($value) {

$simple_encode = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwx yz0123456789";

return substr($simple_encode,$value,1);
}

/**
* Scale data and encode
*/
function scale_data() {

$scaled = array();
foreach ($this->data as $key => $dataset) {
foreach ($dataset as $value) {
if ($value < $this->min)
$value = $this->min;
$index = floor(($value - $this->min) / ($this->max - $this->min) * 61.999);
$scaled[$key] .= $this->simple_encode($index);
}
}
return implode(",", $scaled);
}

/**
* Create scale parameters for pie chart
*/
function pie_scale() {

// Set min and max
$this->min = 0;
$this->max = max(1, array_sum(array_map('array_sum',$this->data)));
}

/**
* Create scale parameters for y-axis
*/
function axis_scale() {

// Set min and max
$this->min = min(array_map('min', $this->data));
$this->max = max(1, max(array_map('max', $this->data)));

if ($this->force_zero) $this->min = 0;
if (! $this->allow_negative) $this->min = max(0, $this->min);

// Make a round number
$range = max(1, $this->max - $this->min);
$tmpstep = $range / $this->y_steps;

$mag = floor(log10($tmpstep));
$pow = pow(10, $mag);

$msd = ceil($tmpstep / $pow);

if ($msd > 5)
$msd = 10;
elseif ($msd > 2)
$msd = 5;
elseif ($msd > 1)
$msd = 2;

$this->step = max(1, $msd * $pow);

$this->min = floor($this->min / $this->step) * $this->step;
$this->max = max(1, ceil($this->max / $this->step) * $this->step);

// Set zero point
if ($this->min < 0) {
$this->zero = round(- $this->min / ($this->max - $this->min), 2);
} else {
$this->zero = 0;
}

// Create labels
$this->y_labels = array();
if ($this->max > $this->min && $this->step > 0)
for ($x = $this->min; $x <= $this->max; $x += $this->step) {
$this->y_labels[] = number_format($x);
}
}

/**
* Set various parameters
*/

/**
* Chart title
*/
function set_title($t) {
$this->title = $t;
}

/**
* Chart type: bvs, lc, p3
*/
function set_chart_type($t) {
$this->chart_type = $t;
}

/**
* Chart size in pixels, '500x200'
*/
function set_chart_size($v) {
$this->chart_size = $v;
}

/**
* A single color, or an array of colors
*/
function set_colors($v) {
$this->colors = $v;
}

/**
* Bar size argument, as "width,space"
*/
function set_bar_size($v) {
$this->bar_size = $v;
}

/**
* Array of legend text
*/
function set_legend($t) {
$this->legend = $t;
}

/**
* The line style argument for line chart
*/
function set_line_style($v) {
$this->line_style = $v;
}

/**
* The style attribute for the image, such as 'float:right;'
*/
function set_img_style($v) {
$this->img_style = $v;
}

/**
* Set to 1 to force the minimum value for y-axis to zero
*/
function set_force_zero($v) {
$this->force_zero = $v;
}

/**
* Set to 1 to allow the graph to have a negative area
*/
function set_allow_negative($v) {
$this->allow_negative = $v;
}

/**
* Number of intervals suggested for the y-axis labels
*/
function set_y_steps($v) {
$this->y_steps = $v;
}

/**
* Array of labels for the x-axis
*/
function set_x_labels($v) {
$this->x_labels = $v;
}

/**
* Data is a two-dimensioanl array: $data[Data set][Data points]
*/
function set_data($v) {
$this->data = $v;
}

/**
* Render the graph as an image
*/
function render_img() {

$chart = "";

if ($this->chart_type == "bvs")
$chart = $this->bar_params();
elseif ($this->chart_type == "lc")
$chart = $this->lc_params();
elseif ($this->chart_type == "p3")
$chart = $this->p3_params();


// Build the image tag
$img = "<img src='http://chart.apis.google.com/chart?" . $chart;

// Add image style, if any
if ($this->img_style) {
$img .= "' style='{$this->img_style}'";
}

// Alternate text
$img .= "' title='{$this->title}'>";

return $img;
}

/**
* Render the params for a Bar Chart - Vertical Stacked
*/
function bar_params() {

$chart = "";

// Chart type, Title, Bar size and color
$x_title = urlencode($this->title);
$chart .= "cht={$this->chart_type}" .
"&chs={$this->chart_size}" .
"&chtt={$x_title}" .
"&chbh={$this->bar_size}" .
"&chts=000000";
if (is_array($this->colors))
$chart .= "&chco=" . implode("|", $this->colors);
elseif ($this->colors)
$chart .= "&chco=" . $this->colors;

// Transparent background, light gray chart
$chart .= "&chf=bg,s,FFFFFF00|c,s,EEEEEE";

$this->axis_scale($this->data);

// Shade negative region
if ($this->zero) {
$chart .= "&chp={$this->zero}&chm=r,FFAAAA,0,0,{$this->zero}";
}

// Axis labels, Black
$x_labels = str_replace(" ","+","|" . implode("|", $this->x_labels) . "|");
$y_labels = str_replace(" ","+","|" . implode("|", $this->y_labels));

$chart .= "&chxt=x,y&chxl=0:" . $x_labels . "1:" . $y_labels;
$chart .= "&chxs=0,000000|1,000000";

// Get the data points
$chart .= "&chd=s:".$this->scale_data();

return $chart;
}

/**
* Render the params for a Pie Chart - 3D
*/
function p3_params() {

$chart = "";

// Chart type, Title, Bar size and color
$x_title = urlencode($this->title);
$chart .= "cht={$this->chart_type}" .
"&chs={$this->chart_size}" .
"&chtt={$x_title}" .
"&chts=000000";

if (is_array($this->colors))
$chart .= "&chco=" . implode(",", $this->colors);
elseif ($this->colors)
$chart .= "&chco=" . $this->colors;

$this->pie_scale($this->data);

// Transparent background
$chart .= "&chf=bg,s,FFFFFF00";

// Axis labels, Black
$x_labels = str_replace(" ","+",implode("|", $this->x_labels));

$chart .= "&chl=" . $x_labels;

// Get the data points
$chart .= "&chd=s:".$this->scale_data();

return $chart;
}

/**
* Render the params for a Line Chart
*/
function lc_params() {

$chart = "";

// Chart type, Title, Bar size and color
$x_title = urlencode($this->title);
$chart .= "cht={$this->chart_type}" .
"&chs={$this->chart_size}" .
"&chtt={$x_title}" .
"&chts=000000";

if (is_array($this->colors))
$chart .= "&chco=" . implode(",", $this->colors);
elseif ($this->colors)
$chart .= "&chco=" . $this->colors;

// Transparent background, light gray chart
$chart .= "&chf=bg,s,FFFFFF00|c,s,EEEEEE";

if ($this->legend)
$chart .= "&chdl=" . implode("|", $this->legend);

if ($this->line_style)
$chart .= "&chls=" . $this->line_style;

$this->axis_scale($this->data);

// Shade negative region
if ($this->zero) {
$chart .= "&chp={$this->zero}&chm=r,FFAAAA,0,0,{$this->zero}";
}

// Axis labels, Black
$x_labels = str_replace(" ","+","|" . implode("|", $this->x_labels) . "|");
$y_labels = str_replace(" ","+","|" . implode("|", $this->y_labels));

$chart .= "&chxt=x,y&chxl=0:" . $x_labels . "1:" . $y_labels;
$chart .= "&chxs=0,000000|1,000000";

// Get the data points
$chart .= "&chd=s:".$this->scale_data();

return $chart;
}
}

?>
Hope you stand number one on google.
offgamers (http://www.offgamers.com/)