php 2007-7-14 10:13
php一个很不错的作图类JpGraph
今天新来的。也不知道写点什么好。想想还是把自己以前写在BLOG上的一篇文章拿来吧。呵呵
PHP绘图类库jpgraph,
下载地址:[url=http://www.aditus.nu/jpgraph/jpdownload.php]http://www.aditus.nu/jpgraph/jpdownload.php[/url]
开始以为他不支持中文,所以先在网上找了教程,教程写着要设置字库路径等。结果我下的那个版本一点都不用设置人家就写好了。如果是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了。
我想一定有其它的方法。不过我没有找到。有人找到的话告诉我一下呀。谢谢 。
其它的实现如下
[color=#000000][color=#0000cc]<[/color][color=#0000cc]?[/color][color=#0000ff]php[/color]
[color=#0000ff]include[/color] [color=#0000cc]([/color][color=#ff00ff]"src/jpgraph.php"[/color][color=#0000cc])[/color][color=#0000cc];[/color]
[color=#0000ff]include[/color] [color=#0000cc]([/color][color=#ff00ff]"src/jpgraph_bar.php"[/color][color=#0000cc])[/color][color=#0000cc];[/color]
[color=#ff9900]// Some data 所有的数据也就是我们想要的数据
[/color]
[color=#0000ff]$[/color][color=#008080]datay1[/color][color=#0000cc]=[/color][color=#ff0000]array[/color][color=#0000cc]([/color]140[color=#0000cc],[/color]110[color=#0000cc],[/color]50[color=#0000cc],[/color]270[color=#0000cc],[/color]78[color=#0000cc])[/color][color=#0000cc];[/color]
[color=#0000ff]$[/color][color=#008080]datay2[/color][color=#0000cc]=[/color][color=#ff0000]array[/color][color=#0000cc]([/color]35[color=#0000cc],[/color]90[color=#0000cc],[/color]190[color=#0000cc],[/color]190[color=#0000cc],[/color]56[color=#0000cc])[/color][color=#0000cc];[/color]
[color=#0000ff]$[/color][color=#008080]datay3[/color][color=#0000cc]=[/color][color=#ff0000]array[/color][color=#0000cc]([/color]20[color=#0000cc],[/color]60[color=#0000cc],[/color]70[color=#0000cc],[/color]140[color=#0000cc],[/color]34[color=#0000cc])[/color][color=#0000cc];[/color]
[color=#ff9900]// Create the basic graph 设置图片属性
[/color]
[color=#0000ff]$[/color][color=#008080]graph[/color] [color=#0000cc]=[/color] [color=#0000ff]new[/color] Graph[color=#0000cc]([/color]450[color=#0000cc],[/color]250[color=#0000cc],[/color][color=#ff00ff]'auto'[/color][color=#0000cc])[/color][color=#0000cc];[/color] [color=#ff9900]//图片大小
[/color]
[color=#0000ff]$[/color][color=#008080]graph[/color][color=#0000cc]-[/color][color=#0000cc]>[/color]SetScale[color=#0000cc]([/color][color=#ff00ff]"textlin"[/color][color=#0000cc])[/color][color=#0000cc];[/color] [color=#ff9900]//图片类型
[/color]
[color=#0000ff]$[/color][color=#008080]graph[/color][color=#0000cc]-[/color][color=#0000cc]>[/color]img[color=#0000cc]-[/color][color=#0000cc]>[/color]SetMargin[color=#0000cc]([/color]40[color=#0000cc],[/color]90[color=#0000cc],[/color]30[color=#0000cc],[/color]40[color=#0000cc])[/color][color=#0000cc];[/color] [color=#ff9900]//margin(填充)左、右、上、下
[/color]
[color=#ff9900]// Adjust the position of the legend box
[/color]
[color=#0000ff]$[/color][color=#008080]graph[/color][color=#0000cc]-[/color][color=#0000cc]>[/color]legend[color=#0000cc]-[/color][color=#0000cc]>[/color][color=#ff0000]Pos[/color][color=#0000cc]([/color]0[color=#0000cc].[/color]01[color=#0000cc],[/color]0[color=#0000cc].[/color]10[color=#0000cc])[/color][color=#0000cc];[/color] [color=#ff9900]//显示右上角的注释的位置 X,Y
[/color]
[color=#ff9900]// Adjust the color for theshadow of the legend
[/color]
[color=#0000ff]$[/color][color=#008080]graph[/color][color=#0000cc]-[/color][color=#0000cc]>[/color]legend[color=#0000cc]-[/color][color=#0000cc]>[/color]SetShadow[color=#0000cc]([/color][color=#ff00ff]'darkgray@0.5'[/color][color=#0000cc])[/color][color=#0000cc];[/color] [color=#ff9900]//是否有阴影 类型@程度值
[/color]
[color=#0000ff]$[/color][color=#008080]graph[/color][color=#0000cc]-[/color][color=#0000cc]>[/color]legend[color=#0000cc]-[/color][color=#0000cc]>[/color]SetFillColor[color=#0000cc]([/color][color=#ff00ff]'lightblue@0.3'[/color][color=#0000cc])[/color][color=#0000cc];[/color] [color=#ff9900]//填充颜色
[/color]
[color=#ff9900]// Get localised version of the month names
[/color]
[color=#ff9900]//var_dump($gDateLocale->GetShortMonth());
[/color]
[color=#0000ff]$[/color][color=#008080]graph[/color][color=#0000cc]-[/color][color=#0000cc]>[/color]xaxis[color=#0000cc]-[/color][color=#0000cc]>[/color]SetTickLabels[color=#0000cc]([/color][color=#ff0000]array[/color][color=#0000cc]([/color][color=#ff00ff]"1月"[/color][color=#0000cc],[/color][color=#ff00ff]"2月"[/color][color=#0000cc],[/color][color=#ff00ff]"3月"[/color][color=#0000cc],[/color][color=#ff00ff]"4月"[/color][color=#0000cc],[/color][color=#ff00ff]"5月"[/color][color=#0000cc],[/color][color=#ff00ff]"6月"[/color][color=#0000cc],[/color][color=#ff00ff]"7月"[/color][color=#0000cc],[/color][color=#ff00ff]"8月"[/color][color=#0000cc],[/color][color=#ff00ff]"9月"[/color][color=#0000cc],[/color][color=#ff00ff]"10月"[/color][color=#0000cc],[/color][color=#ff00ff]"11月"[/color][color=#0000cc],[/color][color=#ff00ff]"12月"[/color][color=#0000cc])[/color][color=#0000cc])[/color][color=#0000cc];[/color] [color=#ff9900]// X 轴数据
[/color]
[color=#ff9900]// Set a nice summer (in Stockholm) image //如果有背影图 可以指定
[/color]
[color=#0000ff]$[/color][color=#008080]graph[/color][color=#0000cc]-[/color][color=#0000cc]>[/color]SetBackgroundImage[color=#0000cc]([/color][color=#ff00ff]'stship.jpg'[/color][color=#0000cc],[/color]BGIMG_COPY[color=#0000cc])[/color][color=#0000cc];[/color]
[color=#ff9900]// Set axis titles and fonts
[/color]
[color=#0000ff]$[/color][color=#008080]graph[/color][color=#0000cc]-[/color][color=#0000cc]>[/color]xaxis[color=#0000cc]-[/color][color=#0000cc]>[/color]title[color=#0000cc]-[/color][color=#0000cc]>[/color]Set[color=#0000cc]([/color][color=#ff00ff]'Year 2006'[/color][color=#0000cc])[/color][color=#0000cc];[/color] [color=#ff9900]//图片最右下角的文字。一般为单位
[/color]
[color=#0000ff]$[/color][color=#008080]graph[/color][color=#0000cc]-[/color][color=#0000cc]>[/color]xaxis[color=#0000cc]-[/color][color=#0000cc]>[/color]title[color=#0000cc]-[/color][color=#0000cc]>[/color]SetFont[color=#0000cc]([/color]FF_SIMSUN[color=#0000cc],[/color]FS_BOLD[color=#0000cc])[/color][color=#0000cc];[/color] [color=#ff9900]//设置字体
[/color]
[color=#ff9900]//设置X轴的说明。与图片的距离
[/color]
[color=#0000ff]$[/color][color=#008080]graph[/color][color=#0000cc]-[/color][color=#0000cc]>[/color]xaxis[color=#0000cc]-[/color][color=#0000cc]>[/color]title[color=#0000cc]-[/color][color=#0000cc]>[/color]SetMargin[color=#0000cc]([/color]8[color=#0000cc])[/color][color=#0000cc];[/color]
[color=#0000ff]$[/color][color=#008080]graph[/color][color=#0000cc]-[/color][color=#0000cc]>[/color]xaxis[color=#0000cc]-[/color][color=#0000cc]>[/color]title[color=#0000cc]-[/color][color=#0000cc]>[/color]SetColor[color=#0000cc]([/color][color=#ff00ff]'white'[/color][color=#0000cc])[/color][color=#0000cc];[/color]
[color=#0000ff]$[/color][color=#008080]graph[/color][color=#0000cc]-[/color][color=#0000cc]>[/color]xaxis[color=#0000cc]-[/color][color=#0000cc]>[/color]SetFont[color=#0000cc]([/color]FF_SIMSUN[color=#0000cc],[/color]FS_BOLD[color=#0000cc])[/color][color=#0000cc];[/color]
[color=#0000ff]$[/color][color=#008080]graph[/color][color=#0000cc]-[/color][color=#0000cc]>[/color]xaxis[color=#0000cc]-[/color][color=#0000cc]>[/color]SetColor[color=#0000cc]([/color][color=#ff00ff]'white'[/color][color=#0000cc])[/color][color=#0000cc];[/color]
[color=#ff9900]//$graph->yaxis->SetFont(FF_FONT1,FS_BOLD); //设置 Y 轴
[/color]
[color=#0000ff]$[/color][color=#008080]graph[/color][color=#0000cc]-[/color][color=#0000cc]>[/color]yaxis[color=#0000cc]-[/color][color=#0000cc]>[/color]title[color=#0000cc]-[/color][color=#0000cc]>[/color]SetFont[color=#0000cc]([/color]FF_SIMSUN[color=#0000cc],[/color]FS_BOLD[color=#0000cc])[/color][color=#0000cc];[/color]
[color=#0000ff]$[/color][color=#008080]graph[/color][color=#0000cc]-[/color][color=#0000cc]>[/color]yaxis[color=#0000cc]-[/color][color=#0000cc]>[/color]SetColor[color=#0000cc]([/color][color=#ff00ff]'white'[/color][color=#0000cc])[/color][color=#0000cc];[/color]
[color=#ff9900]//$graph->ygrid->Show(false);
[/color]
[color=#0000ff]$[/color][color=#008080]graph[/color][color=#0000cc]-[/color][color=#0000cc]>[/color]ygrid[color=#0000cc]-[/color][color=#0000cc]>[/color]SetColor[color=#0000cc]([/color][color=#ff00ff]'white@0.5'[/color][color=#0000cc])[/color][color=#0000cc];[/color]
[color=#ff9900]// Setup graph title 设置标题
[/color]
[color=#0000ff]$[/color][color=#008080]graph[/color][color=#0000cc]-[/color][color=#0000cc]>[/color]title[color=#0000cc]-[/color][color=#0000cc]>[/color]Set[color=#0000cc]([/color][color=#ff00ff]'会员域名注册量分析'[/color][color=#0000cc])[/color][color=#0000cc];[/color]
[color=#ff9900]// Some extra margin (from the top)
[/color]
[color=#0000ff]$[/color][color=#008080]graph[/color][color=#0000cc]-[/color][color=#0000cc]>[/color]title[color=#0000cc]-[/color][color=#0000cc]>[/color]SetMargin[color=#0000cc]([/color]3[color=#0000cc])[/color][color=#0000cc];[/color]
[color=#0000ff]$[/color][color=#008080]graph[/color][color=#0000cc]-[/color][color=#0000cc]>[/color]title[color=#0000cc]-[/color][color=#0000cc]>[/color]SetFont[color=#0000cc]([/color]FF_SIMSUN[color=#0000cc],[/color]FS_BOLD[color=#0000cc],[/color]12[color=#0000cc])[/color][color=#0000cc];[/color]
[color=#ff9900]//创建三个柱图 其中数组$datay1-3是数据
[/color]
[color=#ff9900]// Create the three var series we will combine
[/color]
[color=#0000ff]$[/color][color=#008080]bplot1[/color] [color=#0000cc]=[/color] [color=#0000ff]new[/color] BarPlot[color=#0000cc]([/color][color=#0000ff]$[/color][color=#008080]datay1[/color][color=#0000cc])[/color][color=#0000cc];[/color]
[color=#0000ff]$[/color][color=#008080]bplot2[/color] [color=#0000cc]=[/color] [color=#0000ff]new[/color] BarPlot[color=#0000cc]([/color][color=#0000ff]$[/color][color=#008080]datay2[/color][color=#0000cc])[/color][color=#0000cc];[/color]
[color=#0000ff]$[/color][color=#008080]bplot3[/color] [color=#0000cc]=[/color] [color=#0000ff]new[/color] BarPlot[color=#0000cc]([/color][color=#0000ff]$[/color][color=#008080]datay3[/color][color=#0000cc])[/color][color=#0000cc];[/color]
[color=#ff9900]//设置颜色并且有40%的透明度(alpha channel)
[/color]
[color=#ff9900]// Setup the colors with 40% transparency (alpha channel)
[/color]
[color=#0000ff]$[/color][color=#008080]bplot1[/color][color=#0000cc]-[/color][color=#0000cc]>[/color]SetFillColor[color=#0000cc]([/color][color=#ff00ff]'orange@0.4'[/color][color=#0000cc])[/color][color=#0000cc];[/color]
[color=#0000ff]$[/color][color=#008080]bplot2[/color][color=#0000cc]-[/color][color=#0000cc]>[/color]SetFillColor[color=#0000cc]([/color][color=#ff00ff]'brown@0.4'[/color][color=#0000cc])[/color][color=#0000cc];[/color]
[color=#0000ff]$[/color][color=#008080]bplot3[/color][color=#0000cc]-[/color][color=#0000cc]>[/color]SetFillColor[color=#0000cc]([/color][color=#ff00ff]'darkgreen@0.4'[/color][color=#0000cc])[/color][color=#0000cc];[/color]
[color=#ff9900]//$bplot1->SetFont(FF_SIMSUN,FS_BOLD);
[/color]
[color=#ff9900]// Setup legendsSetWidth //设置显示右上角的注释 文字显示
[/color]
[color=#0000ff]$[/color][color=#008080]bplot1[/color][color=#0000cc]-[/color][color=#0000cc]>[/color]SetLegend[color=#0000cc]([/color][color=#ff00ff]'北方区'[/color][color=#0000cc])[/color][color=#0000cc];[/color]
[color=#0000ff]$[/color][color=#008080]bplot2[/color][color=#0000cc]-[/color][color=#0000cc]>[/color]SetLegend[color=#0000cc]([/color][color=#ff00ff]'华东区'[/color][color=#0000cc])[/color][color=#0000cc];[/color]
[color=#0000ff]$[/color][color=#008080]bplot3[/color][color=#0000cc]-[/color][color=#0000cc]>[/color]SetLegend[color=#0000cc]([/color][color=#ff00ff]'华南区'[/color][color=#0000cc])[/color][color=#0000cc];[/color]
[color=#ff9900]//$bplot1->SetWidth(10);
[/color]
[color=#ff9900]//$bplot2->SetWidth(10);
[/color]
[color=#ff9900]//$bplot3->SetWidth(10);
[/color]
[color=#ff9900]// Setup each bar with a shadow of 50% transparency
[/color]
[color=#0000ff]$[/color][color=#008080]bplot1[/color][color=#0000cc]-[/color][color=#0000cc]>[/color]SetShadow[color=#0000cc]([/color][color=#ff00ff]'black@0.4'[/color][color=#0000cc])[/color][color=#0000cc];[/color]
[color=#0000ff]$[/color][color=#008080]bplot2[/color][color=#0000cc]-[/color][color=#0000cc]>[/color]SetShadow[color=#0000cc]([/color][color=#ff00ff]'black@0.4'[/color][color=#0000cc])[/color][color=#0000cc];[/color]
[color=#0000ff]$[/color][color=#008080]bplot3[/color][color=#0000cc]-[/color][color=#0000cc]>[/color]SetShadow[color=#0000cc]([/color][color=#ff00ff]'black@0.4'[/color][color=#0000cc])[/color][color=#0000cc];[/color]
[color=#ff9900]//可以让每一个柱的真实数值显示是柱上
[/color]
[color=#0000ff]$[/color][color=#008080]bplot1[/color][color=#0000cc]-[/color][color=#0000cc]>[/color]value[color=#0000cc]-[/color][color=#0000cc]>[/color]Show[color=#0000cc]([/color][color=#0000cc])[/color][color=#0000cc];[/color]
[color=#0000ff]$[/color][color=#008080]bplot2[/color][color=#0000cc]-[/color][color=#0000cc]>[/color]value[color=#0000cc]-[/color][color=#0000cc]>[/color]Show[color=#0000cc]([/color][color=#0000cc])[/color][color=#0000cc];[/color]
[color=#0000ff]$[/color][color=#008080]bplot3[/color][color=#0000cc]-[/color][color=#0000cc]>[/color]value[color=#0000cc]-[/color][color=#0000cc]>[/color]Show[color=#0000cc]([/color][color=#0000cc])[/color][color=#0000cc];[/color]
[color=#ff9900]//
[/color]
[color=#0000ff]$[/color][color=#008080]gbarplot[/color] [color=#0000cc]=[/color] [color=#0000ff]new[/color] GroupBarPlot[color=#0000cc]([/color][color=#ff0000]array[/color][color=#0000cc]([/color][color=#0000ff]$[/color][color=#008080]bplot1[/color][color=#0000cc],[/color][color=#0000ff]$[/color][color=#008080]bplot2[/color][color=#0000cc],[/color][color=#0000ff]$[/color][color=#008080]bplot3[/color][color=#0000cc])[/color][color=#0000cc])[/color][color=#0000cc];[/color]
[color=#ff9900]//三个柱一个单元。设置这个单元的宽度
[/color]
[color=#0000ff]$[/color][color=#008080]gbarplot[/color][color=#0000cc]-[/color][color=#0000cc]>[/color]SetWidth[color=#0000cc]([/color]0[color=#0000cc].[/color]6[color=#0000cc])[/color][color=#0000cc];[/color]
[color=#0000ff]$[/color][color=#008080]graph[/color][color=#0000cc]-[/color][color=#0000cc]>[/color]Add[color=#0000cc]([/color][color=#0000ff]$[/color][color=#008080]gbarplot[/color][color=#0000cc])[/color][color=#0000cc];[/color]
[color=#ff9900]//显示
[/color]
[color=#0000ff]$[/color][color=#008080]graph[/color][color=#0000cc]-[/color][color=#0000cc]>[/color]Stroke[color=#0000cc]([/color][color=#0000cc])[/color][color=#0000cc];[/color]
[color=#0000cc]?[/color][color=#0000cc]>[/color][/color]