Jump to content

Recommended Posts

I've got this script to generate a graph using data in a database. But, how would I combine two fields like Race ad Gender to be displayed correctly.

 

 

<?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(104,153,255), array(0,153,0),

array(255,255,0), array(255,0,51),

array(51,51,204), array(255,102,0),

array(153,0,204), array(255,0,255), array(102,255,255));

 

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

 

// define some spacing

$padding = 5;

$line_width = 80;

$scale = $line_width * 7.5;

$bar_height = 20;

 

$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 += 40;

}

 

$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, 5, $x + $bar + $padding, $y, "$percent%", $black);

 

$y += 5;

 

// print query

$wrapped = explode ("\n", wordwrap ($answers['query'][$i], $line_width));

foreach ($wrapped as $line)

{

ImageString ($image, 3, $x, $y, $line, $black);

$y += 8;

}

 

$y += 20;

}

 

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

$nps_count = 0;

$dos_count = 0;

$cs_count = 0;

$wpu_count = 0;

$sccu_count = 0;

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

;

 

// 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 where Status like 'Closed' and Date_Recieved between '$_POST[date1]' and '$_POST[date2]'",

$connection)))

;

 

$row = @ mysql_fetch_array ($result);

 

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

 

// Extract the number of QSOs made per mode

if (! ($result = mysql_query (" SELECT count(*),Unit

FROM dis where Status like 'Closed' and Date_Recieved between '$_POST[date1]' and '$_POST[date2]'

GROUP BY Unit",

$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['Unit'];

 

switch ($mode)

{

case "NPS":

$nps_count = $count;

break;

 

case "DOS":

$dos_count = $count;

break;

 

case "CS":

$cs_count = $count;

break;

 

case "WPU":

$wpu_count = $count;

break;

 

case "SCCU":

$sccu_count = $count;

break;

 

case "AFU":

$afu_count = $count;

break;

}

 

}

 

// Display text

$question = 'Carried Over Unit Totals  = ' . $qso_count;

 

// Bar Chart for KZN QSOs

$answers['query'][] = ' NPS ' .$nps_count;

$answers['qsos'][] = $nps_count;

 

// Bar Chart for GP QSOs

$answers['query'][] = ' DOS  ' .$dos_count;

$answers['qsos'][] = $dos_count;

 

// Bar Chart for GP QSOs

$answers['query'][] = ' CS  ' .$cs_count;

$answers['qsos'][] = $cs_count;

 

// Bar Chart for GP QSOs

$answers['query'][] = ' WPU  ' .$wpu_count;

$answers['qsos'][] = $wpu_count;

 

// Bar Chart for GP QSOs

$answers['query'][] = ' SCCU  ' .$sccu_count;

$answers['qsos'][] = $sccu_count;

 

// Bar Chart for GP QSOs

$answers['query'][] = ' AFU  ' .$afu_count;

$answers['qsos'][] = $afu_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/72280-solved-graph/
Share on other sites

<?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(104,153,255), array(0,153,0),
			array(255,255,0), array(255,0,51),
			array(51,51,204), array(255,102,0),
			array(153,0,204), array(255,0,255), array(102,255,255));

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

	// define some spacing
	$padding = 5;
	$line_width = 80;
	$scale = $line_width * 7.5;
	$bar_height = 20;

	$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 += 40;
	}

	$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, 5, $x + $bar + $padding, $y, "$percent%", $black);

		$y += 5;

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

		$y += 20;
	}

	// 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;
$nps_count = 0;
$dos_count = 0;
$cs_count = 0;
$wpu_count = 0;
$sccu_count = 0;
$afu_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))
	;

// Query the total number of QSOs made by 9M2/G4ZFE (logbooks 2,3,4,5 and 10)
if (! ($result = mysql_query ("SELECT count(*) 
				FROM  appeals where Status like 'Closed' and Date_Recieved between '$_POST[date1]' and '$_POST[date2]'",
		$connection)))
	;

$row = @ mysql_fetch_array ($result);

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

// Extract the number of QSOs made per mode
if (! ($result = mysql_query (" SELECT count(*),Unit 
				FROM appeals where Status like 'Closed' and Date_Recieved between '$_POST[date1]' and '$_POST[date2]'
				GROUP BY Unit",
		$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['Unit'];

	switch ($mode)
	{
		case "NPS":
		$nps_count = $count;
		break;

		case "DOS":
		$dos_count = $count;
		break;

		case "CS":
		$cs_count = $count;
		break;

		case "WPU":
		$wpu_count = $count;
		break;

		case "SCCU":
		$sccu_count = $count;
		break;

		case "AFU":
		$afu_count = $count;
		break;
	}

}

// Display text
$question = 'Carried Over Unit Totals  = ' . $qso_count;

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

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

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

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

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

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

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

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

 

Well the above code displays the business units. But i want the graph to display the count of to concatinated fields like Race and Gender. The basic mysql syntax would look something like:

 

select * from appeals where Race like 'African' and Gender like 'Male';

Link to comment
https://forums.phpfreaks.com/topic/72280-solved-graph/#findComment-364504
Share on other sites

  • 1 year later...

I'm searching for the PHP-Script wich make an output

like this http://www.g4zfe.com/9m2qsochart.php

the small one i have but i want the chart ist sorted

by band, like shown in the grafik ( Link ).

 

Somebody have this script or may be Richard is member of

this board ?

 

:confused: I need it, but i can't do it by myself ...  :facewall:

 

Thank you ...

Link to comment
https://forums.phpfreaks.com/topic/72280-solved-graph/#findComment-897060
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.