libchart PHP下的统计图表类库-中文乱码解决方案
libchart默认对中文支持不太友好,当我们在图表中使用中文时会变成乱码
如果想要解决中文码问题,也是很简单的,在windows下搜索中文字体,搜索ttf开头的字体文件,放在libchart源码font目录下,再将源码修改下就能正常显示了
乱码示例
示例
<?php
include "../libchart/classes/libchart.php";
$chart = new VerticalBarChart();
$dataSet = new XYDataSet();
$dataSet->addPoint(new Point("就绪", 10));
$dataSet->addPoint(new Point("示忙", 2));
$dataSet->addPoint(new Point("排队", 0));
$dataSet->addPoint(new Point("振铃", 0));
$dataSet->addPoint(new Point("通话", 3));
$dataSet->addPoint(new Point("外呼", 1));
$chart->setDataSet($dataSet);
$chart->setTitle("示例图表");
$chart->render("generated/demo1.png");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Libchart vertical bars demonstration</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-15" />
</head>
<body>
<img alt="Vertical bars chart" src="generated/demo1.png" style="border: 1px solid gray;"/>
</body>
</html>
解决步骤
查找字体文件
查找*ttf,看见上面显示简体字一般都是中文字体
将找到的字体文件复制到libchart/font目录下
修改步骤
打开libchart/classes/view/text/text.php
字体引用修改
在构造函数 public function Text()将
$this->fontCondensed = $baseDir . "fonts/DejaVuSansCondensed.ttf";
$this->fontCondensedBold = $baseDir . "fonts/DejaVuSansCondensed-Bold.ttf";
改为
//中文字体
$this->fontCondensed = $baseDir . "fonts/Deng.ttf";
$this->fontCondensedBold = $baseDir . "fonts/DengB.ttf";
标题乱码的修改
标题文字修改
printText函数打印图表标题文字
将
$this->printText($img, imagesx($img) / 2, $py, $color, $text, $fontFileName, $this->HORIZONTAL_CENTER_ALIGN | $this->VERTICAL_CENTER_ALIGN);
改为
$str = iconv('gb2312','utf-8',$text);//解决乱码问题
$this->printText($img, imagesx($img) / 2, $py, $color, $str, $fontFileName, $this->HORIZONTAL_CENTER_ALIGN | $this->VERTICAL_CENTER_ALIGN);
底部标签文本
printCentered函数打印底部标签文本
将
imagettftext($img, $fontSize, $angle, $px, $py, $color->getColor($img), $fontFileName, $text);
改为
$str = iconv('gb2312','utf-8',$text);//解决乱码问题
imagettftext($img, $fontSize, $angle, $px-5, $py+12, $color->getColor($img), $fontFileName, $str );
在这里将字体改为 $fontSize = 18;
取消文字角度 $angle = 0;
查看效果
查看前请记得清缓存,以免缓存看不到效果
标签:类库,img,text,libchart,乱码,new,ttf From: https://blog.51cto.com/u_12668715/7089396