get_results("SELECT abs_dat, count(*) AS cnt FROM ".$wpdb->prefix."ad_buttons_stats
WHERE abs_view <> 0 GROUP by abs_dat");
$click_counter = $wpdb->get_results("SELECT abs_dat, count(*) AS cnt FROM ".$wpdb->prefix."ad_buttons_stats
WHERE abs_view = 0 GROUP by abs_dat");
$stat_values = array(array());
foreach($view_counter as $view){
$stat_values[$view->abs_dat]['views']= $view->cnt;
if($view->cnt > $max_view){
$max_view = $view->cnt;
}
}
foreach($click_counter as $click){
$stat_values[$click->abs_dat]['clicks']= $click->cnt;
if($click->cnt > $max_click){
$max_click = $click->cnt;
}
}
if(ceil($max_view/200)>floor(200/$max_view)){
$view_scale = ceil($max_view/200);
$view_scale_dir = 'div';
}else{
$view_scale = floor(200/$max_view);
$view_scale_dir = 'mul';
}
if(ceil($max_click/200)>floor(200/$max_click)){
$click_scale = ceil($max_click/200);
$click_scale_dir = 'div';
}else{
$click_scale = floor(200/$max_click);
$click_scale_dir = 'mul';
}
$statdays = array_keys($stat_values);
//echo"
view scale: $view_scale ($view_scale_dir) click scale: $click_scale ($click_scale_dir)
";
//echo "
clicks: ".$stat_values['2009-09-01']['clicks']."
";
//print_r($stat_values);
//echo"
max clicks: $max_click, max views: $max_view";
//echo"
graph height: 200px -> ".$max_view / 200 ."units/px or ". 200 / $max_view." px/unit";
//echo"
we'll use ". ceil($max_view / 200) ."units/px";
// set the HTTP header type to PNG
header("Content-type: image/png");
$days = monthdays(09,2009);
//echo "days 09: $days";
// set the width and height of the new image in pixels
$width = 640;
$height = 220;
$graph_width = 20 * $days;
$graph_height = 200;
// create a pointer to a new true colour image
$im = imagecreatetruecolor($width, $height);
// switch on image antialising if it is available
imageantialias($im, true);
// define colours
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
$blue = imagecolorallocate($im, 0, 0, 255);
$light_grey = imagecolorallocate($im, 230, 230, 230);
// sets background to white
imagefilltoborder($im, 0, 0, $white, $white);
// define the dimensions of our rectangle
//$r_width = 150;
//$r_height = 100;
$r_x = 10;
$r_y = 10;
imagerectangle($im, $r_x, $r_y, $r_x+$graph_width, $r_y+$graph_height, $black);
// define the dimensions of our filled rectangle
$r_width = 18;
$r_height = 100;
$r_x = 11;
$r_y = 110;
$day = 1;
$statdays = array_keys($stat_values);
//echo "day: ".$day." date: ".$statdays[$day]." views: ".$stat_values[$statdays[$day]]['views']." clicks: ".$stat_values[$statdays[$day]]['clicks'];
while($count<$days){
$count = $count + 1;
if($view_scale_dir == 'mul'){
$r_height = $stat_values[$statdays[$count]]['views'] * $view_scale;
}else{
$r_height = round($stat_values[$statdays[$count]]['views'] / $view_scale);
}
$r_y = 209 - $r_height;
imagefilledrectangle($im, $r_x, $r_y, $r_x+$r_width, $r_y+$r_height, $light_grey);
$r_x = $r_x + 20;
}
$count = 0;
$r_x = 20;
while($count<$days - 1){
$count = $count + 1;
if($click_scale_dir == 'mul'){
$r_y = 210 - $stat_values[$statdays[$count]]['clicks'] * $click_scale;
$r_y2 = 210 - $stat_values[$statdays[$count + 1]]['clicks'] * $click_scale;
}else{
$r_y = round($stat_values[$statdays[$count]]['clicks'] / $click_scale);
$r_y2 = round($stat_values[$statdays[$count + 1]]['clicks'] / $click_scale);
}
// $r_y = 210 - $stat_values[$count];
// $r_y2 = 210 - $stat_values[$count + 1];
// make a new line and add it to the image
imageline($im, $r_x, $r_y, $r_x + 20, $r_y2, $black);
// draw a dot at the line start
imagefilledellipse($im, $r_x, $r_y, 5, 5, $black);
$r_x = $r_x + 20;
}
// send the new PNG image to the browser
imagepng($im);
// destroy the reference pointer to the image in memory to free up resources
imagedestroy($im);
?>