Connecting Tech Pros Worldwide Forums | Help | Site Map

working with jpgraph

Familiar Sight
 
Join Date: Jan 2008
Posts: 199
#1: Apr 9 '08
im using jp graph and i want to put a graph code at the middle of my table. Then I got error:
JpGraph Error: HTTP headers have already been sent.
Caused by output from file test.php at line 2.
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".


Could anyone tell me how can i solve that?

TheServant's Avatar
Expert
 
Join Date: Feb 2008
Location: Australia
Posts: 914
#2: Apr 9 '08

re: working with jpgraph


I'm confused? Did u check the top of the page where it says <?php ? Straight after the word php, if there is a space (black character) you will have a problem. That's what your post says, so you would already know that. Can you post the codefor the jpgraph? If it's too big, post your page code (mainly where your header is) and then have a look in the jpgraph code for the word header and post surrounding code for that.
Familiar Sight
 
Join Date: Jan 2008
Posts: 199
#3: Apr 9 '08

re: working with jpgraph


Quote:

Originally Posted by TheServant

I'm confused? Did u check the top of the page where it says <?php ? Straight after the word php, if there is a space (black character) you will have a problem. That's what your post says, so you would already know that. Can you post the codefor the jpgraph? If it's too big, post your page code (mainly where your header is) and then have a look in the jpgraph code for the word header and post surrounding code for that.

Thanks for your reply. This is my graph code and this is working.
[PHP]
<?php
include ("../rainfall_ITI/jpgraph2.3/src/jpgraph.php");
include ("../rainfall_ITI/jpgraph2.3/src/jpgraph_bar.php");
include 'dbconnect.php';

$sql = mysql_query("SELECT Rainfall,Date FROM rainfall WHERE Station_ID = 'st1 St 02' LIMIT 0, 10") or die(
mysql_error());


while($row = mysql_fetch_array($sql))
{
$i=0;

$Query1 = explode(" ",$row['Date']);
$Query2 = $Query1[0];

$data[] = $row[0];
$leg[] = $Query2[8].$Query2[9];

}

$graph = new Graph(300,300,"auto");// set the size of the image
$graph->SetScale("textint");
$graph->img->SetMargin(40,30,30,60);
//$graph->AdjBackgroundImage(0.4,0.7,-1); //setting BG type
//$graph->SetBackgroundImage("linux_pez.png",BGIMG_FILLFRAM E); //adding image
$graph->SetShadow();

$graph->xaxis->SetTickLabels($leg);
$bplot = new BarPlot($data);
$bplot->SetFillColor("darkblue"); // Fill color

// Set up the title for the graph
//$graph->title->Set("Rainfall During last 10 hours"."($Query2)");
$graph->title->Set("Rainfall During last 10 hours");
$graph->title->SetFont(FF_VERDANA,FS_NORMAL,10);
$graph->title->SetColor("darkred");

// Setup X-axis labels
$graph->xaxis->title->Set("Date");
//$graph->xaxis->SetLabelAngle(90);
$graph->yaxis->title->Set("Rainfall(mm)");

$bplot->value->Show();
$bplot->value->SetFont(FF_ARIAL,FS_BOLD);
$bplot->value->SetAngle(45);
$bplot->value->SetColor("black","navy");

$graph->Add($bplot);
$graph->Stroke(); ?>

[/PHP]


when i put the above code in to my table it gave an error
[PHP]
<table>
<tr>
<td width="60">&nbsp;</td>
<td>
/* jp graph code add here*/
</td>
</tr>
</table>
[/PHP]
TheServant's Avatar
Expert
 
Join Date: Feb 2008
Location: Australia
Posts: 914
#4: Apr 9 '08

re: working with jpgraph


Are there any headers set in
"../rainfall_ITI/jpgraph2.3/src/jpgraph.php" and "../rainfall_ITI/jpgraph2.3/src/jpgraph_bar.php"
Familiar Sight
 
Join Date: Jan 2008
Posts: 199
#5: Apr 9 '08

re: working with jpgraph


Quote:

Originally Posted by TheServant

Are there any headers set in
"../rainfall_ITI/jpgraph2.3/src/jpgraph.php" and "../rainfall_ITI/jpgraph2.3/src/jpgraph_bar.php"

