Jump to content

Graph only showing 1 month


timmah1

Recommended Posts

I'm trying to get a bar graph from total number of hit on a month to month basis.

 

Everything works out find, except the bar graph is only showing 1 month, not everything that's being pulled out of the db.

 

Can anybody look and see what I'm doing wrong?

 

$userid = '44';
$t_month = date("Y-m-d h:i:s");
$y_month = date("Y-m-d h:i:s", strtotime("-1 Year"));

//Grabs Unique Visitors to App Page
$query = "SELECT CONCAT_WS(' ', YEAR(date), MONTHNAME(date)) AS RptDate, COUNT(DISTINCT referrer) 'referrer' 
FROM app_analytics 
WHERE appid = $userid 
AND date BETWEEN '" .$y_month."'  AND  '" .$t_month."' 
GROUP BY CONCAT_WS(' ', YEAR(date), MONTHNAME(date))
ORDER BY YEAR(date), MONTH(date)";

    $result = mysql_query($query) or die(mysql_error());        
        //$totals = array();
        while($row = mysql_fetch_array( $result )){
           
            $months = date("M", strtotime($row['RptDate']));
            $ip = $row['referrer'];  
             
           $values = array("$months" => $ip);
      
    $img_width=450;
$img_height=300; 
$margins=20;


# ---- Find the size of graph by substracting the size of borders
$graph_width=$img_width - $margins * 2;
$graph_height=$img_height - $margins * 2; 
$img=imagecreate($img_width,$img_height);


$bar_width=20;
$total_bars=count($values);
$gap= ($graph_width- $total_bars * $bar_width ) / ($total_bars +1);


# -------  Define Colors ----------------
$bar_color=imagecolorallocate($img,0,64,128);
$background_color=imagecolorallocate($img,240,240,255);
$border_color=imagecolorallocate($img,200,200,200);
$line_color=imagecolorallocate($img,220,220,220);

# ------ Create the border around the graph ------

imagefilledrectangle($img,1,1,$img_width-2,$img_height-2,$border_color);
imagefilledrectangle($img,$margins,$margins,$img_width-1-$margins,$img_height-1-$margins,$background_color);


# ------- Max value is required to adjust the scale	-------
$max_value=max($values);
$ratio= $graph_height/$max_value;


# -------- Create scale and draw horizontal lines  --------
$horizontal_lines=20;
$horizontal_gap=$graph_height/$horizontal_lines;

for($i=1;$i<=$horizontal_lines;$i++){
	$y=$img_height - $margins - $horizontal_gap * $i ;
	imageline($img,$margins,$y,$img_width-$margins,$y,$line_color);
	$v=intval($horizontal_gap * $i /$ratio);
	imagestring($img,0,5,$y-5,$v,$bar_color);

}


# ----------- Draw the bars here ------
for($i=0;$i< $total_bars; $i++){ 
	# ------ Extract key and value pair from the current pointer position
	list($key,$value)=each($values); 
	$x1= $margins + $gap + $i * ($gap+$bar_width) ;
	$x2= $x1 + $bar_width; 
	$y1=$margins +$graph_height- intval($value * $ratio) ;
	$y2=$img_height-$margins;
	imagestring($img,0,$x1+3,$y1-10,$value,$bar_color);
	imagestring($img,0,$x1+3,$img_height-15,$key,$bar_color);		
	imagefilledrectangle($img,$x1,$y1,$x2,$y2,$bar_color);
}

header("Content-type:image/png");
imagepng($img);
}  

 

Thanks in advance

Link to comment
Share on other sites

It looks like you are running all the graph drawing code for every row returned from the database.  Is that the way it is supposed to be working?  Or do you want to read all the data from the database first and then draw the graph in one go?

Link to comment
Share on other sites

Ok, the first thing to do is use consistent indentation.  The "}" which ends the while loop needs to be directly under the "w" from the start of the word "while".  That will make it clear where the end of the loop is.

 

If the end of the loop is a long way away, add a comment after the "}" to tell you which loop it's from.

 

I can't actually tell from your code where the while loop ends, but I suspect it includes all your graph drawing code.

Link to comment
Share on other sites

ok, here is the code, better formatted to understand.

 

It is still only showing 1 month in the graph

<?php

include ($_SERVER['DOCUMENT_ROOT'] . "/inc/config.db.php");

