= DATE_SUB(NOW(), interval $t_period day) group by DATE(DATE_ADD(ts, interval 3 hour)) ) order by ts asc "; } else { $sql = " SELECT *, DATE_ADD(ts, interval 3 hour) as ts from books.wastelands WHERE ts >= DATE_SUB(NOW(), interval $t_period hour) order by ts asc "; } $result = mysql_query($sql); $min_value = $max_value = $first = $last = "NaN"; $cnt = 0; $d_money = $o_money = 0; while ($row = mysql_fetch_assoc($result)) { if($unit == 'days') $ts = substr($row['ts'],0,10); else $ts = substr($row['ts'],0,13) . ":00"; if($measure == 'total') $money = $row['money']; else $money = $row['money'] - $o_money; if($cnt >= 1) { if($min_value == "NaN" or $min_value > $money) $min_value = $money; if($max_value == "NaN" or $max_value < $money) $max_value = $money; } if($cnt == 1) $first = $ts; $last = $ts; if($cnt > 0) $data[$ts] = $money; $last_max_value = $money; $o_money = $row['money']; $cnt++; } # print "
"; print_r($data); print "
"; # die; # $last_max_value = $max_value; if(count($data) < $period) $min_value = 0; $total_w = 400; $total_h = 200; ### determine y autoscale (nearest order of magnitude units) $range = $max_value - $min_value; $range = floor( $range / 6 ); $min_value -= $range/2; if($min_value < 0) $min_value = 0; $mag = floor( log10($range) ); $round_nearest = floor($range/pow(10,$mag)) * pow(10,$mag); $x_border = 45; $y_border = 20; $h = $total_h - $y_border; $w = $total_w - $x_border; ### create chart data area $image = imagecreatetruecolor($w, $h); $colors['black'] = imagecolorallocatealpha($image, 0,0,0,0); $colors['white'] = imagecolorallocatealpha($image, 255,255,255,0); $colors['red'] = imagecolorallocatealpha($image, 255,0,0,0); $colors['green'] = imagecolorallocatealpha($image, 0,255,0,0); $colors['blue'] = imagecolorallocatealpha($image, 0,0,255,64); $colors['b_blue'] = imagecolorallocatealpha($image, 0,0,255,0); $colors['grey'] = imagecolorallocatealpha($image, 204,204,204,0); ### chart background color imagefilledrectangle($image, 0, 0, $w-1, $h-1, $colors['white']); # make best fit with spacing $bar_spacing = 4; #minimum $bar_width = floor( ($w - $period*$bar_spacing*2 ) / ($period) ); $bar_spacing += floor(($w - (($bar_width + $bar_spacing) * $period )) / $period); $min = floor($min_value/$round_nearest)*$round_nearest; $max = ceil($max_value/$round_nearest)*$round_nearest; $y_pixel = ($max - $min) / ($h); $time = strtotime($first); $last_time = strtotime($last); #print "$first $time $last $last_time

