在PHP中生成折线统计图时,可以使用不同的颜色来区分不同的折线。以下是一个示例代码,展示如何生成带有不同颜色折线的统计图:
<?php
// 创建一个图像对象
$width = 800;
$height = 400;
$image = imagecreatetruecolor($width, $height);
// 定义颜色
$colors = array(
imagecolorallocate($image, 255, 0, 0), // 红色
imagecolorallocate($image, 0, 255, 0), // 绿色
imagecolorallocate($image, 0, 0, 255), // 蓝色
);
// 绘制背景色和边框
imagefilledrectangle($image, 0, 0, $width, $height, imagecolorallocate($image, 255, 255, 255));
imagerectangle($image, 0, 0, $width - 1, $height - 1, imagecolorallocate($image, 0, 0, 0));
// 绘制折线
$data = array(
array(10, 50, 80, 30, 40, 70), // 第一条折线的数据
array(60, 40, 20, 70, 50, 30), // 第二条折线的数据
array(30, 70, 50, 20, 40, 60), // 第三条折线的数据
);
$lineCount = count($data);
$pointCount = count($data[0]);
for ($i = 0; $i < $lineCount; $i++) {
for ($j = 0; $j < $pointCount - 1; $j++) {
$x1 = ($width / ($pointCount - 1)) * $j;
$y1 = $height - (($height / 100) * $data[$i][$j]);
$x2 = ($width / ($pointCount - 1)) * ($j + 1);
$y2 = $height - (($height / 100) * $data[$i][$j + 1]);
imageline($image, $x1, $y1, $x2, $y2, $colors[$i]);
}
}
// 输出图像
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
?>
在上述示例代码中,我们首先创建了一个800x400像素的图像对象。然后,我们定义了3种颜色:红色、绿色和蓝色,并将它们存储在一个数组中。接下来,我们使用imagefilledrectangle()
函数绘制了一个白色的背景和黑色的边框。最后,我们使用imageline()
函数在图像上绘制了3条折线,每条折线使用不同的颜色。
注意:在实际应用中,你可能需要根据自己的需求修改代码,以适应不同的数据和图像尺寸。
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站