Jump to content

PEAR Image_Graph from a database?


fj1200

Recommended Posts

Off home in a minute but has anyone used PEAR::Image_Graph with a DB back end?  I have both MySQL and SQL Server that I'd like to be able to display live manufacturing production data from without resorting to Xelcius but can't get it to pull the data.  I already have a tabular dashboard that displays summary info but if I could graph in instead that would be seriously cool here.  Sad, I know but true. 

 

Been searching all afternoon, lots of people seem to have the same question but haven't managed to find an answer.

 

Managed to get a test graph with static data into my dashboard and that looks great, just need the live stuff now.  Be good if you could use $_GET[] and re-use the data already pulled from the db but that doesn't seem to work.

Link to comment
Share on other sites

fj1200,

 

    Why not use session data ?

 

Scot

 

To the top of your script, Add:

 


<?php

$serverHasSessionSupportEnabled = ini_get('session.auto_start');

if (!$serverHasSessionSupportEnabled) {

	$sessionSupport = session_start();

	if (!$sessionSupport) {

		echo "You will have to compile you PHP envionment with Session Support before using this script. <br /><br > ";
		echo "So Long, and thanks for all the fish...  <br /><br > <ul><ul><h2>  </h2> \n"; 
		exit();

	}
}

?>

 

    Then, when you first get the datapoints you want to graph, do:

 


<?php

$_SESSION['graphPair'] = array(); // array( X-axis Value, Y-axis Value) 

$yIDX = 0;

// For Example: 

$xAxisFromDB = range(1,12);

$yAxisFromDB = array(0,3,5,3,2,6,11,12,'',7,7, 15);

foreach($xAxisFromDB as $xIDX => $x) {

	$_SESSION['graphPair'][] = array($x, $yAxisFromDB[$yIDX]);

	$yIDX++;

}

// Then later, ( even many scripts / html pages later )
// access the data point array like this :

$myGraphData = $_SESSION['graphPair'];

$xForGraph = array();

$yForGraph = array();

foreach ($myGraphData as $pairIDX => $currentDataPair) {

	/**
	 *  
	 *  Use data in $currentDataPair to generate graph... 
	 * 
	 */


	foreach($currentDataPair as $scaleIDX => $scalePoint) {

		switch($scaleIDX) {

			case 0 :
				$xForGraph[] = $scalePoint;
			break;

			case 1 :
				$yForGraph[] = $scalePoint;
			break;

		}

	}

}

// See the results

$display = TRUE;

if ($display) { 

	require_once('include/debugFunctions.php');
	echo "<font color=\"#205E75\"> \n";
	printArray($xForGraph, '$xForGraph');
	printArray($yForGraph, '$yForGraph'); 
	echo "<font color=\"black\"> \n";

}

?>

 

 

This will produce this:

 

    Name : $xForGraph

 

 

        Array

        (

            [0] => 1

            [1] => 2

            [2] => 3

            [3] => 4

            [4] => 5

            [5] => 6

            [6] => 7

            [7] => 8

            [8] => 9

            [9] => 10

            [10] => 11

            [11] => 12

        )

 

    Name : $yForGraph

 

 

        Array

        (

            [0] => 0

            [1] => 3

            [2] => 5

            [3] => 3

            [4] => 2

            [5] => 6

            [6] => 11

            [7] => 12

            [8] =>

            [9] => 7

            [10] => 7

            [11] => 15

        )

 

    If your printArray() function performs similar to this:

 


<?php

    #-- Assignment : List arbitary number of name in an HTML table
    function listNames() {
	GLOBAL $table;
	$table = "<table border=\"1\"> \n";
	if (func_num_args() > 0) {
		$numOfArgs = func_num_args();

		for ($i=0; $i < $numOfArgs; $i++) {

			$table .= "  <tr> \n";
			$table .= "    <td>    \n";
			$table .= func_get_arg($i);
			$table .= "      </td> \n";
			$table .= "  </tr> \n";
		}
	}
	else {
		echo "No Args Passed" . " <br /> \n";
	}
	$table .= "</table> \n";
	return $table;
}

   #-- END Assignment : List arbitary number of name in an HTML table
   # -- printArray : List members in a array -- #
   
function printArray($arrayName, $name = false) {
	if ($name)  {
		echo "Name : $name" . " <br /> \n";
	}
	if (!is_array($arrayName)) {

		$saveArray = $arrayName;

		$arrayName = 'Not An Array';

		$possibleString = TRUE;
	}
	if (empty($arrayName)) {
		$arrayName = 'Array Contain No Data';
	}


	echo "<font color=\"#205E75\"> \n";
	echo "<pre> \n";

		print_r($arrayName);

	echo "</pre> \n";

	echo "</font> \n";

	if ($possibleString == TRUE) {

		$string = $saveArray;

		if ($string == NULL) {

			$string = 'Null String';
		}

		echo "<font color=\"#205E75\"> \n";
		echo " <br /> <br /> \n";
		echo '$array as string: ' . $string  . "<br /> <br /> \n";
		echo " <br /> <br /> \n";
		echo "</font> \n";

	}
}

function printArrayVarDump($arrayName, $name = false) {

	if ($name)  {
		echo "Name : $name" . " <br /> \n";
	}
	echo "<pre> \n";
		var_dump($arrayName);
	echo "</pre> \n";

	if ($possibleString == TRUE) {

		$string = $arrayName;

		if ($string == NULL) {

			$string = 'Null String';
		}

		echo '$array as string: ' . $string  . "<br /> <br /> \n";

	}
	// echo " <br /> <br /> \n";
}

function arrayPrint($arrayName, $name = false) {
	printArray($arrayName, $name = false);
}

?>

 

   

Link to comment
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.