Jump to content


Photo

php graph


  • Please log in to reply
24 replies to this topic

#1 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 15 September 2006 - 09:20 PM

advance thank you.

Can you please advise me i have been reading on creating a graph and i am stuck.

i need to design a grapth that will have the number of months on and show the number of hits on that month.

example.
ect date till 31st
4
3
2
1
0
  jan feb march april may jun july aug sep oct nov dec

can it be done were do i go to get a proper tutoral to use to get this together in php cheers.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 15 September 2006 - 09:26 PM

http://www.builderau...39200806,00.htm
Legend has it that reading the manual never killed anyone.
My site

#3 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 15 September 2006 - 09:35 PM

thank you andy

andy all i get is a line but why please help cheers.

this is an example from the url provided from andy.

<?php

$bar_w = 100;
$max_y = 300;
$margin_top = 20;
$margin_bottom = 20;
$margin_left = 20;
$margin_right = 20;
$y_div = 10;
$rects = array();

$last_x2 = $margin_left;
$i = 1;
$pt = "pt".$i;
while (isset($_GET[$pt])) {
	$x1 = $last_x2 + 1;
	$x2 = $x1 + $bar_w;
	$y1 = $margin_top + $max_y - $_GET[$pt];
	$y2 = $margin_top + $max_y;
	$ar = array($x1, $y1, $x2, $y2);
	array_push($rects, $ar);
	$i++;
	$last_x2 = $x2;
	$pt = "pt".$i;
}
$img_w = $last_x2 + $margin_right;
$img_h = $margin_top + $max_y + $margin_bottom;

$ih = imagecreate($img_w, $img_h);

$black = imagecolorallocate($ih, 0, 0, 0);
$white = imagecolorallocate($ih, 255, 255, 255);

imagefill($ih, 0, 0, $white);
for ($r = 0; $r < count($rects); $r++) {
	$red = rand(0, 255);
	$green = rand(0, 255);
	$blue = rand(0, 255);
	$hist_color = imagecolorallocate($ih, $red, $green, $blue);
	imagefilledrectangle($ih, $rects[$r][0], $rects[$r][1], $rects[$r][2], $rects[$r][3], $hist_color);
	imageline($ih, $rects[$r][2], $margin_top + $max_y, $rects[$r][2], $margin_top + $max_y + 3, $black);
	$ttfbox = imagettfbbox(8, 0, 'c:\winnt\fonts\arial.ttf', "pt" . ($r + 1));
	$half_pt = ($bar_w/2) - ceil(($ttfbox[4] - $ttfbox[6])/2);
	imagettftext($ih, 8, 0, $rects[$r][0] + $half_pt, $rects[$r][3] + 10, $black, 'c:\winnt\fonts\arial.ttf', "pt" . ($r 

+ 1));
} 

imageline($ih, $margin_left, $margin_top, $margin_left, $margin_top + $max_y + 3, $black);
imageline($ih, $margin_left, $margin_top + $max_y, $last_x2, $margin_top + $max_y, $black);
$tick = 0;
while ($tick < $max_y) {
	$tick += $y_div;
	$tick_y = $margin_top + $max_y - $tick;
	imageline($ih, $margin_left - 3, $tick_y, $margin_left, $tick_y, $black);
}

imagepng($ih);
imagedestroy($ih);

?>

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 15 September 2006 - 10:02 PM

You need to provide some data. Save as "graph.php", say then in another page, put

<img src="graph.php?pt1=100&pt2=50&pt3=60">

This will provide it with 3 data points to plot
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#5 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 15 September 2006 - 10:10 PM

ok i got that part going know i need to know how to add months to it sorry but this is really hard cheers.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#6 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 15 September 2006 - 10:18 PM

That script is a bit limiting in that it only accepts data in the form "PtN=value" and only echos "Pt2, Pt2 ... PtN" as x-axis values.

The baaChart link in my sig will give you more flexibility.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#7 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 15 September 2006 - 10:24 PM

what i am going to do is this if it's possable and thank you.

setup a database feild called chart_data in chart data have the user id then jan to dec and every time a user looks at a profile update the correct current month to increment by one and then show the information via a chart.

