Home php教程 php手册 推荐功能超级强大的PHP画图库JpGraph

推荐功能超级强大的PHP画图库JpGraph

Jun 13, 2016 am 10:38 AM
jpgraph php Drawing Function Gallery powerful recommend use painting Knowledge background

  背景知识

  JpGraph:以前用PHP作图时必须要掌握复杂抽象的画图函数,或者借助一些网上下载的花柱形图、饼形图的类来实现。没有一个统一的chart类来实现图表的快速开发。

  现在我们有了一个新的选择:JpGraph。专门提供图表的类库。它使得作图变成了一件非常简单的事情,你只需从数据库中取出相关数据,定义标题,图表类型,然后的事情就交给JpGraph,只需掌握为数不多的JpGraph内置函数(可以参照JpGraph附带例子学习),就可以画出非常炫目的图表!

  一、 JpGraph安装方法:

  1、先到http://www.aditus.nu/jpgraph/下载最新的版本。

  2、确保你的PHP版本最低为4.04(最好是4.1.1),并且支持GD库。必须确保GD库可以正常运行,可以通过运行phpinfo()来查看GD库的信息是否存在的方法来判断。同时要有要求GD库的版本应为2.0,而不是1.0。

  3、将下载的JpGraph压缩包解压到任意文件夹。

  4、设置jpgraph.php(jpgraph的主配置文件)。设置jpgraph的cache(缓存)文件夹,和TTF(字体)文件夹。

  分别在35行和38行

  35 // DEFINE("CACHE_DIR","/tmp/jpgraph_cache/");

  38 // DEFINE("TTF_DIR","/usr/X11R6/lib/X11/fonts/truetype/");

  Linux系统改为:

  DEFINE("CACHE_DIR","/tmp/jpgraph_cache/");

  DEFINE("TTF_DIR","/usr/X11R6/lib/X11/fonts/truetype/");

  Windows系统改为:

  DEFINE("CACHE_DIR","c:/apache/htdocs/ jpgraph_cache/");

  DEFINE("TTF_DIR","c:/windows/fonts");

  注意事项:

  (1)cache(缓存)文件夹路径可以自己定义,而TTF(字体)文件夹必须是%system%/Fonts。

  (2)确保PHP对cache(缓存)文件夹有写的权限。

  5、完成上述设置后就可以使用JpGraph了,可以先将JpGraph的例子copy到htdocs文件夹中,运行一下看看。呵呵,200多个例子,包含各类图表,够学一阵子的。

  在实际使用中,如果还有一些不懂或者不太明白地方,可以参考bkJia中文发布的 bkJia视频教程85:PHP 图表类 JPGraph 入门配置与应用

  从数据库中读取数据到jpgraph图表中

  1、将./src/Examples目录中的文件example16.2.php以及./src目录中的文件jpgraph_bar.php、jpgraph_gradient.php、jpgraph_line.php、jpgraph_plotmark.inc、jpgraph.php拷贝到同一目录下。

  2、建立数据库jpg,数据库表test

  建立2个字段:

  id(主键):int

  number:int

  并添加一些数据

  3、修改example16.2.php

  修改后的代码

 

