Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 27th, 2006, 4:55 PM   #1
Pizentios
Programming Guru
 
Pizentios's Avatar
 
Join Date: May 2004
Location: Brandon, Manitoba, Canada
Posts: 2,023
Rep Power: 7 Pizentios is on a distinguished road
Send a message via ICQ to Pizentios Send a message via MSN to Pizentios
Jpgraph help

Hello,

I have been working on a script to parse data into line graphs, using jpgraph.

here's the code that i am working with:
[php]
<?PHP
////////////////////////////////////////////////////////////////////////////////
// Purpose: This file outputs the mapping by mile post speed report. //
// Author: Nick Peters //
// Date Created: 2006-04-26 //
// Date Finished: 2006-04-XX //
////////////////////////////////////////////////////////////////////////////////

include "../include/dbobj.php";
include "../include/graphs/jpgraph.php";
include "../include/graphs/jpgraph_line.php";

function color($tint)
{

$frag = range(0,255);

$red = "";
$green = "";
$blue = "";

for (; {

$red = $frag[mt_rand(0, count($frag)-1)];
$green = $frag[mt_rand(0, count($frag)-1)];
$blue = $frag[mt_rand(0, count($frag)-1)];

switch ($tint) {
case 'light':
if (($red + $green + $blue / 3) >= 200) break 2;
break;
case 'dark' :
default:
if (($red + $green + $blue / 3) <= 50) break 2;
break;
}
}
return sprintf("#%02s%02s%02s", dechex($red), dechex($green),dechex($blue));
}



//data grab.
$db = new dbobj();
$db->setUser("postgres");
$db->setDBname("cemr_tracking");

if ($db->conn())
{
//we have connection
//select the raw data for the given dates.
$raw_data = $db->dbSelect("*", $requiredSubD, "date BETWEEN '" . $requiredStartD . "' AND '" . $requiredEndD . "' ORDER By date ASC");
$raw_months = $db->dbSelect("text, month", "text_months", "");
$raw_miles = $db->dbSelect("mile_number", $requiredSubD . "_miles", "");
if ($raw_data && $raw_months && $raw_miles)
{
for ($k=0; $k<pg_num_rows($raw_miles); $k++)
{
$miles_data = pg_fetch_object($raw_miles, $k);
$miles[$k] = $miles_data->mile_number;
}
for ($y=0; $y<pg_num_rows($raw_months); $y++)
{
$monthD = pg_fetch_object($raw_months, $y);
$text_m[$y] = $monthD->text;
$num_m[$y] = $monthD->month;
}
for ($x=0; $x<pg_num_rows($raw_data); $x++)
{
$data = pg_fetch_object($raw_data, $x);
$d_chunks = explode("-", $data->date);
$result = array_search($d_chunks[1], $num_m);
$result2 = array_search($data->mile_number, $miles);
$month_key[$x] = $text_m[$result];
//averageS == the averages for slow order speed.
//averageR == the averages for the rated speed.
$averageS[$text_m[$result]]["$miles[$result2]"][0] = $averageS[$text_m[$result]]["$miles[$result2]"][0] + $data->slow_order_speed;
$averageS[$text_m[$result]]["$miles[$result2]"][1] += 1;
$averageR["$miles[$result2]"][0] += $data->rated_speed;
$averageR["$miles[$result2]"][1] +=1;
}

$actual_months = array_unique($month_key); //gives us the number of months.
$g_width = (count($miles)) * 35;
if ($g_width < 900)
{
$g_width = 900;
}
$graph = new Graph($g_width, 700);
$graph->img->SetMargin(90,90,90,90);
$graph->SetScale("textint");

$graph ->xgrid->Show( true);
$real_m = array_values($actual_months);
//echo count($actual_months);
for ($x=0; $x<count($actual_months); $x++)
{
//echo $real_m[$x] . "<br>";
//echo $real_m[$x];
foreach ($miles as $mile)
{
//calculates the average for each mile/slow order speed, dumps back into the array.
//echo "Mile Totals for mile " . $mile . ": " . $averageS[$real_m[$x]][$mile] . "<br>";
$total_avgS[$real_m[$x]][$mile] = ($averageS[$real_m[$x]]["$mile"][0] / $averageS[$real_m[$x]]["$mile"][1]);
//echo "AverageS for Mile " . $mile . " And for the month of " . $real_m[$x] . ": " . $total_avgS[$real_m[$x]][$mile] . "<br>";
//calculates the average for each mile/rated speed, dumps back into the array.
$total_avgR[$mile] = ($averageR[$mile][0] / $averageR[$mile][1]);
}
/*echo "<br>";
var_dump($total_avgS);*/
$plot = $total_avgS[$real_m[$x]];
$lineplots[$x] = new LinePlot($plot);
$lineplots[$x]->SetColor(color("light"));
$lineplots[$x]->mark->SetType(MARK_UTRIANGLE);
$lineplots[$x]->SetLegend("Slow Order (Actual) Speed For " . $real_m[$x]);
}

//echo "Values of mile 8.9: " . $total_avgS['Jan']['8.9'] . " , " . $total_avgS['Feb']['8.9'];

$rated_lineplot = new LinePlot($total_avgR);
$rated_lineplot->SetColor(color("dark"));
$rated_lineplot->mark->SetType(MARK_UTRIANGLE);
$rated_lineplot->SetLegend("Rated Train Speed");

$graph->legend->Pos(0.8,0.03,"center","top");
$graph->yaxis->title->SetFont(FF_FONT1,FS_BOLD);
$graph->xaxis->title->SetFont(FF_FONT1,FS_BOLD);
$graph->title->Set($requiredSubD . " Mapping By Milepost Speed");
$graph->xaxis->SetTickLabels($miles);
$graph->xaxis->SetTitle("Mile Number", "middle");
$graph->xaxis->SetTitleMargin(50);
$graph->yaxis->SetTitle("Speed In Miles Per Hour", "middle");
$graph->yaxis->SetTitleMargin(50);
for ($x=0; $x<count($actual_months); $x++)
{
$graph->Add($lineplots[$x]);
}
$graph->Add($rated_lineplot);
$graph->Stroke("/home/nick/CEMR-Tracking/reports/report.png");
echo "<img src=reports/report.png>";

}
else
{
//error while grabing data.
?>
<center>
<h2>There has been an error while trying to get results from the database.<br>Please Try Again.</h2>
<p>If the problem continues, please contact the system administrator.</p>
<br>
<a href="index.php">Main Menu</a>
</center>
<?PHP
} //end of data if
$db->connClose();
}
else
{
//error while connecting.
?>
<center>
<h2>There has been an error while trying to conenct to the database.<br>Please Try Again.</h2>
<p>If the problem continues, please contact the system administrator.</p>
<br>
<a href="index.php">Main Menu</a>
</center>
<?PHP
} //end of connection if.
[/php]

anyways, two of the three graphs that the user can generate using a page that calls the above code display correctly, but the third doesn't display all the data. Here's some links to see what my output is.

The Good
http://www.candoltd.com/graphics/carman.png
http://www.candoltd.com/graphics/norcran.png

The Bad
http://www.candoltd.com/graphics/pinefalls.png

As you can see, with the pinefalls.png it doesn't display the data correctly. the first point on the x-axis should be on 8.9 not 17. Also, the last part of the graph is cut off, even if i increase the number of x-axis points that are displayed (when i do, it creates a gap on the right side, much like the one at the left side).

I have been trying to fix this for about 6-7 hours now. I haven't been able to find anything in the jpgraph docs, or on their forums (although i have found a few things that i thought might work but didn't). I must apologize since my code is pretty messy, but i guess that's what happens when i get stuck for this long :-)

