Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [php] Wykres kolowy, biblioteka GD
kalpio
post 22.04.2006, 11:16:46
Post #1





Grupa: Zarejestrowani
Postów: 17
Pomógł: 0
Dołączył: 3.12.2003

Ostrzeżenie: (0%)
-----


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?
Go to the top of the page
+Quote Post
zuczek
post 22.04.2006, 13:39:43
Post #2





Grupa: Zarejestrowani
Postów: 68
Pomógł: 0
Dołączył: 6.06.2005
Skąd: CBŚ

Ostrzeżenie: (0%)
-----


U mnie działa w nie zmienionej postaci. php/5.1.2


--------------------
"Kraj, który w technice nie postępuje, cofa się" - prof.S.Wł.Bryła
pajacyk.pl - a co Ci szkodzi kliknąć
SOS please someone help me...
Go to the top of the page
+Quote Post
-Guest-
post 22.04.2006, 17:36:20
Post #3





Goście







a jaka wersje gd masz ?
Go to the top of the page
+Quote Post
Master Miko
post 22.04.2006, 17:52:18
Post #4





Grupa: Zarejestrowani
Postów: 530
Pomógł: 0
Dołączył: 15.01.2005
Skąd: Warszawa

Ostrzeżenie: (0%)
-----


U mnie działa:

php 5 & php 4.

Masz starą wersję GD

Ten post edytował Master Miko 22.04.2006, 17:53:04


--------------------
Go to the top of the page
+Quote Post
GrayHat
post 23.04.2006, 01:12:41
Post #5





Grupa: Zarejestrowani
Postów: 566
Pomógł: 18
Dołączył: 23.08.2003
Skąd: Łomża

Ostrzeżenie: (0%)
-----


albo twoj localhost olewa naglowek...


--------------------
*Note: No animals were killed durning the construction of this post.
Go to the top of the page
+Quote Post
kalpio
post 23.04.2006, 10:00:48
Post #6





Grupa: Zarejestrowani
Postów: 17
Pomógł: 0
Dołączył: 3.12.2003

Ostrzeżenie: (0%)
-----


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"
Go to the top of the page
+Quote Post
GrayHat
post 23.04.2006, 10:07:35
Post #7





Grupa: Zarejestrowani
Postów: 566
Pomógł: 18
Dołączył: 23.08.2003
Skąd: Łomża

Ostrzeżenie: (0%)
-----


Cytat
Po IE wyswietla mi zamiast obrazka duzo "krzakow"


czyli wysylasz zly naglowek...


--------------------
*Note: No animals were killed durning the construction of this post.
Go to the top of the page
+Quote Post
Master Miko
post 23.04.2006, 10:22:08
Post #8





Grupa: Zarejestrowani
Postów: 530
Pomógł: 0
Dołączył: 15.01.2005
Skąd: Warszawa

Ostrzeżenie: (0%)
-----


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

Ten post edytował Master Miko 23.04.2006, 10:22:31


--------------------
Go to the top of the page
+Quote Post
kalpio
post 24.04.2006, 10:57:24
Post #9





Grupa: Zarejestrowani
Postów: 17
Pomógł: 0
Dołączył: 3.12.2003

Ostrzeżenie: (0%)
-----


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?
Go to the top of the page
+Quote Post
GrayHat
post 24.04.2006, 13:09:13
Post #10





Grupa: Zarejestrowani
Postów: 566
Pomógł: 18
Dołączył: 23.08.2003
Skąd: Łomża

Ostrzeżenie: (0%)
-----


do tego pliku nic nie mozesz dozucic... ten plik zapisujesz na serwer i ladujesz go w innym poprzez <img src="nazwa_pliku.php" alt="" />


--------------------
*Note: No animals were killed durning the construction of this post.
Go to the top of the page
+Quote Post
kalpio
post 26.04.2006, 08:59:05
Post #11





Grupa: Zarejestrowani
Postów: 17
Pomógł: 0
Dołączył: 3.12.2003

Ostrzeżenie: (0%)
-----


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
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 18.07.2025 - 02:50