PHP


How to create bar graph in PHP with dynamic scaling
By Junaid Shabbir
06-Aug-08
Views: 77432

Hi everyone, this tutorial will help you in creating a bar graph from PHP with the ability to adjust the scale depending upon the values provided. The technique used is smart enough to handle the number and range of values
 
How to create bar graph with dynamic scaling (Page 1 of 1)
Hi everyone, this tutorial will help you in creating a bar graph from PHP with the ability to adjust the scale depending upon the values provided. The technique used is smart enough to handle the number and range of values. A preview of the graph is shown below

All you need to understand this tutorial is the knowledge of following PHP image functions in addition the following PHP functions are also used and offcourse some mathematics as well If you have a good understanding of all these function, its good to go otherwise you should click on function which is new to you to consult the documentation and get back after learning all these functions

Lets get started
First of all declare an array of values for the graph. Declare these values in the form of associative array (i.e key and value pairs). Keys will be shown at the bottom as the graph legend and the values will be used to draw bars.

 $values=array(
	"Jan" => 110,
	"Feb" => 130,
	"Mar" => 215,
	"Apr" => 81,
	"May" => 310,
	"Jun" => 110,
	"Jul" => 190,
	"Aug" => 175,
	"Sep" => 390,
	"Oct" => 286,
	"Nov" => 150,
	"Dec" => 196
); 

Now define the size of image, i have used an image of size 600x400 for this tutorial.

 $img_width=600;
 $img_height=400; 

The graph we are going to create has a border around it, i have declared a variable $margins to create that border around the four sides.

 $margins=20;

Now find the size of graph by subtracting the size of borders.

 $graph_width=$img_width - $margins * 2;
 $graph_height=$img_height - $margins * 2; 

Create an image of the size defined above

 $img=imagecreate($img_width,$img_height);

Define the width of bar. Gap between the bars will depend upon the width and number of bars and the gaps will be one more than the total number of bars as there is gap on the right and left of the graph. You can see in our example, we have 12 bars but 13 gaps, thats why you see ($total_bars+1) in the denominator.

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

Define colors to be used in the graph

 $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 a border around the graph by filling in rectangle

 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); 

Now the maximum value is required to adjust the scale. Ratio is calculated by dividing graph height by maximum graph value. Each value will be multiplied with ratio, so that no bar goes beyond the graph height.

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

Drawing horizontal lines is optional, note that the margin variable is subtracted from image height so that first line is positioned inside the graph area (Discarding the margins). If you have trouble understanding this code, use a paper and pencil to manually find the values of variable at each repitition of the loop

 $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);
 }

Here comes the most crucial part of our graph, drawing the bars. Each of the 8 lines in the for loop are individually explained below
  1. Extract key and value pair from the current pointer position, each iteration of loop moves the internal pointer of array to the next entry
  2. The x1 value (i.e. left) of each bar gets and increment by $gap+$bar_width with each iteration of loop
  3. The x2 value (i.e. right) is calculated by adding bar width with x1
  4. y1 is the top of each bar. ratio is multiplied with individual values to mare sure that bars remain inside the graph boundries.
  5. y2 (i.e bottom) is fix for all bars. Can also be placed outside the loop
  6. Draw the graph with calculated left, top, right and bottom positions
  7. The numeric value of each bar is shown at the top. Some plus or minus will be required to center align the displayed value with the bar
  8. Display the legend i.e. Month names

 for($i=0;$i< $total_bars; $i++){
	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;
	imagefilledrectangle($img,$x1,$y1,$x2,$y2,$bar_color);
	imagestring($img,0,$x1+3,$y1-10,$value,$bar_color);
	imagestring($img,0,$x1+3,$img_height-15,$key,$bar_color);
} 

Show the graph as a png image

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

The whole script is givenbelow, just copy and enjoy your own php graphs

<?
	# ------- The graph values in the form of associative array
	$values=array(
		"Jan" => 110,
		"Feb" => 130,
		"Mar" => 215,
		"Apr" => 81,
		"May" => 310,
		"Jun" => 110,
		"Jul" => 190,
		"Aug" => 175,
		"Sep" => 390,
		"Oct" => 286,
		"Nov" => 150,
		"Dec" => 196
	);

 
	$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);

