phpjpgraph

JpGraph php graph library - trouble embedding graph


I have managed to create a graph using JpGraph, I want to now take it a step further and embed this graph in my template/view php file.

So I put the graph code into a seperate php file to then embed however, when I try to embed it via <embed src="templates/ctl008/graphshow.php" height=80em; width=160em;> </embed> it leaves me with a requested URL not found error.

I have fiddled with the path and tried just: graphshow.php, ctl008/graphshow.php and other such variations. Is it my path that is not correct or is there something else I am missing? I have also tried to embed it with <img src="graphshow.php" /> .

A funny quirk is when I turn on ini_set('display_errors', 1); error_reporting(E_ALL); and try to view the graph on its own I am left with an error message: JpGraph Error: HTTP headers have already been sent. Caused by output from file gd_image.inc.php at line 92. but if i turn off the error_reporting it displays the graph as expected.

I would appreciate any pointers in the right direction with this :)

Here is my graphshow.php file:

<?php require_once ('../library/include/jpgraph/src/jpgraph.php');
require_once ('../library/include/jpgraph/src/jpgraph_line.php');
require_once ('../library/include/jpgraph/src/jpgraph_bar.php');
ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);
$gasArray = array();
foreach($searchArray as $gArray){
    $gasArray[] = $gArray['GASVB'];
}
$stromArray = array();
foreach($searchArray as $sArray){
    $stromArray[] = $sArray['STROVB'];
}
$olArray = array(); 
foreach($searchArray as $oArray){
    $olArray[] = $oArray['OLVB'];
}

 if(!empty($gasArray) || !empty($stromArray) || !empty($olArray)){
     ob_end_clean();
 $width= 1200;
 $height = 400;
 $graph = new Graph($width, $height);
 $graph->SetScale('intint');
 
 $graph->title->Set('Energie Verbrauch');
 $graph->SetMargin(80,25,30,80);
 
 $graph->xaxis->title->Set('Datum');
 $graph->yaxis->title->Set('Verbrauch');
 // print_r($dateArray);
 
 $lineGas = new LinePlot($gasArray);
 $lineGas->SetLegend('Gas');
 $lineStrom = new LinePlot($stromArray);
 $lineStrom->SetLegend('Strom');
 $lineOl = new LinePlot($olArray);
 $lineOl->SetLegend('ÖL');
 
 $graph->Add($lineGas);
 $graph->Add($lineStrom);
 $graph->Add($lineOl);
 $graph->Stroke();
 }
?>

Solution

  • I'd like to start off with a few helpful links:
    JpGraph Error: HTTP headers have already been sent
    http://www.digialliance.com/docs/jpgraph/html/2210intro.html
    https://coursesweb.net/php-mysql/jpgraph-graph-charts-plots-php
    https://jpgraph.net/download/manuals/chunkhtml/ch05s05.html

    now that that is out of the way, how I managed to get it working:

    Starting with the advice/help in the above StackOverflow link I turned my seperate php file into a function the final line of my graphshow.php function was this: $graph->Stroke("charts/graphEnergy.png"); where charts is the folder I am saving in and graphEnergy.png is the name of the file being saved.

    then in my view/template page I require_once('chart/ctl008/graphshow.php'); made my php file accessible, called my function like so generateGraph($gasArray, $stromArray, $olArray);

    and then in the html section embedded the graph with <img src="./charts/graphEnergy.png" />

    What really tripped me up was needing to call the graph.png with the ./ before the charts/graphEnergy.php, and then I was able to display my bootiful graph on my view/template page.

    I hope this helps someone in the future :)