Jump to content

Need help w making graphs in PHP using JPGraph

- - - - -

  • Please log in to reply
No replies to this topic

#1
Darkone85

Darkone85

    Learning Programmer

  • Members
  • PipPipPip
  • 30 posts
Hi! We have an assignment that is using the data from an .TCX-file (xml) and presenting it in php. We are also to display an graph of these data.

We have been able to get the pure data from de tcx-file but the graph is harder. We have our tcx-file in our www-map and theres where we also have JPGraph. This is our code:

<?php

$dom = new DOMDocument();

$dom->load('G2.tcx');

include ('C:\wamp\www\JPGraph\src\jpgraph.php');

include ('C:\wamp\www\JPGraph\src\jpgraph_line.php');


$xml = simplexml_load_file('G2.tcx');

$activity = $dom->getElementsByTagName('Activity');

$maxBpm = $dom->getElementsByTagName('MaximumHeartRateBpm');

$snittBpm = $dom->getElementsByTagName('AverageHeartRateBpm');

$lap = $dom->getElementsByTagName('Lap');


$food = $dom->getElementsByTagName('Trackpoint');


function secondsToWords($seconds)

{

    /*** return value ***/

    $ret = "";


    /*** get the hours ***/

    $hours = intval(intval($seconds) / 3600);

    if($hours > 0)

    {

        $ret .= "$hours hours ";

    }

    /*** get the minutes ***/

    $minutes = bcmod((intval($seconds) / 60),60);

    if($hours > 0 || $minutes > 0)

    {

        $ret .= "$minutes minutes ";

    }

  

    /*** get the seconds ***/

    $seconds = bcmod(intval($seconds),60);

    $ret .= "$seconds seconds";


    return $ret;

}

?>



<?php

//Loop through each item

foreach ($activity as $selem) {

	$date = explode("T", $selem->getElementsByTagName('Id')->item(0)->nodeValue);

	echo "Datum: $date[0]<br />";

	$time = substr_replace($date[1], "", 8);

	echo "Tid: $time <br />";

}

?>


<?php

//Loop through each item

foreach ($lap as $belem) {

	$distance = $belem->getElementsByTagName('DistanceMeters')->item(0)->nodeValue;

	

	echo "Distans: " .round(($belem->getElementsByTagName('DistanceMeters')->item(0)->nodeValue)/1000, 2) ."km<br />";

}

?>

<?php

//Loop through each item

foreach ($lap as $belem) {

	$strTime = $belem->getElementsByTagName('TotalTimeSeconds')->item(0)->nodeValue;

	

	echo "Totaltid: " .secondsToWords($strTime). "<br />";

}

?>


<?php

//Loop through each item

foreach ($lap as $belem) {

	$maxSpeed = round($belem->getElementsByTagName('MaximumSpeed')->item(0)->nodeValue * 3.6, 2);

	

	echo "Maxhastighet: $maxSpeed km/h<br />";

}

?>


<?php

//Loop through each item

	$averageSpeed = round((($distance / $strTime)*3.6), 2);


	echo "Snitthastighet: $averageSpeed km/h<br />";

?>


<?php

//Loop through each item

foreach ($maxBpm as $gelem) {

	$maxBpm = $gelem->getElementsByTagName('Value')->item(0)->nodeValue;

	echo "Maxpuls: $maxBpm bpm<br />";

}

?>


<?php

//Loop through each item

foreach ($snittBpm as $gelem) {

	$snittBpm = $gelem->getElementsByTagName('Value')->item(0)->nodeValue;

	echo "Snittpuls: $snittBpm bpm<br />";

}

?>


<?php

//Loop through each item

$bpmArray = array();

foreach ($food as $elem) {

	array_push($bpmArray, $elem->getElementsByTagName('HeartRateBpm')->item(0)->nodeValue);

}

?>

<br>

<?php

foreach ($bpmArray as $value){

	echo $value;	

}


$graph = new Graph(400, 200, "auto");    

$graph->SetScale('textlin');


// Add the graph

$graph->SetShadow();

$line1 = new LinePlot($bpmArray);

$graph->Add($line1);

$graph->Stroke();


// Setup margin and titles

$graph->img->SetMargin(40,20,20,40);

$graph->title->Set('Implementationsuppgift: XML Parser');

$graph->xaxis->title->Set("X-axis");

$graph->yaxis->title->Set("Y-axis");


// Setup text layout

$graph->title->SetFont(FF_FONT1,FS_BOLD);

$graph->yaxis->title->SetFont(FF_FONT1,FS_BOLD);

$graph->xaxis->title->SetFont(FF_FONT1,FS_BOLD);

$line1->SetColor("blue");

$line1->SetWeight(2);  // Two pixel wide

$graph->yaxis->SetColor("red");

$graph->yaxis->SetWidth(2);

$graph->SetShadow();


?>

<br>



I attached a ss of the printout from our localhost and the error message reads "Error: 25044 Cannot use auto scaling since it is impossible to determine a valid min/max value of the Y-axis (only null values)."

We tried to change the values all over the place but cant find the right ones and the web is kinda short of just this problem.

Can anyone of you guys help?

Thanks in advance,
Manne

Attached Files






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users