Introduction to JpGraph
In the past, when drawing with PHP, you had to master complex and abstract drawing functions, or use some column chart and pie chart classes downloaded from the Internet to achieve it. There is no unified chart class to achieve rapid development of charts.
Now we have a new choice: JpGraph. A class library dedicated to providing charts. It makes drawing a very simple thing. You only need to retrieve the relevant data from the database, define the title and chart type, and then leave the rest to JpGraph. You only need to master a few JpGraph built-in functions (you can Follow the example provided with JpGraph to learn) and you can draw very dazzling charts!
JpGraph installation method:
1.
First download the latest version from major websites. For example: http://www.jb51.net/codes/38194.html
2,
Make sure your PHP version is at least 4.04 (preferably 4.1.1) and supports the GD library. You must ensure that the GD library can run normally. You can check whether the GD library information exists by running phpinfo(). At the same time, it is required that the version of the GD library should be 2.0, not 1.0.
3.
Extract the downloaded JpGraph compressed package to any folder.
4.
Set up jpgraph.php (jpgraph’s main configuration file). Set jpgraph's cache (cache) folder and TTF (font) folder.
Copy the code
at lines 35 and 38 respectively The code is as follows:
35 // DEFINE("CACHE_DIR" ,"/tmp/jpgraph_cache/");
38 // DEFINE("TTF_DIR","/usr/X11R6/lib/X11/fonts/truetype/");
Linux system Change to:
Copy the code The code is as follows:
DEFINE("CACHE_DIR","/tmp/jpgraph_cache/"); '
DEFINE("TTF_DIR","/usr/X11R6/lib/X11/fonts/truetype/");
Windows system changed to:
Copy code The code is as follows:
DEFINE("CACHE_DIR","c:/apache/htdocs/ jpgraph_cache/");'
DEFINE("TTF_DIR ","c:/windows/fonts");
Notes:
(1) The cache (cache) folder path can be defined by yourself, while the TTF (font) folder must be %system%/Fonts.
(2) Make sure PHP has write permissions on the cache folder.
5.
After completing the above settings, you can use JpGraph. You can first copy the JpGraph example to the htdocs folder and run it to take a look. Haha, there are more than 200 examples, including various charts, which is enough to study for a while.
In actual use, the author also encountered some problems, such as font errors, etc., which are still being studied...
Read data from the database into the jpgraph chart
1.
Will. The file example16.2.php in the /src/Examples directory and the files jpgraph_bar.php, jpgraph_gradient.php, jpgraph_line.php, jpgraph_plotmark.inc, and jpgraph.php in the ./src directory are copied to the same directory.
2.
Create database jpg, database table test
Create 2 fields:
id (primary key): int
number: int
And add some data
3.
Modify example16.2.php
Modified code
Copy code The code is as follows:
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();
?>
4、
刷新页面即可看到结果
历史信息
网络优化,300*200的图片大小大概为2K,但普通图片要比JpGraph生成的大4-5K
支持GD1和GD2,并且JpGraph会自动探测系统安装了哪个库
支持多种图表样式,包括常见的网状图、花柱形图、饼形图(2D和3D的都可以)等等
支持3D透明,α混合技术
支持超过400种的已命名颜色
支持多种方式带背景图片的绘图
支持生成的图表网络缓存以减轻HTTP服务器负担
2009年9月17日:更新JpGraph 1.27.1。
2009年4月18日:更新JpGraph 1.27和JpGraph 2.34。
12月2日:今日有两位phpchina的朋友加入我们的翻译团队:刺猬和Deman。
2008年6月28日:JpGraph中文站发布,虽然这是用ASP制作的网站,但并不代表站长偏爱ASP
2008年6月15日:JpGraph 1.26发布。修复了饼形图分割片问题。也许这将是1.x发布版宣布停止更新后的最后一版2
008年6月14日:JpGraph 2.33发布
http://www.bkjia.com/PHPjc/324128.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/324128.htmlTechArticleJpGraph简介 以前用PHP作图时必须要掌握复杂抽象的画图函数,或者借助一些网上下载的花柱形图、饼形图的类来实现。没有一个统一的char...