Created
April 24, 2013 21:17
-
-
Save petenelson/5455661 to your computer and use it in GitHub Desktop.
PHP - generate a date range progress bar chart as an image
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// some defaults | |
$start = new DateTime('2013-04-11'); | |
$end = new DateTime('2013-05-09'); | |
$current = new DateTime('2013-04-19'); | |
if (isset($_REQUEST['s'])) | |
$start = new DateTime($_REQUEST['s']); | |
if (isset($_REQUEST['e'])) | |
$end = new DateTime($_REQUEST['e']); | |
if (isset($_REQUEST['c'])) | |
$current = new DateTime($_REQUEST['c']); | |
$dateChart = new GGADateProgressChart(); | |
$dateChart->init(); | |
// vertical grey line to the left of the blue bar | |
$dateChart->draw_line($dateChart->bar_start-1, 1, $dateChart->bar_start-1, $dateChart->bottom_line_y); | |
// horizontal grey line to the bottom of the blue bar | |
$dateChart->draw_line($dateChart->bar_start-1, $dateChart->bottom_line_y, $dateChart->bar_end, $dateChart->bottom_line_y); | |
// start date tick | |
$dateChart->draw_line($dateChart->bar_start-1, $dateChart->bottom_line_y, $dateChart->bar_start-1, $dateChart->bottom_line_y + $dateChart->tick_height); | |
// end date tick | |
$dateChart->draw_line($dateChart->bar_end, $dateChart->bottom_line_y, $dateChart->bar_end, $dateChart->bottom_line_y + $dateChart->tick_height); | |
// draw start date | |
$dateChart->draw_date($start, $dateChart->bar_start-1); | |
// draw end date | |
$dateChart->draw_date($end, $dateChart->bar_end); | |
// figure out how far into the date range we are and draw a progress bar accordingly | |
$total_days = date_diff($start, $end)->days; | |
$days_into_range = date_diff($start, $current)->days; | |
$percent_into_range = $days_into_range / $total_days; | |
// draw ticks at 25%, 50%, 75% | |
$percent_ticks_x = []; | |
for ($i = .25; $i <= .75 ; $i += .25) | |
$percent_ticks_x[] = $dateChart->bar_start + floor($dateChart->bar_max_width * $i); | |
foreach ($percent_ticks_x as $p) | |
$dateChart->draw_line($p, $dateChart->bottom_line_y, $p, $dateChart->bottom_line_y + $dateChart->tick_height); | |
// create dates for 25%, 50% and 75% | |
$percent_date = []; | |
for ($i = .25; $i <= .75 ; $i += .25) | |
$percent_date[] = date_add(clone $start, new DateInterval('P' . floor($total_days * $i) . 'D')); | |
// draw dates at 25%, 50%, 75% | |
for ($i=0; $i < 3; $i++) | |
$dateChart->draw_date($percent_date[$i], $percent_ticks_x[$i]); | |
$dateChart->draw_progress_bar($percent_into_range); | |
header('Content-type: image/png'); | |
imagepng($dateChart->im); | |
imagedestroy($dateChart->im); | |
die(); | |
class GGADateProgressChart { | |
var $month_year = 'n/d'; | |
var $font = 'arial.ttf'; | |
var $font_size = 9; | |
var $bottom_line_y = 26; | |
var $tick_height = 3; | |
var $text_top_padding = 3; | |
var $bar_max_width; | |
var $bar_width; | |
var $bar_height = 22; | |
var $bar_top_x = 2; | |
var $bar_start = 18; | |
var $bar_end = 244; | |
var $im; | |
var $textcolor; | |
var $bluebar; | |
var $greyline; | |
var $width = 260; | |
var $height = 45; | |
function init() { | |
$this->bar_max_width = $this->bar_end - $this->bar_start; | |
// create image resource | |
$this->im = imagecreate($this->width, $this->height); | |
//background | |
$bg = imagecolorallocate($this->im, 246, 246, 247); | |
// grey text | |
$this->textcolor = imagecolorallocate($this->im, 103, 103, 103); | |
// blue progress bar | |
$this->bluebar = imagecolorallocate($this->im, 26, 117, 187); | |
// lines and ticks | |
$this->greyline = imagecolorallocate($this->im, 175, 175, 175); | |
} | |
function draw_date($date, $text_center) { | |
$date_string = date($this->month_year, date_timestamp_get( $date)); | |
$box = imagettfbbox($this->font_size, 0, $this->font, $date_string); | |
$text_width = abs($box[4] - $box[0]); | |
$text_height = abs($box[5] - $box[1]); | |
$text_start = $text_center - ($text_width / 2); | |
imagettftext($this->im, $this->font_size, 0, $text_start, $this->bottom_line_y + $text_height + $this->tick_height + $this->text_top_padding, $this->textcolor, $this->font, $date_string); | |
} | |
function draw_line($from_x, $from_y, $to_x, $to_y) { | |
imageline($this->im, $from_x, $from_y, $to_x, $to_y, $this->greyline); | |
} | |
function draw_progress_bar($percent_progress) { | |
$bar_width = ceil($this->bar_max_width * $percent_progress); | |
imagefilledrectangle($this->im, $this->bar_start, $this->bar_top_x, $this->bar_start + $bar_width, $this->bar_top_x + $this->bar_height, $this->bluebar); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment