PHP 画图——使用jpgraph画图

PHP 画图——使用jpgraph画图


1.要支持中文须要用到simhei.ttfsimsun.ttc这两个字体,在使用中文的时候须要使用SetFont(FF_SIMSUN,FS_BOLD)设置字体。
将须要的字体放入到项目文件夹下的srcfonts文件夹里
jpgraph.php中有下面这样一段代码是设置字体文件路径的
//
// Setup path for western/latin TTF fonts
//
if (!defined('TTF_DIR')) {
    if (strstr( PHP_OS, 'WIN') ) {
        $sroot = getenv('SystemRoot');
        if( empty($sroot) ) {
            $t = new ErrMsgText();
            $msg = $t->Get(12,$file,$lineno);
            die($msg);
        }
        else {
            define('TTF_DIR', $sroot.'/fonts/');
        }
    } else {
        define('TTF_DIR','/usr/share/fonts/truetype/');
    }
2.须要注意的是:要想适用jpgraph,你的PHP必须开启了GD2扩展。



假设是在window下首先须要改动文件的路径

改动jpgraph_ttf.inc.php文件
$jpgraph_font_dir = dirname(__FILE__).'\fonts\';//改动字体的路径从原来的/fonts/ 改为 \fonts\
假设没改动在windows下会报以下的错误
PHP 画图——使用jpgraph画图

解决中文问题:
假设你的文件编码为utf-8,改动方法例如以下
方法一:
找到
        elseif( $aFF === FF_SIMSUN) {
            // Do Chinese conversion
            if( $this->g2312 == null ) {
                include_once 'jpgraph_gb2312.php' ;
                $this->g2312 = new GB2312toUTF8();
            }
            return $this->g2312->gb2utf8($aTxt);
        }
改动为
        elseif( $aFF === FF_SIMSUN) {
            // Do Chinese conversion
            /*
            if( $this->g2312 == null ) {
                include_once 'jpgraph_gb2312.php' ;
                $this->g2312 = new GB2312toUTF8();
            }
            return $this->g2312->gb2utf8($aTxt);
            */
         return $aTxt;
        }
方法二:在程序中改动
$title="流量图";
$title = iconv("UTF-8", "gb2312", $title);
$graph->title->Set($title);

注:
jpgraph默认显示汉字时是把汉字编码觉得gb2312,转化为utf-8以后再显示。


这种话,假设你的文件编码是gb2312,SetFont方法的第一个參数为FF_SIMSUN就可以。


假设你是utf-8编码你还须要先把汉字编码转化为gb2312。这样你的汉字才干够正常显示。


代码例如以下:



<?php
/**
 * 使用jpgraph生成3D饼图
 *
 */
include 'src/jpgraph.php';
include 'src/jpgraph_pie.php';
include 'src/jpgraph_pie3d.php';//引用3D饼图pieplot3D对象所在的类文件


$result = array(5,8,11,1,1,1);
$vote_content = array("张三","丽丽","lili","张三","丽丽","lili");
$title = '标题';

$graph = new PieGraph(500,245);//创建图像
$graph->SetShadow();//创建图像阴影
$graph->tabtitle->SetFont(FF_SIMSUN,FS_BOLD,14);//设置标题字体
$graph->tabtitle->Set($title);//输出标题
$graph->title->SetColor("darkblue");//定义标题颜色


$p1 = new PiePlot3D($result);//创建图像
//$p1->SetTheme("water");//控制图像的颜色
//$p1->SetCenter(0.4);//设置图像位置
//$p1->SetSize(0.4);//设置图像的大小
//$p1->SetHeight(20);//设置饼图的高度
//$p1->SetAngle(45);//设置图像的倾斜角度
//$p1->Explode(array(5,40,10,30,20));//控制饼图的切割
//$p1->value->SetFont(FF_SIMSUN,FS_BOLD,20);//设置字体
/* 凝视文字 */
$p1->SetLegends($vote_content);
$graph->legend->SetFont(FF_SIMSUN,FS_BOLD);//设置凝视文字字体
$graph->legend->Pos(0.05,0.3,"right","center");//控制凝视文字的位置
$graph->legend->SetShadow();//边界
$graph->legend->SetLayout(LEGEND_VERT);//设置图例样式和位置

$graph->Add($p1);//加入数据
$graph->Stroke();//生成图像
PHP 画图——使用jpgraph画图