Anyways, i hope somebody can help, cause i am just about out of ideas on how to fix it. I have a feeling it has something to do with the fact that i have a float data type on the x-axis, which i am starting to think jpgraph doesn't like.

Thanks for the help in advance. I'll keep looking around the jpgraph site/docs and see if i can come up with something.
__________________
Profanity is the one language that all programmers understand.

Check out my Blog <---updated Nov 30 2007!
Pizentios is offline   Reply With Quote
Old May 1st, 2006, 9:22 AM   #2
Pizentios
Programming Guru
 
Pizentios's Avatar
 
Join Date: May 2004
Location: Brandon, Manitoba, Canada
Posts: 2,023
Rep Power: 7 Pizentios is on a distinguished road
Send a message via ICQ to Pizentios Send a message via MSN to Pizentios
Update:

I ahve gotten a little better results. Here's a image of the graph:

http://www.candoltd.com/graphics/pinefalls.png

and the code/changes:
[php]
<?PHP
////////////////////////////////////////////////////////////////////////////////
// Purpose: This file outputs the mapping by mile post speed report. //
// Author: Nick Peters //
// Date Created: 2006-04-26 //
// Date Finished: 2006-04-26 //
////////////////////////////////////////////////////////////////////////////////

include "../include/dbobj.php";
include "../include/graphs/jpgraph.php";
include "../include/graphs/jpgraph_line.php";