?>
Comments
Chris
[02-Sep-2008]
#1

Very nice example, thanks a lot.

Ranjini
[27-Aug-2008]
#2

superb,i was searching for this for a very long period, the code helped me to complete the task, good work

Tejas Mehta
[16-Sep-2008]
#3

This awesome. i thank Junaid Shabbir for this

eijaz sheikh
[26-Sep-2008]
#4

Salaam Alaikum & Ramadan Mubarak. Its an Awesome tutorial! Thnx :) BTW, i am trying to get hold of a 3D horizontal stack graph script inorder to include it into my php code. I have an 3 Associative arrays : (where Key is Empcode & Value is the value). 1 for BudgetHours 1 for CompletedHours 1 for AvailableHours (i.e. BudgetHours - CompletedHours) So i need a 3D Horizontal Stack Bar graph to display per Empcode. Is this possible & if so have u written somewhat similar code for it? Please reply ASAP. Allah Haafiz. Eijaz Sheikh

Unreal Media
[30-Sep-2008]
#5

Excellent. I might use this for my hit counter script.

fauzi
[14-Oct-2008]
#6

I'm not having a successfull trial, there's an error on the script line 82, which is header("Content-type:image/png"); and the output ar weard symbols.thank you very much sir

Hafiz
[08-Oct-2008]
#7

Nice Tutorial. but there is a drawback. every thing will be the image and as it will be rendered through the php so this will take a little more time too. It is good for programmers but I suggest that just do the calculation and database relative thing from php but use CSS for making images. Use PHP where you need some sort of curves.

shree Rijal
[18-Oct-2008]
#8

its awesome working gr8 Thanks! shree

Mujie
[20-Oct-2008]
#9

Thumbs up, great article. Its make me clear.

UMAKANT
[21-Oct-2008]
#10

totorial of graph

Vesko Ivanov
[21-Oct-2008]
#11

The script is very helpful. Thanks.

rano
[16-Dec-2008]
#12

Thanks a lot... It's more easy script

sreesreedhar
[09-Jan-2009]
#13

Thank you very much.I am trying for this code for a long time.finally i got it

Herman
[20-Jan-2009]
#14

Perfect tutorial! it will help me creating dynamic statistical graphs for my site.

sanmehmi
[29-Jan-2009]
#15

Hi! This is working very fine and its very easy. Thanks

Seenivasan
[04-Mar-2009]
#16

Hi this script is very useful to create our own logic...very helpful things...Thanks..

Chris Preston
[15-Mar-2009]
#17

Its great but do you have an example where i could take the data from a table hence different person can view there sales at any time

Vishwa
[17-Mar-2009]
#18

Thanks for the tutorial.

John
[01-Jun-2009]
#19

How this coding running? Easyphp or else?

amir meysami
[17-May-2009]
#20

Thank you man .

Rishikesh
[25-May-2009]
#21

this very nice script. but when i used this script in ajax page it is not working i. can anyone help me.

Todd
[02-Jun-2009]
#22

