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

 更新时间:2016年11月25日 16:57  点击:1963
 代码如下 复制代码

<?php教程

/**
 *  ecshop jpgraph图表类库
 *  ==============================================================
 *  利用开源的jpgraph库实现对各类型数据以图表,走势图的形式表现出来。
 *  ==============================================================
**/

class cls_jpgraph
{
 var $db = null;
 var $ydata = array();
 var $width = 350;
 var $height = 250;

 var $graph = ''; //图形对象
 var $piegraph = ''; //丙状图形

 var $lineplot = ''; //线性对象
 var $barpot = ''; //柱状对象
 var $gbplat = ''; //柱状组对象
 var $txt = '';  //文本对象
 var $p1 = '';  //丙状图对象

 var $scale = ''; //图表类型? (textlin,textlog,intlin)
 var $yscale = '';   // (log,)
 var $xgrid = false; //x轴网格显示
 var $ygrid = false; //y轴网格显示
 var $title = ''; //标题
 var $subtitle = ''; //子标题(一般为日期)
 var $xaxis = ''; //x轴名称
 var $yaxis = ''; //y轴名称

 /*margin position*/
 var $left = 0;
 var $right = 0;
 var $top = 0;
 var $bottom = 0;

 /**
 *构造函数
 */
 function cls_jpgraph($width=350,$height=250)
 {
  $this->width = $width;
  $this->height = $height;

  $this->graph = new graph($this->width,$this->height);

 }

 /**
 * 图表类库的构造函数
 *
 */
 /*
 function __construct($parms,$width,$height)
 {
  cls_jpgraph($parms,$width,$height);
 }
 */

 /*图片基本信息设置*/
 function set_jpgraph($scale='intlin',$title='',$subtitle='',$bgcolor='',$xaxis='',
               $yaxis='',$xgrid=false,$ygrid=false,$margin='') {

  $this->scale = $scale;
  $this->title = $title;
  $this->subtitle = $subtitle;
  $this->xaxis = $xaxis;
  $this->yaxis = $yaxis;
  $this->xgrid = $xgrid;
  $this->ygrid = $ygrid;

  if(!empty($scale)) {
   $this->graph->setscale($this->scale);
  }
  else {
  $this->graph->setscale('intlin');
  }
  $this->graph->xgrid->show($this->xgrid,$this->xgrid);
  $this->graph->ygrid->show($this->ygrid,$this->ygrid);

  if(!empty($bgcolor)) {
   $this->graph->setmargincolor($bgcolor);
  }

  /*如果手工设置了图片位置*/
  if(is_array($margin)) {
   while(list($key,$val) = each($margin)) {
    $this->$key = $val;
   }
   $this->graph->setmargin($this->left,$this->right,$this->top,$this->bottom);
  }

  if(!empty($this->title)) {
   $this->graph->title->set($this->title);
  }
  if(!empty($this->subtitle)) {
   $this->graph->subtitle->set($this->subtitle);
  }
  else {
   $this->graph->subtitle->set('('.date('y-m-d').')'); //默认子标题设置为当前日期
  }
  if(!empty($this->xaxis)) {
   $this->graph->xaxis->title->set($this->xaxis);
  }
  if(!empty($this->yaxis)) {
   $this->graph->yaxis->title->set($this->yaxis);
  }

 }

 /*创建线性关系图表(linear plot)*/
 function create_lineplot($parms,$color='black',$weight=1)
 {
  $this->ydata = $parms;
  $this->lineplot = new lineplot($this->ydata);
  $this->lineplot->setcolor($color);
  $this->lineplot->setweight($weight);

  return $this->lineplot;
 }

 /*创建柱状图表(bar pot)*/
 function create_barpot($parms,$color='black',$width='0')
 {
  $this->ydata = $parms;
  $this->barpot = new barplot($this->ydata);
  $this->barpot->setfillcolor($color);
  if(!empty($width)) {
   $this->barpot->setwidth($width);
  }

  return $this->barpot;
 }

 /*创建数据柱状图表组*/
 function create_bargroup($plotarr,$width='0.8')
 {
  $this->gbplot = new groupbarplot($plotarr);
  $this->gbplot->setwidth($width);

  return $this->gbplot;
 }

 /*创建文本内容*/
 function create_text($str,$postion='',$color='black')
 {
  $this->txt = new text($str);

  if(is_array($postion)) {
   while(list($key,$val) = each($postion)) {
    $this->$key = $val;
   }
   $this->txt->setpos($this->left,$this->top);
  }
  else {
   $this->txt->setpos(10,20);
  }
  $this->txt->setcolor($color);

  $this->graph->add($this->txt);
 }