is that possable with your link and thank you.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#8 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 15 September 2006 - 10:34 PM

like this example m8 from your link but how does it get the results from the database.?

include('baaChart.php');
$mygraph = new baaChart(600);
$mygraph->setTitle('Regional Sales','Jan - Jun 2002');
$mygraph->setXLabels("Jan,Feb,Mar,Apr,May,Jun");
$mygraph->addDataSeries('C',COLS_STACKED,"25,30,35,40,30,35","South");
$mygraph->addDataSeries('C',0,"65,70,80,90,75,48","North");
$mygraph->addDataSeries('C',0,"12,18,25,20,22,30","West");
$mygraph->addDataSeries('C',0,"50,60,75,80,60,75","East");
$mygraph->addDataSeries('L',3,"30,45,50,55,52,60","Europe");
$mygraph->setBgColor(0,0,0,1);  //transparent background
$mygraph->setChartBgColor(0,0,0,1);  //as background
$mygraph->setXAxis("Month",1);
$mygraph->setYAxis("Sales (£000)",0,250,50,1);
$mygraph->drawGraph();

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#9 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 15 September 2006 - 10:46 PM

That example was for a stacked column chart. You would only need a simpler single series chart.

Easiest way is to read the data into an array , value for each month.

<?php
include('baaChart.php');

$data = array (
        300, 500, 600, 450, 400, 200, 750, 670, 420, 375, 250, 400
    );

   $mygraph = new baaChart(600);
   $mygraph->setTitle('Hits per Month');
   $mygraph->setXLabels("Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec");
   $mygraph->addDataSeries('C',0,$data,"Hits");
   $mygraph->setXAxis("Month",1);
   $mygraph->setYAxis("Hits",0,250,50,1);
   $mygraph->drawGraph();  
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#10 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 15 September 2006 - 11:00 PM

thank you as you can see from my code below each month will get a hit depending on the month it is, so how can i set your code up to also output the correct result.

thank you so much for your help.


<?php

$month=date("m");

$stat="15";

if($month=="01"){

$jan=$jan+1;

}elseif($month=="02"){

$feb=$feb+1;

}elseif($month=="03"){

$mar=$mar+1;

}elseif($month=="04"){

$apr=$apr+1;

}elseif($month=="05"){

$may=$may+1;

}elseif($month=="06"){

$jun=$jun+1;

}elseif($month=="07"){

$jul=$jul+1;

}elseif($month=="08"){

$aug=$aug+1;

}elseif($month=="09"){

$sep=$sep+1;

}elseif($month=="10"){

$oct=$oct+1;

}elseif($month=="11"){

$nov=$nov+1;

}elseif($month=="12"){

$dec=$dec+1;
}

?>

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#11 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 15 September 2006 - 11:07 PM

Life is so much easier with arrays

<?php
include('baaChart.php');

$data = array (
        300, 500, 600, 450, 400, 200, 750, 670, 420, 375, 250, 400
    );
    
$month = date('n')-1;      // get current month number
$data[$month]++;         // increment that month's count by 1

   $mygraph = new baaChart(600);
   $mygraph->setTitle('Hits per Month');
   $mygraph->setXLabels("Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec");
   $mygraph->addDataSeries('C',0,$data,"Hits");
   $mygraph->setXAxis("Month",1);
   $mygraph->setYAxis("Hits",0,1000,100,1);
   $mygraph->drawGraph();  
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#12 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 15 September 2006 - 11:26 PM

thank you last question

is this the correct code that will update the correct database field
this would be for the update page for the stats but does it look ok cheers.

<?php

database connection

$update_array=array("jan","feb","mar","apr","may","jun","jul"
,"aug","sep","oct","nov","dec");

$date=date("m");

if($date==$update_array){

$update_array=$update_array+1;

$query="UPDATE hits set jan='$jan', feb='$feb', mar='$mar', 
apr='$apr',may='$may',jun='$jun',jul='$jul',aug='$aug',sep='$sep'
oct='$oct',nov='$nov',dec='$dec'";

$result=mysql_query($query); 

}

?>

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#13 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 15 September 2006 - 11:33 PM

If you have a table with colnames 'jan', feb, ... , 'dec' then the update code to increment the hit count is simply
<?php
$colname = date('M');   // get the column name

$query = "UPDATE hits SET $colname = $colname+1";

mysql_query($query);
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#14 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 15 September 2006 - 11:40 PM

so does that mean that the colname of abrevated date will update any date abrevation in the database.

very clever thank you never new that

you see the code i provided before 4 posts up your way does all that in one?
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#15 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 15 September 2006 - 11:49 PM

you see the code i provided before 4 posts up your way does all that in one?


It does, it but takes 26 lines of code against 3. Do you charge per line of code ;D
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#16 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 15 September 2006 - 11:54 PM

Thank you so much your such a good php mysql programmer, what you show me tonight as i am in the uk is about 1 book but you show me in a few sentences thank you.

as your such a good guru can you kindly if you got time point me in the correct direction to learn joins as i find them so confuessing.

once agin thank you and all the best redarrow.


Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#17 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 15 September 2006 - 11:56 PM

If you want a quick and easy way to produce a graph, save this code as "bar.php" in same folder as the script below it
<?php
// set dimensions
     $w = 300;
     $h = 12;
// create image
     $im = imagecreate($w, $h);
// set colours to be used
     $bg = imagecolorallocate($im, 0xE0, 0xE0, 0xE0);
     $black = imagecolorallocate($im, 0x00, 0x00, 0x00);
     $barcolor  = imagecolorallocate($im, 0xFF, 0xFF, 0x00);
// draw border
     imagerectangle($im, 0,0,$w-1,$h-1,$black);
// get value and max value from query string
     $val = $_GET['val'];
     $max = $_GET['max'];
// calculate dimensions of inner bar
     $barw = $max ? floor(($w-2) * $val / $max) : 0;
     $barh = $h - 2;
// draw inner bar
	 if ($barw)
     	imagefilledrectangle($im, 1, 1, $barw, $barh, $barcolor);
// send image header
     header("content-type: image/png");
// send png image
     imagepng($im);
     imagedestroy($im);
?>

Now try this script

<?php
$data = array (
        300, 500, 600, 450, 400, 200, 750, 670, 420, 375, 250, 400
    );
$months = array (
        'Jan', 'Feb', 'Mar',
        'Apr', 'May', 'Jun',
        'Jul', 'Aug', 'Sep',
        'Oct', 'Nov', 'Dec',
    );
    
echo "<table>\n";
foreach ($data as $m => $hits) {
    echo "<tr>
            <td>{$months[$m]}</td>
            <td><img src='bar.php?val=$hits&max=1000'></td>
            <td>$hits</td>
            </tr>\n";
}
echo "</table>\n";
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#18 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 16 September 2006 - 12:01 AM

dont no how you do that so quickly but thank you well nice?

cheers.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#19 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 16 September 2006 - 12:06 AM

"bar.php" is one I use frequently in my scripts when I produce a table of values. I just add an extra column to the table and drop it in so values can be compared more easily.

So all I had to to do was create the sample 2nd script for you
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#20 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 16 September 2006 - 12:13 AM

thank you i get what you mean know and yes i will use you code as provided and try to learn the other page you made but i must say looks grate thank you.

you see the first array is it possable to get from the database all the information in that array

so whats in my database will be the lines total cheers.
example only.


<?php

database connection

while($record=mysql_fetch_assoc($result)){

$jan=$record['jan'];

$data = array ($jan, 500, 600, 450, 400, 200, 750, 670, 420, 375, 250, 400
    );
$months = array (
        'Jan', 'Feb', 'Mar',
        'Apr', 'May', 'Jun',
        'Jul', 'Aug', 'Sep',
        'Oct', 'Nov', 'Dec',
    );
    
echo "<table>\n";
foreach ($data as $m => $hits) {
    echo "<tr>
            <td>{$months[$m]}</td>
            <td><img src='bar.php?val=$hits&max=1000'></td>
            <td>$hits</td>
            </tr>\n";
}
echo "</table>\n";
}
?>

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users