Current Location: Home> Latest Articles> How to Use imagefilledarc to Draw Complex Multi-Segment Arc Charts? One-Step Graph Visualization

How to Use imagefilledarc to Draw Complex Multi-Segment Arc Charts? One-Step Graph Visualization

M66 2025-06-12

In PHP, imagefilledarc() is a very powerful function primarily used for drawing arc charts. When you need to display a complex multi-segment arc chart, it is undoubtedly a very useful tool. This article will show you how to use the imagefilledarc() function to draw multiple segmented arc charts, and through simple code examples, help you understand step by step how to build this graphic.

1. Introduction to the imagefilledarc() Function

imagefilledarc() is a function in the GD library that draws a filled arc. Its basic syntax is as follows:

imagefilledarc(resource $image, int $cx, int $cy, int $width, int $height, int $start, int $end, int $color, int $style)  
  • $image: The target image resource.

  • $cx, $cy: Coordinates of the center point.

  • $width, $height: Width and height of the arc area.

  • $start: Starting angle of the arc.

  • $end: Ending angle of the arc.

  • $color: Fill color of the arc.

  • $style: Fill style of the arc.

This function is particularly suitable for drawing pie charts, circular progress bars, complex statistical charts, and especially useful when displaying multiple data segments.

2. Creating a Basic Chart

First, let's create a simple chart to demonstrate how to draw an arc. We will use imagefilledarc() to draw an arc from 0 to 180 degrees and set its color.

<?php  
// Create an image resource  
$image = imagecreatetruecolor(400, 400);  
<p>// Allocate colors<br>
$bgColor = imagecolorallocate($image, 255, 255, 255); // White background<br>
$arcColor = imagecolorallocate($image, 255, 0, 0); // Red arc</p>
<p>// Fill background color<br>
imagefill($image, 0, 0, $bgColor);</p>
<p>// Draw an arc<br>
imagefilledarc($image, 200, 200, 300, 300, 0, 180, $arcColor, IMG_ARC_PIE);</p>
<p>// Output image<br>
header('Content-Type: image/png');<br>
imagepng($image);</p>
<p>// Free memory<br>
imagedestroy($image);<br>
?><br>

In the above code, we create a 400x400 image with a white background. Then, we draw a red arc from 0 to 180 degrees.

3. Drawing a Multi-Segment Arc Chart

To draw a complex multi-segment arc chart, we need to split the arc into several segments, each with different start and end angles. The following is a more complex example where we draw a circle containing four segments, each representing different data values.

<?php  
// Create an image resource  
$image = imagecreatetruecolor(400, 400);  
<p>// Allocate colors<br>
$bgColor = imagecolorallocate($image, 255, 255, 255); // White background<br>
$colors = [<br>
imagecolorallocate($image, 255, 0, 0),   // Red<br>
imagecolorallocate($image, 0, 255, 0),   // Green<br>
imagecolorallocate($image, 0, 0, 255),   // Blue<br>
imagecolorallocate($image, 255, 255, 0)  // Yellow<br>
];</p>
<p>// Fill background color<br>
imagefill($image, 0, 0, $bgColor);</p>
<p>// Data definition (each value represents the angle of the corresponding segment)<br>
$data = [40, 30, 60, 90]; // Total is 220 degrees</p>
<p>// Calculate the start and end angle for each segment<br>
$startAngle = 0;<br>
foreach ($data as $i => $value) {<br>
$endAngle = $startAngle + $value;<br>
imagefilledarc($image, 200, 200, 300, 300, $startAngle, $endAngle, $colors[$i], IMG_ARC_PIE);<br>
$startAngle = $endAngle;<br>
}</p>
<p>// Output image<br>
header('Content-Type: image/png');<br>
imagepng($image);</p>
<p>// Free memory<br>
imagedestroy($image);<br>
?><br>

In this example, we draw multiple segments based on the values in the $data array, where each value represents the arc's angle. Each segment's color comes from a predefined color array.

4. Dynamically Loading Data via URL

To make the chart more flexible, we can dynamically load different data via a URL. Suppose the data comes from a URL and is passed in JSON format. We can use PHP’s file_get_contents() function to fetch data from the specified URL and convert it into a PHP array using json_decode(). Here's a simple example:

<?php  
// Get JSON data from URL  
$dataUrl = 'https://m66.net/data.json'; // Assume data is stored at this URL  
$jsonData = file_get_contents($dataUrl);  
$data = json_decode($jsonData, true);  
<p>// Create image resource<br>
$image = imagecreatetruecolor(400, 400);</p>
<p>// Allocate colors<br>
$bgColor = imagecolorallocate($image, 255, 255, 255); // White background<br>
$colors = [<br>
imagecolorallocate($image, 255, 0, 0),   // Red<br>
imagecolorallocate($image, 0, 255, 0),   // Green<br>
imagecolorallocate($image, 0, 0, 255),   // Blue<br>
imagecolorallocate($image, 255, 255, 0)  // Yellow<br>
];</p>
<p>// Fill background color<br>
imagefill($image, 0, 0, $bgColor);</p>
<p>// Calculate the start and end angle for each segment<br>
$startAngle = 0;<br>
foreach ($data as $i => $value) {<br>
$endAngle = $startAngle + $value;<br>
imagefilledarc($image, 200, 200, 300, 300, $startAngle, $endAngle, $colors[$i % count($colors)], IMG_ARC_PIE);<br>
$startAngle = $endAngle;<br>
}</p>
<p>// Output image<br>
header('Content-Type: image/png');<br>
imagepng($image);</p>
<p>// Free memory<br>
imagedestroy($image);<br>
?><br>

In this code, we assume the data is stored at a URL. The JSON-formatted data is fetched using file_get_contents() and parsed into a PHP array. Then, multiple arc segments are dynamically drawn based on the data. Notably, we loop through the color array to handle more data segments gracefully.