Search code examples
phpjpgraph

How to add graph to website?


I'm a beginner here and need help. I have this code, which works and outputs a graph in my browser (if this is the only code in php file). I don't know how to add text below or above just like any other site. When I try, it returns my whole code in the browser. How do I go on about this?

<?php 
  // content="text/plain; charset=utf-8"
  require_once ('jpgraph/src/jpgraph.php');
  require_once ('jpgraph/src/jpgraph_line.php');
  // Some data
  $ydata = array(11,3,8,12,5,1,9,8,5,7);
  // Create the graph. These two calls are always required
  $graph = new Graph(350,250);
  $graph->SetScale('textlin');
  // Create the linear plot
  $lineplot=new LinePlot($ydata);
  $lineplot->SetColor('blue');
  // Add the plot to the graph
  $graph->Add($lineplot);
  // Display the graph
  $graph->Stroke();
?>

Thanks in advance!


Solution

  • What you need is quite simply:

    <img src="graph.php">
    

    Put that in a separate HTML file or PHP script. You cannot output the image and text in the same script / web page. It needs to be separated.

    Don't worry about the .php extension for the image src= attribute. It will display despite the lack of .jpeg extension. (The Graph class already outputs the correct MIME type I assume.)