<?php
include ("../jpgraph.php");
include ("../jpgraph_line.php");
$ydata = array(11,3,8,12,5,1,9,13,5,7);
$y2data = array(354,200,265,99,111,91,198,225,293,251);
$datax = array("2006-01-01","2006-01-01","2006-01-01","2006-01-01","2006-01-01","2006-01-01","2006-01-01","2006-01-01","2006-01-01","2006-01-01");
// Create the graph. These two calls are always required
$graph = new Graph(800,400,"auto");
$graph->img->SetMargin(40,40,20,40);
$graph->title->SetFont(FF_SIMSUN,FS_BOLD);
$title = "展示pv/展示ip";
$graph->title->Set($title);
$graph->SetScale("textlin");
$graph->SetY2Scale("lin");
$graph->xaxis->title->Set("时间");
$graph->xaxis->title->SetFont(FF_SIMSUN,FS_BOLD);
$graph->xaxis->SetTickLabels($datax);
$graph->yaxis->title->Set("展示pv");
$graph->yaxis->SetColor("blue");
$graph->yaxis->title->SetFont(FF_SIMSUN,FS_BOLD);
$graph->y2axis->title->Set("展示IP");
$graph->y2axis->SetColor("orange");
$graph->y2axis->title->SetFont(FF_SIMSUN,FS_BOLD);
$lineplot=new LinePlot($ydata);
$lineplot->SetColor("blue");
$lineplot->SetWeight(2);
$lineplot->SetLegend("展示pv");
$graph->legend->SetFont(FF_SIMSUN,FS_BOLD);
$lineplot2=new LinePlot($y2data);
$lineplot2->SetColor("orange");
$lineplot2->SetWeight(2);
$lineplot2->SetLegend("展示ip");
$graph->Add($lineplot);
$graph->AddY2($lineplot2);
// Display the graph
$graph->Stroke();
?>
注意SetFont方法,如果你的文件编码是gb2312,SetFont方法的第一个参数为FF_SIMSUN即可正常显示汉字
如果你的文件编码为utf-8,还需加一句$title = iconv("UTF-8", "gb2312", $title)
在jpgraph.php中,有这么一段语句:
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);
}
就是说,jpgraph默认显示汉字时是把汉字编码认为gb2312,转化为utf-8以后再显示。
这样的话,如果你的文件编码是gb2312,SetFont方法的第一个参数为FF_SIMSUN即可
如果你是utf-8编码你还需要先把汉字编码转化为gb2312,这样你的汉字才可以正常显示。