Thank you for providing this code, it helped me accomplish something I needed to achieve fairly quickly. The one major modification I made was the ability to round the maximum value to a next higher logical value, and to draw the horizontal lines to represent humanly logical blocks of the rounded number (ie, increments of 5 for a maximum round of 25, resulting in 5,10,15,20,25) $maxval = 48; $maxy = 0; $step = 0; $lines = 5; $pattrn = array(1,2,5,10,20,25); $i=0; $j=1; while($maxy < $maxval) { $step = $pattrn[$i]; $maxy = $step * $lines; $i++; if($i == count($pattrn)) { $j = ($j * 10); for($k=0; $kMaxVal : {$maxval}

"; echo "

MaxY : {$maxy}

"; for($i=1; $i<=$lines; $i++) { echo "Line {$i} : ".($i * $step)."
"; } Enter any integer value as $maxval and observe the results... this can easily be incorporated into your original code for easier readability. Thanks again :-)

leyan
[04-Jun-2009]
#23

I tried to embed this code in one of my site but header function does not allow to display any other text . Can any one help me how to embed this code in any page .

maddy
[12-Jun-2009]
#24

nice script

sumit
[12-Jun-2009]
#25

mozilla firefox display an error msg that code contains an error

itnagusak
[24-Jun-2009]
#26

the graph is not being displayed. when i right click and say view image it says "the image cannot be displayed because it contains errors"

daniel
[12-Jul-2009]
#27

this is a great piece of code! however I am trying to have the image embedded half-way down a page with other text, and this is creating the "the image cannot be displayed because it contains errors". Does anyone know to display this within a page? It needs to have the image/png header to show the graphic... but then that's also what is preventing it from appearing within another page...

Dan
[12-Jul-2009]
#28

I figured it out, you can add extra parameters to the imagepng function so it creates an image and saves on the server instead of outputting it to the browser window. So you can modify that line to say: $temp_chart_file_name = "temp_images/chart1.png"; imagepng($img, $temp_chart_file_name,0); I did this, and then just used the whole script as an include file and deleted: header("Content-type:image/png"); so I could continue with the other output I had on my page. Then after the reference to the include file I just included a link to the temp file on the server: I included the file multiple times to chart different arrays I was loading dynamically. Make sure you unset($values) at the end of the include file if you do this otherwise the arrays will append from each time you use the include which messes up all the subsequent charts. Thanks again for the code to get me started on dynamic graphs!

Susan
[16-Jul-2009]
#29

This is great. Any idea how I can do stacked bar graphs, line graphs or pie charts?

Jinto Jose
[18-Jul-2009]
#30

Thanks for the code............... but i have some problem when this page is loaded with jquery........... the picture is not displayed instead it displays some characters on the screen.........can u help me.........

simon
[18-Jul-2009]
#31

Jinto Jose Read comment #27 and #28, the some one has already resolved this problem

David
[31-Jul-2009]
#32

is there any way to change the fonts used on this graph?

Joy
[11-Sep-2009]
#33

Thanks for this code but I have one problem that how can i change the bar scale means for 100 height of bar I want it should display little bit small.

Rashmi
[25-Sep-2009]
#34

Thanks a Lot

MikeH
[06-Oct-2009]
#35

Very insightful article but I am having the same frustrations as some of the others had. First had the 'header' issue and thanks to Dan Cmt# 28 who suggestion seemed to got me one step closer leaves me with one more error: Warning: imagepng() [function.imagepng]: Unable to open 'temp_images/chart1.png' for writing: No such file or directory in C:xampplitehtdocsgraph.php on line 78 I subbed out the header code but it looks like im not creating the image. any suggestions? Thanks

Mehedi Hasan
[21-Oct-2009]
#36

Its awesome ... thanks a lot... :D

Kevin
[21-Oct-2009]
#37

After playing around with versions requiring gdchart and other complicated set ups, this was a brilliant piece of coded tutorial and just what I needed to create some simple graphs for this new website - http://www.tuneseeker.co.uk. Thanks for this.

selva
[04-Nov-2009]
#38

Hi there.. The graph is useful for me. But im having a problem. I cant display the graph with the content of the page. It only show the grpah. I have header and footer. Everything is missing. Only the graph is showing,Is there any way for me to display the graph with the content. Hoping for ur answer. Thanks.

benz
[04-Nov-2009]
#39

Instead of creating temp image file, why don't just reference above script in tag? It's like, copy the whole script and paste it into graph.php. Then insert it into other page using graph. To pass the array, just pass it via session. sumthing like " . But you have to modified above code to retrieve array in $_SESSION[$_GET['gid'].'']. or maybe just pass it as parameter (ie. ... well, it's up to your imagination(not really difficult to make it 'dynamic'). But it would be difficult if you want to include textual content inside code above directly, inside single php, as source code above was design to stream the image. Just use another php to do that. You can, but you have to make it something like this,

Here is your text

graph

Using this, you can include html text content and codes above..

This is a nice code..

Nathan Lyle
[10-Nov-2009]
#40

Great code, the one thing I wasn't able to figure out though was how to change the scale to zoom in closer on a range of numbers. With it as it is, if the set of numbers are too close (for example, 8, 8.5, 9) it would be nice to be able to change the scale to have a less even set of bars.

Sanjay
[14-Nov-2009]
#41

Very good job. Very useful, Great work. Qualitycodes you are dooing a very good job .. ROGER(COUNTER STRIKE)

chandru
[20-Nov-2009]
#42

Hi this is very useful to me... After a long time i find it.. thanks to u....

joseph kurt leonardo
[15-Dec-2009]
#43

very nice indeed. thanks for the tutorial. it really helps me a lot improving my skills in php specially in the image part.

Sheetal
[16-Dec-2009]
#44

Hi

Very nice tutorial, all are working fine but when I integrate php code into html page it gives error "header already sent". Please give solution.

Thanks

Sheetal

Zac
[28-Dec-2009]
#45

To those of you having issues: A post already put up by Dan solved all my issues:

"I figured it out, you can add extra parameters to the imagepng function so it creates an image and saves on the server instead of outputting it to the browser window. So you can modify that line to say: $temp_chart_file_name = "temp_images/chart1.png"; imagepng($img, $temp_chart_file_name,0); I did this, and then just used the whole script as an include file and deleted: header("Content-type:image/png"); so I could continue with the other output I had on my page. Then after the reference to the include file I just included a link to the temp file on the server "

Just follow his instructions for the last 2 lines of the code.

Sramp
[28-Dec-2009]
#46

Very cool article dude. Keep up the good work

Thank you very much

Bhoj Raj Bhatt
[07-Jan-2010]
#47

Dear Friends,

Any body can inform how we can show the title on above chart ??

eg. Monthly Sales Chart

Please if any body know then plz let me inform this will great help.

Thank you

 

 

samawat
[08-Jan-2010]
#48

this was such a great help for my project.Thanks a lot

Ross Howatson
[16-Jan-2010]
#49

If the page does not work you might have to change the following:


short_open_tag = On

 

Peer Imran Ali Shah
[21-Feb-2010]
#50

Thanx!

very nice article.

Kaustubh Ekbote
[24-Feb-2010]
#51

Thanx!

This is what i required.

RLI
[25-Feb-2010]
#52

Its really nice php code. Any possibility to create pie chart  in the same way of the above example. plz suggest for me.............

DBE
[26-Mar-2010]
#53

This is a great tutorial. I'm having one problem though. I'm fetching and array from a mySql query. On the bottom of chart, I want to print out years instead of 0 through 17 (18 total results from mySql query). Does anyone have an idea how to alter array to return say 1990 to 2007 instead? Here is code. If anyone can help, you can email me at bpe.optimus (((at))) gmail (((dot))) com. Any help would be greatly aprpeciated.

$resultChart = mysql_query("SELECT year.y_id, year.year, emissions.emissions
      FROM emissions
      INNER JOIN year  ON  year.y_id = emissions.y_id
      INNER JOIN industry  ON  industry.i_id = emissions.i_id
      WHERE emissions.s_id = 2
      AND emissions.i_id = 2
      ORDER BY year.year ASC");

$values=array();

while($row = mysql_fetch_array($resultChart))
{
  $values[] = (number_format($row['emissions'],1));
}

DBE
[28-Mar-2010]
#54

Actually, Dan's post above alleviated the problem with header. Now I'm wondering how to produce different labels on the bottom of table. I need years to be represented. So instead of the 18 bars labeled 0 to 17, I need them labeled as 1990 to 2007. Any ideas?

Query is provided in above post.

Junaid
[28-Mar-2010]
#55

@ DBE

Create an associated array, the key will be shown at the bottom. Change your code to some like this


$resultChart = mysql_query("SELECT year.y_id, year.year, emissions.emissions
FROM emissions
INNER JOIN year ON year.y_id = emissions.y_id
INNER JOIN industry ON industry.i_id = emissions.i_id
WHERE emissions.s_id = 2
AND emissions.i_id = 2
ORDER BY year.year ASC"
);
 
$values=array();
 
while($row = mysql_fetch_array($resultChart))
{
$key=$row['year'];
$values[$key] = (number_format($row['emissions'],1));
}
 
?>

Alam
[17-May-2010]
#56

Great Job!

Thankssssssssssssssss a lotttttttttt :D

splatterkep
[21-Jun-2010]
#57

1st? This is great - thank you very much for your generosity in sharing it, as well as to the folks who spelled out the details above.

2nd, I apologize if this is a stupid question, but instead of using 2 array values ("Jan" => 110,) could I use more? I'd like to plot several items from a recordset showing change over time for each on one chart.

Thanks again!

Dave
[05-Jul-2010]
#58

Thanks for sharing this code firstly

Secondly, I have tried concatenating line breaks but within this array this does not work, how can I 

1. Make the labels bold

2. have a line break so I can fit text under jan in this example

Thanks for any help

pavithra
[09-Aug-2010]
#59

Hi,
I am not understand the below line. What i have to use in place of image/png

header
("Content-type:image/png");

Callum
[13-Aug-2010]
#60

I've tried to use this code.

Even with the fix outlined in comments #27/#28, i cannot get the code to display anything other than text when opened on my server?

Can someone clearly outline how to get this promising code functioning?

 

-Callum

Callum Johnson
[13-Aug-2010]
#61

Okay!   Sorted it.

Thank you for the code!

 

Callum Johnson
[13-Aug-2010]
#62

if its displaying as text for you, replace the "<?" with "<?php" :)

Gino
[21-Aug-2010]
#63

How to replace the fix numbers with numbers from mysql?

thank you

Pasti Nyala
[20-Sep-2010]
#64

Works for me

Muslim
[06-Oct-2010]
#65

Hi All,

Very nice example, thanks a lot.

for all those its not working, follow these steps

just replace these lines

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

just

        imagegif( $img, "graph.gif", 100);
imagedestroy($img);
echo ""
;

and now include the file in any other file you want....

enjoy...


 

shankar
[09-Oct-2010]
#66

Very nice example, thanks a lot.

Raj
[13-Oct-2010]
#67

Hi all

For the above code i am getting error as below when copied this as it is into my localhost.

Fatal error: Call to undefined function imagecreate() in /var/www/graph.php on line 27

armonde23
[03-Nov-2010]
#68

great tutorial!

how can i customize its color?

Tim
[03-Nov-2010]
#69

Just change the color values of variable

"$bar_color"

armonde23
[07-Nov-2010]
#70

the script doesnt work in UBUNTU 10.04, how can i solve this?

armonde23
[10-Nov-2010]
#71

how to enlarge the value in the graph?

Josh
[13-Nov-2010]
#72

I love it, i edited it a little bit to fit my needs but now i have 3 line graphs that show how many visitors view my site per day, per month, and yesterday's stats. Thanks much!

jack
[15-Nov-2010]
#73

It display error msg like "The image can not be displayed, because it contains errors". how to fix the problem pls anyone help me............

jack
[15-Nov-2010]
#74

just replace these lines

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

just

imagegif( $img, "graph.png", 100);
    imagedestroy($img);
    echo "";

armonde23
[26-Nov-2010]
#75

The graph is not supported by LAMPP server, is there any library to fix this?

ray
[28-Nov-2010]
#76

Thanx a lot. An example in my PHP book didn't work. Yours does! I am displaying a bar chart with pageviews per page. Next step will be altering the code to get horizontal bars, because my labels are to long.

Amarjeet
[30-Nov-2010]
#77

Thanks a lot for nice script. Nicely explained points helped me to learn about drawing the charts.

chenna
[30-Dec-2010]
#78

thanks a lot for nice script, its very simple and easy.

Francesca
[08-Jan-2011]
#79

hi Thanks for such a clear tutorial but what if i want to use data from my database instead of putting values into an array. Entering values into an array will be rather impractical for my project so can you teach me how to do that please?  you can email me at smiles_cool0226@yahoo.com.sg Thanks a lot, really.

Petar Velkov
[19-Jan-2011]
#80

Hi guys, there is one more way to display the image with jQuery AJAX request! Just place the graph code in its own php file, like graph.php. After that, in ajax_file.php put HTML tag <img src="graph.php?maybe=some?data=etc"> and when you call $.ajax({ url: ajax_file.php..... }) it will show you the graph. It's much easier and much faster, because it doesn't write the image on our server's hard drive.

Cheers!

Elexys
[21-Feb-2011]
#81

Great! thanks a lot for this tutorial!

You just saved my life! :D

Sunny
[24-Feb-2011]
#82

Anyone has idea how to increase the font size of the $key(months)..???

I tried a lot but was not successfull.

Junaid
[24-Feb-2011]
#83

@Sunny, use this function http://www.php.net/manual/en/function.imagettftext.php

Meena
[25-Feb-2011]
#84

Thank you very much. Exactly what i want.

srinivas
[09-Mar-2011]
#85

Hi Every one

Thanks for coding

My problem is I am not getting the image

when I use

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

this code it is displaying error message

when I replace this code with

 

imagegif( $img, "graph.gif", 100);
imagedestroy($img);
echo ""
;

it is displaying nothing

One more thing is I am getting the values dynamically from the database
but values are displaying correctly

Please help me
Thanks in advance

 

 

sri
[10-Mar-2011]
#86

hai I need help in displaying the image to the mozilla browser.

when I am using code

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

browser displays image in unknown format with different symbols

how to solve please put a reply

 


shi
[16-Mar-2011]
#87

Hi, I create a php file and add this source code on it. but the page diplay some unknown symbollike :

�PNG  IHDR�,-8[$ PLTE@����������OS� zIDATx���M��8��ʦ��d�����;@�D� �͜��| �+˧���]e$�hz��Ip......

Can you help me how I can fix it?

Thanks.

amen
[21-Mar-2011]
#88

thumbs up! u make my day.

Ajay
[21-Mar-2011]
#89

The script doesnt seem to output anything..I just copied the code given above.

shahid khan
[09-Apr-2011]
#90

thanx alot

hitesh nimavat
[14-Apr-2011]
#91

thanks ................

 

it really nice example of php graph......

 

it help me to understand php gd library or graph functionality...........

shabnam
[15-Apr-2011]
#92

Thanks........................

This code is perfect........

But I want  to fetch  values from database to show student result in graph ...

please help me to solve ....

 

 

mehu
[13-May-2011]
#93

thak u very very much

Ali
[27-May-2011]
#94

Excellent script mate. Highly appreciate your help. Need it to create something important for the site. Cheers for you help.

Ninad
[10-Jul-2011]
#95

thnx for the code.....awesome work

I took a day to get it to work. For those who could not get the graph to work jst follow d steps below:-

  1. copy the entire code paste it. Save the page as graph.php
  2. Change (< ?) with (< ? php)
  3. Create another php page & write

                 < img src="graph.php?maybe=some?data=etc" >                                 

U have the graph now.

To get the values from mysql refer comment #55

 

Ahmad Ali
[13-Aug-2011]
#96

This code is very much helpful I will try it for my website and will come back to thank after completion.

firos
[09-Aug-2011]
#97

hello,

its great work. thanks

have a doubt. Is it possible to put different background colors for the gap in between horizontal lines ($horizontal_lines). Actually I want to generate a map something like this by showing 0 to 10 levels on y access with different colors.

Please let me know if you have idea

Thanks

Firos

 

Trevor North
[06-Sep-2011]
#98

Another alternative is to use a php framework like atk4 and then you have access to a charting javascript library like jqplot which will let you create fantastic graphs, pie charts etc.. with scales based on the parameters you choose.

 

Click my name above to see an example on my site written in php.

Imran Khan
[07-Oct-2011]
#99

Dear All,

below table field are static but i like to take field and data from MySql with complete script.

i shall be vary thankfull to any who help me.

 

//Data for the table
    $header = array('Col A', 'Col B', 'Col C');
    $data = array(
        array('A1', 'B1', 'C1'),
        array('A2', 'B2', 'C2'),
        array('A2', 'B2', 'C2')
    );

surendra
[31-Oct-2011]
#100

Hi,very thannks for Given this code for us ,it's really works Good 

Anup
[09-Nov-2011]
#101

Hi,

your code worked perfectly fine. I am wondersing if it is possible to show different bar colors as well. For eg. one bar in dark red, second blue, third green and so on. 

In your example, it is only one bar color i.e. blue.

if any one have idea, please share. It will be great help and highly appreciated.

:)

Anusha
[08-Dec-2011]
#102

 

hey,

Thanks a ton. Your code is working well though i found a little problem initially for getting the values from the database and putting in into the array to suit the requirement.

But, phew am finally successfull. I wanted to know if we can make the graph location a bit more flexible to suit our requirement. The graph is being displayed on the top left. Can i change th location of the graph ? if so how? 

Please help!!!!

Thanks :)

 

Leave a Comment
Age (Required, will not be shown)
Name
Email (Required, will not be shown)
Website (Optional, starting with http://)
 
Are you human ?

Enter the code shown above