0

I have this set of codes from JpGraph to help me with creating a bar chart.

<?php
require_once ('src/jpgraph.php');
require_once ('src/jpgraph_bar.php');
$datay=array(1992,1993,1995,1996,1997,1998,2001);

// Size of graph
$width=400;
$height=500;

// Set the basic parameters of the graph
$graph = new Graph($width,$height);
$graph->SetScale('textlin');

$top = 60;
$bottom = 30;
$left = 80;
$right = 30;
$graph->Set90AndMargin($left,$right,$top,$bottom);

// Nice shadow
$graph->SetShadow();

// Setup labels
$lbl = array("Andrew\nTait","Thomas\nAnderssen","Kevin\nSpacey","Nick\nDavidsson",
"David\nLindquist","Jason\nTait","Lorin\nPersson");
$graph->xaxis->SetTickLabels($lbl);

// Label align for X-axis
$graph->xaxis->SetLabelAlign('right','center','right');

// Label align for Y-axis
$graph->yaxis->SetLabelAlign('center','bottom');

// Titles
$graph->title->Set('Number of incidents');

// Create a bar pot
$bplot = new BarPlot($datay);
$bplot->SetFillColor('orange');
$bplot->SetWidth(0.5);
$bplot->SetYMin(1990);

?>

However, this will only work if I put it right at the top of my code. If I put it anywhere else, it will fail to display. Is there any way to overcome this so that if I put the code specifically at one place for example under it will appear there? Also, I'll be using some data from my own database as values for this graph.

Thank you.

  • Do you get any error messages? – gen_Eric Jan 19 '12 at 20:03
  • "JpGraph Error: HTTP headers have already been sent. Caused by output from file studentcourse.php at line 38." – user1159340 Jan 19 '12 at 20:05
  • That message happens when you echo something before a `header` call. Make sure you aren't echoing anything before this code (when you put it in another spot). – gen_Eric Jan 19 '12 at 20:06
  • See also [Headers already sent](http://stackoverflow.com/questions/8028957/headers-already-sent-by-php), and remember that you can't mix html and binary output in one script. – mario Jan 19 '12 at 23:43

4 Answers4

1

Requires/includes are resolved at runtime, not during parse. This allows them to be invoked dynamically. Therefore, they must be completed before any of their code can be referenced.

Alternative methods to look at are autoload, spl_autoload. This allows class files to be loaded on first reference.

However, from your comments, it appears that the issue is the usage of JpGraph requiring the sending of headers. You'll need to check studentcourse.php to see if any output is generated (including inadvertent whitespace).

webbiedave
  • 48,414
  • 8
  • 88
  • 101
1

JpGraph creates an image, which is displayed. You can't output text and images at the same time.

0

Well, you can't escape the fact that when you call new Graph(), the class should already exist, so if you include the files after that line it'll never work.

cambraca
  • 27,014
  • 16
  • 68
  • 99
0

This generates an image, right?

I would put this in a file by itself, and then on the page where you want the graph to show up, just do:

<img src="myGraph.php">
  • No, if you put this lines inside the main code, then it will overwrite other things and you will see just the only graph image in your browser without any other content of your web-page. – Klausos Klausos May 07 '12 at 19:43