Einzelnen Beitrag anzeigen
  #5  
Alt 17.06.2002, 19:01:37
DiJae DiJae ist offline
Anfänger
 
Registriert seit: May 2002
Beiträge: 149
Eine Torte

Das Script ist nicht von mir, sondern aus einem Tutorial. Ich habe es für meine Zwecke angepaßt. Im folgenden werden über ein Formular 5 Werte übergeben, die im zweiten Script dann zur Bildung eines Tortendiagramms genutzt werden. Ich habe es für meine Zwecke dann so abgeändert, dass ich eine Torte mit nur einem hervorgehobenen Stück bekomme. Mehr hatte ich halt nicht nötig.

Hier die Scripts:

<html>
<head>
<basefont face=arial>
</head>

<body>
<h3>Pie Chart Generator</h3>
<table>
<form action="pie.php4" method=get>
<tr>
<td>Slice 1</td>
<td><input type=text name=slice[1] value=0></td>
</tr>
<tr>
<td>Slice 2</td>
<td><input type=text name=slice[2] value=0></td>
</tr>
<tr>
<td>Slice 3</td>
<td><input type=text name=slice[3] value=0></td>
</tr>
<tr>
<td>Slice 4</td>
<td><input type=text name=slice[4] value=0></td>
</tr>
<tr>
<td>Slice 5</td>
<td><input type=text name=slice[5] value=0></td>
</tr>
<tr>
<td colspan=2 align=center><input type=submit value="Generate Pie
Chart"></td>
</tr>
</form>
</table>

</body>
</html>


Das zweite Script:

<?

// initialize some variables
$sum = 0;
$degrees = Array();
$diameter = 200;
$radius = $diameter/2;

// calculate sum of slices
for ($x=1; $x<=5; $x++)
{
$sum += $slice[$x];
}

// convert each slice into corresponding percentage of 360-degree circle
for ($y=1; $y<=5; $y++)
{
$degrees[$y] = ($slice[$y]/$sum) * 360;
}

// set up image and colours
Header("Content-Type: image/gif");
$im = ImageCreate(300, 300);
$red = ImageColorAllocate($im, 255, 0, 0);
$blue = ImageColorAllocate($im, 0, 0, 255);
$green = ImageColorAllocate($im, 0, 255, 0);
$yellow = ImageColorAllocate($im, 255, 255, 0);
$black = ImageColorAllocate($im, 0, 0, 0);
$white = ImageColorAllocate($im, 255, 255, 255);

// fill image with white
ImageFill($im, 0, 0, $white);

// draw baseline
ImageLine($im, 150,150, 225, 150, $black);

for ($z=1; $z<=5; $z++)
{
// calculate and draw arc corresponding to each slice
ImageArc($im, 150, 150, $diameter, $diameter, $last_angle,
($last_angle+$degrees[$z]), $black);
$last_angle = $last_angle+$degrees[$z];

// calculate coordinate of end-point of each arc by obtaining
// length of segment and adding radius
// remember that cos() and sin() return value in radians
// and have to be converted back to degrees!
$end_x = round(150 + ($radius * cos($last_angle*pi()/180)));
$end_y = round(150 + ($radius * sin($last_angle*pi()/180)));

// demarcate slice with another line
ImageLine($im, 150, 150, $end_x, $end_y, $black);
}

// this section is meant to calculate the mid-point of each slice
// so that it can be filled with colour

// initialize some variables
$prev_angle = 0;
$pointer = 0;

for ($z=1; $z<=5; $z++)
{
// to calculate mid-point of a slice, the procedure is to use an angle
bisector
// and then obtain the mid-point of that bisector
$pointer = $prev_angle + $degrees[$z];
$this_angle = ($prev_angle + $pointer) / 2;
$prev_angle = $pointer;

// get end-point of angle bisector
$end_x = round(150 + ($radius * cos($this_angle*pi()/180)));
$end_y = round(150 + ($radius * sin($this_angle*pi()/180)));

// given start point (150,150) and end-point above, mid-point can be
// calculated with standard mid-point formula
$mid_x = round((150+($end_x))/2);
$mid_y = round((150+($end_y))/2);

// depending on which slice, fill with appropriate colour
if ($z == 1)
{
ImageFillToBorder($im, $mid_x, $mid_y, $black, $red);
}
else if ($z == 2)
{
ImageFillToBorder($im, $mid_x, $mid_y, $black, $blue);
}
else if ($z == 3)
{
ImageFillToBorder($im, $mid_x, $mid_y, $black, $green);
}
else if ($z == 4)
{
ImageFillToBorder($im, $mid_x, $mid_y, $black, $yellow);
}
else if ($z == 5)
{
ImageFillToBorder($im, $mid_x, $mid_y, $black, $black);
}
}

// write string
ImageString($im, 5, 100, 10, "Pie Chart", $black);

// output to browser
ImageGIF($im);

?>

Das komplette Tutorial findest Du aber auch unter:

http://www.devshed.com/Server_Side/P...neration/print

Gruß
Mit Zitat antworten