"; ### draw horizontal grid lines $lines = ($max - $min)/$round_nearest; for($i = 0; $i <= $lines; $i++) { $y = $h/$lines*$i; imageline($image, 0, $y, $w, $y, $colors['grey']); imageline($image, 0, $y, 4, $y, $colors['black']); imageline($image, $w, $y, $w-5, $y, $colors['black']); } ### draw goal lines foreach ($goals as $v1) { $y1 = convertValueToY($y_pixel, $min, $h, $v1); imageline($image, 1, $y1, $w-1, $y1, $colors['red']); } $o_date = ""; $advance = 1; if($unit == 'days') $advance = 24; for ($i = 0; $i <= $period ; $i++) { $x = $period - $i; if($unit == 'days') $key = date('Y-m-d', $last_time - $x*3600*$advance); else $key = date('Y-m-d H:00', $last_time - $x*3600*$advance); $date = date('Y-m-d', $last_time - $x*3600*$advance); if($o_date == "") $o_date = $date; $v1 = $data[$key]; #print "$x $last_time $date $key $v1 $y1
"; $y1 = convertValueToY($y_pixel, $min, $h, $v1); $x2 = $w - $bar_spacing/2+1 - ($bar_width + $bar_spacing) * $x; $x1 = $x2 - $bar_width; imagefilledrectangle($image, $x1, $h-1, $x2, $y1, $colors['blue']); imagerectangle($image, $x1, $h-1, $x2, $y1, $colors['b_blue']); ### new days are separated by a black vertical line, otherwise it's grey $line_color = $colors['grey']; if($o_date != $date) $line_color = $colors['black']; imageline($image, $x1- $bar_spacing/2, $h-1, $x1- $bar_spacing/2, 1,$line_color); $o_date = $date; } ### black border around chart data area imagerectangle($image, 0, 0, $w-1, $h-1, $colors['black']); $f_w = $w+$x_border*2; $f_h = $h+$y_border*2; ### create "whole" image $f_image = imagecreatetruecolor($f_w, $f_h); $f_colors['black'] = imagecolorallocatealpha($f_image, 0,0,0,0); $f_colors['white'] = imagecolorallocatealpha($f_image, 255,255,255,0); $f_colors['red'] = imagecolorallocatealpha($f_image, 255,0,0,0); $f_colors['green'] = imagecolorallocatealpha($f_image, 0,255,0,0); $f_colors['blue'] = imagecolorallocatealpha($f_image, 0,0,255,0); $f_colors['grey'] = imagecolorallocatealpha($f_image, 204,204,204,0); ### make background white imagefilledrectangle($f_image, 0, 0, $f_w, $f_h, $f_colors['white']); ### stick chart into image imagecopymerge($f_image, $image, $x_border, $y_border, 0, 0, $w, $h, 100); ### "cool" dropshadow around chart imageline($f_image, $x_border+2, $f_h-$y_border, $x_border+$w, $f_h-$y_border, $f_colors['grey']); imageline($f_image, $x_border+$w, $y_border+2, $x_border+$w, $y_border+$h, $f_colors['grey']); ### add Y labels on left and right for($i = 0; $i <= $lines; $i++) { $y = $h/$lines * $i; if($round_nearest >= 1000) { $lval = sprintf("%4dK", ($max - $round_nearest*$i)/1000); $rval = sprintf("%-4dK", ($max - $round_nearest*$i)/1000); } else if($round_nearest >= 100) { $lval = $rval = sprintf("%5.1fK", ($max - $round_nearest*$i)/1000); } else if($round_nearest >= 10) { $lval = $rval = sprintf("%6.2fK", ($max - $round_nearest*$i)/1000); } else { $lval = $rval = sprintf("%7.3fK", ($max - $round_nearest*$i)/1000); } imagettftext($f_image, $font_size, 0, 4, $y_border+$y+4, $f_colors['black'], $font, $lval); imagettftext($f_image, $font_size, 0, $f_w-$x_border+2, $y_border+$y+4, $f_colors['black'], $font, $rval); } ### add chart title $title = "Wasteland 2 Kickstarter Progress"; imagettftext($f_image, $font_size, 0, $x_border+2, $y_border-5, $f_colors['red'], $font, $title); ### add subtitle $m_x = strlen($title) * $font_size; $m_x -= 50; $money = number_format($last_max_value); imagettftext($f_image, $font_size, 0, $x_border+2 + $m_x + 2, $y_border-5, $f_colors['blue'], $font, "\$$money [last $period $unit $measure]"); ### add X labels $font_size -= 2; for ($i = 1; $i <= $period ; $i++) { $x = $period - $i; if($unit == 'days') $hr = date('M d', $last_time - $x*3600*$advance); else $hr = date('H:00', $last_time - $x*3600*$advance); $len = strlen($hr) * $font_size; $m_x=0; $x2 = $w - $bar_spacing/2+1 - ($bar_width + $bar_spacing) * $x; $x1 = $x2 - $bar_width; imagettftext($f_image, $font_size, 0, $x1+$x_border+$m_x, $f_h - 5 - 3 , $f_colors['black'], $font, $hr); } header("Content-Type: image/png"); imagesavealpha($f_image, true); imagepng($f_image); function convertValueToY($y_pixel, $min, $h, $value) { $value -= $min; $value /= $y_pixel; $value = $h-$value; return $value; } ?>