• JpGraph使用详解之中文乱码解决方法


    在前面的JpGraph使用详解这篇文章,已经对JpGraph的使用方法作了详细的交代,前面说好的,接下来解决中文乱码。

    JpGraph为什么会出现中文乱码

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

    解决中文乱码

    取前篇的代码片断如下

    1 //设置图表的标题字体、大小
    2 $graph->title->Set("Accumulated bar plots");
    3 $graph->xaxis->title->Set("X-title");
    4 $graph->yaxis->title->Set("Y-title");
    5  
    6 //和上面标题对应,设置标题的字体和大小
    7 $graph->title->SetFont(FF_FONT1,FS_BOLD);
    8 $graph->yaxis->title->SetFont(FF_FONT1,FS_BOLD);
    9 $graph->xaxis->title->SetFont(FF_FONT1,FS_BOLD);

    把它改为

    1 //设置图表的标题字体、大小
    2 $graph->title->Set(iconv("UTF-8","GB2312//IGNORE","网志博客信息统计表"));
    3 $graph->xaxis->title->Set(iconv("UTF-8","GB2312//IGNORE","X-标题"));
    4 $graph->yaxis->title->Set(iconv("UTF-8","GB2312//IGNORE","Y-标题"));
    5  
    6 //和上面标题对应,设置标题的字体和大小
    7 $graph->title->SetFont(FF_SIMSUN,FS_BOLD);
    8 $graph->yaxis->title->SetFont(FF_SIMSUN,FS_BOLD);
    9 $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也表示中文但是它们对应的字体文件是不同的,所以不要弄错。

    下面是正确转换后生成的图

    下面是本例调试的完整代码

    01 require_once ('jpgraph/jpgraph.php');
    02 require_once ('jpgraph/jpgraph_bar.php');
    03  
    04 $data1y=array(0,8,9,3,5,6);
    05 $data2y=array(18,2,1,7,5,4);
    06  
    07 // Create the graph. These two calls are always required
    08 $graph new Graph(500,400);
    09 $graph->SetScale("textlin");
    10  
    11 $graph->SetShadow();
    12 $graph->img->SetMargin(40,30,20,40);//设置图形的边距
    13  
    14 // Create the bar plots
    15 $b1plot new BarPlot($data1y);
    16 $b1plot->SetFillColor("orange");
    17 $b1plot->value->Show();
    18 $b2plot new BarPlot($data2y);
    19 $b2plot->SetFillColor("blue");
    20 $b2plot->value->Show();
    21  
    22 // Create the grouped bar plot
    23 $gbplot new AccBarPlot(array($b1plot,$b2plot));
    24  
    25 // ...and add it to the graPH
    26 $graph->Add($gbplot);
    27 //设置标题字体样式
    28 $graph->title->Set(iconv("UTF-8","GB2312//IGNORE","网志博客信息统计表"));
    29 $graph->xaxis->title->Set(iconv("UTF-8","GB2312//IGNORE","X-标题"));
    30 $graph->yaxis->title->Set(iconv("UTF-8","GB2312//IGNORE","Y-标题"));
    31  
    32 $graph->title->SetFont(FF_SIMSUN,FS_BOLD);
    33 $graph->yaxis->title->SetFont(FF_SIMSUN,FS_BOLD);
    34 $graph->xaxis->title->SetFont(FF_SIMSUN,FS_BOLD);
    35  
    36 $graph->Stroke();

    当然了,我这里只介绍了一种方法,还有一种就是修改源码,但不推荐。因为我觉得改动源码可能会给其它地方带来意想不到的麻烦。

    使用JpGraph,要知道其版本、运行服务器以及操作系统的息息,不能张冠李戴,否则麻烦多多。

    好了,至此JpGraph使用介绍也就这么多了。

  • 相关阅读:
    CF1008D Pave the Parallelepiped
    Luogu P3324 [SDOI2015]星际战争
    Luogu P3157 [CQOI2011]动态逆序对
    CF1295E Permutation Separation
    位元算
    JavaScript 寻找数组中的第N大的元素
    JS常见的数组排序算法
    最新面试总结
    JavaScript BOM浏览器对象模型
    JavaScript DOM 事件
  • 原文地址:https://www.cnblogs.com/caicaizi/p/5104528.html
Copyright © 2020-2023  润新知