Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [php] Wykres kolowy
Forum PHP.pl > Forum > Przedszkole
kalpio
Znalazlem w ksiazce CORE php Programming taki przyklad wykresu kolowego
  1. <?php
  2.  
  3. //fill in chart parameters
  4. $ChartDiameter = 300;
  5. $ChartFont = 5;
  6. $ChartFontHeight = imagefontheight($ChartFont);
  7. $ChartData = array(
  8. "Beef"=>"99",
  9. "Pork"=>"75",
  10. "Chicken"=>"15",
  11. "Lamb"=>"66",
  12. "Fish"=>"22");
  13.  
  14. //determine graphic size
  15. $ChartWidth = $ChartDiameter + 20;
  16. $ChartHeight = $ChartDiameter + 20 +
  17. (($ChartFontHeight + 2) * count($ChartData));
  18.  
  19. //determine total of all values
  20. $ChartTotal = array_sum($ChartData);
  21.  
  22. //set center of pie
  23. $ChartCenterX = $ChartDiameter/+ 10;
  24. $ChartCenterY = $ChartDiameter/+ 10;
  25.  
  26. //create image
  27. $image = imagecreate($ChartWidth, $ChartHeight);
  28. imageantialias($image, TRUE);
  29.  
  30. //create a round brush for drawing borders
  31. $dot = imagecreate(10, 10);
  32. $dotColorBlack = imagecolorallocate($dot, 0, 0, 0);
  33. $dotColorTransparent = imagecolorallocate($dot, 255, 0, 255);
  34. imagecolortransparent($dot, $dotColorTransparent);
  35. imagefill($dot, 0, 0, $dotColorTransparent);
  36. imagefilledellipse($dot, 4, 4, 5, 5, $dotColorBlack);
  37. imagesetbrush($image, $dot);
  38.  
  39.  
  40. //allocate colors
  41. $colorBody = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
  42. $colorBorder = imagecolorallocate($image, 0x00, 0x00, 0x00);
  43. $colorText = imagecolorallocate($image, 0x00, 0x00, 0x00);
  44. $colorSlice = array(
  45.  
  46. imagecolorallocate($image, 0xFF, 0x00, 0x00),
  47. imagecolorallocate($image, 0x00, 0xFF, 0x00),
  48. imagecolorallocate($image, 0x00, 0x00, 0xFF),
  49. imagecolorallocate($image, 0xFF, 0xFF, 0x00),
  50. imagecolorallocate($image, 0xFF, 0x00, 0xFF),
  51. imagecolorallocate($image, 0x00, 0xFF, 0xFF),
  52. imagecolorallocate($image, 0x99, 0x00, 0x00),
  53. imagecolorallocate($image, 0x00, 0x99, 0x00),
  54. imagecolorallocate($image, 0x00, 0x00, 0x99),
  55. imagecolorallocate($image, 0x99, 0x99, 0x00),
  56. imagecolorallocate($image, 0x99, 0x00, 0x99),
  57. imagecolorallocate($image, 0x00, 0x99, 0x99));
  58.  
  59. //fill background
  60. imagefill($image, 0, 0, $colorBody);
  61.  
  62.  
  63. /*
  64. ** draw each slice
  65. */
  66. $Degrees = 0;
  67. $slice=0;
  68. foreach($ChartData as $label=>$value)
  69. {
  70. $StartDegrees = round($Degrees);
  71. $Degrees += (($value/$ChartTotal)*360);
  72. $EndDegrees = round($Degrees);
  73.  
  74. $CurrentColor = $colorSlice[$slice%(count($colorSlice))];
  75.  
  76. //draw pie slice
  77. imagefilledarc(
  78. $image,
  79. $ChartCenterX, $ChartCenterY,
  80. $ChartDiameter,$ChartDiameter,
  81. $StartDegrees, $EndDegrees,
  82. $CurrentColor, IMG_ARC_PIE);
  83.  
  84.  
  85. //draw legend for this slice
  86. $LineY = $ChartDiameter + 20 +
  87. ($slice*($ChartFontHeight+2));
  88.  
  89. imagerectangle($image,
  90. 10,
  91. $LineY,
  92. 10 + $ChartFontHeight,
  93. $LineY+$ChartFontHeight,
  94. $colorBorder);
  95.  
  96. imagefilltoborder($image,
  97. 12,
  98. $LineY + 2,
  99. $colorBorder,
  100. $CurrentColor);
  101.  
  102. imagestring($image,
  103. $ChartFont,
  104. 20 + $ChartFontHeight,
  105. $LineY,
  106. "$label: $value",
  107. $colorText);
  108.  
  109. $slice++;
  110. }
  111.  
  112.  
  113. //draw border
  114. imageellipse($image,
  115. $ChartCenterX, $ChartCenterY,
  116. $ChartDiameter,$ChartDiameter,
  117. IMG_COLOR_BRUSHED);
  118.  
  119. //output image
  120. header("Content-type: image/png");
  121. imagepng($image);
  122.  
  123. ?>


Chcialem sobie go wykorzystac lecz niestety nie wyswietla mi tego obrazka. Jest tylko taki znaczek jak przegladarka nie moze znalesc obrazka. Zmienilem
header("Content-type: image/png"); na header("Content-type: image/gif"); i
imagepng($image); na imagegif($image);
bo mam wersje gd 2.0.28 a w tej wersji nie wyswietla chyba png. Zreszta przy png wyswietalo mi blad "obrazek nie moze byc wyswietlony bo zawieral bledy"
Przyznaje odrazu ze nie mam pojecia o gd poprostu chcialem skorzystac z gotowego przykladu.
Ma ktos jakis pomysl?
zuczek
U mnie działa w nie zmienionej postaci. php/5.1.2
Guest
a jaka wersje gd masz ?
Master Miko
U mnie działa:

php 5 & php 4.

Masz starą wersję GD
GrayHat
albo twoj localhost olewa naglowek...
kalpio
Wresje GD mam 2.0.28 tak mi pokazuje w phpinfo a php 4.3.9

to jest fragment z phpinfo dotyczacego gd
GD Support enabled
GD Version bundled (2.0.28 compatible)
FreeType Support enabled
FreeType Linkage with freetype
GIF Read Support enabled
GIF Create Support enabled
JPG Support enabled
PNG Support enabled
WBMP Support enabled
XBM Support enabled

@master miko - jaka masz wersje gd i czy zmieniales z png na gif

@GrayHat - to nie tlyko mam problem na localhoscie wrzucilem to na dwa serewery i tam jest to samo a wersje php powyzej 4 a gd 2.0.28

Po IE wyswietla mi zamiast obrazka duzo "krzakow"
GrayHat
Cytat
Po IE wyswietla mi zamiast obrazka duzo "krzakow"


czyli wysylasz zly naglowek...
Master Miko
Wersja GD: bundled (2.0.28 compatible)
Czyli coś masz, jak zauważył GrayHat, źle z headerami.

Wrzuciłem sobie skrypt niemodyfikowany prosto z forum
kalpio
Faktycznie jest cos przez naglowki bo czysty skrypt dziala. Zaczyna sie psuc jak do pliku dorzuce jakiegos html'a (informacje meta itp) Da sie to jakos pogodzic lub obejsc?
GrayHat
do tego pliku nic nie mozesz dozucic... ten plik zapisujesz na serwer i ladujesz go w innym poprzez <img src="nazwa_pliku.php" alt="" />
kalpio
Cytat(GrayHat @ 2006-04-24 13:09:13)
do tego pliku nic nie mozesz dozucic... ten plik zapisujesz na serwer i ladujesz go w innym poprzez <img src="nazwa_pliku.php" alt="" />

wlasnie tak zrobilem smile.gif dzieki wszystkim za podpowiedzi
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.