Thinkphp3.2整合Jpgraph函数 调用数据库生成统计折线图(多图)

最近做预约系统,为了整合Jpgraph函数弄了半天...

网站系统后台是用:Thinkphp3.2.3

Jpgraph版本:jpgraph-3.5.0b1

如下图所示类似效果~~

Thinkphp3.2整合Jpgraph函数 调用数据库生成统计折线图(多图)_第1张图片

首先Jpgraph代码放到如下位置:

jpgraph.php和jpgraph_line.php加上 .class方便文件引用(jpgraph.class.php、jpgraph_line.class.php)

控制器YejiController.class.php:

public function draw(){
		$patient=D("patient");
		import("Org.Util.jpgraph.jpgraph");
		import("Org.Util.jpgraph.jpgraph_line");
		
		$frommounth = date("Y-m-01 00:00:00");
	
		
		if(I('way') == '1'){
			$map['yuyueren'] = array('eq',14);  //14 车xx
		}else if(I('way') == '2'){
			$map['yuyueren'] = array('eq',11);  //11 王xx
		}else if(I('way') == '3'){
			$map['yuyueren'] = array('eq',12); //王x
		}else if(I('way') == '4'){
			$map['yuyueren'] = array('eq',13); //刘xx
		}else if(I('way') == '5'){
			$map['yuyueren'] = array('eq',15); //邵xx
		}
		
		
		
		for($i = 1; $i <= date('d'); $i++){
			if($i < 10){
				$md[] = date("m").'-0'.$i;
			}else{
				$md[] = date("m").'-'.$i;
			}
		}
		//dump($md);/*1234567  横坐标*/
		
		
		$info1 = $patient->field("time1")->where($map)->where("time1>='%s' and time1<='%s'",array($frommounth,date("Y-m-d H:i:s")))->select();//预约时间
		$info1Count=count($info1);
			
		$info2 = $patient->field("fenzhenshijian")->where($map)->where("fenzhenshijian>='%s' and fenzhenshijian<='%s'",array($frommounth,date("Y-m-d H:i:s")))->select();//分诊时间
		$info2Count=count($info2);
		
		
		//预约
		foreach($info1 as $k => $v){
			if(!empty($v['time1']) && $v['time1']!='0000-00-00 00:00:00'){
				$time1[] = date("m-d",strtotime($v['time1']));
			}			
		}
		//分诊
		foreach($info2 as $k => $v){
			if(!empty($v['fenzhenshijian']) && $v['fenzhenshijian']!='0000-00-00 00:00:00'){
				$fenzhenshijian[] = date("m-d",strtotime($v['fenzhenshijian']));
			}			
		}
		
			
		$time1 = array_count_values($time1); //对数组中的所有值进行计数 (数组形式)
		$fenzhenshijian = array_count_values($fenzhenshijian);
		
		
		$time1 = isset($time1)?$time1:array(); //空 折线图
		$fenzhenshijian = isset($fenzhenshijian)?$fenzhenshijian:array();
		
		foreach($md as $v){			
			if(!array_key_exists($v,$time1)){//array_key_exists() 函数检查某个数组中是否存在指定的键名
				$time1 = array_merge($time1,array($v=>'0'));
		    }
		}
		foreach($md as $v){			
			if(!array_key_exists($v,$fenzhenshijian)){//array_key_exists() 函数检查某个数组中是否存在指定的键名
				$fenzhenshijian = array_merge($fenzhenshijian,array($v=>'0'));
		    }
		}
		

		ksort($time1);//对关联数组按照键名进行升序排序
		ksort($fenzhenshijian);
	
		$time1=array_values($time1);//返回一个包含给定数组中所有键值的数组,但不保留键名。
		$fenzhenshijian=array_values($fenzhenshijian);
		//$time1 = array_splice($time1,0,date('d'));
	
		//dump($time1);
		//dump($fenzhenshijian);
		
		//计算预约人数最大值
		$appdatenum = $time1;
		sort($appdatenum,SORT_NUMERIC);
		$appdatenum = end($appdatenum);
		// echo $appdatenum;
		//计算到诊人数最大值
		$dzdatenum = $fenzhenshijian;
		sort($dzdatenum,SORT_NUMERIC);
		$dzdatenum = end($dzdatenum);
		
		$topnum = array($appdatenum,$dzdatenum);
		sort($topnum,SORT_NUMERIC);
		$topnum = end($topnum);
		
		
	
		//设置直线图
		//1 创建画布
		$graph = new \Graph(1000,400);
		$graph->title->SetFont(FF_SIMSUN,FS_BOLD,14);//中文

		//2.设置横纵坐标刻度样式
		$graph->SetScale("textint",0,$topnum+1);
		//$graph->SetScale("textint");
		
		//3,设置统计图的标题
		if(I('way') == '1'){
			$graph->title->Set(iconv("UTF-8","GB2312//IGNORE",date("Y-m").'车xx统计图'));
		}else if(I('way') == '2'){
			$graph->title->Set(iconv("UTF-8","GB2312//IGNORE",date("Y-m").'王xx统计图'));
		}else if(I('way') == '3'){
			$graph->title->Set(iconv("UTF-8","GB2312//IGNORE",date("Y-m").'王x统计图'));
		}else if(I('way') == '4'){
			$graph->title->Set(iconv("UTF-8","GB2312//IGNORE",date("Y-m").'刘xx统计图'));
		}else if(I('way') == '5'){
			$graph->title->Set(iconv("UTF-8","GB2312//IGNORE",date("Y-m").'邵xx统计图'));
	}
		

		
		
			//4.得到LinePlot对象
			$linePlot=new \LinePlot($time1);
			//得到第二条曲线
			$linePlot2y=new \LinePlot($fenzhenshijian);
			
			
			// 加入 x 轴标注  
			$graph->xaxis->SetTickLabels($md);  
			
			//5,设置图例
			$linePlot->SetLegend(iconv("UTF-8","GB2312//IGNORE",'预约人数('.$info1Count.')'));
			$linePlot2y->SetLegend(iconv("UTF-8","GB2312//IGNORE",'到诊人数('.$info2Count.')'));
			
			
			//6,将统计图添加到画布上
			$graph->Add($linePlot);
			$graph->Add($linePlot2y);
			
			//$linePlot->value->SetFormatCallback("cbFmtPercentage");//设置数据显示格式
			$linePlot->value->SetColor("red");
			$linePlot->value->Show();//让具体数据在顶端显示出来
			$linePlot->value->SetFormat('%d'); //人数显示格式
			$linePlot2y->value->SetColor("#1DA848");
			$linePlot2y->value->Show();//让具体数据在顶端显示出来
			$linePlot2y->value->SetFormat('%d'); //人数显示格式
			
			$graph->xaxis->title->SetFont(FF_SIMSUN,FS_BOLD);
			$graph->yaxis->title->SetFont(FF_SIMSUN,FS_BOLD);
			//设置三个坐标轴名称
			$graph->xaxis->title->Set(iconv("UTF-8","GB2312//IGNORE",'日期'));
			$graph->yaxis->title->Set(iconv("UTF-8","GB2312//IGNORE",'人数'));
			
			//设置统计图的颜色,一定要在添加到画布之后再设置
			$linePlot->SetColor('red');
			$linePlot2y->SetColor('#1DA848');
			//输出画布
			if(I('way')){
				$graph->Stroke();
			}
			
	

		$this->display();
	}

视图模版文件draw.html:




    
    后台管理
    
    
    


{include file="Public/header.html"}
{include file="Public/left.html"}
首页>业绩图表












结束

你可能感兴趣的:(PHP)