This is the first few lines in jpgraph.php
[PHP]
<?php
//================================================== =====================
// File: JPGRAPH.PHP
// Description: PHP Graph Plotting library. Base module.
// Created: 2001-01-08
// Ver: $Id: jpgraph.php 957 2007-12-01 14:00:29Z ljp $
//
// Copyright 2006 (c) Aditus Consulting. All rights reserved.
//================================================== ======================

require_once('jpg-config.inc.php');
require_once('jpgraph_gradient.php');
require_once('jpgraph_errhandler.inc.php');
require_once('jpgraph_ttf.inc.php');

// Version info
DEFINE('JPG_VERSION','2.3');

// Minimum required PHP version
DEFINE('MIN_PHPVERSION','5.1.0');

// Should the image be a truecolor image?
DEFINE('USE_TRUECOLOR',true);
..................................

[/PHP]
TheServant's Avatar
Expert
 
Join Date: Feb 2008
Location: Australia
Posts: 914
#6: Apr 9 '08

re: working with jpgraph


If it's a header problem, it's likely to be in one of those files. Provided you ahven't changed any orders of those include commands from the original set up, my bet would be 'jpg-config.inc.php'. What I am looking for and what you can look for is the php function header() like header(Location:somepage.php). If you can find that, there's where your prob is. I hope I'm not leading you on a wild goose chase, but if you're getting a header warning it's a good thing to look for. Another thing is do you have session_start() anywhere?
Familiar Sight
 
Join Date: Jan 2008
Posts: 199
#7: Apr 9 '08

re: working with jpgraph


Quote:

Originally Posted by TheServant

If it's a header problem, it's likely to be in one of those files. Provided you ahven't changed any orders of those include commands from the original set up, my bet would be 'jpg-config.inc.php'. What I am looking for and what you can look for is the php function header() like header(Location:somepage.php). If you can find that, there's where your prob is. I hope I'm not leading you on a wild goose chase, but if you're getting a header warning it's a good thing to look for. Another thing is do you have session_start() anywhere?


Thanks for your reply. But i could not understand about header thing? I don't have session_start() in any jpgraph pages. but having all my ather pages.
Ranjan kumar Barik's Avatar
Member
 
Join Date: Aug 2007
Posts: 95
#8: Apr 9 '08

re: working with jpgraph


Quote:

Originally Posted by ghjk

when i put the above code in to my table it gave an error
[PHP]
<table>
<tr>
<td width="60">&nbsp;</td>
<td>
/* jp graph code add here*/
</td>
</tr>
</table>
[/PHP]

I don't think you can use jp Graph code like this inside a table or even inside a html file.
It must be a separate pure php file. Otherwise if server found any html code before your php code, it will start sending them to browser before processing your jp Graph code and so you will get an error like header already send.
To solve this, I think you can can create a separate jp Graph file with the code you have, run the code, save the graph image and display it inside your table. To get the updated image, you have to run the jp Graph file and save the image automatically.

Thanks
Familiar Sight
 
Join Date: Jan 2008
Posts: 199
#9: Apr 10 '08

re: working with jpgraph


Quote:

Originally Posted by Ranjan kumar Barik

I don't think you can use jp Graph code like this inside a table or even inside a html file.
It must be a separate pure php file. Otherwise if server found any html code before your php code, it will start sending them to browser before processing your jp Graph code and so you will get an error like header already send.
To solve this, I think you can can create a separate jp Graph file with the code you have, run the code, save the graph image and display it inside your table. To get the updated image, you have to run the jp Graph file and save the image automatically.

Thanks

Could you please tell me how can i do that? Really in a trouble..
Ranjan kumar Barik's Avatar
Member
 
Join Date: Aug 2007
Posts: 95
#10: Apr 10 '08

re: working with jpgraph


Quote:

Originally Posted by ghjk

Could you please tell me how can i do that? Really in a trouble..

You just create a php file for you graph. Then at the end in the graph->stroke() function give a path to where you want to save the graph image.

[PHP]graph->stroke('file path');[/PHP]

See if it is saved or not. The path must have a file permission to save the image. If its done then you put the hole code inside a function. Pass all parameters the function requires and call it in your pages. Every time your page loads the function will be called and the image will be replaced with the updated one. Then you can show the saved image anywhere you want.

You function should be defined before you call it.

:)
Reply


Similar PHP bytes