$userid = '44'; #--will be pulled from login after testing
$t_month = date("Y-m-d h:i:s");
$y_month = date("Y-m-d h:i:s", strtotime("-1 Year"));

//Grabs Unique Visitors to App Page
$query = "SELECT CONCAT_WS(' ', YEAR(date), MONTHNAME(date)) AS RptDate, COUNT(DISTINCT referrer) 'referrer' 
FROM app_analytics 
WHERE appid = $userid 
AND date BETWEEN '" .$y_month."'  AND  '" .$t_month."' 
GROUP BY CONCAT_WS(' ', YEAR(date), MONTHNAME(date))
ORDER BY YEAR(date), MONTH(date)";

    $result = mysql_query($query) or die(mysql_error());        
        //$totals = array();
        while($row = mysql_fetch_array( $result )){
           
            $months = date("M", strtotime($row['RptDate']));
            $ip = $row['referrer'];  
             
           $values = array("$months" => $ip);
           
        } #-- end while loop
      
    $img_width=450;
$img_height=300; 
$margins=20;

# ---- Find the size of graph by substracting the size of borders
$graph_width=$img_width - $margins * 2;
$graph_height=$img_height - $margins * 2; 
$img=imagecreate($img_width,$img_height);

$bar_width=20;
$total_bars=count($values);
$gap= ($graph_width- $total_bars * $bar_width ) / ($total_bars +1);

# -------  Define Colors ----------------
$bar_color=imagecolorallocate($img,0,64,128);
$background_color=imagecolorallocate($img,240,240,255);
$border_color=imagecolorallocate($img,200,200,200);
$line_color=imagecolorallocate($img,220,220,220);

# ------ Create the border around the graph ------
imagefilledrectangle($img,1,1,$img_width-2,$img_height-2,$border_color);
imagefilledrectangle($img,$margins,$margins,$img_width-1-$margins,$img_height-1-$margins,$background_color);

# ------- Max value is required to adjust the scale	-------
$max_value=max($values);
$ratio= $graph_height/$max_value;

# -------- Create scale and draw horizontal lines  --------
$horizontal_lines=20;
$horizontal_gap=$graph_height/$horizontal_lines;

for($i=1;$i<=$horizontal_lines;$i++){
	$y=$img_height - $margins - $horizontal_gap * $i ;
	imageline($img,$margins,$y,$img_width-$margins,$y,$line_color);
	$v=intval($horizontal_gap * $i /$ratio);
	imagestring($img,0,5,$y-5,$v,$bar_color);

}


# ----------- Draw the bars here ------
for($i=0;$i< $total_bars; $i++){ 
	# ------ Extract key and value pair from the current pointer position
	list($key,$value)=each($values); 
	$x1= $margins + $gap + $i * ($gap+$bar_width) ;
	$x2= $x1 + $bar_width; 
	$y1=$margins +$graph_height- intval($value * $ratio) ;
	$y2=$img_height-$margins;
	imagestring($img,0,$x1+3,$y1-10,$value,$bar_color);
	imagestring($img,0,$x1+3,$img_height-15,$key,$bar_color);		
	imagefilledrectangle($img,$x1,$y1,$x2,$y2,$bar_color);
}

header("Content-type:image/png");
imagepng($img);  
?>

Link to comment
Share on other sites

Ok that's an improvement on the previous version.  Are you familiar with arrays?  What you need to do is add one item into the $values array each time around the while loop.  For example, it might look like this:

 

        while($row = mysql_fetch_array( $result )){
           
            $months = date("M", strtotime($row['RptDate']));
            $ip = $row['referrer'];  
             
           $values[] = array("$months" => $ip);
           
        } #-- end while loop

 

At the end of this you can insert the following line to debug:

 

print "<pre>"; var_dump($values); exit;

 

If it all looks correct, remove the line and continue coding.

Link to comment
Share on other sites

Thank you.

No, I'm not very familiar with array's. I'm trying to learn more about them

 

The code puts out what's it suppose too

array(10) {
  [0]=>
  array(1) {
    ["Jun"]=>
    string(2) "61"
  }
  [1]=>
  array(1) {
    ["Jul"]=>
    string(4) "1156"
  }
  [2]=>
  array(1) {
    ["Aug"]=>
    string(3) "430"
  }
  [3]=>
  array(1) {
    ["Sep"]=>
    string(3) "205"
  }
  [4]=>
  array(1) {
    ["Oct"]=>
    string(3) "151"
  }
  [5]=>
  array(1) {
    ["Nov"]=>
    string(3) "165"
  }
  [6]=>
  array(1) {
    ["Dec"]=>
    string(2) "96"
  }
  [7]=>
  array(1) {
    ["Jan"]=>
    string(3) "175"
  }
  [8]=>
  array(1) {
    ["Feb"]=>
    string(3) "148"
  }
  [9]=>
  array(1) {
    ["Mar"]=>
    string(2) "30"
  }
}

 

Now I'm getting an error, and to be honest, I have no idea how to fix it.

 

The error is this

Fatal error: Unsupported operand types in /home/local/public_html/members-new/checkip1.php on line 80

 

And line 80 is this

$y1=$margins +$graph_height- intval($value * $ratio) ;

 

and that's inside this for loop

for($i=0;$i< $total_bars; $i++){ 
	# ------ Extract key and value pair from the current pointer position
	list($key,$value)=each($values); 
	$x1= $margins + $gap + $i * ($gap+$bar_width) ;
	$x2= $x1 + $bar_width; 
	$y1=$margins +$graph_height- intval($value * $ratio) ;
	$y2=$img_height-$margins;
	imagestring($img,0,$x1+3,$y1-10,$value,$bar_color);
	imagestring($img,0,$x1+3,$img_height-15,$key,$bar_color);		
	imagefilledrectangle($img,$x1,$y1,$x2,$y2,$bar_color);
}

Link to comment
Share on other sites

That line looks sensible to me.  Can you insert this line just before it please:

 

print "Margins: " ; var_dump($margins); print "graph_height: " ; var_dump($graph_height); print "value: " ; var_dump($value) ; print "ratio: " ; var_dump($ratio);

 

With any luck that will show you that one of those variables is not what it should be.

Link to comment
Share on other sites

Taking this code

print "<pre>Margins: " ; var_dump($margins); print "graph_height: " ; var_dump($graph_height); print "value: " ; var_dump($values) ; print "ratio: " ; var_dump($ratio);

and running it before the error, this is what prints out

 


Margins: int(20)
graph_height: int(260)
value: array(10) {
  [0]=>
  array(1) {
    ["Jun"]=>
    string(2) "61"
  }
  [1]=>
  array(1) {
    ["Jul"]=>
    string(4) "1156"
  }
  [2]=>
  array(1) {
    ["Aug"]=>
    string(3) "430"
  }
  [3]=>
  array(1) {
    ["Sep"]=>
    string(3) "205"
  }
  [4]=>
  array(1) {
    ["Oct"]=>
    string(3) "151"
  }
  [5]=>
  array(1) {
    ["Nov"]=>
    string(3) "165"
  }
  [6]=>
  array(1) {
    ["Dec"]=>
    string(2) "96"
  }
  [7]=>
  array(1) {
    ["Jan"]=>
    string(3) "175"
  }
  [8]=>
  array(1) {
    ["Feb"]=>
    string(3) "148"
  }
  [9]=>
  array(1) {
    ["Mar"]=>
    string(2) "36"
  }
}
ratio: float(4.26229508197)


Fatal error:  Unsupported operand types in /home/local/public_html/members-new/checkip1.php on line 80

 

and the code producing the error

for($i=0;$i< $total_bars; $i++){ 
	# ------ Extract key and value pair from the current pointer position
	list($key,$value)=each($values); 
	$x1= $margins + $gap + $i * ($gap+$bar_width) ;
	$x2= $x1 + $bar_width; 
	$y1=$margins +$graph_height- intval($values * $ratio) ;  #<--LINE 80
	$y2=$img_height-$margins;
	imagestring($img,0,$x1+3,$y1-10,$value,$bar_color);
	imagestring($img,0,$x1+3,$img_height-15,$key,$bar_color);		
	imagefilledrectangle($img,$x1,$y1,$x2,$y2,$bar_color);
}

Link to comment
Share on other sites

Checking some other things, I'm getting an error here as well

Fatal error: Unsupported operand types in /home/local/public_html/members-new/checkip1.php on line 59

$max_value=max($values);
$ratio= $graph_height/$max_value;

Link to comment
Share on other sites

The first time you posted line 80 it said intval($value * $ratio), but the second time you posted it it said intval($values * $ratio).  It needs to be $value, not $values.

 

Whenever you have the error about unsupported operand types, use var_dump() to print out the values you are using in that line.  You should find that the values aren't what you expect them to be.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.