首页 / 育儿亲子 / 正文
实例PHP实现圆盘图绘制

Time:2025年11月25日 Read: 评论:0 作者:安守

以下是一个使用PHP绘制圆盘图的简单实例。在这个例子中,我们将使用`imagearc()`函数来绘制一个圆盘图。

```php

实例PHP实现圆盘图绘制 育儿亲子

// 创建图像资源

$width = 200;

$height = 200;

$image = imagecreatetruecolor($width, $height);

// 分配颜色

$white = imagecolorallocate($image, 255, 255, 255);

$black = imagecolorallocate($image, 0, 0, 0);

$red = imagecolorallocate($image, 255, 0, 0);

$green = imagecolorallocate($image, 0, 255, 0);

$blue = imagecolorallocate($image, 0, 0, 255);

// 填充背景

imagefill($image, 0, 0, $white);

// 绘制圆盘图

// 表格数据

$data = array(

'红色' => 45,

'绿色' => 35,

'蓝色' => 20

);

// 绘制每个部分

$centerX = $width / 2;

$centerY = $height / 2;

$radius = min($width, $height) / 2 - 10;

$last_angle = 0;

foreach ($data as $color => $value) {

$angle = ($value / array_sum($data)) * 360;

imagearc($image, $centerX, $centerY, $radius * 2, $radius * 2, $last_angle, $last_angle + $angle, $color);

$last_angle += $angle;

}

// 输出图像

header('Content-Type: image/png');

imagepng($image);

// 释放图像资源

imagedestroy($image);

>

```

在上面的代码中,我们首先创建了一个200x200像素的图像资源。然后,我们定义了几个颜色,包括白色、黑色、红色、绿色和蓝色。接下来,我们使用`imagefill()`函数填充背景为白色。

然后,我们定义了一个包含数据(颜色和百分比)的数组。我们计算每个颜色的角度,并使用`imagearc()`函数绘制圆盘图中的每个部分。

我们输出图像,并释放图像资源。

这个实例展示了如何使用PHP和GD库来绘制基本的圆盘图。

标签:
排行榜
关于我们
我们成立于2024年夏,是给广大网友提供生活知识的平台,非盈利性质平台,主为广大网友交流分享生活当中的一些生活技巧,知识类的经验交流。
关于我们
主体介绍
扫码关注
琼ICP备2024040253号-18