jpgraph的一个使用例子及汉字乱码的解决方法

导读:
  jpgraph的一个使用例子及汉字乱码的解决方法
  最近因为工作原因,需要做一些图表出来,上网查了一下,PHP中主要的做图工具有两种。一个是jpgraph,一个是image_graph.
  JPGRAPH是PHP世界里一款非常方便的做各种图表的工作,柱状图、饼图、雷达图等,基本上对所有的图表无所不能。
  在PEAR中,也有一款做图表的工具,叫image_graph,这个工具功能也是非常强大,但jpgraph和image_graph比起来的最大好处,就是有很多的例子和非常详细的文档说明,这就使jpgraph用起来是相当的方便。
  最后决定选用jpgraph。
  下面是一段代码。同时在用的过程中遇到了汉字乱码的问题,原因已经找到,也和大家说明一下。
  如果您在使用jpgraph的过程遇到了什么问题,可以MAIL给我,大家一直探讨。同时,如果你有什么使用心得,也希望您能够发到LAMPER上,让更多的人分享你的经验。
  
  
<   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,这样你的汉字才可以正常显示。
  网上有种方法是修改jpgraph.php文件,我本人不提倡这样做,我们应该尽量修改自己实际应用程序,而尽量少修改基本程序。

本文转自
http://www.lamper.cn/html/2007/01-21/57.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值