jpgraph 折线图--解决中文乱码的问题(标题和图例)

时间:2022-02-17 23:09:29

在jpgraph根目录中:

如Jpg\jpgraph_ttf.inc.php 中开头添加

define('CHINESE_TTF_FONT','SIMYOU.TTF');

\Jpg\jpgraph_legend.inc.php 中大约24行添加 修改

public $font_family=FF_CHINESE,$font_style=FS_NORMAL,$font_size=8; // old. 12

然后

 /**作图 前期准备  set_time_limit(0);*/
$this->root_path=realpath(dirname(__FILE__).'/../../../');
$this->root_path=str_replace('\\','/',$this->root_path);
dd($this->root_path);
/**图1 故障停机时间折线图 */
//x轴刻度数据
$x_zuobiaos=Array
(
[0] => 16/10
[1] => 16/11
[2] => 16/12
[3] => 17/01
[4] => 17/02
[5] => 17/03
[6] => 17/04
[7] => 17/05
[8] => 17/06
[9] => 17/07
[10] => 17/08
[11] => 17/09
[12] => 17/10
);
//折线数据
$xdata=Array
(
'其它故障因素'=> Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 0
[6] => 0
[7] => 0
[8] => 0
[9] => 0
[10] => 0
[11] => 2.90
[12] => 0
), '工艺故障因素' => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 0
[6] => 0
[7] => 2.25
[8] => 0
[9] => 0
[10] => 0
[11] => 0
[12] => 0
)
)
//最大值,最小值区间
$mimax[0]=$min-$max*0.1;
$mimax[1]=$max*1.1;
//返回路径
$path[]=$this->dojpgraph_zx('故障停机时间曲线图','','小时',$x_zuobiaos,$xdata,$mimax,'ReportPdf_1.png');

图例:

jpgraph 折线图--解决中文乱码的问题(标题和图例)

变量:

public $root_path;
public $iColorList = array('azure4', 'orange', 'darkgreen', 'red', 'aquamarine3', 'blue','chocolate',
'brown', 'cadetblue3', 'chartreuse4', 'AntiqueWhite3', 'darkblue');

函数:

  //折线,支持中文
public function dojpgraph_zx($title,$x_title,$y_title,$x_zuobiao,$xdata,$mimax,$picname=null){ require_once($this->root_path."/Jpg/jpgraph.php");
require_once($this->root_path."/Jpg/jpgraph_line.php");
$x_zuobiao=array_merge(array(''),$x_zuobiao);//把原点坐标空出来
// $data1 = array(523,634,371,278,685,587,490,256,398,545,367,577); //第一条曲线的数组 $graph = new \Graph(900,450);// 图表的长宽
$graph->SetScale("textlin",0,$mimax[1]);//设置刻度样式 $mimax 刻度多10%
// $graph->SetY2Scale('lin');//设置右边刻度样式 http://www.jb51.net/article/105672.htm
$graph->SetShadow();//设置背景带阴影
//设置图表灰度四周边距,顺序为左右上下
$graph->img->SetMargin(60,30,30,70);
$graph->graph_theme = null; //设置主题为null,否则value->Show(); 无效 // $lineplot1->SetFillColor("orange"); //填充色 //设置图表的标题字体、大小 解决中文乱码
$graph->title->Set(iconv("UTF-8","GB2312//IGNORE",$title)); //设置图像标题
$graph->xaxis->title->Set(iconv("UTF-8","GB2312//IGNORE",$x_title)); //设置坐标轴名称
$graph->yaxis->title->Set(iconv("UTF-8","GB2312//IGNORE",$y_title));
// $graph->y2axis->title->Set(iconv('utf-8', 'GB2312//IGNORE', "book B 销售金额(万元)"));//设置y轴的标题
// $graph->y2axis->title->SetMargin(20);//设置右边的title到图的距离
$graph->title->SetMargin(10);//设置title到图的距离
$graph->xaxis->title->SetMargin(10);
$graph->yaxis->title->SetMargin(10);
//设置字体
$graph->title->SetFont(FF_SIMSUN,FS_BOLD);
$graph->xaxis->title->SetFont(FF_SIMSUN,FS_BOLD); //设置图表的标题字体、大小
$graph->yaxis->title->SetFont(FF_SIMSUN,FS_BOLD);// FF_SIMSUN表示中文简体
//折线颜色图例
$graph->legend->SetLayout(LEGEND_HOR);
// $graph->legend->Pos( 0.025, 0.08, "right","center" ); //图例文字框的位置 0.025,0.08 是以右上角为基准的,0.025是距左右距离,0.08是上下距离。 http://nodonkey.iteye.com/blog/761700
$graph->legend->Pos(0.4, 0.95, 'center', 'bottom');// 图例文字框的位置 0.4,0.95 是以右上角为基准的,0.4是距左右距离,0.95是上下距离
//数据
//颜色图例
$i=0;
foreach($xdata as $k=>$v){
$v=array_merge(array(''),$v);
$lineplot='lineplot'.$i;
$$lineplot=new \LinePlot($v); //创建设置n条曲线对象
$$lineplot->value->SetColor($this->iColorList[$i]); //设置线上字体颜色
/* $color=new \ColorFactory();
$$lineplot->value->SetColor($color->getColor()); //设置线颜色随机*/
$$lineplot->SetWeight(4); //线宽 加深加粗
$$lineplot->value->Show();
// $bt=iconv("UTF-8","gb2312",$legend[$k-1]);
// $$lineplot->SetFont(FF_SIMSUN,FF_SIMSUN,8);
$$lineplot->SetLegend($k); $graph->Add($$lineplot); //在统计图上绘制曲线
$$lineplot->SetColor($this->iColorList[$i]); //设置统计图的颜色,一定要在添加到画布之后再设置
$i++;
}
//x轴坐标 自带月份 $gDateLocale=new \DateLocale; $graph->xaxis->SetTickLabels($gDateLocale->GetShortMonth());
$graph->xaxis->SetTickLabels($x_zuobiao); //x轴坐标 月份 if($picname==null) {
$graph->Stroke(); //生成本地图表,黙认留空,生成在当前目录,可以Stroke(“路径/文件名.png”)这样指定路径
} else{
$path=$this->root_path.'/Uploads/report/'.$picname;
if(file_exists($path)){
@unlink($path);
}
$graph->Stroke($path); //保存图像
return $path;
}
}