Sunday, November 18, 2007

Dynamic Chart Drawn by JpGraph

Load data from database to JpGraph

1、
copy
example16.2.php from ./src/Examples
and
jpgraph_bar.php、 jpgraph_gradient.php、jpgraph_line.php、jpgraph_plotmark.inc、jpgraph.php from ./src
to current folder

2、create database name: jpg,Table: test
two fields:
id(Key):int
number:int
add some data

3、Modify example16.2.php

#?php
include ("jpgraph.php");
include ("jpgraph_line.php");
include ("jpgraph_bar.php");

$connect=mysql_connect("localhost","root","");
mysql_select_db("jpg",$connect);
$query=mysql_query("select * from test",$connect);
$i=0;
while ($array=mysql_fetch_array($query)) {
$l2datay[$i]=$array["number"];
$i++;
}
mysql_close($connect);

// Create the graph.
$graph = new Graph(400,200,"auto");
$graph->SetScale("textlin");

$graph->img->SetMargin(40,130,20,40);
$graph->SetShadow();

// Create the bar plot
$bplot = new BarPlot($l2datay);
$bplot->SetFillColor("orange");
$bplot->SetLegend("Result");

// Add the plots to t'he graph

$graph->Add($bplot);

$graph->title->Set("Adding a line plot to a bar graph v1");
$graph->xaxis->title->Set("X-title");
$graph->yaxis->title->Set("Y-title");

$graph->title->SetFont(FF_FONT1,FS_BOLD);
$graph->yaxis->title->SetFont(FF_FONT1,FS_BOLD);
$graph->xaxis->title->SetFont(FF_FONT1,FS_BOLD);

//$graph->xaxis->SetTickLabels($datax);
//$graph->xaxis->SetTextTickInterval(2);

// Display the graph
$graph->Stroke();
?>

No comments: