JpGraph使用时中文乱码解决方法

1.直接通过函数来进行转换

在JpGraph中默认是要把字符串转成utf8的,但是如果你的文件本身就是utf8的,并且要用中文字体,它还会转一遍,结果多转了一次,就会出现乱码,如下图:

原始代码:

//设置图表的标题字体、大小
$graph->title->Set("Accumulated bar plots");
$graph->xaxis->title->Set("X-title");
$graph->yaxis->title->Set("Y-title");

//和上面标题对应,设置标题的字体和大小
$graph->title->SetFont(FF_FONT1,FS_BOLD);
$graph->yaxis->title->SetFont(FF_FONT1,FS_BOLD);
$graph->xaxis->title->SetFont(FF_FONT1,FS_BOLD);

改为:

//设置标题字体样式
$graph->title->Set(iconv("UTF-8","GB2312//IGNORE","博客信息统计表"));
$graph->xaxis->title->Set(iconv("UTF-8","GB2312//IGNORE","X-标题"));
$graph->yaxis->title->Set(iconv("UTF-8","GB2312//IGNORE","Y-标题"));

$graph->title->SetFont(FF_SIMSUN,FS_BOLD);
$graph->yaxis->title->SetFont(FF_SIMSUN,FS_BOLD);
$graph->xaxis->title->SetFont(FF_SIMSUN,FS_BOLD);

使用php函数据中文由UTF-8转为GB2312,记住由于iconv本身的一个bug,iconv在转换字符"—"到gb2312时会出错,所以在需要转成的编码后加上 "//IGNORE" ,FF_SIMSUN表示中文简体,对应的字体文件是simsun.ttc,虽然FF_CHINESE和FF_BIG5也表示中文但是它们对应的字体文件是不同的,所以不要弄错。

转换后如下图:

完整代码:

<?php

require_once ('jpgraph/jpgraph.php');
require_once ('jpgraph/jpgraph_bar.php');

$data1y=array(0,20,9,3,5,6);
$data2y=array(12,2,1,7,5,4);

// Create the graph. These two calls are always required
$graph = new Graph(500,400);
$graph->SetScale("textlin");

$graph->SetShadow();
$graph->img->SetMargin(40,30,20,40);//设置图形的边距

// Create the bar plots
$b1plot = new BarPlot($data1y);
$b1plot->SetFillColor("orange");
$b1plot->value->Show();
$b2plot = new BarPlot($data2y);
$b2plot->SetFillColor("blue");
$b2plot->value->Show();

// Create the grouped bar plot
$gbplot = new AccBarPlot(array($b1plot,$b2plot));

// ...and add it to the graPH
$graph->Add($gbplot);

//设置标题字体样式
$graph->title->Set(iconv("UTF-8","GB2312//IGNORE","博客信息统计表"));
$graph->xaxis->title->Set(iconv("UTF-8","GB2312//IGNORE","X-标题"));
$graph->yaxis->title->Set(iconv("UTF-8","GB2312//IGNORE","Y-标题"));

$graph->title->SetFont(FF_SIMSUN,FS_BOLD);
$graph->yaxis->title->SetFont(FF_SIMSUN,FS_BOLD);
$graph->xaxis->title->SetFont(FF_SIMSUN,FS_BOLD);

$graph->Stroke();
View Code

2.通过更改配置文件

修改文件:src/jpgraph_ttf.inc.php

找到如下代码(我的是3.5版本大约183行)

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_BIG5 ) {
            if( !function_exists('iconv') ) {
                JpGraphError::RaiseL(25006);
                //('Usage of FF_CHINESE (FF_BIG5) font family requires that your PHP setup has the iconv() function. By default this is not compiled into PHP (needs the "--width-iconv" when configured).');
            }
            return iconv('BIG5','UTF-8',$aTxt);
        }

修改为:

elseif( $aFF === FF_SIMSUN ) {
         return $aTxt;
    }
         elseif( $aFF === FF_CHINESE ) {
           return $aTxt;
    }

更改后只需指定字体就可以了,不必在用函数进行转码了。

 

posted on 2016-03-21 21:08  gimin  阅读(585)  评论(0)    收藏  举报