Jump to content

Recommended Posts

Can somebody pls help me modify the attached code:

 

Currently the graph is displayed like

 

KZN ******************************

GP  ********

LP  *****************

 

I would like it to be displayed like:

 

  *     

  *  *

  *  *

  *  *  *

KZN GP LP

 

Everything I've tried so far just buggers up the code

 

 

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/68178-graph/
Share on other sites

Sorry forgot to add code

 

<?php
include 'dbinc.php';	// Variables for DB connection
include 'error.inc';	// Error handler
  
// Function to display a bar chart from O'Reilly 'PHP Cookbook'
function pc_bar_chart ($question, $answers)
{

	// define colours to draw the bars
	$colours = array (array(255,102,0), array(0,153,0),
			array(51,51,204), array(255,0,51),
			array(255,255,0), array(102,255,255),
			array(153,0,204));

	$total = array_sum($answers['qsos']);

	// define some spacing
	$padding = 15;
	$line_width = 80;
	$scale = $line_width * 7.5;
	$bar_height = 30;

	$x = $y = $padding;

	// Allocate a large palette for drawing
	$image = ImageCreate (550, 1000);
	$bg_colour = ImageColorAllocate($image, 224, 224, 224);
	$black = ImageColorAllocate($image, 0, 0, 0);

	// print the query
	$wrapped = explode ("\n", wordwrap($question, $line_width));
	foreach ($wrapped as $line)
	{
		ImageString($image,3,$x,$y,$line,$black);
		$y += 10;
	}

	$y += $padding;

	// print the results
	for ($i = 0; $i < count ($answers['query']); $i++)
	{

		// format percentages
		$percent = sprintf ('%1.1f', 100 * $answers ['qsos'][$i]/$total);
		$bar = sprintf ('%d', $scale * $answers ['qsos'][$i]/$total);

		// grab colour
		$c = $i % count($colours);
		$text_colour = ImageColorAllocate($image, $colours[$c][0],
						$colours[$c][1], $colours[$c][2]);

		// draw bar and percentage numbers
		ImageFilledrectangle ($image, $x, $y, $x + $bar,
					$y + $bar_height, $text_colour);

		ImageString ($image, 3, $x + $bar + $padding, $y, "$percent%", $black);

		$y += 10;

		// print query
		$wrapped = explode ("\n", wordwrap ($answers['query'][$i], $line_width));
		foreach ($wrapped as $line)
		{
			ImageString ($image, 2, $x, $y, $line, $black);
			$y += 12;
		}

		$y += 25;
	}

	// crop image by copying it
	$chart = ImageCreate (550, $y);
	ImageCopy ($chart, $image, 0, 0, 0, 0, 550, $y);

	//deliver image
	header ('Content-type: image/png');
	ImagePng($chart);

	//clean up
	ImageDestroy ($image);
	ImageDestroy ($chart);
}

// Start of main program
$qso_count = 0;
$kzn_count = 0;
$gp_count = 0;
$lp_count = 0;
$mp_count = 0;
$wc_count = 0;
$nc_count = 0;
$ec_count = 0;
$nw_count = 0;
$fs_count = 0;


// Connect to MySQL
if (! ($connection = @ mysql_connect($hostName,$username,$password)))
	die ("Could not connect to the database");

// Connect to the log database.
if (!mysql_select_db ($databaseName, $connection))
	showerror();

// Query the total number of QSOs made by 9M2/G4ZFE (logbooks 2,3,4,5 and 10)
if (! ($result = mysql_query ("SELECT count(*) 
				FROM  dis",
		$connection)))
	showerror();

$row = @ mysql_fetch_array ($result);

        $qso_count = $row['count(*)'];

// Extract the number of QSOs made per mode
if (! ($result = mysql_query (" SELECT count(*),Region 
				FROM dis
				GROUP BY Region",
		$connection)))
	showerror();

// Fetch each row. It is returned in the following order - CW, DIG, SSB
while ($row = @ mysql_fetch_array ($result))
{
	$count = $row['count(*)'];
	$mode = $row['Region'];

	switch ($mode)
	{
		case "KZN":
		$kzn_count = $count;
		break;

		case "GP":
		$gp_count = $count;
		break;

		case "LP":
		$lp_count = $count;
		break;

		case "MP":
		$mp_count = $count;
		break;

		case "WC":
		$wc_count = $count;
		break;

		case "NC":
		$nc_count = $count;
		break;

		case "EC":
		$ec_count = $count;
		break;

		case "NW":
		$nw_count = $count;
		break;

		case "FS":
		$fs_count = $count;
		break;

	}			
}

// Display text
$question = 'Total  = ' . $qso_count;

// Bar Chart for KZN QSOs
$answers['query'][] = $kzn_count . ' KZN';
$answers['qsos'][] = $kzn_count;

// Bar Chart for GP QSOs
$answers['query'][] = $gp_count . ' GP';				
$answers['qsos'][] = $gp_count;

// Bar Chart for LP QSOs
$answers['query'][] = $lp_count . ' LP';
$answers['qsos'][] = $lp_count;

// Bar Chart for MP QSOs
$answers['query'][] = $mp_count . ' MP';
$answers['qsos'][] = $mp_count;

// Bar Chart for WC QSOs
$answers['query'][] = $wc_count . ' WC';
$answers['qsos'][] = $wc_count;

// Bar Chart for NC QSOs
$answers['query'][] = $nc_count . ' NC';
$answers['qsos'][] = $nc_count;

// Bar Chart for EC QSOs
$answers['query'][] = $ec_count . ' EC';
$answers['qsos'][] = $ec_count;

// Bar Chart for NW QSOs
$answers['query'][] = $nw_count . ' NW';
$answers['qsos'][] = $nw_count;

// Bar Chart for FS QSOs
$answers['query'][] = $fs_count . ' FS';
$answers['qsos'][] = $fs_count;

// Display the Bar Chart
pc_bar_chart ($question, $answers);

// Close the database connection
if (!mysql_close ($connection))
	showerror();  
?>

Link to comment
https://forums.phpfreaks.com/topic/68178-graph/#findComment-342777
Share on other sites

<table cellpadding="5" cellspacing="5" border="0">
<tr><td>
<?php
header("Content-Type:image/jpeg");
$img1 = imagecreatetruecolor(50,10);
$bgcolor = imagecolorallocate($img1,100,120,210);
imagejpeg($img1,"img1.jpg"); imagedestroy($img1);
header("Content-Type:text/html"); 
?>
<img src="img1.jpg" />
</td></tr>
<tr><td>
<?php
header("Content-Type:image/jpeg");
$img2 = imagecreatetruecolor(100,10);
$bgcolor = imagecolorallocate($img2,100,120,210);
imagejpeg($img2,"img2.jpg"); imagedestroy($img2);
header("Content-Type:text/html"); 
?>
<img src="img2.jpg" />
</td></tr>
<tr><td>
<?php
header("Content-Type:image/jpeg");
$img3 = imagecreatetruecolor(70,10);
$bgcolor = imagecolorallocate($img3,100,120,210);
imagejpeg($img3,"img3.jpg"); imagedestroy($img3);
header("Content-Type:text/html"); 
?>
<img src="img3.jpg" />
</td></tr>
</table>
<table cellpadding="5" cellspacing="5" border="0">
<tr><td width="20" valign="bottom"><?php
header("Content-Type:image/jpeg");
$img4 = imagecreatetruecolor(10,50);
$bgcolor = imagecolorallocate($img4,100,120,210);
imagejpeg($img4,"img4.jpg"); imagedestroy($img4);
header("Content-Type:text/html"); 
?>
<img src="img4.jpg" /></td>
<td width="20" valign="bottom"><?php
header("Content-Type:image/jpeg");
$img5 = imagecreatetruecolor(10,100);
$bgcolor = imagecolorallocate($img5,100,120,210);
imagejpeg($img5,"img5.jpg"); imagedestroy($img5);
header("Content-Type:text/html"); 
?><img src="img5.jpg" /></td>
<td width="20" valign="bottom"><?php
header("Content-Type:image/jpeg");
$img6 = imagecreatetruecolor(10,70);
$bgcolor = imagecolorallocate($img6,100,120,210);
imagejpeg($img6,"img6.jpg"); imagedestroy($img6);
header("Content-Type:text/html"); 
?>
<img src="img6.jpg" /></td>

</tr>
</table>

Link to comment
https://forums.phpfreaks.com/topic/68178-graph/#findComment-342897
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.