设为首页 收藏本站
查看: 551|回复: 0

[经验分享] 用PHP生成图表

[复制链接]

尚未签到

发表于 2015-8-28 13:16:28 | 显示全部楼层 |阅读模式
功能还比较简单,有兴趣可以随便扩展

DSC0000.gif <?php
class MultiGraph{
    public $border_color;
    public $grid_color;
    public $data_color;
    public $g;
    public $data;
    public $draw_border=true;
    public $horizontal_grid_segments=5;
    public $vertical_grid_segments=0;
    public $x=0;
    public $y=0;
    public $w=200;
    public $h=100;
    public $point_size=5;
    public $y_axis_autolabel=true;
    public $x_axis_autolabel=true;
    public $font='arial.ttf';
    public $fontsize=8;
    public function __construct($graphics){
        $this->g=$graphics;
        imageantialias($this->g,true);
    }
    public function setDimensions($x,$y,$w,$h){
        $this->x=$x;
        $this->y=$y;
        $this->w=$w;
        $this->h=$h;
    }
    public function drawLineGraph(){
        $this->_drawborder();
        $yscale=$this->_calcYscale();
        $xscale=$this->_calcXscale();
        $this->_drawHorizontal();
        $this->_drawVertical();
        $tmpdata=$this->data;
        ksort($tmpdata);
        list($ldx,$ldy)=each($tmpdata);
        while (list($dx,$dy)=each($tmpdata)) {
            imageline($this->g,
                $this->x+$ldx*$xscale,
                $this->y+$this->y-$ldx*$yscale,
                $this->x+$dx*$xscale,
                $this_>y+$this->y-$dy*$yscale,
                $this->data_color);
            $ldx=$dx;
            $ldy=$dy;
        }
    }
    public function drawPointGraph(){
        $this->_drawBorder();
        $yscale=$this->_calcYscale();
        $xscale=$this->_calcXscale();
        $this->_drawHorizontal();
        $this->_drawVertical();
        foreach ($this->data as $dx=>$dy){
            imagefilledellipse($this->g,
                $this->x+$dx*$xscale,
                $this->y+$this->h-$dy*$yscale,
                $this->point_size,
                $this->point_size,
                $this->data_color);
        }
    }
    public function drawBarGraph(){
        $this->_drawBorder();
        $yscale=$this->_calcYscale();
        $this->_drawHorizontal();
        $section=(float)$this->w/count($this->data);
        $bar=(int)($section*0.7);
        $count=0;
        foreach ($this->data as $label=>$val){
            $count++;
            imagefilledrectangle($this->g,
                $this->x+($section*$count)-$bar,
                $this->y+$this->h,
                $this->x+($section*$count),
                $this->y+$this->h-$val*$yscale,
                $this->data_color);
            imagerectangle($this->g,
                $this-x+($section*$count)-$bar,
                $this->y+$this->h,
                $this->x+($section*$count),
                $this->y+$this->h-$val*$yscale,
                $this->border_color);
            if ($this->x_axis_autolabel) {
                $box=imagettfbbox($this->fontsize,270,$this->font,$label);
                $texwidth=abs($box[4]-$box[0]);
                imagettftext($this->g,
                    $this->fontsize,270,
                    ($this->x+($section*$count)-($bar/2)-($texwidth/2)),
                    $this->y+$this_>h+4,
                    $this->border_color,
                    $this->font,
                    $label);
            }
        }
    }
    private function _drawBorder(){
        if ($this->draw_border) {
            imageline($this->g,
                $this->x,
                $this->y,
                $this->x,
                $this->h+$this->y,
                $this->border_color);
            imageline($this->g,
                $this->x,
                $this->h+$this->y,
                $this->w+$this->x,
                $this->h+$this->y,
                $this->border_color);
        }
    }
    private function _calcYscale(){
        return (float)$this->h/$this->_calcYMax();
    }
    private function _calcXscale(){
        return (float)$this->w/$this->_calcXMax();
    }
    private function _calcYMax(){
        $max=(float)max($this->data)*1.05;
        $len=(float)((int)$max);
        if ($len<2) {
            return $max;
        }else {
            return intval(substr($max,0,2).str_repeat('0',$len-2));
        }
    }
    private function _calcXMax(){
        return max(array_keys($this->data));
    }
    private function _drawHorizontal(){
        if ($this->horizontal_grid_segments) {
            foreach (range(1,$this->horizontal_grid_segments) as $hg){
                $yheight=(int)$this->y+$this->h-($hg*($this->h/$this->horizontal_grid_segments));
                imageline($this->g,
                    $this->x+1,
                    $yheight,
                    $this->w+$this-x,
                    $yheight,
                    $this->grid_color);
                if ($this->y_axis_autolabel) {
                    $ax_step=(int)($this->_calcYMax()/$this->horizontal_grid_segments)*$hg;
                    $box=imagettfbbox($this->fontsize,0,
                        $this->font,$ax_step);
                    $texwidth=abs($box[4]-$box[0]);
                    $texheight=abs($box[5]-$box[1]);
                    imagettftext($this->g,
                        $this->fontsize,0,
                        $this->x-3-$texwidth,
                        $yheight+$texheight/2,
                        $this->border_color,
                        $this->font,
                        $ax_step);
                }
            }
        }
    }
    private function _drawVertical(){
        if ($this->vertical_grid_segments) {
            foreach (range(1,$this->vertical_grid_segments) as $vg){
                $xloc=(int)($this->x+($vg*($this->w/$this->vertical_grid_segments)));
                imageline($this->g,
                    $xloc,
                    $this->y,
                    $xloc,
                    $this->y+$this->h-1,
                    $this->grid_color);
                if ($this->x_axis_autolabel) {
                    $ax_step=(int)(($this->_calcXMax()/$this->vertical_grid_segments)*$vg);
                    $box=imagettfbbox($this->fontsize,270,$this->font,$ax_step);
                    $texwidth=abs($box[4]-$box[0]);
                    imagettftext($this->g,
                        $this->fontsize,270,
                        $xloc-$texwidth/2,
                        $this->y+$this->h+3,
                        $this->border_color,
                        $this->font,
                        $ax_step);
                }
            }
        }
    }
}
?>
实例:


<?php
require_once('MultiGraph.php');
$gfx=imagecreatetruecolor(950,650);
$red=imagecolorallocate($gfx,255,0,0);
$manilla=imagecolorallocate($gfx,255,255,245);
$black=imagecolorallocate($gfx,0,0,0);
$lgray=imagecolorallocate($gfx,200,200,200);
imagefill($gfx,0,0,$manilla);
$graph=new MultiGraph($gfx);
$graph->border_color=$black;
$graph->grid_color=$lgray;
$graph->data_color=$red;
$graph->vertical_grid_segments=10;
$graph->font='MSYH.ttf';
$chartdata=array();
for ($i=0;$i<50;$i++){
    $chartdata[rand(1,200)]=rand(1,1000);
}
$graph->data=$chartdata;
$graph->setDimensions(50,50,300,200);
$graph->drawLineGraph();
$graph->horizontal_grid_segments=9;
$graph->vertical_grid_segments=15;
$graph->setDimensions(50,350,300,200);
$graph->drawPointGraph();
$bardata=array();
foreach (range(1,12) as $mon){
    $bardata[date('F',mktime(0,0,0,$mon))]=rand(1,5000);
}
$cyan=imagecolorallocate($gfx,0,255,255);
$graph->data_color=$cyan;
$graph->fontsize=11;
$graph->horizontal_grid_segments=12;
$graph->setDimensions(450,40,400,500);
$graph->data=$bardata;
$graph->drawBarGraph();
header('Content-type:image/png');
imagepng($gfx);
?>

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-105570-1-1.html 上篇帖子: 转载-php试题 下篇帖子: PHP做Web开发的MVC框架(Smarty使用说明 )
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表