Question

The problem is that the JpGraph is not displayed correctly on my web-page. The strange thing is that if I run the above code in isolation, then it works. But if I insert it in my main code, it fails producing the above-shown message. P.S. I'm using 'ob_start();', but it does not solve the problem.

// A new graph with automatic size
$graph = new GanttGraph (0,0, "auto");

//  A new activity on row '0'
$activity = new GanttBar (0,"Project", "2001-12-21", "2002-02-20");
$graph->Add( $activity);

// Display the Gantt chart
$graph->Stroke();
?> 
</div>

JpGraph Error: HTTP headers have already been sent.
Caused by output from file index.php at line 85.
Explanation:
HTTP headers have already been sent back to the browser indicating the data as text before the library got a chance to send it's image HTTP header to this browser. This makes it impossible for the library to send back image data to the browser (since that would be interpretated as text by the browser and show up as junk text).

Most likely you have some text in your script before the call to Graph::Stroke(). If this texts gets sent back to the browser the browser will assume that all data is plain text. Look for any text, even spaces and newlines, that might have been sent back to the browser.

For example it is a common mistake to leave a blank line before the opening "<?php".
Was it helpful?

Solution

JpGraphs can't exist in files with html. They have to be in a pure php file. To get around this, I created a seperate file that generates the graph, and made the whole thing a function. At the end, change

$graph->Stroke();

to

$graph->Stroke(".<filepaht>.jpg");

Then, in your index.php page, reference the image file.

So, what it looks like you need is,

createjpgraph.php:

<?php 
function GenGraph (<input variables>) {

    // A new graph with automatic size        
    $graph = new GanttGraph (0,0, "auto");        

    //  A new activity on row '0'        
    $activity = new GanttBar (0,"Project", "2001-12-21", "2002-02-20");        
    $graph->Add( $activity);        

    // Display the Gantt chart        
    $graph->Stroke("./foler/file.jpg");
}
?> 

index.php:

...
<div>
...
<?php
include 'createjpgraph.php';
GenerateGraph(<variables>);
?>
<img src=\"./folder/file.jpg\" />
</div>

Hope this works for you.

OTHER TIPS

You can even do it in the same html document - first writing the graph to a file, then displaying it ...

//Code generating your graph here ...

// Finally output the image to a file
$graph->Stroke("/var/www/html/tmp/out.jpg");

//end of php code
?>

<!-- now include the newly generated image in the HTML -->
<img src="/tmp/out.jpg">

</body>
</html>

It is seem that the jpeg file that is being created by the function cannot be over written in my case...

to do overwrite it.. I changed my JPGraph file gd_image.inc.php .. you have to comment out the line that says JpGraphError::RaiseL(25111,$aStrokeFileName)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top