php曲线图生成代码 jpgraph类生成php曲线图

发布时间:2020-03-31编辑:脚本学堂
分享一例php使用jpgraph类生成曲线图的代码,学习下jpgraph类库的用法,感兴趣的朋友可以作个参考。

本节内容:
php曲线图生成示例,使用jpgraph类生成曲线图。

一,Config.class.php 配置类
 

复制代码 代码示例:

<?php
class Config {
     public $DB_SERVER = "localhost";
     public $DB_NAME = "report";
     public $DB_USER = "root";
     public $DB_PASSWORD = "";

     public $ARR_TYPE = array(
 '1' => '焦点事件',
 '2' => '人肉搜索',
 '3' => '网络踪迹',
 '4' => '隐私保护',
 '5' => '声誉管理',
 '6' => '外籍搜索',
 '7' => '搜索栏',
 '8' => '优库MSN机器人'
     );

     public $ARR_COLOR = array(
 '1' => 'red',
 '2' => 'purple',
 '3' => 'lime',
 '4' => 'aqua',
 '5' => 'blue',
 '6' => 'teal',
 '7' => 'gray',
 '8' => 'maroon'
     );

     public $INT_CHART_HEIGHT = 600;
     public $INT_CHART_WIDTH = 1200;

     public $ARR_MONTH_DAYS = array(
 '1' => '31',
 '2' => '28',
 '3' => '31',
 '4' => '30',
 '5' => '31',
 '6' => '30',
 '7' => '31',
 '8' => '31',
 '9' => '30',
 '10' => '31',
 '11' => '30',
 '12' => '31'
     );
}
?>

二,createChart.php  生成曲线图
 

复制代码 代码示例:

<?php
  define("ENABLED_DB",1);
  require_once("global.php");
  $int_year = trim($_GET['year']);
  $int_month = trim($_GET['month']);
  $arr_result = $obj_func->getData($int_year,$int_month);

  if( empty($arr_result) ) {
     die("No data! Please have another choice and try again!");
  }

  for( $i=0;$i<count($arr_result);$i++ ) {//按类型和时间获取数组
     $arr_date = explode('-',$arr_result[$i]['date']);
     $ydata[$arr_result[$i]['type']][intval($arr_date[2])] = intval($arr_result[$i]['count']);
  }

  for( $i=1;$i<count($obj_cnf->ARR_TYPE);$i++ ) {//没有值则补0
     for( $j=1;$j<=$obj_cnf->ARR_MONTH_DAYS[$int_month];$j++ ) {
   if( empty($ydata[$i][$j]) ) {
      $ydata[$i][$j] = 0;
   }
     }
     sort($ydata[$i]); //时间排序
  }
  $xdata = $obj_func->getDays($int_year,$int_month); //获取一个月时间

  //Create the graph. These two calls are always required
  $graph = new Graph($obj_cnf->INT_CHART_WIDTH,$obj_cnf->INT_CHART_HEIGHT,"auto"); //生成画图对象  宽,高
  $graph->SetScale("textlin");
  $graph->img->SetMargin(60,50,50,150);    //边框间距
  $graph->SetShadow(); //阴影
  $graph->ygrid->Show(true,true);
  $graph->xgrid->Show(true,false);
  $graph->title->Set('Report');     //标题
  $graph->xaxis->title->Set("");   
  $graph->yaxis->title->Set("");
  $graph->title->SetFont(FF_FONT1,FS_BOLD);
  $graph->yaxis->title->SetFont(FF_FONT1,FS_BOLD);
  $graph->xaxis->title->SetFont(FF_FONT1,FS_BOLD);
  $graph->xaxis->SetTickLabels($xdata);    //x坐标的值 一般放时间
  $graph->xaxis->SetTextLabelInterval(1);  //间隔数
  $graph->xaxis->SetLabelAngle(70);  //旋转角度
  $graph->xaxis->SetFont(FF_SIMSUN); //字体

  for( $i=1;$i<count($obj_cnf->ARR_TYPE);$i++ ) {
     if( !empty($ydata[$i]) ) {
   //Create the linear plot
   $lineplot = new LinePlot($ydata[$i]);   //生成多条曲线在同一曲线图上
   $lineplot->SetColor($obj_cnf->ARR_COLOR[$i]);  //配置不同颜色
   //Add the plot to the graph
   $graph->Add($lineplot);
     }
  }
  //Display the graph
  $graph->Stroke();
?>

三,showChart.php  显示曲线图
 

复制代码 代码示例:
<?php
/**
* 曲线图的显示页面
* by www.jb200.com
*/
  require_once("global.php");
  $int_year = trim($_GET['year']);
  $int_month = trim($_GET['month']);
  ?>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <div style="marign:0 auto;text-align:center;">
     <a href="index.php">Back</a>
  </div>
  <div style="height:20px;" ></div>
  <div><!-- 这里生成颜色代表的含义 -->
     <?php foreach( $obj_cnf->ARR_TYPE as $key=>$val ) { ?>
   <div style="border-top:2px solid <?php echo $obj_cnf->ARR_COLOR[$key];?>;width:20px;float:left;"></div>
   <div style="float:left;margin-right:10px;">:<?php echo $val;?></div>
     <?php } ?>
  </div>
  <div style="height:20px;" ></div>
  <!-- 采用iframe方式 -->
  <iframe src="createChart.php?year=<?php echo $int_year;?>&month=<?php echo $int_month;?>"
  width="<?php echo $obj_cnf->INT_CHART_WIDTH;?>" height="<?php echo $obj_cnf->INT_CHART_HEIGHT;?>" frameborder="0"/>

