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

[经验分享] 超酷PHP饼图

[复制链接]

尚未签到

发表于 2017-3-29 08:54:22 | 显示全部楼层 |阅读模式
<?
//+------------------------+
//| pie3dfun.php//公用函数 |
//+------------------------+

define("ANGLE_STEP", 5);    //定义画椭圆弧时的角度步长

function draw_getdarkcolor($img,$clr)    //求$clr对应的暗色
{
    $rgb    =    imagecolorsforindex($img,$clr);
    return array($rgb["red"]/2,$rgb["green"]/2,$rgb["blue"]/2);
}

function draw_getexy($a, $b, $d)    //求角度$d对应的椭圆上的点坐标
{
    $d        =    deg2rad($d);
    return array(round($a*Cos($d)), round($b*Sin($d)));
}

function draw_arc($img,$ox,$oy,$a,$b,$sd,$ed,$clr)    //椭圆弧函数
{
    $n                    =    ceil(($ed-$sd)/ANGLE_STEP);
    $d                    =    $sd;
    list($x0,$y0)        =    draw_getexy($a,$b,$d);
    for($i=0; $i<$n; $i++)
    {
        $d                =    ($d+ANGLE_STEP)>$ed?$ed:($d+ANGLE_STEP);
        list($x, $y)    =    draw_getexy($a, $b, $d);
        imageline($img, $x0+$ox, $y0+$oy, $x+$ox, $y+$oy, $clr);
        $x0                =    $x;
        $y0                =    $y;
    }
}

function draw_sector($img, $ox, $oy, $a, $b, $sd, $ed, $clr)    //画扇面
{
    $n                    =    ceil(($ed-$sd)/ANGLE_STEP);
    $d                    =    $sd;
    list($x0,$y0)        =    draw_getexy($a, $b, $d);
    imageline($img, $x0+$ox, $y0+$oy, $ox, $oy, $clr);
    for($i=0; $i<$n; $i++)
    {
        $d                =    ($d+ANGLE_STEP)>$ed?$ed:($d+ANGLE_STEP);
        list($x, $y)    =    draw_getexy($a, $b, $d);
        imageline($img, $x0+$ox, $y0+$oy, $x+$ox, $y+$oy, $clr);
        $x0                =    $x;
        $y0                =    $y;
    }
    imageline($img, $x0+$ox, $y0+$oy, $ox, $oy, $clr);
    list($x, $y)        =    draw_getexy($a/2, $b/2, ($d+$sd)/2);
    imagefill($img, $x+$ox, $y+$oy, $clr);
}

function draw_sector3d($img, $ox, $oy, $a, $b, $v, $sd, $ed, $clr)    //3d扇面
{
    draw_sector($img, $ox, $oy, $a, $b, $sd, $ed, $clr);
    if($sd<180)
    {
        list($R, $G, $B)    =    draw_getdarkcolor($img, $clr);
        $clr=imagecolorallocate($img, $R, $G, $B);
        if($ed>180) $ed        =    180;
        list($sx, $sy)        =    draw_getexy($a,$b,$sd);
        $sx                    +=    $ox;
        $sy                    +=    $oy;
        list($ex, $ey)        =    draw_getexy($a, $b, $ed);
        $ex                    +=    $ox;
        $ey                    +=    $oy;
        imageline($img, $sx, $sy, $sx, $sy+$v, $clr);
        imageline($img, $ex, $ey, $ex, $ey+$v, $clr);
        draw_arc($img, $ox, $oy+$v, $a, $b, $sd, $ed, $clr);
        list($sx, $sy)        =    draw_getexy($a, $b, ($sd+$ed)/2);
        $sy                    +=    $oy+$v/2;
        $sx                    +=    $ox;
        imagefill($img, $sx, $sy, $clr);
    }
}

function draw_getindexcolor($img, $clr)    //RBG转索引色
{
    $R        =    ($clr>>16) & 0xff;
    $G        =    ($clr>>8)& 0xff;
    $B        =    ($clr) & 0xff;
    return imagecolorallocate($img, $R, $G, $B);
}

// 绘图主函数,并输出图片
// $datLst 为数据数组, $datLst 为标签数组, $datLst 为颜色数组
// 以上三个数组的维数应该相等
function draw_img($datLst,$labLst,$clrLst,$a=250,$b=120,$v=20,$font=10)
{
    $ox        =    5+$a;
    $oy        =    5+$b;
    $fw        =    imagefontwidth($font);
    $fh        =    imagefontheight($font);

    $n        =    count($datLst);//数据项个数

    $w        =    10+$a*2;
    $h        =    10+$b*2+$v+($fh+2)*$n;

    $img    =    imagecreate($w, $h);

    //转RGB为索引色
    for($i=0; $i<$n; $i++)
        $clrLst[$i]    =    draw_getindexcolor($img,$clrLst[$i]);

    $clrbk    =    imagecolorallocate($img, 0xff, 0xff, 0xff);
    $clrt    =    imagecolorallocate($img, 0x00, 0x00, 0x00);

    //填充背景色
    imagefill($img, 0, 0, $clrbk);

    //求和
    $tot    =    0;
    for($i=0; $i<$n; $i++)
        $tot    +=    $datLst[$i];

    $sd        =    0;
    $ed        =    0;
    $ly        =    10+$b*2+$v;
    for($i=0; $i<$n; $i++)
    {
        $sd    =    $ed;
        $ed    +=    $datLst[$i]/$tot*360;

        //画圆饼
        draw_sector3d($img, $ox, $oy, $a, $b, $v, $sd, $ed, $clrLst[$i]);    //$sd,$ed,$clrLst[$i]);

        //画标签
        imagefilledrectangle($img, 5, $ly, 5+$fw, $ly+$fh, $clrLst[$i]);
        imagerectangle($img, 5, $ly, 5+$fw, $ly+$fh, $clrt);
        //imagestring($img, $font, 5+2*$fw, $ly, $labLst[$i].":".$datLst[$i]."(".(round(10000*($datLst[$i]/$tot))/100)."%)", $clrt);

        $str    =    iconv("GB2312", "UTF-8", $labLst[$i]);
        ImageTTFText($img, $font, 0, 5+2*$fw, $ly+13, $clrt, "./simsun.ttf", $str.":".$datLst[$i]."(".(round(10000*($datLst[$i]/$tot))/100)."%)");
        $ly        +=    $fh+2;
    }

    //输出图形
    header("Content-type: image/png");

    //输出生成的图片
    $imgFileName = "../temp/".time().".png";
    imagepng($img,$imgFileName);
    echo '<IMG SRC="'.$imgFileName.'" BORDER="1" ALT="统计饼图">';
}

$datLst    =    array(30, 10, 20, 20, 10, 20, 10, 20);    //数据
$labLst    =    array("中国科技大学", "安徽理工大学", "清华大学", "北京大学", "南京大学", "上海大学", "河海大学", "中山大学");    //标签
$clrLst    =    array(0x99ff00, 0xff6666, 0x0099ff, 0xff99ff, 0xffff99, 0x99ffff, 0xff3333, 0x009999);

//画图
draw_img($datLst,$labLst,$clrLst);
?>   效果图如下:

运维网声明 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-356790-1-1.html 上篇帖子: 用PHP实现文件上传 下篇帖子: php中数组函数总结
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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