标题: php一个很不错的作图类JpGraph
smallcat
新手上路
Rank: 1



UID 65
精华 2
积分 20
帖子 10
翻译 0
原创 0
阅读权限 10
注册 2007-6-9
状态 离线
发表于 2007-6-9 22:29  资料  短消息  加为好友  添加 smallcat 为MSN好友 通过MSN和 smallcat 交谈
php一个很不错的作图类JpGraph

今天新来的。也不知道写点什么好。想想还是把自己以前写在BLOG上的一篇文章拿来吧。呵呵

PHP绘图类库jpgraph,
下载地址:http://www.aditus.nu/jpgraph/jpdownload.php
开始以为他不支持中文,所以先在网上找了教程,教程写着要设置字库路径等。结果我下的那个版本一点都不用设置人家就写好了。如果是WIN系统就是根据环境变量找到系统的要目录。在加上“/fonts/”从而自动就找到字体了。
当然如果是UNIX你可以把你WINDOWS/FONTS下的所有的TTF文件和一个simsun.ttc文件传入一个目录上。然后在jpgraph的src目录下找到jpgraph.php文件有一句
DEFINE('TTF_DIR','/usr/X11R6/lib/X11/fonts/truetype/');
改成你上传的那个字体目录就OK了。

在你运行时可能会遇到说错误说你的PHP少FREETYPE2.支持
你可以安装一下FREETYPE并编译进你的PHP。你只要在你的./configure选项加入一个 --with-freetype-dir=/usr/local/include/freetype2 我的freetype2是在这个下面的。你可以看看你安装在哪了。改成你的目录就可以了。


只不过要有写代码时加入一个$graph->xaxis->title->SetFont(FF_SIMSUN,FS_BOLD);
其中FF_SIMSUN就是中文,后面那个是字体的样试。



背景图是可以自己指定的。
这个其实没有什么难点中不过那个右上角的那些中文显示有点难度,因为那个类没有SetFont方法,也就是说那东西是不能显示中文的。所以我就改了基础类库
修改了jpgraph.php文件里的。
class Legend 类中有这样一句
  private $font_family=FF_FONT1,$font_style=FS_NORMAL,$font_size=12;
我把他改为
private $font_family=FF_SIMSUN,$font_style=FS_NORMAL,$font_size=8;
就OK了。
我想一定有其它的方法。不过我没有找到。有人找到的话告诉我一下呀。谢谢 。

其它的实现如下

<?php

include ("src/jpgraph.php");
include ("src/jpgraph_bar.php");

// Some data  所有的数据也就是我们想要的数据 
$datay1=array(140,110,50,270,78);
$datay2=array(35,90,190,190,56);
$datay3=array(20,60,70,140,34);

// Create the basic graph 设置图片属性
$graph = new Graph(450,250,'auto');   //图片大小
$graph->SetScale("textlin");   //图片类型
$graph->img->SetMargin(40,90,30,40); //margin(填充)左、右、上、下

// Adjust the position of the legend box
$graph->legend->Pos(0.01,0.10);   //显示右上角的注释的位置 X,Y


// Adjust the color for theshadow of the legend
$graph->legend->SetShadow('darkgray@0.5');   //是否有阴影 类型@程度值 
$graph->legend->SetFillColor('lightblue@0.3');  //填充颜色

// Get localised version of the month names
//var_dump($gDateLocale->GetShortMonth());
$graph->xaxis->SetTickLabels(array("1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"));  // X 轴数据

// Set a nice summer (in Stockholm) image  //如果有背影图 可以指定
$graph->SetBackgroundImage('stship.jpg',BGIMG_COPY);

// Set axis titles and fonts
$graph->xaxis->title->Set('Year 2006');     //图片最右下角的文字。一般为单位
$graph->xaxis->title->SetFont(FF_SIMSUN,FS_BOLD);  //设置字体
//设置X轴的说明。与图片的距离
$graph->xaxis->title->SetMargin(8);

$graph->xaxis->title->SetColor('white');

$graph->xaxis->SetFont(FF_SIMSUN,FS_BOLD);
$graph->xaxis->SetColor('white');

//$graph->yaxis->SetFont(FF_FONT1,FS_BOLD);    //设置 Y 轴
$graph->yaxis->title->SetFont(FF_SIMSUN,FS_BOLD);
$graph->yaxis->SetColor('white');

//$graph->ygrid->Show(false);
$graph->ygrid->SetColor('white@0.5');

// Setup graph title 设置标题
$graph->title->Set('会员域名注册量分析');
// Some extra margin (from the top)
$graph->title->SetMargin(3);
$graph->title->SetFont(FF_SIMSUN,FS_BOLD,12);


//创建三个柱图 其中数组$datay1-3是数据
// Create the three var series we will combine
$bplot1 = new BarPlot($datay1);
$bplot2 = new BarPlot($datay2);
$bplot3 = new BarPlot($datay3);

//设置颜色并且有40%的透明度(alpha channel)
// Setup the colors with 40% transparency (alpha channel)
$bplot1->SetFillColor('orange@0.4');
$bplot2->SetFillColor('brown@0.4');
$bplot3->SetFillColor('darkgreen@0.4');

//$bplot1->SetFont(FF_SIMSUN,FS_BOLD);

// Setup legendsSetWidth //设置显示右上角的注释 文字显示
$bplot1->SetLegend('北方区');
$bplot2->SetLegend('华东区');
$bplot3->SetLegend('华南区');

//$bplot1->SetWidth(10);
//$bplot2->SetWidth(10);
//$bplot3->SetWidth(10);

// Setup each bar with a shadow of 50% transparency
$bplot1->SetShadow('black@0.4');
$bplot2->SetShadow('black@0.4');
$bplot3->SetShadow('black@0.4');

//可以让每一个柱的真实数值显示是柱上
$bplot1->value->Show();
$bplot2->value->Show();
$bplot3->value->Show();
//
$gbarplot = new GroupBarPlot(array($bplot1,$bplot2,$bplot3));
//三个柱一个单元。设置这个单元的宽度
$gbarplot->SetWidth(0.6);
$graph->Add($gbarplot);
//显示
$graph->Stroke();
?>

注释我已写的很详细了。大家看就可以了。

[ 本帖最后由 smallcat 于 2007-6-9 22:31 编辑 ]

顶部
Haohappy
超级版主
Rank: 8Rank: 8
PHPEye站长


UID 2
精华 11
积分 110
帖子 283
翻译 6
原创 1
阅读权限 150
注册 2007-5-2
状态 离线
发表于 2007-6-9 23:55  资料  短消息  加为好友  添加 haohappy 为MSN好友 通过MSN和 haohappy 交谈
good,加入精华,jpgraph确实是现在最强大的PHP画图类库。

顶部
Ben
新手上路
Rank: 1



UID 54
精华 0
积分 0
帖子 5
翻译 0
原创 0
阅读权限 10
注册 2007-6-1
来自 抚琴居
状态 离线
发表于 2007-6-10 19:44  资料  主页 短消息  加为好友 
我说这篇文章怎么这么熟悉,原来是恰巧是我审的那一篇~  文章不错~赞!

顶部
smallcat
新手上路
Rank: 1



UID 65
精华 2
积分 20
帖子 10
翻译 0
原创 0
阅读权限 10
注册 2007-6-9
状态 离线
发表于 2007-6-11 16:04  资料  短消息  加为好友  添加 smallcat 为MSN好友 通过MSN和 smallcat 交谈
谢谢呀.呵呵.其实也是以前在BLOG上写过的.呵呵.

顶部
dzjzmj
新手上路
Rank: 1



UID 78
精华 0
积分 0
帖子 8
翻译 0
原创 0
阅读权限 10
注册 2007-6-16
状态 离线
发表于 2007-6-16 16:16  资料  短消息  加为好友 
确实,我也有在用





顶部
gbbnvc
新手上路
Rank: 1



UID 80
精华 0
积分 0
帖子 7
翻译 0
原创 0
阅读权限 10
注册 2007-6-17
状态 离线
发表于 2007-6-17 00:09  资料  短消息  加为好友  添加 gbbnvc 为MSN好友 通过MSN和 gbbnvc 交谈
不错~





顶部
hzysoft
新手上路
Rank: 1



UID 86
精华 0
积分 0
帖子 3
翻译 0
原创 0
阅读权限 10
注册 2007-6-18
状态 离线
发表于 2007-6-18 12:12  资料  短消息  加为好友  添加 hzysoft 为MSN好友 通过MSN和 hzysoft 交谈
好强大啊,以后做图不用愁了。

顶部
ringtail
PHPEye Developer
Rank: 8Rank: 8



UID 3
精华 1
积分 10
帖子 9
翻译 9
原创 0
阅读权限 1
注册 2007-5-2
状态 离线
发表于 2007-6-25 08:48  资料  短消息  加为好友 
它最强的是文档写的很好,容易上手。

顶部
+雨霖+
新手上路
Rank: 1



UID 113
精华 0
积分 0
帖子 1
翻译 0
原创 0
阅读权限 10
注册 2007-6-25
状态 离线
发表于 2007-6-26 13:57  资料  短消息  加为好友 
强~~顶个~~好东东~~

顶部
quan.zhao
新手上路
Rank: 1



UID 146
精华 0
积分 0
帖子 11
翻译 0
原创 0
阅读权限 10
注册 2007-7-3
状态 离线
发表于 2007-7-4 12:37  资料  短消息  加为好友 
太好了,我去下载

太好了,我去下载





http://www.xcopy.net.cn 网络收藏夹
顶部
 


PHPEye开源社区


当前时区 GMT+8, 现在时间是 2008-12-3 04:27

    Powered by Discuz! 5.5.0  © 2001-2007 Comsenz Inc.
Processed in 0.029343 second(s), 6 queries , Gzip enabled

清除 Cookies - 联系我们 - PHPEye开源社区 - Archiver