四,Func.class.php  函数类
 

复制代码 代码示例:

<?php
/**
* php曲线图生成的公共函数类
* by www.jb200.com
*/
class Func {
   public function getData( $intYear,$intMonth,$intType='' ) {
   global $obj_conn;
   $str_sql_where = empty($intType) ? " " : " AND type=".$intType." ";
   $str_sql_where = " AND date between '".$intYear."-".$intMonth."-01 00:00:01' and '".$intYear."-".$intMonth."-31 23:59:59' ";
   $str_sql = " SELECT id,type,count,date(date) as date FROM report WHERE 1=1 ".$str_sql_where." ORDER BY date(date) asc";
   $obj_statm = $obj_conn->prepare($str_sql);
   $obj_statm->execute();
   $arr_result = $obj_statm->fetchAll();
   return $arr_result;
     }

   public function insertData( $intCount,$intType,$strDate='' ) {
   global $obj_conn;
   $str_sql = " INSERT INTO report(count,type,date) VALUES(".$intCount.",".$intType.",'".$strDate."')";
   $obj_statm = $obj_conn->prepare($str_sql);
   return $obj_statm->execute();
     }

     /**
      * get date option(year,month,day)
      * @param $strSelectedDate
      * @param $strStartDate
      * @param $strEndDate
      * @return string
      * @author andy
      */
   public function getDateOptions( $strSelectedDate='',$strStartDate='1950',$strEndDate='2015' ) {
   for( $i=$strStartDate;$i<=$strEndDate;$i++ ) {
      if( $i==$strSelectedDate ) {
    $str_option .= "<option value='".$i."' selected>".$i."</option> rn";
      } else {
    $str_option .= "<option value='".$i."'>".$i."</option> rn";
      }
   }
   return $str_option;
     }

     /**
      * get days
      * @param $intYear
      * @param $intMonth
      * @param $intMaxDay
      * @return string
      * @author andy
      */
     public function getDays( $intYear,$intMonth ) {
   global $obj_cnf;
   $int_max_day = $obj_cnf->ARR_MONTH_DAYS[$intMonth];
   for( $i=1;$i<=$int_max_day;$i++ ) {
      $arr_result[] = $intYear."-".$intMonth."-".$i;
   }
   return $arr_result;
     }
  }
?>

五,global.php  全局导入文件
 

复制代码 代码示例:

<?php
  require_once("jpgraph/src/jpgraph.php");
  require_once("jpgraph/src/jpgraph_line.php");
  require_once("Config.class.php");
  require_once("Func.class.php");
  $obj_func = new Func();
  $obj_cnf = new Config();

  if ( @constant( "ENABLED_DB" ) ) {//连接数据库
     try {
   $obj_conn = new Pdo("mysql:host=".$obj_cnf->DB_SERVER.";dbname=".$obj_cnf->DB_NAME,$obj_cnf->DB_USER,$obj_cnf->DB_PASSWORD);
     } catch ( Exception $e ) {
   echo $e;
     }
  }
  ?>

六,index.php
 

复制代码 代码示例:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  <html xmlns="http://www.w3.org/1999/xhtml"  xml:lang="zh-CN" lang="zh-CN">
  <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Report-曲线图-www.jb200.com</title>
  <script type="text/javascript" src="jquery.js"></script>
  <style type="text/css">
  body {
     text-align:center;
  }

  #main {
     margin:100px auto;
     width:800px;
  }

  .box {
     border:1px solid #fff;
     padding:5px;
     margin:0 auto;
     height:300px;
     width:500px;
  }

  fieldset {
     border:1px solid #99CCFF;
     height:200px;
  }

  .blank {
     height:20px;
  }

  .button {
     width:70px;
     height:22px;
     line-height:20px;
     border:1px solid #ccc;
     margin:0 5px;
     color:#fff;
     background:#6495ED;
     font-weight:bold;
  }
  </style>
  <script type="text/javascript">
  $(function() {
     $('#button_submit').bind(
   'click',
   function() {
      var year = $('#year').val();
      var month = $('#month').val();
      document.location.target = "_blank";
      document.location.href = "showChart.php?year="+year+"&month="+month;
   }
     )
  }
  );
  </script>
  </head>
  <body>
  <div id="main">
     <div class="box">
   <fieldset>
      <legend>Please select date first</legend>
      <div class="blank"></div>
      <div>YEAR:
    <?php
 define("ENABLED_DB",1);
 require_once("global.php");
 $str_year_options = $obj_func->getDateOptions('2008','2008','2010');
 $str_month_options = $obj_func->getDateOptions('1','1','12');
    ?>
    <select name="year" id="year">
 <?php echo $str_year_options; ?>
    </select>
    MONTH:
    <select name="month" id="month">
 <?php echo $str_month_options; ?>
    </select>
      </div>
      <div class="blank"></div>
      <div class="blank"></div>
      <div>
    <button class="button" id="button_submit">Go</button>
      </div>
   </fieldset>
   <div class="blank"></div>
   <div>
    Andy.2008-8-21
   </div>
     </div>
  </div>
  </body>
  </html>

七,表结构 `report`
 

复制代码 代码示例:
  CREATE TABLE `report` (
   `id` int(10) unsigned NOT NULL auto_increment,
   `count` int(11) NOT NULL,
   `type` smallint(6) NOT NULL,
   `date` date NOT NULL,
   PRIMARY KEY  (`id`),
   UNIQUE KEY `type` (`type`,`date`)
  ) ENGINE=innodb DEFAULT CHARSET=utf8 AUTO_INCREMENT=1257 ;