转自:笑笑驿站

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了。

代码如下:

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(); ?>