2

I want to show percentages near or over each parts of the chart.

Do I have to convert coordinates from Polar to Cartesian?

To convert polar coordinates (r, θ) to rectangular coordinates (x, y) I used the following conversion:

x = r * cos( θ )
y = r * sin( θ )

in this graph

$x = $radius * cos( $Arc ); $y = $radius * sin( $Arc );

But results doesn't match (the percentages values aren't any close to each single parts of the chart), where I go wrong?

How can I fix it?

<?php
$W = 500; $H = 500;
$Image = imagecreate($W,$H);
$colors = array();
imagecolorallocate($Image,150,150,150); // grey background
$colors[0] = imagecolorallocate($Image,255,0,0); // red
$colors[1] = imagecolorallocate($Image,100,255,10); // green
$colors[2] = imagecolorallocate($Image,0,0,255); // bleu
$colors[3] = imagecolorallocate($Image,255,255,0); // yellow

$Country = array('England', 'Northern Ireland', 'Scotland', 'Wales');
$Population = array(53012456,1810863,5295403,3063456);
$TotPopulation = array_sum($Population);

$radius = 300; $StarAngle = 0;

foreach($Population  as $index => $V )
{
    $Arc = (360 * $V) / $TotPopulation;
    $Percentage = (100 * $V) / $TotPopulation;

    imagefilledarc(
        $Image,
        $W / 2, $H / 2, //  center x,y
        $radius,
        $radius,
        $StarAngle,
        ($StarAngle + $Arc), // end arc
        $colors[$index],
        IMG_ARC_PIE
    );

    $x = $radius * cos($Arc);
    $y = $radius * sin($Arc);
    imagestring($Image, 15, 5, 30 + $index*15 , 'x= '.number_format($x, 2, '.', '').' y='.number_format($y, 2, '.', '').' Country='.$Country[$index].' %='. number_format($Percentage, 2, '.', '').' ' , $colors[$index]);

    $StarAngle += $Arc;
    imagestring($Image, 15, 5, 430 + $index*15 , 'Country='.$Country[$index].' Population= $V %='.number_format($Percentage, 2, '.', '').' ' , $colors[$index]);
}
imagestring($Image, 5, 35, 10 , 'The population of the United Kingdom year 2011' , $colors[3]);
imagepng($Image);
timclutton
  • 12,682
  • 3
  • 33
  • 43
sandra
  • 29
  • 2
  • Is $Arc degrees or radians? Your definition of `$Arc = (360 * $V) / $TotPopulation;` suggests degrees.... should probably be `$Arc = ((2 * M_PI) * $V) / $TotPopulation;` – Mark Baker Dec 07 '15 at 17:22
  • are degrees, can you help me? – sandra Dec 08 '15 at 19:01
  • 1
    Well I've suggested one approach already; another is to use the [deg2rad()](http://www.php.net/manual/en/function.deg2rad.php) function to convert those degrees to radians – Mark Baker Dec 08 '15 at 19:05
  • I want to do is this: imagestring($Image, 5, 40, 250 , "83.90" , $colors[0]); imagestring($Image, 5, 340, 110 , "2.87" , $colors[1]); imagestring($Image, 5, 380, 150 , "8.38" , $colors[2]); imagestring($Image, 5, 410, 230 , "4.85" , $colors[3]); – sandra Dec 08 '15 at 19:14
  • You need to use radians in your calculations for angles – Mark Baker Dec 08 '15 at 19:17
  • Ho fatto quello che mi hai suggerito (spero). Il risultato è sempre sbagliato. Cosa non ho capito? Dove sbaglio? Ti rigrazio della tua pazienza $degrees = deg2rad ($Arc); // degrees $radians = $degrees * M_PI / 180; // radians $x = $radius * cos($radians); $y = $radius * sin($radians); imagestring($Image, 15, 5, 100 + $index*15 , "radians=$radians:x=".number_format($x, 0, '.', '')." y=".number_format($y, 0, '.', '')." Country=".$Country[$index]." %=".number_format($Percentage, 2, '.', '')."++++" , $colors[$index]); – sandra Dec 09 '15 at 10:35
  • I did what you suggested (I hope). The result is always wrong. What I did not understand? Where am I wrong? I give you thanks for your patience – sandra Dec 09 '15 at 10:44

1 Answers1

0

You can try the library geo-math-php

composer require rkondratuk/geo-math-php:^1

Convert coordinates polar to cartesian:

<?php

use PhpGeoMath\Model\Polar3dPoint;

$polarPoint1 = new Polar3dPoint(
    40.758742779050706, -73.97855507715238, Polar3dPoint::EARTH_RADIUS_IN_METERS
);

$cartesianpoint1 = $polarPoint1->buildCartesian3DPoint();

// Result:
$cartesianpoint1->x;
$cartesianpoint1->y;
$cartesianpoint1->z;

Convert coordinates cartesian to polar:

<?php

use PhpGeoMath\Model\Cartesian3dPoint;

$x = 1001;
$y = 205;
$z = 512;

$cartesianPoint2 = new Cartesian3dPoint($x, $y, $z);
$polarPoint2 = $cartesianPoint2->buildPolar3dPoint();

// Result:
$polarPoint2->lat;
$polarPoint2->lng;
$polarPoint2->radius;
Crystal-R
  • 71
  • 6