redarrow Posted September 15, 2006 Share Posted September 15, 2006 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 31st43210 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. Quote Link to comment Share on other sites More sharing options...
AndyB Posted September 15, 2006 Share Posted September 15, 2006 http://www.builderau.com.au/program/php/soa/Using_GD_functions_in_PHP_to_create_a_bar_chart/0,339028448,339200806,00.htm Quote Link to comment Share on other sites More sharing options...
redarrow Posted September 15, 2006 Author Share Posted September 15, 2006 thank you andyandy all i get is a line but why please help cheers.this is an example from the url provided from andy.[code]<?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);?>[/code] Quote Link to comment Share on other sites More sharing options...
Barand Posted September 15, 2006 Share Posted September 15, 2006 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 Quote Link to comment Share on other sites More sharing options...
redarrow Posted September 15, 2006 Author Share Posted September 15, 2006 ok i got that part going know i need to know how to add months to it sorry but this is really hard cheers. Quote Link to comment Share on other sites More sharing options...
Barand Posted September 15, 2006 Share Posted September 15, 2006 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. Quote Link to comment Share on other sites More sharing options...
redarrow Posted September 15, 2006 Author Share Posted September 15, 2006 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. Quote Link to comment Share on other sites More sharing options...
redarrow Posted September 15, 2006 Author Share Posted September 15, 2006 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(); Quote Link to comment Share on other sites More sharing options...
Barand Posted September 15, 2006 Share Posted September 15, 2006 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.[code]<?phpinclude('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(); ?>[/code] Quote Link to comment Share on other sites More sharing options...
redarrow Posted September 15, 2006 Author Share Posted September 15, 2006 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.[code]<?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;}?>[/code] Quote Link to comment Share on other sites More sharing options...
Barand Posted September 15, 2006 Share Posted September 15, 2006 Life is so much easier with arrays[code]<?phpinclude('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(); ?>[/code] Quote Link to comment Share on other sites More sharing options...
redarrow Posted September 15, 2006 Author Share Posted September 15, 2006 thank you last questionis 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.[code]<?phpdatabase 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); }?>[/code] Quote Link to comment Share on other sites More sharing options...
Barand Posted September 15, 2006 Share Posted September 15, 2006 If you have a table with colnames 'jan', feb, ... , 'dec' then the update code to increment the hit count is simply[code]<?php$colname = date('M'); // get the column name$query = "UPDATE hits SET $colname = $colname+1";mysql_query($query);?>[/code] Quote Link to comment Share on other sites More sharing options...
redarrow Posted September 15, 2006 Author Share Posted September 15, 2006 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? Quote Link to comment Share on other sites More sharing options...
Barand Posted September 15, 2006 Share Posted September 15, 2006 [quote author=redarrow link=topic=108234.msg435247#msg435247 date=1158363632]you see the code i provided before 4 posts up your way does all that in one?[/quote]It does, it but takes 26 lines of code against 3. Do you charge per line of code ;D Quote Link to comment Share on other sites More sharing options...
redarrow Posted September 15, 2006 Author Share Posted September 15, 2006 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. Quote Link to comment Share on other sites More sharing options...
Barand Posted September 15, 2006 Share Posted September 15, 2006 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[code]<?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);?>[/code]Now try this script[code]<?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";?>[/code] Quote Link to comment Share on other sites More sharing options...
redarrow Posted September 16, 2006 Author Share Posted September 16, 2006 dont no how you do that so quickly but thank you well nice?cheers. Quote Link to comment Share on other sites More sharing options...
Barand Posted September 16, 2006 Share Posted September 16, 2006 "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 Quote Link to comment Share on other sites More sharing options...
redarrow Posted September 16, 2006 Author Share Posted September 16, 2006 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 arrayso whats in my database will be the lines total cheers.example only.[code]<?phpdatabase connectionwhile($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";}?>[/code] Quote Link to comment Share on other sites More sharing options...
Barand Posted September 16, 2006 Share Posted September 16, 2006 To pull data from your table and use the bar.php graph method[code]<?php$query = "SELECT jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec FROM hits";$result = mysql_query($query);$data = mysql_fetch_assoc($result);echo "<table>\n";foreach ($data as $month => $hits) { echo "<tr> <td>$month</td> <td><img src='bar.php?val=$hits&max=1000'></td> <td>$hits</td> </tr>\n";}echo "</table>\n";?>[/code] Quote Link to comment Share on other sites More sharing options...
redarrow Posted September 16, 2006 Author Share Posted September 16, 2006 Thank you barand.This is a link so that you can crate a stats graph by month full instructions all thanks to barand. http://freesingles.ath.cx/stats.inc Quote Link to comment Share on other sites More sharing options...
Barand Posted September 16, 2006 Share Posted September 16, 2006 In your database table, all those fields (except id) are hitcounts and therefore numeric. Change them from varchar() to INT. Quote Link to comment Share on other sites More sharing options...
redarrow Posted September 16, 2006 Author Share Posted September 16, 2006 done cheers.i love it your the best thank you.barand can you kindly deatail this even more as i got it on a white board at home and trying to study it but it's really hard i also have you instuctions but that goes far but not as far as i need cheers.ps only if you got time thank you.[code]<?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);?>[/code] Quote Link to comment Share on other sites More sharing options...
Barand Posted September 16, 2006 Share Posted September 16, 2006 I added a few more explantory comments. Hope it helps.[code]<?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 // Called by, for example : <img src='bar.php?val=25&max=100'> // This gets the val and max sent in the query string $val = $_GET['val']; $max = $_GET['max'];// calculate dimensions of inner bar // Check that $max is not 0 as we need to divide by it // Calculate the width of bar to be plotted // As the full width of the bar represents a value of $max we // need total width * ($val / $max) // Since there is a 1 pixel wide border all round, our total width // for the value bar is the image width - 2 // Floor() rounds it down to nearest whole number // Similarly, out bar height is image height - 2 $barw = $max ? floor(($w-2) * $val / $max) : 0; $barh = $h - 2;// draw inner bar // Now draw the bar as a filled rectangle which fits inside the border if ($barw) imagefilledrectangle($im, 1, 1, $barw, $barh, $barcolor);// send image header header("content-type: image/png");// send png image imagepng($im); imagedestroy($im);?>[/code] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.