<ol class="dp-c">
<li class="alt"><span><span><?php  </span></span></span></li>
<li>
<span class="keyword">include</span><span> (</span><span class="string">"jpgraph.php"</span><span>);  </span>
</li>
<li class="alt">
<span class="keyword">include</span><span> (</span><span class="string">"jpgraph_line.php"</span><span>);  </span>
</li>
<li>
<span class="keyword">include</span><span> (</span><span class="string">"jpgraph_bar.php"</span><span>);  </span>
</li>
<li class="alt">
<span class="vars">$connect</span><span>=mysql_connect(</span><span class="string">"localhost"</span><span>,</span><span class="string">"root"</span><span>,</span><span class="string">""</span><span>);  </span>
</li>
<li>
<span>mysql_select_db(</span><span class="string">"jpg"</span><span>,</span><span class="vars">$connect</span><span>);  </span>
</li>
<li class="alt">
<span class="vars">$query</span><span>=mysql_query(</span><span class="string">"select * from test"</span><span>,</span><span class="vars">$connect</span><span>);  </span>
</li>
<li>
<span class="vars">$i</span><span>=0;  </span>
</li>
<li class="alt">
<span class="keyword">while</span><span> (</span><span class="vars">$array</span><span>=mysql_fetch_array(</span><span class="vars">$query</span><span>)) {  </span>
</li>
<li>
<span class="vars">$l2datay</span><span>[</span><span class="vars">$i</span><span>]=</span><span class="vars">$array</span><span>[</span><span class="string">"number"</span><span>];  </span>
</li>
<li class="alt">
<span class="vars">$i</span><span>++;  </span>
</li>
<li><span>}  </span></li>
<li class="alt">
<span>mysql_close(</span><span class="vars">$connect</span><span>);  </span>
</li>
<li>
<span class="comment">// Create the graph.  </span><span> </span>
</li>
<li class="alt">
<span class="vars">$graph</span><span> = </span><span class="keyword">new</span><span> Graph(400,200,</span><span class="string">"auto"</span><span>);   </span>
</li>
<li>
<span class="vars">$graph</span><span>->SetScale(</span><span class="string">"textlin"</span><span>);  </span>
</li>
<li class="alt">
<span class="vars">$graph</span><span>->img->SetMargin(40,130,20,40);  </span>
</li>
<li>
<span class="vars">$graph</span><span>->SetShadow();  </span>
</li>
<li class="alt">
<span class="comment">// Create the bar plot </span><span> </span>
</li>
<li>
<span class="vars">$bplot</span><span> = </span><span class="keyword">new</span><span> BarPlot(</span><span class="vars">$l2datay</span><span>);  </span>
</li>
<li class="alt">
<span class="vars">$bplot</span><span>->SetFillColor(</span><span class="string">"orange"</span><span>);  </span>
</li>
<li>
<span class="vars">$bplot</span><span>->SetLegend(</span><span class="string">"Result"</span><span>);  </span>
</li>
<li class="alt">
<span class="comment">// Add the plots to the graph </span><span> </span>
</li>
<li>
<span class="vars">$graph</span><span>->Add(</span><span class="vars">$bplot</span><span>);  </span>
</li>
<li class="alt">
<span class="vars">$graph</span><span>->title->Set(</span><span class="string">"Adding a line plot to a bar graph v1"</span><span>);  </span>
</li>
<li>
<span class="vars">$graph</span><span>->xaxis->title->Set(</span><span class="string">"X-title"</span><span>);  </span>
</li>
<li class="alt">
<span class="vars">$graph</span><span>->yaxis->title->Set(</span><span class="string">"Y-title"</span><span>);  </span>
</li>
<li>
<span class="vars">$graph</span><span>->title->SetFont(FF_FONT1,FS_BOLD);  </span>
</li>
<li class="alt">
<span class="vars">$graph</span><span>->yaxis->title->SetFont(FF_FONT1,FS_BOLD);  </span>
</li>
<li>
<span class="vars">$graph</span><span>->xaxis->title->SetFont(FF_FONT1,FS_BOLD);  </span>
</li>
<li class="alt">
<span class="comment">//$graph->xaxis->SetTickLabels($datax); </span><span> </span>
</li>
<li>
<span class="comment">//$graph->xaxis->SetTextTickInterval(2); </span><span> </span>
</li>
<li class="alt">
<span class="comment">// Display the graph </span><span> </span>
</li>
<li>
<span class="vars">$graph</span><span>->Stroke();  </span>
</li>
<li class="alt"><span>?> </span></li>
</ol>
Copy after login

 4、刷新页面即可看到结果

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

See all articles