Current Location: Home> Latest Articles> How to use imageopenpolygon() to simulate the drawing of radar image/spider image

How to use imageopenpolygon() to simulate the drawing of radar image/spider image

M66 2025-05-31

In data visualization, a radar graph (or spider graph) is a very useful graph that can help us visually display multi-dimensional data. For PHP developers, the imageopenpolygon() function can be used to simulate drawing radar diagrams. This function is part of the GD library, which allows us to draw polygons in images. By setting the vertices reasonably, the effect of the radar map can be achieved. This article will introduce how to draw a radar diagram using PHP's imageopenpolygon() function.

1. The concept of radar diagram

Radar Chart, also known as Spider Chart, is a chart that displays multidimensional data. Each data point represents a dimension of the graph, and the data values ​​are connected through vertices to form a closed polygon. It is suitable for displaying relationships between multiple variables, especially when you need to compare multiple objects or dimensions.

2. Use imageopenpolygon() to draw the radar diagram

The imageopenpolygon() function is a function in the PHP GD library and is usually used to draw polygons in images. When drawing a radar graph, we can use this function to draw a polygon and adapt to different data dimensions by adjusting the vertices of the polygon.

Basic syntax:

 imageopenpolygon($image, $points, $num_points, $color);
  • $image : Target image resource.

  • $points : an array containing all vertex coordinates.

  • $num_points : The number of vertices.

  • $color : The color of the polygon.

Step 1: Initialize the image

First, we need to create an image resource and set a background color for the image.

 <?php
// Create a 500x500 Blank image
$image = imagecreatetruecolor(500, 500);

// Set background color to white
$bgColor = imagecolorallocate($image, 255, 255, 255);  // RGB (255, 255, 255) Represents white
imagefill($image, 0, 0, $bgColor);
?>

Step 2: Set the center and radius of the radar map

The center point of the radar graph is the center of the graph, and the data points of each dimension determine their distance from the center based on their values. Suppose we divide the graph into 6 dimensions and set the radius to 200.

 <?php
// Radar map center coordinates
$centerX = 250;
$centerY = 250;
$radius = 200;
?>

Step 3: Calculate the data points of each dimension

We need to calculate the coordinates of data points for each dimension. These coordinates determine the vertices of the polygon.

 <?php
// Assume there is 6 Data in each dimension
$data = [80, 70, 90, 60, 85, 75];  // 每Data in each dimension值 (scope:0 - 100)

// 将每Data in each dimension映射到半径
$points = [];
$numPoints = count($data);

for ($i = 0; $i < $numPoints; $i++) {
    // Calculate angle
    $angle = (2 * M_PI * $i) / $numPoints;
    
    // Calculate the coordinates of each data point
    $x = $centerX + cos($angle) * ($radius * $data[$i] / 100);
    $y = $centerY + sin($angle) * ($radius * $data[$i] / 100);
    
    // Save vertex coordinates
    $points[] = $x;
    $points[] = $y;
}
?>

Step 4: Draw the radar diagram

Next, we use the imageopenpolygon() function to draw the polygons and join these vertices.

 <?php
// Set the color of the polygon to blue
$polygonColor = imagecolorallocate($image, 0, 0, 255);

// Drawing polygons of radar diagrams
imageopenpolygon($image, $points, $numPoints, $polygonColor);
?>

Step 5: Output the image

Finally, we output the generated image.

 <?php
// The output image is PNG Format
header('Content-Type: image/png');
imagepng($image);

// Destroy image resources
imagedestroy($image);
?>

3. Complete code

Combine the above code snippets, and the final code is as follows:

 <?php
// Create image resources
$image = imagecreatetruecolor(500, 500);

// Set background colors
$bgColor = imagecolorallocate($image, 255, 255, 255);  // RGB (255, 255, 255) Represents white
imagefill($image, 0, 0, $bgColor);

// Radar map center coordinates和半径
$centerX = 250;
$centerY = 250;
$radius = 200;

// data
$data = [80, 70, 90, 60, 85, 75];  // 每Data in each dimension值 (scope:0 - 100)

// 计算data点坐标
$points = [];
$numPoints = count($data);

for ($i = 0; $i < $numPoints; $i++) {
    $angle = (2 * M_PI * $i) / $numPoints;
    $x = $centerX + cos($angle) * ($radius * $data[$i] / 100);
    $y = $centerY + sin($angle) * ($radius * $data[$i] / 100);
    $points[] = $x;
    $points[] = $y;
}

// Set color
$polygonColor = imagecolorallocate($image, 0, 0, 255);

// Draw polygons
imageopenpolygon($image, $points, $numPoints, $polygonColor);

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

// Free up resources
imagedestroy($image);
?>

4. Conclusion

Through PHP's imageopenpolygon() function, we can easily simulate and draw radar graphs, helping us better display multidimensional data. In practical applications, you can adjust the number of dimensions, colors, and data mapping according to your needs, thereby creating a more complex radar map.