前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PHP使用GD库生成柱状图

PHP使用GD库生成柱状图

作者头像
用户7657330
发布2020-08-14 11:24:46
8700
发布2020-08-14 11:24:46
举报
文章被收录于专栏:程序生涯程序生涯
代码语言:javascript
复制
<?php
function createImage($data, $twidth, $tspace, $height)
{
    $dataName = array();
    $dataValue = array();
    $i = 0;
    $j = 0;
    $k = 0;
    $num = sizeof($data);

    foreach ($data as $key => $val) {
        $dataName[] = $key;
        $dataValue[] = $val;
    }

    $maxnum = max($data);
    $width = ($twidth + $tspace) * $num + 4;//image's width
    $im = imagecreate($width + 40, $height + 20);
    $lineColor = imagecolorallocate($im, 12, 12, 12);
    $bgColor = imagecolorallocate($im, 255, 233, 233);
    $tColor = imagecolorallocate($im, 123, 200, 56);
    imagefill($im, 0, 0, $bgColor);
    imageline($im, 30, 0, 30, $height - 2, $lineColor);
    imageline($im, 30, $height - 2, $width + 30 - 2, $height - 2, $lineColor);
    while ($i < $num) {
        imagefilledrectangle($im, $i * ($tspace + $twidth) + 40, $height - $dataValue[$i], $i * ($tspace + $twidth) + 40 + $twidth, $height - 3, $tColor);
        imagestringup($im, 4, $i * ($tspace + $twidth) + $twidth / 2 + 30, $height - 10, $dataName[$i] . "(" . $dataValue[$i] . ")", $lineColor);
        $i++;
    }
    while ($j <= (500 / 10)) {
        imagestringup($im, 4, 2, $height - $j * 10 + 10, $j * 10, $lineColor);
        $j = $j + 10;
    }
    while ($k <= (500 / 10)) {
        if ($k != 0)
            imageline($im, 28, $height - $k * 10, 32, $height - $k * 10, $lineColor);
        $k = $k + 10;
    }

    imagepng($im);
}


header("content-type:image/png");
$data = array("Yahoo" => 100, "Google" => 260, "Microsoft" => 320, "IBM" => 250, "Sun System" => 150, "Inter" => 220);
createImage($data, 50, 25, 500);
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-03-06 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档