Coding Thread, Creating simple bar graphs in PHP in Coding and Web Development; Hello
I am trying to create simple bar graphs in my php file wherein in the first part of my ...
-
30th August 2010, 08:30 PM #1
- Rep Power
- 0
Creating simple bar graphs in PHP
Hello
I am trying to create simple bar graphs in my php file wherein in the first part of my code I am retrieving the values from the database, and based on the count of those values I want to create simple bar graph. But whenever I am trying to create a graph it throws me an junk values like this,
PNG ��� IHDR��@������-��� PLTE�����ɝ���{IDATxα 0E( {e gH(=!Žɥ5 @ @ @ @ @ @ @ @ w5wyf8["#~$Iz'����IENDB`
Please let me know how can I solve this error and how do I create a simple bar graph based on values from database.
Regards
ivn
-
-
IDG Tech News
-
30th August 2010, 09:13 PM #2 what are you using to create the graphs?
I've had good success with google charts, have a search around on the web and you'll find a good few helper classes to download to interface it with PHP.
I've also used jpgraph which is pretty good
-
-
30th August 2010, 09:44 PM #3 
Originally Posted by
ivn
But whenever I am trying to create a graph it throws me an junk values
The string you posted looks like it might be the first part of a PNG file - remember to set the MIME type for PNG instead of plain text.
--
David Hicks
-
-
30th August 2010, 09:51 PM #4 Can you post the code for the image generation script, and for the file where you're then embedding the image?
-
-
30th August 2010, 10:21 PM #5
- Rep Power
- 0
I kind of do not have an idea how to set the MIME type. Can someone please let me know how should i do it? the code I have written over all is as follows, the first part is retrieving the values I want the graph for, the second part is creating the graph. Please go through the code nd let me know what I can do.
<?php
require_once("../../config.php");
require_once("lib.php");
$qval='';
$ansval=array();
$arr=array();
$options=array();
$alloptions=array();
$selectedoptions=array();
$countA=0;
$countB=0;
$countC=0;
$countD=0;
$percentA=0;
$percentB=0;
$percentC=0;
$percentD=0;
$formatA=0;
$formatB=0;
$formatC=0;
$formatD=0;
echo "HI<BR>";
$query="select c.currentquestion from {$CFG->prefix}quiz_currentquestion c where c.id=1";
$execute=get_records_sql($query);
foreach($execute as $currentquestion => $execute1)
{
$qval=$execute1->currentquestion;
echo "$qval<br>";
}
$opquery="select s.id,s.answer from {$CFG->prefix}question_states s where s.question in ($qval) and s.seq_number=1";
$opexec=get_records_sql($opquery);
foreach($opexec as $answer => $opexec1)
{
$ansval[]=$opexec1->answer;
}
$n=count($ansval);
echo "$n";
for($i=0;$i<$n-1;$i++)
{
$arr = explode(":",$ansval[$i]);
$selectedoptions[$i]=$arr[1];
//echo "<br>$selectedoptions[$i]<br>";
}
$options=explode(",",$arr[0]);
$opcount=count($options);
//Keeping a count of options selected by user.
for($r=0;$r<$n-1;$r++)
{ switch($selectedoptions[$r])
{ case $options[0]:
$countA=$countA+1;
//$percentA=$countA/$c*100;
break;
case $options[1]:
$countB=$countB+1;
//$percentB=$countB/$c*100;
//$formatB=number_format($percentB, 2, '.', '');
break;
case $options[2]:
$countC=$countC+1;
//$percentC=$countC/$c*100;
//$formatC=number_format($percentC, 2, '.', '');
break;
case $options[3]:
$countD=$countD+1;
//$percentD=$countD/$c*100;
//$formatD=number_format($percentD, 2, '.', '');
break;
}
}
$percentA=$countA/$n*100;
$formatA=number_format($percentA, 2, '.', '');
$percentB=$countB/$n*100;
$formatB=number_format($percentB, 2, '.', '');
$percentC=$countC/$n*100;
$formatC=number_format($percentC, 2, '.', '');
$percentD=$countD/$n*100;
$formatD=number_format($percentD, 2, '.', '');
//Displaying answer text for feedback table.
for($p=0;$p<$opcount;$p++)
{
$optionquery="select a.answer from {$CFG->prefix}question_answers a where a.id in ($options[$p])";
$opanswer=get_records_sql($optionquery);
foreach($opanswer as $answer => $opanswer1)
{
$oneoption=$opanswer1->answer;
$alloptions[$p]=$oneoption;
}
}
//Displaying the questiontext
$qtextquery="select q.questiontext from {$CFG->prefix}question q where q.id in ($qval)";
$qtextanswer=get_records_sql($qtextquery);
foreach($qtextanswer as $questiontext => $qtextanswer1)
{
$prevques=$qtextanswer1->questiontext;
}
echo "<div class=\"studenthelp mdl-align\">";
echo "<h2 style='font-family:Sans Serif; font-size:16pt; text-align:center; color:red; background-color:#FFFF66;'>Count</h2>";
echo "<table> <tr> <th> Q.Text </th> <th> Options </th> <th> Count </th> <th> % Count </th> </tr>";
echo "<tr> <td rowspan=5>$prevques</td></tr>";
echo "<tr> <td>$alloptions[0]</td> <td> $countA</td><td> $formatA %</td></tr>";
echo "<tr> <td>$alloptions[1]</td> <td> $countB</td><td> $formatB %</td></tr>";
echo "<tr> <td>$alloptions[2]</td> <td> $countC</td><td> $formatC %</td></tr>";
echo "<tr> <td>$alloptions[3]</td> <td> $countD</td><td> $formatD %</td></tr>";
echo "</table><br>";
echo "</div>";
$values=array($alloptions[0] => $countA, $alloptions[1] => $countB, $alloptions[2] => $countC, $alloptions[3] => $countD);
$sum = array_sum($values);
$height = 255;
$width = 320;
$im = imagecreate($width,$height); // width , height px
$white = imagecolorallocate($im,255,255,255);
$black = imagecolorallocate($im,0,0,0);
$red = imagecolorallocate($im,255,0,0);
imageline($im, 10, 5, 10, 230, $black);
imageline($im, 10, 230, 300, 230, $black);
$x = 15;
$y = 230;
$x_width = 20;
$y_ht = 0;
$i=0;
for ($i=0;$i<9;$i++){
$y_ht = ($values[$i]/$sum)* $height;
imagerectangle($im,$x,$y,$x+$x_width,($y-$y_ht),$red);
imagestring( $im,2,$x-1,$y+10,$values[$i],$black);
$x += ($x_width+20);
}
imagejpeg($im);
?>
-
-
30th August 2010, 10:30 PM #6 
Originally Posted by
ivn
I kind of do not have an idea how to set the MIME type.
Me neither, but checking Google ("php set mime type") suggests you need to call the header function first thing:
<?php
header('Content-Type: image/png');
...
--
David Hicks
-
-
30th August 2010, 11:08 PM #7
- Rep Power
- 0
Setting the header right at the start would give me no output, as the values of which I want to create a bar graph is in the first part of the code. So putting the header in the start of the code just prints the url of that file back on the screen.
-
-
30th August 2010, 11:47 PM #8 You appear to be creating a HTML page, and then trying to embed an image directly into it. That won't work - a PHP page is simply pre-processing and then presenting an HTML page. You can't embed an image directly into HTML.
Instead, you need 2 scripts. One which presents the page, and one which creates the image. The first one would then have <img> tags for any images, calling the second script, and passing parameters to it for the creation of any dynamic features.
-
-
31st August 2010, 04:26 PM #9 Try this
Save your code as a separate PHP file and then use this:
Code:
<img src="[Insert PHP file name]">
-
SHARE:
Similar Threads
-
By ranj in forum Office Software
Replies: 1
Last Post: 11th June 2010, 09:23 PM
-
By phpelite in forum Scripts
Replies: 2
Last Post: 1st December 2009, 01:56 AM
-
By dhicks in forum How do you do....it?
Replies: 21
Last Post: 11th May 2009, 12:15 PM
-
By Jona in forum Web Development
Replies: 2
Last Post: 2nd May 2008, 02:43 PM
-
By ChrisS in forum Scripts
Replies: 1
Last Post: 29th November 2007, 11:25 AM
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules