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


    在jpgraph根目录中:

    如Jpgjpgraph_ttf.inc.php 中开头添加

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

    Jpgjpgraph_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');

    图例:

    变量:

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

    函数:

     1  //折线,支持中文
     2     public function dojpgraph_zx($title,$x_title,$y_title,$x_zuobiao,$xdata,$mimax,$picname=null){
     3 
     4         require_once($this->root_path."/Jpg/jpgraph.php");
     5         require_once($this->root_path."/Jpg/jpgraph_line.php");
     6         $x_zuobiao=array_merge(array(''),$x_zuobiao);//把原点坐标空出来
     7 //        $data1 = array(523,634,371,278,685,587,490,256,398,545,367,577); //第一条曲线的数组
     8 
     9         $graph = new Graph(900,450);// 图表的长宽
    10         $graph->SetScale("textlin",0,$mimax[1]);//设置刻度样式  $mimax 刻度多10%
    11 //        $graph->SetY2Scale('lin');//设置右边刻度样式 http://www.jb51.net/article/105672.htm
    12         $graph->SetShadow();//设置背景带阴影
    13         //设置图表灰度四周边距,顺序为左右上下
    14         $graph->img->SetMargin(60,30,30,70);
    15         $graph->graph_theme = null; //设置主题为null,否则value->Show(); 无效
    16 
    17 //        $lineplot1->SetFillColor("orange"); //填充色
    18 
    19 
    20         //设置图表的标题字体、大小  解决中文乱码
    21         $graph->title->Set(iconv("UTF-8","GB2312//IGNORE",$title));   //设置图像标题
    22         $graph->xaxis->title->Set(iconv("UTF-8","GB2312//IGNORE",$x_title)); //设置坐标轴名称
    23         $graph->yaxis->title->Set(iconv("UTF-8","GB2312//IGNORE",$y_title));
    24 //        $graph->y2axis->title->Set(iconv('utf-8', 'GB2312//IGNORE', "book B 销售金额(万元)"));//设置y轴的标题
    25 //        $graph->y2axis->title->SetMargin(20);//设置右边的title到图的距离
    26         $graph->title->SetMargin(10);//设置title到图的距离
    27         $graph->xaxis->title->SetMargin(10);
    28         $graph->yaxis->title->SetMargin(10);
    29         //设置字体
    30         $graph->title->SetFont(FF_SIMSUN,FS_BOLD);
    31         $graph->xaxis->title->SetFont(FF_SIMSUN,FS_BOLD); //设置图表的标题字体、大小
    32         $graph->yaxis->title->SetFont(FF_SIMSUN,FS_BOLD);// FF_SIMSUN表示中文简体
    33         //折线颜色图例
    34         $graph->legend->SetLayout(LEGEND_HOR);
    35 //        $graph->legend->Pos( 0.025, 0.08, "right","center" ); //图例文字框的位置 0.025,0.08 是以右上角为基准的,0.025是距左右距离,0.08是上下距离。 http://nodonkey.iteye.com/blog/761700
    36         $graph->legend->Pos(0.4, 0.95, 'center', 'bottom');// 图例文字框的位置 0.4,0.95 是以右上角为基准的,0.4是距左右距离,0.95是上下距离
    37         //数据
    38         //颜色图例
    39         $i=0;
    40         foreach($xdata as $k=>$v){
    41             $v=array_merge(array(''),$v);
    42             $lineplot='lineplot'.$i;
    43             $$lineplot=new LinePlot($v); //创建设置n条曲线对象
    44             $$lineplot->value->SetColor($this->iColorList[$i]); //设置线上字体颜色
    45             /*            $color=new ColorFactory();
    46                         $$lineplot->value->SetColor($color->getColor()); //设置线颜色随机*/
    47             $$lineplot->SetWeight(4); //线宽 加深加粗
    48             $$lineplot->value->Show();
    49 //            $bt=iconv("UTF-8","gb2312",$legend[$k-1]);
    50 //            $$lineplot->SetFont(FF_SIMSUN,FF_SIMSUN,8);
    51             $$lineplot->SetLegend($k);
    52 
    53             $graph->Add($$lineplot); //在统计图上绘制曲线
    54             $$lineplot->SetColor($this->iColorList[$i]); //设置统计图的颜色,一定要在添加到画布之后再设置
    55             $i++;
    56         }
    57 //x轴坐标 自带月份 $gDateLocale=new DateLocale; $graph->xaxis->SetTickLabels($gDateLocale->GetShortMonth());
    58         $graph->xaxis->SetTickLabels($x_zuobiao); //x轴坐标 月份
    59 
    60         if($picname==null) {
    61             $graph->Stroke();  //生成本地图表,黙认留空,生成在当前目录,可以Stroke(“路径/文件名.png”)这样指定路径
    62         } else{
    63             $path=$this->root_path.'/Uploads/report/'.$picname;
    64             if(file_exists($path)){
    65                 @unlink($path);
    66             }
    67             $graph->Stroke($path); //保存图像
    68             return $path;
    69         }
    70     }
  • 相关阅读:
    WPF Prefix 'attach' does not map to a namespace.
    C# 用ManulResetEvent 控制Thread的 Suspend、Resume
    C# 监控Windows睡眠与恢复
    c# DataTable to Object Mapping
    C# DispatcherTimer Start之后立即执行
    Visual studio 编译时copy文件、文件夹
    c# 无法加载xxx.dll 找不到指定的模块(如何指定文件夹)
    EntityFramework 找不到方法:“Void System.Data.Entity.DbModelBuilder.RegisterEntityType
    wpf 全局异常捕获处理
    pandas入门
  • 原文地址:https://www.cnblogs.com/xujian2016/p/7699447.html
Copyright © 2020-2023  润新知