当前位置:首页 > PHP教程 > php图像处理 > 列表

jpgraph 生成类型数据以图表走势图的形式表现出来

发布:smiling 来源: PHP粉丝网  添加日期:2014-08-19 14:23:27 浏览: 评论:0 
  1. <?php 
  2.  
  3. /** 
  4.  *  ecshop jpgraph图表类库 
  5.  *  ============================================================== 
  6.  *  利用开源的jpgraph库实现对各类型数据以图表,走势图的形式表现出来。 
  7.  *  ============================================================== 
  8. **/ 
  9.  
  10. class cls_jpgraph 
  11. { 
  12.  var $db = null; 
  13.  var $ydata = array(); 
  14.  var $width = 350; 
  15.  var $height = 250; 
  16.  
  17.  var $graph = ''; //图形对象 
  18.  var $piegraph = ''; //丙状图形 
  19.  
  20.  var $lineplot = ''; //线性对象 
  21.  var $barpot = ''; //柱状对象 
  22.  var $gbplat = ''; //柱状组对象 
  23.  var $txt = '';  //文本对象 
  24.  var $p1 = '';  //丙状图对象 
  25.  
  26.  var $scale = ''; //图表类型? (textlin,textlog,intlin) 
  27.  var $yscale = '';   // (log,) 
  28.  var $xgrid = false; //x轴网格显示 
  29.  var $ygrid = false; //y轴网格显示 
  30.  var $title = ''; //标题 
  31.  var $subtitle = ''; //子标题(一般为日期) 
  32.  var $xaxis = ''; //x轴名称 
  33.  var $yaxis = ''; //y轴名称 
  34.  
  35.  /*margin position*/ 
  36.  var $left = 0; 
  37.  var $right = 0; 
  38.  var $top = 0; 
  39.  var $bottom = 0; 
  40.  
  41.  /** 
  42.  *构造函数 
  43.  */ 
  44.  function cls_jpgraph($width=350,$height=250) 
  45.  { 
  46.   $this->width = $width; 
  47.   $this->height = $height; 
  48.  
  49.   $this->graph = new graph($this->width,$this->height); 
  50.  
  51.  } 
  52.  
  53.  /** 
  54.  * 图表类库的构造函数 
  55.  * 
  56.  */ 
  57.  /* 
  58.  function __construct($parms,$width,$height) 
  59.  { 
  60.   cls_jpgraph($parms,$width,$height); 
  61.  } 
  62.  */ 
  63.  
  64.  /*图片基本信息设置*/ 
  65.  function set_jpgraph($scale='intlin',$title='',$subtitle='',$bgcolor='',$xaxis='', 
  66.      $yaxis='',$xgrid=false,$ygrid=false,$margin='') { 
  67. //开源代码phpfensi.com 
  68.   $this->scale = $scale; 
  69.   $this->title = $title; 
  70.   $this->subtitle = $subtitle; 
  71.   $this->xaxis = $xaxis; 
  72.   $this->yaxis = $yaxis; 
  73.   $this->xgrid = $xgrid; 
  74.   $this->ygrid = $ygrid; 
  75.  
  76.   if(!emptyempty($scale)) { 
  77.    $this->graph->setscale($this->scale); 
  78.   } 
  79.   else { 
  80.   $this->graph->setscale('intlin'); 
  81.   } 
  82.   $this->graph->xgrid->show($this->xgrid,$this->xgrid); 
  83.   $this->graph->ygrid->show($this->ygrid,$this->ygrid); 
  84.  
  85.   if(!emptyempty($bgcolor)) { 
  86.    $this->graph->setmargincolor($bgcolor); 
  87.   } 
  88.  
  89.   /*如果手工设置了图片位置*/ 
  90.   if(is_array($margin)) { 
  91.    while(list($key,$val) = each($margin)) { 
  92.     $this->$key = $val; 
  93.    } 
  94.    $this->graph->setmargin($this->left,$this->right,$this->top,$this->bottom); 
  95.   } 
  96.  
  97.   if(!emptyempty($this->title)) { 
  98.    $this->graph->title->set($this->title); 
  99.   } 
  100.   if(!emptyempty($this->subtitle)) { 
  101.    $this->graph->subtitle->set($this->subtitle); 
  102.   } 
  103.   else { 
  104.    $this->graph->subtitle->set('('.date('y-m-d').')'); //默认子标题设置为当前日期 
  105.   } 
  106.   if(!emptyempty($this->xaxis)) { 
  107.    $this->graph->xaxis->title->set($this->xaxis); 
  108.   } 
  109.   if(!emptyempty($this->yaxis)) { 
  110.    $this->graph->yaxis->title->set($this->yaxis); 
  111.   } 
  112.  
  113.  } 
  114.  
  115.  /*创建线性关系图表(linear plot)*/ 
  116.  function create_lineplot($parms,$color='black',$weight=1) 
  117.  { 
  118.   $this->ydata = $parms; 
  119.   $this->lineplot = new lineplot($this->ydata); 
  120.   $this->lineplot->setcolor($color); 
  121.   $this->lineplot->setweight($weight); 
  122.  
  123.   return $this->lineplot; 
  124.  } 
  125.  
  126.  /*创建柱状图表(bar pot)*/ 
  127.  function create_barpot($parms,$color='black',$width='0') 
  128.  { 
  129.   $this->ydata = $parms; 
  130.   $this->barpot = new barplot($this->ydata); 
  131.   $this->barpot->setfillcolor($color); 
  132.   if(!emptyempty($width)) { 
  133.    $this->barpot->setwidth($width); 
  134.   } 
  135.  
  136.   return $this->barpot; 
  137.  } 
  138.  
  139.  /*创建数据柱状图表组*/ 
  140.  function create_bargroup($plotarr,$width='0.8') 
  141.  { 
  142.   $this->gbplot = new groupbarplot($plotarr); 
  143.   $this->gbplot->setwidth($width); 
  144.  
  145.   return $this->gbplot; 
  146.  } 
  147.  
  148.  /*创建文本内容*/ 
  149.  function create_text($str,$postion='',$color='black') 
  150.  { 
  151.   $this->txt = new text($str); 
  152.  
  153.   if(is_array($postion)) { 
  154.    while(list($key,$val) = each($postion)) { 
  155.     $this->$key = $val; 
  156.    } 
  157.    $this->txt->setpos($this->left,$this->top); 
  158.   } 
  159.   else { 
  160.    $this->txt->setpos(10,20); 
  161.   } 
  162.   $this->txt->setcolor($color); 
  163.  
  164.   $this->graph->add($this->txt); 
  165.  } 
  166.  
  167.  /*创建丙状图表*/ 
  168.  function create_pie($parms,$title,$type='3d',$size='0.5',$center='0.5',$width='350',$height='250') 
  169.  { 
  170.   $this->width = $width; 
  171.   $this->height = $height; 
  172.  
  173.   $this->piegraph = new piegraph(300,200); 
  174.   $this->piegraph->setshadow(); 
  175.  
  176.   $this->piegraph->title->set($title); 
  177.  
  178.   if('3d' != $type) { 
  179.    $this->p1 = new pieplot($parms); 
  180.    $this->piegraph->add($this->p1); 
  181.   } 
  182.   else { 
  183.    $this->p1 = new pieplot3d($parms); 
  184.    $this->p1->setsize($size); 
  185.    $this->p1->setcenter($center); 
  186. //   $this->p1->setlegends($gdatelocale->getshortmonth()); 
  187.    $this->piegraph->add($this->p1); 
  188.   } 
  189.   $this->piegraph->stroke(); 
  190.  } 
  191.  
  192.  function get_auth_code($length=4) 
  193.  { 
  194.   $spam = new antispam(); 
  195.   $chars = $spam->rand($length); 
  196.  
  197.   if( $spam->stroke() === false ) { 
  198.    return false; 
  199.   } 
  200.  
  201.   return $chars; 
  202.  } 
  203.  
  204.  /*完成图形创建并显示*/ 
  205.  function display($obj) 
  206.  { 
  207.   $this->graph->add($obj); 
  208.   $this->graph->stroke(); 
  209.  } 
  210. } 
  211.  
  212. ?> 

Tags: jpgraph php图表 php走势图

分享到: