search
HomeBackend DevelopmentPHP TutorialJpGraph php柱状图使用介绍_PHP

JpGraph简介  

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

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

JpGraph安装方法:
1、
  先到各大网站上下载最新的版本。如: http://www.bitsCN.com/codes/38194.html
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多个例子,包含各类图表,够学一阵子的。
  在实际使用中,笔者还遇到了一些问题,比如字体错误等等,还在研究中……
从数据库中读取数据到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
  修改后的代码
复制代码 代码如下:
    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发布
Statement
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
Optimize PHP Code: Reducing Memory Usage & Execution TimeOptimize PHP Code: Reducing Memory Usage & Execution TimeMay 10, 2025 am 12:04 AM

TooptimizePHPcodeforreducedmemoryusageandexecutiontime,followthesesteps:1)Usereferencesinsteadofcopyinglargedatastructurestoreducememoryconsumption.2)LeveragePHP'sbuilt-infunctionslikearray_mapforfasterexecution.3)Implementcachingmechanisms,suchasAPC

PHP Email: Step-by-Step Sending GuidePHP Email: Step-by-Step Sending GuideMay 09, 2025 am 12:14 AM

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

How to Send Email via PHP: Examples & CodeHow to Send Email via PHP: Examples & CodeMay 09, 2025 am 12:13 AM

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

Advanced PHP Email: Custom Headers & FeaturesAdvanced PHP Email: Custom Headers & FeaturesMay 09, 2025 am 12:13 AM

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Guide to Sending Emails with PHP & SMTPGuide to Sending Emails with PHP & SMTPMay 09, 2025 am 12:06 AM

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

What is the best way to send an email using PHP?What is the best way to send an email using PHP?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

Best Practices for Dependency Injection in PHPBest Practices for Dependency Injection in PHPMay 08, 2025 am 12:21 AM

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHP performance tuning tips and tricksPHP performance tuning tips and tricksMay 08, 2025 am 12:20 AM

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

See all articles

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

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

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor