login  Naam:   Wachtwoord: 
Registreer je!
 Scripts:

Scripts > PHP > GD library > [class] Grafiekjes

[class] Grafiekjes

Auteur: Rubdos - 14 februari 2009 - 18:11 - Gekeurd door: Koen - Hits: 5392 - Aantal punten: 4.50 (2 stemmen)




Voorbeeldje staat er bij, even uitleggen:

Versie 1 RC 1
class graph:
-Methods:
addValue($value as array or point) -> value toevoegen, array van point, of point
parsecolor($img as resouce, $color as string) -> hexadecimale kleur naar gd-kleur
parse([$file as string]) -> zonder argumenten wordt de header image/png...
-vars:
$font: spreekt voor zich ;)
$textheight: hoeveel plaats dat de tekst krijgt
$colorPositive: positive waarden worden standaard groen, hexadecimale kleur!
$colorNegative: standaard rood
$width: een voor-zich-spreker
$height: idem...
$values: array met Points

class point($point as integer, $name as string) -> $point is het nummer, $name is een titel.
In de zelfde map als de class fontbestand uploaden!

Veel plezier, bugs te vermelden!

php source: http://scriptso...graph.phps
php script: http://scriptso....graph.php

Code:
  1. <?php
  2. class graph{
  3. var $font = "ariali.ttf";
  4. var $textheight = 90;
  5. var $colorPositive = "#2FFF00";
  6. var $colorNegative = "#FF0000";
  7. var $width = 400;
  8. var $height = 250;
  9. var $values = array();
  10. function addValue($value){
  11. if(is_array($value)){
  12. for($i=0;$i<count($value);$i++) $this->values[] = $value[$i];
  13. }else{
  14. $values[] = $value;
  15. }
  16. }
  17. function parsecolor($img,$color){
  18. $hex = ereg_replace("#", "", $color);
  19. $color = array();
  20. $color['r'] = hexdec(substr($hex, 0, 2));
  21. $color['g'] = hexdec(substr($hex, 2, 2));
  22. $color['b'] = hexdec(substr($hex, 4, 2));
  23. return imagecolorallocate($img,$color['r'],$color['g'],$color['b']);
  24. }
  25. function parse($file = false){
  26. $width = $this->width;
  27. $height = $this->height;
  28. $img = imagecreate($width,$height);
  29. $bg = imagecolorallocate($img,255,255,255);
  30. $clr = $this->parsecolor($img,$this->colorPositive);
  31. $clrneg = $this->parsecolor($img,$this->colorNegative);
  32. $barwidth = (($width - 40) / count($this->values)) - ( (($width - 40) / count($this->values)) / 4 * 3 );
  33. $space = ( (($width - 40) / count($this->values)) / 4 * 3 );
  34. $graphspace = $height - $this->textheight;
  35. $highest = 1;
  36. $lowest = 0;
  37. foreach($this->values as $key => $value){ //hogerlager?
  38. if($highest < $value->point){
  39. $highest = $value->point;
  40. }
  41. if($lowest > $value->point){
  42. $lowest = $value->point;
  43. }
  44. }
  45. $highest += 1;
  46. $lowest += -1;
  47. $res = ($graphspace / ($highest + abs($lowest)));
  48. $nulpoint = ($highest * $res + abs($lowest * $res)) + $lowest * $res;
  49. $res2 = round($res);
  50. $aantlijntjes = ($highest + abs($lowest));
  51. if($res < 10){
  52. $mod = 10 - $res;
  53. }else{
  54. $mod = 1;
  55. }
  56. for($i = 0;$i <= $aantlijntjes + 1;$i++){ //lijntjes tekenen, met de nummertjes ervoor
  57. if((($i % $mod) == 1) OR ($highest - $i) == 0){
  58. $x1 = 20;
  59. $y1 = ($i * $res);
  60. $x2 = $width - 2;
  61. $y2 = ($i * $res);
  62. imagestring($img,1,1,$y1-2,$highest - $i,$this->parsecolor($img,"#CACACA"));
  63. if(($highest - $i)==0){
  64. imageline($img,$x1,$y1,$x2,$y2, $this->parsecolor($img,"#000000"));
  65. imagestring($img,1,1,$y1-2,$highest - $i,$this->parsecolor($img,"#000000"));
  66. }else{
  67. imageline($img,$x1,$y1,$x2,$y2, $this->parsecolor($img,"#CACACA"));
  68. imagestring($img,1,1,$y1-2,$highest - $i,$this->parsecolor($img,"#CACACA"));
  69. }
  70. }
  71. }
  72. foreach($this->values as $key => $value){
  73. $x1 = ($space / 2 + 40) + ($key * $barwidth) + ($key * $space);
  74. if($value->point>0){
  75. $y1 = $nulpoint - 1;
  76. $currcol = $clr;
  77. }else{
  78. $y1 = $nulpoint + 1;
  79. $currcol = $clrneg;
  80. }
  81.  
  82. $x2 = ($space / 2 + 40) + ($key * $barwidth) + ($key * $space) + $barwidth;
  83. $y2 = $nulpoint - ($value->point * $res);
  84. imagefilledrectangle($img,$x1,$y1,$x2,$y2,$currcol);
  85. $textpoints = imagettfbbox(12,45,$this->font,$value->name);
  86. $textx = ($x1 + ($barwidth/2)) - ($textpoints[4] - $textpoints[0]);
  87. $texty = ($graphspace + 5) - ($textpoints[5] - $textpoints[1]);
  88. imagettftext($img,12,45,$textx,$texty,$this->parsecolor($img,"#CACACA"),$this->font,$value->name);
  89. }
  90. if($file){
  91. imagepng($img,$file);
  92. }else{
  93. header("content-type: image/png");
  94. imagepng($img);
  95. }
  96. }
  97. }
  98. class point{
  99. var $point;
  100. var $name;
  101. function __construct($point,$name){
  102. $this->point = $point;
  103. $this->name = $name;
  104. }
  105. }
  106. //VOORBEELD:
  107. $gr = new graph();
  108. $gr->addValue( array(new point(-23.5,"Juni"), new point(25,"Juli"), new point(40,"Augustus"), new point(-20,"September"), new point(23,"Oktober"), new point(21,"November") ) );
  109. $gr->parse();
  110. ?>
Download code! Download code (.txt)

 Bekijk een voorbeeld van dit script!
 Stemmen
Niet ingelogd.

 Reacties
Post een reactie
Lees de reacties (9)
© 2002-2024 Sitemasters.be - Regels - Laadtijd: 0.034s