function color($tint)
{

$frag = range(0,255);

$red = "";
$green = "";
$blue = "";

for (; {

$red = $frag[mt_rand(0, count($frag)-1)];
$green = $frag[mt_rand(0, count($frag)-1)];
$blue = $frag[mt_rand(0, count($frag)-1)];

switch ($tint) {
case 'light':
if (($red + $green + $blue / 3) >= 200) break 2;
break;
case 'dark' :
default:
if (($red + $green + $blue / 3) <= 50) break 2;
break;
}
}
return sprintf("#%02s%02s%02s", dechex($red), dechex($green),dechex($blue));
}



//data grab.
$db = new dbobj();
$db->setUser("postgres");
$db->setDBname("cemr_tracking");

if ($db->conn())
{
//we have connection
//select the raw data for the given dates.
$raw_data = $db->dbSelect("*", $requiredSubD, "date BETWEEN '" . $requiredStartD . "' AND '" . $requiredEndD . "' ORDER By date ASC");
$raw_months = $db->dbSelect("text, month", "text_months", "");
$raw_miles = $db->dbSelect("mile_number", $requiredSubD . "_miles", "");
if ($raw_data && $raw_months && $raw_miles)
{
for ($k=0; $k<pg_num_rows($raw_miles); $k++)
{
$miles_data = pg_fetch_object($raw_miles, $k);
$miles[$k] = (int)$miles_data->mile_number;
}
for ($y=0; $y<pg_num_rows($raw_months); $y++)
{
$monthD = pg_fetch_object($raw_months, $y);
$text_m[$y] = $monthD->text;
$num_m[$y] = $monthD->month;
}
for ($x=0; $x<pg_num_rows($raw_data); $x++)
{
$data = pg_fetch_object($raw_data, $x);
$d_chunks = explode("-", $data->date);
$result = array_search($d_chunks[1], $num_m);
$result2 = array_search((int)$data->mile_number, $miles);
$month_key[$x] = $text_m[$result];
//averageS == the averages for slow order speed.
//averageR == the averages for the rated speed.
$averageS[$text_m[$result]][$miles[$result2]][0] += $data->slow_order_speed;
$averageS[$text_m[$result]][$miles[$result2]][1] += 1;
$averageR[$miles[$result2]][0] += $data->rated_speed;
$averageR[$miles[$result2]][1] +=1;
}

$actual_months = array_unique($month_key); //gives us the number of months.
$g_width = (count($miles)) * 35;
if ($g_width < 900)
{
$g_width = 900;
}
$graph = new Graph($g_width, 700);
$graph->img->SetMargin(90,90,90,90);
$graph->SetScale("intint",0,0,$miles[0],$miles[count($miles) - 1]);

$graph->xgrid->Show(true);
$real_m = array_values($actual_months);
//echo count($actual_months);
for ($x=0; $x<count($actual_months); $x++)
{
//echo $real_m[$x] . "<br>";
//echo $real_m[$x];
foreach ($miles as $mile)
{
//calculates the average for each mile/slow order speed, dumps back into the array.
//echo "Mile Totals for mile " . $mile . ": " . $averageS[$real_m[$x]][$mile][0] . "<br>";
$total_avgS[$x][$mile] = (int)($averageS[$real_m[$x]][$mile][0] / $averageS[$real_m[$x]][$mile][1]);
//echo "AverageS for Mile " . $mile . " And for the month of " . $real_m[$x] . ": " . $total_avgS[$real_m[$x]][$mile] . "<br>";
//calculates the average for each mile/rated speed, dumps back into the array.
$total_avgR[$mile] = (int)($averageR[$mile][0] / $averageR[$mile][1]);
}
/*echo "<br>";
var_dump($total_avgS);*/
$plot = $total_avgS[$x];
$lineplots[$x] = new LinePlot($plot);
$lineplots[$x]->SetColor(color("light"));
$lineplots[$x]->mark->SetType(MARK_UTRIANGLE);
$lineplots[$x]->SetLegend("Slow Order (Actual) Speed For " . $real_m[$x]);
}

//echo "Values of mile 8.9: " . $total_avgS['Jan']['8.9'] . " , " . $total_avgS['Feb']['8.9'];
print_r($miles);
print_r($total_avgS);
$rated_lineplot = new LinePlot($total_avgR);
$rated_lineplot->SetColor(color("dark"));
$rated_lineplot->mark->SetType(MARK_UTRIANGLE);
$rated_lineplot->SetLegend("Rated Train Speed");
$graph->legend->Pos(0.8,0.03,"center","top");
$graph->yaxis->title->SetFont(FF_FONT1,FS_BOLD);
$graph->xaxis->title->SetFont(FF_FONT1,FS_BOLD);
$graph->title->Set($requiredSubD . " Mapping By Milepost Speed");
$graph->xaxis->SetTickLabels($miles);
$graph->xaxis->SetTitle("Mile Number", "middle");
$graph->xaxis->SetTitleMargin(50);
$graph->yaxis->SetTitle("Speed In Miles Per Hour", "middle");
$graph->yaxis->SetTitleMargin(50);
for ($x=0; $x<count($actual_months); $x++)
{
$graph->Add($lineplots[$x]);
}
$graph->Add($rated_lineplot);
if ($requiredView == "view")
{
$graph->Stroke("/home/nick/CEMR-Tracking/reports/pinefalls.png");
echo "<img src=\"reports/pinefalls.png\">";
}
elseif ($requiredView == "print")
{
$image = $graph->Stroke(_IMG_HANDLER);
$rotated = imagerotate($image, 90.0, 0);
header('Content-type: image/png');
imagepng($rotated);
}

}
else
{
//error while grabing data.
?>
<center>
<h2>There has been an error while trying to get results from the database.<br>Please Try Again.</h2>
<p>If the problem continues, please contact the system administrator.</p>
<br>
<a href="index.php">Main Menu</a>
</center>
<?PHP
} //end of data if
$db->connClose();
}
else
{
//error while connecting.
?>
<center>
<h2>There has been an error while trying to conenct to the database.<br>Please Try Again.</h2>
<p>If the problem continues, please contact the system administrator.</p>
<br>
<a href="index.php">Main Menu</a>
</center>
<?PHP
} //end of connection if.
[/php]

anyways, still haven't figured out why the last part of the graph is cut off. I originally thought that it was because there were float data types in the values that i am ploting, but after throwing in some floor functions in a few places it soon became clear that it wasn't the problem. I am begining to think this is a jpgraph issue rather than an issue with my code. Thanks for any insight in advance.
__________________
Profanity is the one language that all programmers understand.

Check out my Blog <---updated Nov 30 2007!
Pizentios is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:54 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC