search
HomeBackend DevelopmentPHP TutorialPHP图形操作之Jpgraph学习笔记_php技巧

本文实例讲述了PHP图形操作之Jpgraph学习笔记。分享给大家供大家参考,具体如下:

一、Jpgraph安装配置

下载Jpgraph安装包

解压后放置磁盘中,(最好是放在和网页一起)

修改php.ini文件:

1、内存  memory_limit=X,至少为32M

2、执行时间  max_execution_time=X,对于复杂的图片加载时需要较多的时间,根据图片的复杂度做相应的修改

3、缓存  output_buffering  注释掉缓存,便于调试

二、使用Jpgraph创建图形的基本步骤(必要的)

1、包含所需要的类库文件

复制代码 代码如下:
require_once();

2、初始化数据

复制代码 代码如下:
$data=array();

可以是php程序中的固定数据,即静态数据,直接得到
可以是存储在文本文件中的数据
可以是存储在数据库中的数据
可以是通过URL参数传递的数据( GET 或 POST方式)

3、 创建Graph类实例

复制代码 代码如下:
$graph=new Graph();

可以在此设置图形的尺寸

4、设置标题、x轴标题、y轴标题的内容,及其字体、颜色、位置等

5、创建对应的图实例

可以是折线图、柱形图、饼状图,3d等

6、将数据添加到图形上

复制代码 代码如下:
$graph->Add();

7、显示图片

复制代码 代码如下:
$graph->Stroke();

至此一个简单的图形就完成了

注意:

中文字体乱码

Gpgraph默认显示汉字时是把汉字编码认为gb2312,转化为utf-8以后再显示,如果文件的编码方式是gb2312,只需把SetFont()方法的第一个参数设置为FF_SIMSUN即可
如果是utf-8编码的,需要先把汉字编码转化为gb2312,这样汉字才能正常显示
转换编码方式可以使用  iconv("UTF-8","gb2312",$x);

一些常用的方法:

$graph->title->Set('设置图表的标题');
$graph->tabtitle->Set('设置图片头部文字');
$graph->xaxis->title->Set("设置X轴的标题");
$graph->yaxis->title->Set("设置Y轴的标题");
$graph->SetScale('textlin');//设置刻度值类型
$graph->img->SetMargin(50,40,40,55);//边框间距(左右上下)
$graph->title->SetFont(FF_SIMSUN,FS_BOLD,12);//标题字体
$graph->xaxis->title->SetFont(FF_SIMSUN,FS_BOLD,10);
$graph->xaxis->title->SetFont(FF_SIMSUN,FS_BOLD,10);
$graph->xaxis->SetFont(FF_SIMSUN,FS_BOLD,12);//坐标柱上的字体
$graph->yaxis->SetFont(FF_SIMSUN,FS_BOLD,12);
$graph->title->SetColor('red'); ///标题颜色
$graph->xaxis->title->SetColor('red');
$graph->yaxis->title->SetColor('red');
$graph->xaxis->title->SetMargin(20);//距离坐标轴的距离
$graph->yaxis->title->SetMargin(20);//距离Y轴的距离
$linepot->SetColor('red');//折线的颜色(折线图)
$linepot->SetWeight(2);//折线的宽度
$linepot->value->SetFormat('%0.1f'); //值的格式化
$linepot->value->show(true);//显示值
$graph->SetBackGroundImage ( );设置背景
$graph->SetMarginColor('lightblue');//设置图形颜色
$graph->SetShadow();//
$graph->Set3DPerspecttive(); //设置3d效果图
/*倾斜3D效果 
 1、' SKEW3D_UP '
 2、' SKEW3D_DOWN'
 3、' SKEW3D_LEFT'
 4、' SKEW3D_RIGHT'*/
$p1->SetTheme('water');//设置样式
$p1->SetCenter(0.5,0.55);//设置图形位置
$graph->legend->Pos(0.1,0.9);//设置注释文字的位置
$graph->legend->SetFont(FF_SIMSUN,FS_BOLD,12);//设置注释文字的字体

希望本文所述对大家PHP程序设计有所帮助。

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
PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

How to make PHP applications fasterHow to make PHP applications fasterMay 12, 2025 am 12:12 AM

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

PHP Performance Optimization Checklist: Improve Speed NowPHP Performance Optimization Checklist: Improve Speed NowMay 12, 2025 am 12:07 AM

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

PHP Dependency Injection: Improve Code TestabilityPHP Dependency Injection: Improve Code TestabilityMay 12, 2025 am 12:03 AM

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

PHP Performance Optimization: Database Query OptimizationPHP Performance Optimization: Database Query OptimizationMay 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

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 Article

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),