Skip to content

Instantly share code, notes, and snippets.

@michvaldes001
Last active April 20, 2016 05:07
Show Gist options
  • Save michvaldes001/2bbf0296e52b5215ac80a67b63d54380 to your computer and use it in GitHub Desktop.
Save michvaldes001/2bbf0296e52b5215ac80a67b63d54380 to your computer and use it in GitHub Desktop.
MV Graphing Library
<?php
//This is a simple library to create bar and line graphs from data array.
//Usage:
//$graph_instance = new GRAPH_CREATE()
//$graph_instance _> gather_data(<'bar_graph' or 'line_graph'>, <height in pixels>, <width in pixels>, <data array (requres
//a sub array with 'label' key and data points to be in sub array with 'value' key)>, <min data point>,
//<max data point>,<text file name to save data in>, <array with RGB color values for graph>,<1 for automatically scaling graph
//to data>, <percentage from min/max value to space graph from top/bottom edge>);
//working example here http://michvaldes001.ddns.net/index.php?section_id=Apps&app_loaded=yes&app_id=weather_station&graph_type=line_graph
//and here http://michvaldes001.ddns.net/index.php?section_id=Apps&app_loaded=yes&app_id=weather_station&graph_type=bar_graph
class GRAPH_CREATE
{
//Gather data via function.
public function gather_data($graph_type, $height, $width, $data_array, $min_data_point, $max_data_point, $image_name, $chart_color_array, $to_scale, $scale_percentage_offset)
{
$graph_line_amount = 20;
$graph_canvas = imagecreatetruecolor($width, $height);
$chart_color = imagecolorallocate($graph_canvas, $chart_color_array[0],$chart_color_array[1], $chart_color_array[2]);
$blue = imagecolorallocate($graph_canvas, 10, 10, 150);
$yellow = imagecolorallocate($graph_canvas, 255, 255, 0);
if ($to_scale == 1)
{
$min_max_points = $this -> scale_graphs($data_array, $scale_percentage_offset);
$min_data_point = $min_max_points[0];
$max_data_point = $min_max_points[1];
}
switch ($graph_type)
{
case 'bar_graph':
$this -> draw_grid_lines($graph_canvas, $data_array, $blue, $width, $height, $graph_line_amount);
$graph_canvas = $this -> bar_graph($height, $width, $data_array, $graph_canvas, $chart_color, $min_data_point, $max_data_point);
$this -> chart_labels_draw($graph_canvas, $data_array, $yellow, $width, $height, $graph_line_amount, $min_data_point, $max_data_point);
break;
case 'line_graph':
$this -> draw_grid_lines($graph_canvas, $data_array, $blue, $width, $height, $graph_line_amount);
$graph_canvas = $this -> line_graph($height, $width, $data_array, $graph_canvas, $chart_color, $min_data_point, $max_data_point);
$this -> chart_labels_draw($graph_canvas, $data_array, $yellow, $width, $height, $graph_line_amount,$min_data_point, $max_data_point);
break;
}
$this -> draw_graph($graph_canvas, $image_name);
}
//Bar graph function.
private function bar_graph($height, $width, $data_array, $graph_canvas, $chart_color, $min_data_point, $max_data_point)
{
$data_array = $data_array['value'];
$data_points_amount = sizeof($data_array);
$bar_width = $width * (2/3);
$x_coordinate = 0;
foreach ($data_array as $data_point)
{
$data_point = $height * (1 - (($data_point - $min_data_point) / ($max_data_point - $min_data_point)));
$x_coordinate = $x_coordinate + (($bar_width/ $data_points_amount) / 4);
imageline($graph_canvas, $x_coordinate, $data_point ,$x_coordinate + ($bar_width / $data_points_amount),$data_point, $chart_color);
imageline($graph_canvas, $x_coordinate,$data_point , $x_coordinate, $height , $chart_color);
$x_coordinate = $x_coordinate + ($bar_width / $data_points_amount);
imageline($graph_canvas, $x_coordinate, $data_point , $x_coordinate, $height , $chart_color);
$x_coordinate = $x_coordinate + (($bar_width / $data_points_amount) / 4);
}
return $graph_canvas;
}
//Line graph function.
private function line_graph($height, $width, $data_array, $graph_canvas, $chart_color, $min_data_point, $max_data_point)
{
$data_array = $data_array['value'];
$data_points_amount = sizeof($data_array);
$x_coordinate = ($width / $data_points_amount)/4;
$draw_point_count = 0;
foreach ($data_array as $data_point)
{
$data_point = $height * (1 - (($data_point - $min_data_point) / ($max_data_point - $min_data_point)));
$data_point_next = $height * (1 - ((next($data_array) - $min_data_point) / ($max_data_point - $min_data_point)));
if ($draw_point_count < ($data_points_amount - 1))
{
imageline($graph_canvas, $x_coordinate, $data_point ,$x_coordinate + ($width / $data_points_amount),$data_point_next, $chart_color);
}
$x_coordinate = $x_coordinate + ($width / $data_points_amount);
$draw_point_count++;
}
return $graph_canvas;
}
//Create grid lines.
private function draw_grid_lines($graph_canvas, $data_array, $blue, $width, $height, $graph_line_amount)
{
for ($i = 1; $i <= $graph_line_amount; $i++)
{
imageline($graph_canvas, 0, ($height / $graph_line_amount) * $i , $width,($height / $graph_line_amount) * $i, $blue);
}
return $graph_canvas;
}
//Create labels.
private function chart_labels_draw($graph_canvas, $data_array, $yellow, $width, $height, $graph_line_amount,$min_data_point, $max_data_point)
{
$max_data_point = $max_data_point - $min_data_point;
$data_array_values = $data_array['value'];
$data_array_labels = $data_array['label'];
$label_points_amount = sizeof($data_array_labels);
for ($i = 0; $i <= $graph_line_amount; $i++)
{
imagestring($graph_canvas, 3, 3, ($height / $graph_line_amount) * $i, ($max_data_point - (($max_data_point / $graph_line_amount) * $i)) + $min_data_point, $yellow);
}
for ($i = 0; $i < $label_points_amount; $i++)
{
imagestring($graph_canvas, 3, (($width / $label_points_amount) * $i) + (($width / $label_points_amount)/4), $height - 15, $data_array_labels[$i] , $yellow);
}
return $graph_canvas;
}
//Scale graphs to min/max values.
private function scale_graphs($data_array, $scale_percentage_offset)
{
$scale_percentage_offset = $scale_percentage_offset * 0.01;
$array_max = max($data_array['value']);
$array_min = min($data_array['value']);
$array_median = (abs($array_max - $array_min)) * $scale_percentage_offset;
$min_data_point = $array_min - $array_median;
$max_data_point = $array_max + $array_median;
$min_max_array = array($min_data_point, $max_data_point);
return $min_max_array;
}
//Render graph.
private function draw_graph($graph_canvas, $image_name)
{
imagepng($graph_canvas, '<GRAPH DIRECTORY>' . $image_name . '.png');
echo '<img src = "<GRAPH DIRECTORY>' . $image_name . '.png">';
imagedestroy($graph_canvas);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment