Creating a graph in PHP using PHPLOT object

Here is my example of creating a graph using an object – PHPLOT.  PHPLOT is an easy-to-use object which is now available and freeing yourself from lengthy code.  The only requirement is your knowledge in using a class, properties and methods.  The object is available is around the corner.

*********************************/
require_once(“mysqlidb.class.php”);
require_once(“config.php”);
require_once(“phplot.php”);

/* initialize connection */
$report = new mysqlidb(DB_HOST,DB_USER,DB_PWORD,DB_DATABASE);
$report->dbConnect(); //specify connection

$year = (!isset($_POST['year']))?date(“Y”):$_POST['year'];
/* retrieve result from database */
$query = “SELECT monthna,revenue FROM listofmonthlyrev WHERE ryear = $year”;
$report->viewRecord($query);

/* create an array */
$array_rep = array();
$ctr = 0;
while ($row = $report->fetchArray()){
$array_rep[$ctr] = array($row[0],$row[1]);
$ctr++;
}
/* free result */
$report->freeResult();

/* instantiate and define graph object */
$graph = new PHPlot();

/* assign data to plot */
$graph->SetImageBorderType(‘plain’);
$graph->SetPlotType(‘bars’);
$graph->SetDataType(‘text-data’);
$graph->SetDataValues($array_rep);

/* Main plot title */
$graph->SetTitle(“REVENUE FOR THE YEAR $year”);

/* Make sure Y=0 is displayed */
$graph->SetPlotAreaWorld(NULL, 0);

/* Y Tick marks are off, but Y Tick Increment also controls the Y grid lines */
$graph->SetYTickIncrement(100);

/* Turn on Y data labels */
$graph->SetYDataLabelPos(‘plotin’);

/* Turn off X tick labels and ticks because they don’t apply here: */
$graph->SetXTickLabelPos(‘none’);
$graph->SetXTickPos(‘none’);

/* With Y data labels, we don’t need Y ticks or their labels, so turn them off */
$graph->SetYTickLabelPos(‘none’);
$graph->SetYTickPos(‘none’);

/* Format the Y Data Labels as numbers with 1 decimal place. */
/* Note that this automatically calls SetYLabelType(‘data’). */
$graph->SetPrecisionY(1);

/* draw the graph */
$graph->DrawGraph();

/* close connection */
$report->disConnect();

graph using phplot

graph using phplot

Leave a Reply

Your email address will not be published. Required fields are marked *

*