Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

php graph (array problem)

Status
Not open for further replies.

JJJ777

Programmer
Jun 5, 2002
33
MY
Below is my coding:

can someone help me on the array part, where i only want the x-label with value will be display. in other words if no value the x-label will not be displayed for that data.

tq.

<?

require (&quot;../core.php&quot;);
$link_id = db_connect($user_dbname);

//menu_header_student_profile();

header(&quot;Content-type: image/gif&quot;); // or &quot;Content-type: image/png&quot;

$query2 = &quot;SELECT * FROM lkp_country&quot;;
$result2 = pg_exec($query2);
$total2 = pg_numrows($result2);
while ($row = pg_fetch_array($result2)) {
$arrsu[] = $row[&quot;ctry_code&quot;];
$arrsu2[] = $row[&quot;ctry_desc&quot;];
}

$strData = '$data=array(';
for($i=0; $i<$total2; $i++) {

$query = &quot;SELECT * FROM adm_student_registered
INNER JOIN adm_student_profile ON adm_student_registered.sreg_stud_profileid = adm_student_profile.stud_profileid
WHERE stud_country = '$arrsu[$i]' &quot;;
$result = pg_exec($query);
$total = pg_numrows($result);
$rows = pg_fetch_array($result);

$strData .= &quot;\&quot;&quot; . &quot; $arrsu2[$i]&quot; . &quot;\&quot;&quot; . ' => ' . $total . ',';
}
$strData .= ');';
eval($strData);


// Here we begin the graph
$width = 480;
$height = 450;
$image = imagecreate($width, $height);

// colors
$white = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
$navy = imagecolorallocate($image, 0x00, 0x00, 0x80);
$black = imagecolorallocate($image, 0x00, 0x00, 0x00);
$gray = imagecolorallocate($image, 0xC0, 0xC0, 0xC0);

// layout
$maxval = max($data);
$nval = sizeof($data);

$vmargin = 40; // top (bottom) vertical margin for title (x-labels)
$hmargin = 70; // left horizontal margin for y-labels

$base = floor(($width - $hmargin) / $nval); // distance between columns

$ysize = $height - 2 * $vmargin; // y-size of plot
$xsize = $nval * $base; // x-size of plot

// title
$titlefont = 3;
$title = &quot;Total Student For Each Country&quot;;

//echo &quot;<img src='/button/bmi.jpg' width='36' height='40'></A>\n&quot;;

$txtsz = imagefontwidth($titlefont) * strlen($title); // pixel-width of title

$xpos = (int)($hmargin + ($xsize - $txtsz)/2); // center the title
$xpos = max(1, $xpos); // force positive coordinates
$ypos = 3; // distance from top

imagestring($image, $titlefont, $xpos, $ypos, $title , $black);

// y labels and grid lines
$labelfont = 2;
$ngrid = 4; // number of grid lines

$dydat = $maxval / $ngrid; // data units between grid lines
$dypix = $ysize / ($ngrid + 1); // pixels between grid lines

for ($i = 0; $i <= ($ngrid + 1); $i++) {
// iterate over y ticks

// height of grid line in units of data
$ydat = (int)($i * $dydat);

// height of grid line in pixels
$ypos = $vmargin + $ysize - (int)($i*$dypix);

$txtsz = imagefontwidth($labelfont) * strlen($ydat); // pixel-width of label
$txtht = imagefontheight($labelfont); // pixel-height of label

$xpos = (int)(($hmargin - $txtsz) / 2);
$xpos = max(1, $xpos);

imagestring($image, $labelfont, $xpos,
$ypos - (int)($txtht/2), $ydat, $black);

if (!($i == 0) && !($i > $ngrid)) {
imageline($image, $hmargin - 3,
$ypos, $hmargin + $xsize, $ypos, $gray);
// don't draw at Y=0 and top
}
}

// columns and x labels
$padding = 3; // half of spacing between columns
$yscale = $ysize / (($ngrid+1) * $dydat); // pixels per data unit

for ($i = 0; list($xval, $yval) = each($data); $i++) {

// vertical columns
$ymax = $vmargin + $ysize;
$ymin = $ymax - (int)($yval*$yscale);
$xmax = $hmargin + ($i+1)*$base - $padding;
$xmin = $hmargin + $i*$base + $padding;

imagefilledrectangle($image, $xmin, $ymin, $xmax, $ymax, $navy);

// x labels
$txtsz = imagefontwidth($labelfont) * strlen($xval);

$xpos = $xmin + (int)(($base - $txtsz) / 2);
$xpos = max($xmin, $xpos);
$ypos = $ymax + 3; // distance from x axis

imagestring($image, $labelfont, $xpos, $ypos, $xval, $black);
}

// plot frame
imagerectangle($image, $hmargin, $vmargin,
$hmargin + $xsize, $vmargin + $ysize, $black);

// flush image
//header(&quot;Content-type: image/gif&quot;); // or &quot;Content-type: image/png&quot;
imagegif($image); // or imagepng($image)
imagedestroy($image);

?>

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top