 /*创建丙状图表*/
 function create_pie($parms,$title,$type='3d',$size='0.5',$center='0.5',$width='350',$height='250')
 {
  $this->width = $width;
  $this->height = $height;

  $this->piegraph = new piegraph(300,200);
  $this->piegraph->setshadow();

  $this->piegraph->title->set($title);

  if('3d' != $type) {
   $this->p1 = new pieplot($parms);
   $this->piegraph->add($this->p1);
  }
  else {
   $this->p1 = new pieplot3d($parms);
   $this->p1->setsize($size);
   $this->p1->setcenter($center);
//   $this->p1->setlegends($gdatelocale->getshortmonth());
   $this->piegraph->add($this->p1);
  }
  $this->piegraph->stroke();
 }

 function get_auth_code($length=4)
 {
  $spam = new antispam();
  $chars = $spam->rand($length);

  if( $spam->stroke() === false ) {
   return false;
  }

  return $chars;
 }

 /*完成图形创建并显示*/
 function display($obj)
 {
  $this->graph->add($obj);
  $this->graph->stroke();
 }
}

?>

使用ImageCreate()创建一个代表空白图像的变量,这个函数要求以像素为单位的图像大小的参数,其格式是ImageCreate(x_size, y_size)。如果要创建一个大小为250×250的图像,就可以使用下面的语句:   

<? header ("content-type: image/png");  

   

 代码如下 复制代码
  $newimg = imagecreate(250,250);  

 

  由于图像还是空白的,因此你可能会希望用一些彩色来填充它。你需要首先使用imagecolorallocate()函数用其rgb值为这种颜色指定一个名字,这一函数的格式为imagecolorallocate([image], [red], [green], [blue])。如果要定义天蓝色,可以使用如下的语句:  

 

 代码如下 复制代码
  $skyblue = imagecolorallocate($newimg,136,193,255);  

 

  接下来,需要使用imagefill()函数用这种颜色填充这个图像,imagefill()函数有几个版本,例如imagefillrectangle()、imagefillpolygon()等。为简单起见,我们通过如下的格式使用imagefill()函数:  

 代码如下 复制代码

  imagefill([image], [start x point], [start y point], [color])

  imagefill($newimg,0,0,$skyblue);  

 

  最后,在图像建立后释放图像句柄和所占用的内存:  

 

 代码如下 复制代码

 imagepng($newimg);

  imagedestroy($newimg); ?>

  这样,创建图像的全部代码如下所示:

      php教程代码:
 

 代码如下 复制代码

<? header ("content-type: image/png");

  $newimg = imagecreate(250,250);

  $skyblue = imagecolorallocate($newimg,136,193,255);

  imagefill($newimg,0,0,$skyblue);

  imagepng($newimg);

  imagedestroy($newimg);

  ?>   

.jpgraph开源项目介绍
jpgraph是一个面向对象图形创建函数库。可用它来生成柱状图,饼状图,甘特图,网状图等常用到的一些图形。支持的图片格式有gif,jpg和png。

jpgraph是一个开源的利用php教程编写的专门提供图表的类库。它使得作图变成了一件非常简单的事情,你只需从数据库教程中取出相关数据,定义标题,图表类型等内容,你只需要学习掌握为数不多的jpgraph内置函数(可以参照jpgraph附带例子学习),利用简单的几行代码就可以做出超酷超炫的图表来!

2.jpgraph下载安装及使用
jpgraph的官方下载地址是:http://jpgraph.net/download/

下载时要注意,jpgraph分为几个版本,你可以根据你的php版本来确定下载那个版本的jpgraph库文件。

安装的话比较简单,不过需要注意下面两点:

确保你的php版本最低为4.04(不过我估计一般现在都5.0以上了,应该不成问题)。
另外一定要支持gd库,jpgraph是基于gd库的,至于gd库版本则可随意。
下载完成jpgraph后,将压缩包解压到任意目录下,进入到jpgraph-版本号目录下,有两个目录,其他的txt文件为简单使用说明文件,可以看看了解即可。docportal目录为帮助系统目录,其中包括从开始安装配置到使用说明,函数介绍等一应俱全,如果有耐心的话,可以好好看看。

我们主要需要的examples目录,在它里面包含了jpgraph库文件和很多的样例文件,我们可以查看学习它的样例文件,这样学习使用起来jpgraph才是最快的。

在jpgraph库文件目录中有一个名为jpg-config.inc的文件,它是jpgraph的配置文件,通过这里可以设置jpgraph的相关参数,例如设置jpgraph的cache(缓存)文件夹,和ttf(字体)文件夹等内容。

注意事项:

cache(缓存)文件夹路径可以自己定义,而ttf(字体)文件夹必须是%system%/fonts。
确保php对cache(缓存)文件夹有写的权限。
注意程序编码为utf-8编码。
3.简单样例
关于jpgraph的样例程序在examples目录下实在够多,我就不再过多罗嗦,就简单说明一下写法及上一个小例子。

首先在程序开始引用包含jpgraph库文件:

require_once ('jpgraph/jpgraph.php');
require_once ('jpgraph/jpgraph_line.php');
然后开始创建图形对象:

$graph = new graph(350,250);
设置图形及图表的各种样式属性参数:

$graph->setscale("textlin");
$graph->img->setmargin(30,90,40,50);
$graph->xaxis->setfont(ff_font1,fs_bold);
最后进行显示:

$graph->add($lineplot);
$graph->stroke();
下面是一个比较简单的但完整的jpgraph程序样例:

setscale("textlin");
$graph->img->setmargin(30,90,40,50);
$graph->xaxis->setfont(ff_font1,fs_bold);
$graph->title->set("dashed lineplot");

// create the linear plot
$lineplot=new lineplot($ydata);
$lineplot->setlegend("test 1");
$lineplot->setcolor("blue");

// style can also be specified as setstyle([1|2|3|4]) or
// setstyle("solid"|"dotted"|"dashed"|"lobgdashed")
$lineplot->setstyle("dashed");

// add the plot to the graph
$graph->add($lineplot);

// display the graph
$graph->stroke();
?>
到此jpgraph库的基本介绍就结束了,再下一篇我会放出我写的已经封装了jpgraph库的类文件,同时可能会有一篇关于jpgraph常见问题总结的说明,欢迎大家到时候给我提出意见哦。

本教程是一款php图片上传然后,把上传的图片生成小图片哦,是一款非常好的文件上传类,如果你正在找类程序可以进来看看
 代码如下 复制代码

function uploadimage($upname,$smallmark=1,$dstsw,$dstsh=0,$path_dim,$path_xim,$newname,$smallname=0,$filetype="null") {
    global $webaddr,$_files,$my;
    $phpv=str_replace('.', '', php_version);
    $filename=$upname;
    $max_file_size = 2147483648;        //上传文件大小限制, 单位byte 2m
    $path_im = $path_dim;               //生成大图保存文件夹路径
    $path_sim = $path_xim;              //缩略图保存文件夹路径
    $simclearly=75;
    $simclearlypng =$phpv>=512?7:75;        //缩略图清晰度0-100,数字越大越清晰,文件尺寸越大
    $smallmark = $smallmark;            //是否生成缩略图(1为加生成,其他为不);
    $dst_sw =$dstsw;                   //定义缩略图宽度,高度我采用等比例缩放,所以只要比较宽度就可以了
    $uptypes=array(
        'image/jpg',
        'image/jpeg',
        'image/png',
        'image/pjpeg',
        'image/gif',
        'image/bmp',
        'image/x-png'
    );

    if (!is_uploaded_file($_files[$filename][tmp_name])) {
        dsetcookie('setok','upload1');
        header("location:111cn.net/profile");
        exit;
    }
    $file = $_files[$filename];
    $pinfo = pathinfo($file["name"]);
    if ($filetype=="null") {
        $filetype = $pinfo['extension'];
    }
    if (!in_array(strtolower($pinfo['extension']),array("jpg","jpeg","png","gif"))) {
        dsetcookie('setok','upload3');
        header("location:111cn.net/profile");
        exit;
    }

    if($max_file_size < $file["size"]) {//检查文件大小
        dsetcookie('setok','upload2');
        header("location:111cn.net/profile");
        exit;
    }
    if(!in_array($file["type"],$uptypes)) { //检查文件类型
        dsetcookie('setok','upload3');
        header("location:111cn.net/profile");
        exit;
    }
    if(!file_exists($path_im)) {
        mkdir($path_im);
    }

    $filename = $file["tmp_name"];
    $im_size = getimagesize($filename);

    $src_w = $im_size[0];
    $src_h = $im_size[1];
    $src_type = $im_size[2];

    $all_path = $path_im.$newname.".".$filetype;//路径+文件名,目前以上传时间命名
    if (file_exists($all_path)) {
        @unlink($all_path);
    }
    if(!move_uploaded_file ($filename,$all_path)) {
        dsetcookie('setok','upload4');
        header("location:111cn.net/profile");
        exit;
    }
    $pinfo = pathinfo($all_path);
    $fname = $pinfo[basename];

    switch($src_type) {//判断源图片文件类型
         case 1://gif
         $src_im = @imagecreatefromgif($all_path);//从源图片文件取得图像
         break;
         case 2://jpg
         $src_im = @imagecreatefromjpeg($all_path);
         break;
         case 3://png
         $src_im = @imagecreatefrompng($all_path);
         break;
         //case 6:
         //$src_im=imagecreatefromwbmp($all_path);
         //break;
         default:
         dsetcookie('setok','upload3');
         header("location:111cn.net/profile");
         exit;
    }

   if($smallmark == 1) {
       if(!file_exists($path_sim)) {//检查缩略图目录是否存在,不存在创建
           mkdir($path_sim);
       }
       if ($smallname) $newname=$smallname;
       $sall_path = $path_sim.$newname.".".$filetype;
       if (file_exists($sall_path)) {
           @unlink($sall_path);
       }
       if($src_w <= $dst_sw) { // 原图尺寸 <= 缩略图尺寸
           if ($dstsh==0)  {
                $dst_sim = @imagecreatetruecolor($src_w,$src_h); //新建缩略图真彩位图
                $sx=$sy=0;
           } else {
                $dst_sim = @imagecreatetruecolor($dstsw,$dstsh); //新建缩略图真彩位图
                $sx=($dstsw-$src_w)/2;
                $sy=($dstsh-$src_h)/2;
           }
           $img = @imagecreatefrompng("images/phbg.png");
           @imagecopymerge($dst_sim,$img,0,0,0,0,$dstsw,$dstsh,100); //原图图像写入新建真彩位图中
           @imagecopymerge($dst_sim,$src_im,$sx,$sy,0,0,$src_w,$src_h,100); //原图图像写入新建真彩位图中
       }

       if($src_w > $dst_sw) { // 原图尺寸 > 缩略图尺寸
           $dst_sh = $dst_sw/$src_w*$src_h;
           if ($dst_sh<$dstsh) {
               $dst_sh=$dstsh;
               $dst_sw=$dst_sh/$src_h*$src_w;
           }
           if ($dstsh==0) {
                $dst_sim = @imagecreatetruecolor($dst_sw,$dst_sh); //新建缩略图真彩位图(等比例缩小原图尺寸)
           } else {
                $dst_sim = @imagecreatetruecolor($dstsw,$dstsh); //新建缩略图真彩位图(等比例缩小原图尺寸)
           }
           @imagecopyresampled($dst_sim,$src_im,0,0,0,0,$dst_sw,$dst_sh,$src_w,$src_h); //原图图像写入新建真彩位图中
       }

       switch($src_type) {
            case 1:@imagegif($dst_sim,$sall_path,$simclearly);//生成gif文件,图片清晰度0-100
            break;
            case 2:@imagejpeg($dst_sim,$sall_path,$simclearly);//生成jpg文件,图片清晰度0-100
            break;
            case 3:@imagepng($dst_sim,$sall_path,$simclearlypng);//生成png文件,图片清晰度0-100
            break;
            //case 6:
            //imagewbmp($dst_sim,$sall_path);
            break;
       }
       //释放缓存
       @imagedestroy($dst_sim);
    }
    @imagedestroy($src_im);
    return $newname.".".$filetype;
}
?>

在php中要获取图片的高与宽度,php自身带有一个函数getimagesize,他会返回一个数组,数组下标1为图片高度,数组下标0一宽度哦。

//you do not need to alter these functions

 代码如下 复制代码
function getheight($image) {
 $size = getimagesize($image);
 $height = $size[1];
 return $height;
}


//you do not need to alter these functions

 代码如下 复制代码
function getwidth($image) {
 $size = getimagesize($image);
 $width = $size[0];
 return $width;
}
[!--infotagslink--]

相关文章

  • php二维码生成

    本文介绍两种使用 php 生成二维码的方法。 (1)利用google生成二维码的开放接口,代码如下: /** * google api 二维码生成【QRcode可以存储最多4296个字母数字类型的任意文本,具体可以查看二维码数据格式】 * @param strin...2015-10-21
  • Java生成随机姓名、性别和年龄的实现示例

    这篇文章主要介绍了Java生成随机姓名、性别和年龄的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-10-01
  • C#生成随机数功能示例

    这篇文章主要介绍了C#生成随机数功能,涉及C#数学运算与字符串操作相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
  • php生成唯一数字id的方法汇总

    关于生成唯一数字ID的问题,是不是需要使用rand生成一个随机数,然后去数据库查询是否有这个数呢?感觉这样的话有点费时间,有没有其他方法呢?当然不是,其实有两种方法可以解决。 1. 如果你只用php而不用数据库的话,那时间戳+随...2015-11-24
  • Element图表初始大小及窗口自适应实现

    这篇文章主要介绍了Element图表初始大小及窗口自适应实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-07-10
  • jQuery为动态生成的select元素添加事件的方法

    下面小编就为大家带来一篇jQuery为动态生成的select元素添加事件的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2016-09-01
  • PHP自动生成后台导航网址的最佳方法

    经常制作开发不同的网站的后台,写过很多种不同的后台导航写法。 最终积累了这种最写法,算是最好的吧...2013-09-29
  • C#实现绘制面形图表的方法详解

    这篇文章主要介绍了C#实现绘制面形图表的方法,对于C#初学者很好的掌握C#图形绘制有一定的借鉴价值,需要的朋友可以参考下...2020-06-25
  • 如何使用RoughViz可视化Vue.js中的草绘图表

    这篇文章主要介绍了如何使用RoughViz可视化Vue.js中的草绘图表,帮助大家更好的理解和使用roughViz,感兴趣的朋友可以了解下...2021-01-31
  • js生成随机数的方法实例

    js生成随机数主要用到了内置的Math对象的random()方法。用法如:Math.random()。它返回的是一个 0 ~ 1 之间的随机数。有了这么一个方法,那生成任意随机数就好理解了。比如实际中我们可能会有如下的需要: (1)生成一个 0 - 1...2015-10-21
  • Echarts实例教程之树形图表的实现方法

    众所周知echarts是一个纯JavaScript的图标库,下面这篇文章主要给大家介绍了关于Echarts实例之树形图表的实现方法,需要的朋友可以参考下...2021-08-06
  • PHP验证码生成与验证例子

    验证码是一个现在WEB2.0中常见的一个功能了,像注册、登录又或者是留言页面,都需要注册码来验证当前操作者的合法性,我们会看到有些网站没有验证码,但那是更高级的验证了,...2016-11-25
  • PHP生成不同颜色、不同大小的tag标签函数

    复制代码 代码如下:function getTagStyle(){ $minFontSize=8; //最小字体大小,可根据需要自行更改 $maxFontSize=18; //最大字体大小,可根据需要自行更改 return 'font-size:'.($minFontSize+lcg_value()*(abs($maxFo...2013-10-04
  • php中利用str_pad函数生成数字递增形式的产品编号

    解决办法:$str=”QB”.str_pad(($maxid[0]["max(id)"]+1),5,”0″,STR_PAD_LEFT ); 其中$maxid[0]["max(id)"]+1) 是利用max函数从数据库中找也ID最大的一个值, ID为主键,不会重复。 str_pad() 函数把字符串填充为指...2013-10-04
  • JS生成某个范围的随机数【四种情况详解】

    下面小编就为大家带来一篇JS生成某个范围的随机数【四种情况详解】。小编觉得挺不错的,现在分享给大家,也给大家做个参考,一起跟随小编过来看看吧...2016-04-22
  • C#生成Word文档代码示例

    这篇文章主要介绍了C#生成Word文档代码示例,本文直接给出代码实例,需要的朋友可以参考下...2020-06-25
  • C# 绘制统计图大全(柱状图, 折线图, 扇形图)

    本篇文章介绍了C# 绘制统计图大全,其中包括状图, 折线图, 扇形图,有需要的同学可以了解一下。...2020-06-25
  • Vue组件文档生成工具库的方法

    本文主要介绍了Vue组件文档生成工具库的方法,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-08-11
  • matplotlib 三维图表绘制方法简介

    这篇文章主要介绍了matplotlib 三维图表绘制方法简介,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-09-21
  • PHP简单实现生成txt文件到指定目录的方法

    这篇文章主要介绍了PHP简单实现生成txt文件到指定目录的方法,简单对比分析了PHP中fwrite及file_put_contents等函数的使用方法,需要的朋友可以参考下...2016-04-28